document360-writer 0.4.42 → 0.4.43

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,96 +1,157 @@
1
1
  #!/usr/bin/env node
2
- var fi=Object.defineProperty;var Xn=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var mi=(e,t)=>{for(var o in t)fi(e,o,{get:t[o],enumerable:!0})};import Ae from"picocolors";var B,gi,hi,ki,Vn,mt,f,$,L,E,R,Q,Pe,H=Xn(()=>{"use strict";B="#7f56d9",[gi,hi,ki]=[127,86,217],Vn=e=>Ae.isColorSupported?`\x1B[38;2;${gi};${hi};${ki}m${e}\x1B[39m`:e,mt=e=>Ae.bold(Vn(e)),f=e=>Ae.dim(e),$=e=>Ae.red(e),L=e=>Ae.yellow(e),E=e=>Ae.green(e),R=Vn,Q=e=>Ae.gray(e),Pe=e=>Ae.bold(e)});var ts={};mi(ts,{doctorCommand:()=>vt,renderDoctorChecks:()=>es,runDoctorChecks:()=>Zr});import{existsSync as ra}from"node:fs";import{d360GetAll as sa,getAccessToken as ia,isExpired as ln,loadProfileMap as la,loadTokens as aa,packageSkillsDir as Qr,projectConfigPath as ca,readProjectConfig as ua,resolveActiveProfile as da,resolveAuth as pa,resolveModelSetting as fa}from"document360-engine";async function Zr(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=pa("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=fa(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=ua(e);if(!s)return t.push({level:"fail",label:`No ${ca(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Sources: docs grounded in ${d.length} path(s) (${d.slice(0,3).join(", ")}${d.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=da(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.production?" \u26A0 PRODUCTION":""}`})}catch(I){return t.push({level:"fail",label:`Profile config: ${I.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let k=aa(g.name);k?ln(k)&&!k.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):ln(k)?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(k.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${g.name})`}),t.push(g.project.workspaceId?{level:"ok",label:`Workspace: ${g.project.workspaceName??g.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let w=la(e,g.name);if(w?w.projectId&&g.project.projectId&&w.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.projectId}) \u2260 profile projectId (${g.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(ra(Qr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Qr()}`,fix:"Reinstall document360-writer (broken install)"}),k&&(!ln(k)||k.refreshToken)){let I={profile:g.name,connection:g.connection};try{await ia(I);let b=await sa(I,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${b.length} project(s) visible`})}catch(b){t.push({level:"fail",label:`API call failed: ${b.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function es(e){let t=[""];for(let r of e){let s=r.level==="ok"?E("\u2713"):r.level==="warn"?L("\u26A0"):$("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${R(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?E(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):$(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function vt(e,t){console.log(f("Running checks\u2026"));for(let o of es(await Zr(t.cwd)))console.log(o);return{kind:"continue"}}var oo=Xn(()=>{"use strict";H()});import{Command as cu}from"commander";import{createRequire as uu}from"node:module";import{AUTH_MODES as du}from"document360-engine";import{input as Ti}from"@inquirer/prompts";import{loginPkce as Ri,refreshTokens as ji,toStoredTokens as Zn,clearTokens as Ai,decodeJwtClaims as Qn,isExpired as Ei,loadTokens as Di,saveTokens as er,resolveActiveProfile as Ft,setProfileProject as Ii,readProjectConfig as _i}from"document360-engine";H();import{select as yi}from"@inquirer/prompts";import{resolveActiveProfile as wi,setProfileProject as $i,resolveProjectId as xi,listWorkspaces as bi}from"document360-engine";async function gt(e,t){let o=wi(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??xi(n);return{workspaces:await bi(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var Jn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function jo(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function Wt(e,t,o,n,r){$i(e,t,{projectId:o,workspaceId:n,workspaceName:r})}async function Kn(e,t,o){let n;try{n=await gt(e,o)}catch(s){return console.log($(`Could not list workspaces: ${s.message}`)),1}let r=jo(n.workspaces,t);return r?(Wt(e,n.profile,n.projectId,r.id,r.name),console.log(E(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log($(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function tt(e,t){let o;try{o=await gt(e,t)}catch(k){console.log($(`Could not list workspaces: ${k.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let k of n)console.log(` ${k.id===c?R("\u25CF"):" "} ${Jn(k)} ${f(k.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let d=await yi({message:"Select the Document360 workspace for this repo:",choices:n.map(k=>({name:`${Jn(k)}${k.id===c?" (current)":""}`,value:k.id}))}),g=n.find(k=>k.id===d);Wt(e,s,r,d,g?.name),console.log(E(`\u2713 Workspace set to "${g?.name??d}" for profile "${s}".`))}H();import vi from"picocolors";function Ci(e=process.env){return e.FORCE_HYPERLINK==="0"||!vi.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 Pi(e,t=e,o){return Ci(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Si=/https?:\/\/[^\s\x1b]+/g;function ht(e,t){return e.replace(Si,o=>Pi(o,o,t))}function tr(e){return{...Qn(e.idToken)??{},...Qn(e.accessToken)??{}}}function Ht(e){let t=tr(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function Bt(e){let t=Ft(process.cwd(),e.profile),o=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Ri(o,{manual:e.manual,promptForRedirect:s=>Ti({message:s})},s=>console.log(ht(s))),r=Zn(t.name,n);if(er(r),Ao(r,t.name,s=>console.log(f(s))),console.log(""),console.log(E(`\u2713 Logged in to "${t.name}" as ${Ht(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{_i(process.cwd())?.profiles?.[t.name]&&(console.log(""),await tt(process.cwd(),t.name))}catch{}}function Ao(e,t,o){let r=tr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Ft(process.cwd(),t).project.projectId)return;Ii(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function or(e){let t=Ft(process.cwd(),e.profile),o=t.connection,n=Di(t.name);if(!n){console.log($(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${R(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Ht(n)}`),Ei(n))if(n.refreshToken)try{let r=Zn(t.name,await ji(o,n.refreshToken));er(r),console.log(E(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${n.expiresAt}`))}async function nr(e){let t=Ft(process.cwd(),e.profile);Ai(t.name)?console.log(E(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as rr,writeProjectConfig as Ni,resolveActiveProfile as Mi,loadTokens as Li,isExpired as Oi}from"document360-engine";function qt(e){let t=rr(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log($("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?R("\u25CF "):" ",s=n.production?L(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=Li(o),g=d?Oi(d)&&!d.refreshToken?L("expired"):f("logged in"):f("not logged in");console.log(`${r}${R(o)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function zt(e,t){let o=rr(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log($(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,Ni(o,e);let n=o.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(E(`\u2713 Default profile is now "${t}"${n}`))}function Gt(e,t){try{let o=Mi(e,t);console.log(""),console.log(`Profile ${R(o.name)}${o.production?L(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${o.connection.apiUrl}`)),console.log(f(` identity: ${o.connection.authorizationUrl}`)),console.log(f(` clientId: ${o.connection.clientId}`)),console.log(f(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(f(` project: ${o.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log($(o.message)),process.exitCode=1}}H();import{existsSync as Ui,readdirSync as Wi,statSync as Fi}from"node:fs";import{join as Hi}from"node:path";import{apiLogDir as Bi}from"document360-engine";function sr(){let e=Bi();if(console.log(""),console.log(`Document360 API logs: ${R(e)}`),!Ui(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Wi(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(Fi(Hi(e,o)).size/1024).toFixed(1);console.log(` ${o} ${f(`${n} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}H();import{createSession as qi,resolveAuth as zi,findByName as Gi,slugify as Yi,touchSession as Xi,upsertSession as Vi,resolveActiveProfile as Ji}from"document360-engine";async function ir(e,t,o,n,r,s){let c=zi(o);c.kind==="none"&&(console.error(""),console.error($("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let d=null;try{d=Ji(e,r)}catch(b){console.error($(`Document360 profile error: ${b.message}`)),process.exit(2)}d.production&&(console.error(L(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error($("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let g;if(n){let b=Gi(e,n);b||(console.error($(`No saved session matches "${n}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),g=b.uuid,console.error(f(`Resuming "${b.name}"`))}let k=qi({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),w=g??null,I=1;for await(let b of k.send(t))switch(b.type){case"session":if(!w){w=b.sessionId;let C=new Date().toISOString();Vi({uuid:w,name:Yi(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:C,updatedAt:C})}break;case"text":process.stdout.write(b.delta);break;case"tool":console.error(Q(` \u2699 ${b.name}`));break;case"result":I=b.ok?0:1,console.error(f(`(${b.inputTokens}\u2192${b.outputTokens} tokens`+(b.costUsd>0?`, $${b.costUsd<.01?b.costUsd.toFixed(4):b.costUsd.toFixed(2)}`:"")+")")),b.ok||console.error($("agent finished with an error result"));break;case"error":console.error(""),console.error($(`agent error: ${b.message}`)),process.exit(1)}w&&Xi(w),process.stdout.write(`
3
- `),process.exit(I)}import{createInterface as Ya}from"node:readline/promises";import{createSession as wn,resolveAuth as Xa,getSession as Va,setTitle as Ja,slugify as Ka,touchSession as Cs,upsertSession as Qa,generateTitle as Za,resolveActiveProfile as Ps,resolveModelSetting as ec,readProjectConfig as tc,decodeJwtClaims as bs,isExpired as oc,loadTokens as nc}from"document360-engine";var kt=[{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:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],Ki=[{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 Yt(){let e=Math.max(...kt.map(o=>o.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:o,label:n}of Ki){t.push("",n);for(let r of kt.filter(s=>s.group===o))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function lr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return kt.filter(o=>o.name.startsWith(t))}function ar(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Eo(){console.log("");for(let e of Yt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}H();import{getSession as Qi}from"document360-engine";async function cr(e,t){let o=t.currentUuid(),n=o?Qi(o):void 0;return console.log(n?f(`
4
- (conversation reset \u2014 "${n.name}" is still available via /resume)
2
+ var ki=Object.defineProperty;var Jo=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var wi=(e,t)=>{for(var n in t)ki(e,n,{get:t[n],enumerable:!0})};import Ae from"picocolors";var B,yi,xi,$i,Ko,mt,f,x,L,E,R,Q,Pe,H=Jo(()=>{"use strict";B="#7f56d9",[yi,xi,$i]=[127,86,217],Ko=e=>Ae.isColorSupported?`\x1B[38;2;${yi};${xi};${$i}m${e}\x1B[39m`:e,mt=e=>Ae.bold(Ko(e)),f=e=>Ae.dim(e),x=e=>Ae.red(e),L=e=>Ae.yellow(e),E=e=>Ae.green(e),R=Ko,Q=e=>Ae.gray(e),Pe=e=>Ae.bold(e)});var os={};wi(os,{doctorCommand:()=>vt,renderDoctorChecks:()=>ns,runDoctorChecks:()=>ts});import{existsSync as ll}from"node:fs";import{d360GetAll as cl,getAccessToken as ul,isExpired as io,loadProfileMap as dl,loadTokens as pl,packageSkillsDir as es,projectConfigPath as fl,readProjectConfig as ml,resolveActiveProfile as gl,resolveAuth as hl,resolveModelSetting as kl}from"document360-engine";async function ts(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=hl("auto");t.push(o.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${o.kind==="api"?"API key":"subscription"}`});let r=kl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=ml(e);if(!s)return t.push({level:"fail",label:`No ${fl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Sources: docs grounded in ${d.length} path(s) (${d.slice(0,3).join(", ")}${d.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=gl(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.production?" \u26A0 PRODUCTION":""}`})}catch(I){return t.push({level:"fail",label:`Profile config: ${I.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let k=pl(g.name);k?io(k)&&!k.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):io(k)?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(k.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 y=dl(e,g.name);if(y?y.projectId&&g.project.projectId&&y.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${y.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(y.articles).length} articles, ${Object.keys(y.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(ll(es())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${es()}`,fix:"Reinstall document360-writer (broken install)"}),k&&(!io(k)||k.refreshToken)){let I={profile:g.name,connection:g.connection};try{await ul(I);let b=await cl(I,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${b.length} project(s) visible`})}catch(b){t.push({level:"fail",label:`API call failed: ${b.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function ns(e){let t=[""];for(let r of e){let s=r.level==="ok"?E("\u2713"):r.level==="warn"?L("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${R(r.fix)}`)}let n=e.filter(r=>r.level==="fail").length,o=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(n===0?E(`\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 vt(e,t){console.log(f("Running checks\u2026"));for(let n of ns(await ts(t.cwd)))console.log(n);return{kind:"continue"}}var nn=Jo(()=>{"use strict";H()});import{Command as Cu}from"commander";import{createRequire as Pu}from"node:module";import{AUTH_MODES as Su}from"document360-engine";import{input as Ei}from"@inquirer/prompts";import{loginPkce as Di,refreshTokens as Ii,toStoredTokens as tr,clearTokens as Ni,decodeJwtClaims as er,isExpired as _i,loadTokens as Mi,saveTokens as nr,resolveActiveProfile as Ft,setProfileProject as Li,readProjectConfig as Oi}from"document360-engine";H();import{select as bi}from"@inquirer/prompts";import{resolveActiveProfile as vi,setProfileProject as Ci,resolveProjectId as Pi,listWorkspaces as Si}from"document360-engine";async function gt(e,t){let n=vi(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??Pi(o);return{workspaces:await Si(o,r),projectId:r,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var Qo=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function jn(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 Wt(e,t,n,o,r){Ci(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function Zo(e,t,n){let o;try{o=await gt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=jn(o.workspaces,t);return r?(Wt(e,o.profile,o.projectId,r.id,r.name),console.log(E(`\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 tt(e,t){let n;try{n=await gt(e,t)}catch(k){console.log(x(`Could not list workspaces: ${k.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 k of o)console.log(` ${k.id===c?R("\u25CF"):" "} ${Qo(k)} ${f(k.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let d=await bi({message:"Select the Document360 workspace for this repo:",choices:o.map(k=>({name:`${Qo(k)}${k.id===c?" (current)":""}`,value:k.id}))}),g=o.find(k=>k.id===d);Wt(e,s,r,d,g?.name),console.log(E(`\u2713 Workspace set to "${g?.name??d}" for profile "${s}".`))}H();import Ti from"picocolors";function Ri(e=process.env){return e.FORCE_HYPERLINK==="0"||!Ti.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 ji(e,t=e,n){return Ri(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ai=/https?:\/\/[^\s\x1b]+/g;function ht(e,t){return e.replace(Ai,n=>ji(n,n,t))}function or(e){return{...er(e.idToken)??{},...er(e.accessToken)??{}}}function Ht(e){let t=or(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 Bt(e){let t=Ft(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 Di(n,{manual:e.manual,promptForRedirect:s=>Ei({message:s})},s=>console.log(ht(s))),r=tr(t.name,o);if(nr(r),An(r,t.name,s=>console.log(f(s))),console.log(""),console.log(E(`\u2713 Logged in to "${t.name}" as ${Ht(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{Oi(process.cwd())?.profiles?.[t.name]&&(console.log(""),await tt(process.cwd(),t.name))}catch{}}function An(e,t,n){let r=or(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Ft(process.cwd(),t).project.projectId)return;Li(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function rr(e){let t=Ft(process.cwd(),e.profile),n=t.connection,o=Mi(t.name);if(!o){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${R(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Ht(o)}`),_i(o))if(o.refreshToken)try{let r=tr(t.name,await Ii(n,o.refreshToken));nr(r),console.log(E(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(L("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${o.expiresAt}`))}async function sr(e){let t=Ft(process.cwd(),e.profile);Ni(t.name)?console.log(E(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as ir,writeProjectConfig as Ui,resolveActiveProfile as Wi,loadTokens as Fi,isExpired as Hi}from"document360-engine";function qt(e){let t=ir(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[n,o]of Object.entries(t.profiles)){let r=n===t.defaultProfile?R("\u25CF "):" ",s=o.production?L(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",d=Fi(n),g=d?Hi(d)&&!d.refreshToken?L("expired"):f("logged in"):f("not logged in");console.log(`${r}${R(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Gt(e,t){let n=ir(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,Ui(n,e);let o=n.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(E(`\u2713 Default profile is now "${t}"${o}`))}function zt(e,t){try{let n=Wi(e,t);console.log(""),console.log(`Profile ${R(n.name)}${n.production?L(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${n.connection.apiUrl}`)),console.log(f(` identity: ${n.connection.authorizationUrl}`)),console.log(f(` clientId: ${n.connection.clientId}`)),console.log(f(` scopes: ${n.connection.scopes.join(" ")}`)),console.log(f(` project: ${n.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${n.project.workspaceId?" "+n.project.workspaceId:" (none)"}`)),console.log("")}catch(n){console.log(x(n.message)),process.exitCode=1}}H();import{existsSync as Bi,readdirSync as qi,statSync as Gi}from"node:fs";import{join as zi}from"node:path";import{apiLogDir as Yi}from"document360-engine";function ar(){let e=Yi();if(console.log(""),console.log(`Document360 API logs: ${R(e)}`),!Bi(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=qi(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=(Gi(zi(e,n)).size/1024).toFixed(1);console.log(` ${n} ${f(`${o} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}H();import{createSession as Vi,resolveAuth as Xi,findByName as Ji,slugify as Ki,touchSession as Qi,upsertSession as Zi,resolveActiveProfile as ea}from"document360-engine";async function lr(e,t,n,o,r,s){let c=Xi(n);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let d=null;try{d=ea(e,r)}catch(b){console.error(x(`Document360 profile error: ${b.message}`)),process.exit(2)}d.production&&(console.error(L(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let g;if(o){let b=Ji(e,o);b||(console.error(x(`No saved session matches "${o}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),g=b.uuid,console.error(f(`Resuming "${b.name}"`))}let k=Vi({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),y=g??null,I=1;for await(let b of k.send(t))switch(b.type){case"session":if(!y){y=b.sessionId;let C=new Date().toISOString();Zi({uuid:y,name:Ki(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:C,updatedAt:C})}break;case"text":process.stdout.write(b.delta);break;case"tool":console.error(Q(` \u2699 ${b.name}`));break;case"result":I=b.ok?0:1,console.error(f(`(${b.inputTokens}\u2192${b.outputTokens} tokens`+(b.costUsd>0?`, $${b.costUsd<.01?b.costUsd.toFixed(4):b.costUsd.toFixed(2)}`:"")+")")),b.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${b.message}`)),process.exit(1)}y&&Qi(y),process.stdout.write(`
3
+ `),process.exit(I)}import{createInterface as rc}from"node:readline/promises";import{createSession as $o,resolveAuth as sc,getSession as ic,setTitle as ac,slugify as lc,touchSession as Rs,upsertSession as cc,generateTitle as uc,resolveActiveProfile as js,resolveModelSetting as dc,readProjectConfig as pc,decodeJwtClaims as Ss,isExpired as fc,loadTokens as mc}from"document360-engine";var kt=[{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:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],ta=[{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 Yt(){let e=Math.max(...kt.map(n=>n.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:n,label:o}of ta){t.push("",o);for(let r of kt.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 cr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return kt.filter(n=>n.name.startsWith(t))}function ur(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function En(){console.log("");for(let e of Yt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}H();import{getSession as na}from"document360-engine";async function dr(e,t){let n=t.currentUuid(),o=n?na(n):void 0;return console.log(o?f(`
4
+ (conversation reset \u2014 "${o.name}" is still available via /resume)
5
5
  `):f(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function Do(){return{kind:"exit"}}H();import{input as yt,confirm as Zi}from"@inquirer/prompts";import{basename as ur,join as Xt}from"node:path";import{existsSync as ot,readFileSync as el,readdirSync as tl,writeFileSync as ol}from"node:fs";import{writeProjectConfig as dr,readProjectConfig as nl,projectConfigPath as pr,writerDir as rl}from"document360-engine";function fr(e,t="berlin"){let o=pr(e);if(ot(o))return{created:!1,path:o,profileName:""};let n={projectId:hr(e)??ur(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:kr(e)};return dr(n,e),mr(e),{created:!0,path:o,profileName:t}}function mr(e){let t=Xt(rl(e),".gitignore");ot(t)||ol(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
- `),"utf8")}async function gr(){let e=process.cwd(),t=pr(e);if(ot(t)&&!await Zi({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let o=hr(e)??ur(e),n=await yt({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await yt({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await yt({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await yt({message:"Default connection profile name:",default:"berlin"}),d=await yt({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:d},production:!1}},defaultProfile:c,authoritativeSourceFiles:kr(e)},k=nl(e);return k?.terminologyGlossary&&(g.terminologyGlossary=k.terminologyGlossary),dr(g,e),mr(e),console.log(""),console.log(E(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${R(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function hr(e){let t=Xt(e,"package.json");if(!ot(t))return null;try{return JSON.parse(el(t,"utf8")).name??null}catch{return null}}function kr(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])ot(Xt(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=Xt(e,o);ot(n)&&!sl(n)&&t.push(o)}return t}function sl(e){try{return tl(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}H();import{readMcpConfig as Io,writeMcpConfig as yr}from"document360-engine";async function Vt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(il(),{kind:"continue"}):t==="add"?(ll(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(al(e.slice(1)),{kind:"continue"}):(console.log($(`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 il(){let e=Io(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
7
+ `)),{kind:"clear"}}async function Dn(){return{kind:"exit"}}H();import{input as wt,confirm as oa}from"@inquirer/prompts";import{basename as pr,join as Vt}from"node:path";import{existsSync as nt,readFileSync as ra,readdirSync as sa,writeFileSync as ia}from"node:fs";import{writeProjectConfig as fr,readProjectConfig as aa,projectConfigPath as mr,writerDir as la}from"document360-engine";function gr(e,t="berlin"){let n=mr(e);if(nt(n))return{created:!1,path:n,profileName:""};let o={projectId:wr(e)??pr(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:yr(e)};return fr(o,e),hr(e),{created:!0,path:n,profileName:t}}function hr(e){let t=Vt(la(e),".gitignore");nt(t)||ia(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
+ `),"utf8")}async function kr(){let e=process.cwd(),t=mr(e);if(nt(t)&&!await oa({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let n=wr(e)??pr(e),o=await wt({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await wt({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await wt({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await wt({message:"Default connection profile name:",default:"berlin"}),d=await wt({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:d},production:!1}},defaultProfile:c,authoritativeSourceFiles:yr(e)},k=aa(e);return k?.terminologyGlossary&&(g.terminologyGlossary=k.terminologyGlossary),fr(g,e),hr(e),console.log(""),console.log(E(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${R(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function wr(e){let t=Vt(e,"package.json");if(!nt(t))return null;try{return JSON.parse(ra(t,"utf8")).name??null}catch{return null}}function yr(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])nt(Vt(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=Vt(e,n);nt(o)&&!ca(o)&&t.push(n)}return t}function ca(e){try{return sa(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}H();import{readMcpConfig as In,writeMcpConfig as xr}from"document360-engine";async function Xt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(ua(),{kind:"continue"}):t==="add"?(da(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(pa(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 ua(){let e=In(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
9
9
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
10
- `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${R(o)} ${f("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${R(o)} ${f(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function ll(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log($("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log($(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=Io(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let d=1;d<n.length;d++)if(n[d]==="-H"||n[d]==="--header"){let g=n[++d],k=g?.match(/^([^:=]+)[:=](.+)$/);if(!k){console.log($(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[k[1].trim()]=k[2].trim()}else{console.log($(`Unexpected argument: ${n[d]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,yr(r),console.log(""),console.log(E(`\u2713 Registered "${t}" (${o})`)),console.log(L(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function al(e){let t=e[0];if(!t){console.log($("Usage: /mcp remove <name>"));return}let o=Io();if(!o.servers[t]){console.log($(`No server named "${t}".`));return}delete o.servers[t],yr(o),console.log(E(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}H();import{select as cl}from"@inquirer/prompts";import{computeSyncStatus as wr}from"document360-engine";function Kt(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
11
- `)}var $r={"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 $r).map(t=>({path:t.path,label:$r[t.status]}))}function wt(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
12
- `)}async function xr(e,t){let o=e[0];if(o==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let n=Jt((await wr({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:wt(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log($(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(f("Checking what needs publishing\u2026"));let n;try{n=Jt((await wr({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log($(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of n)console.log(` ${r.path} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>cl({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:wt(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Kt(o),display:`/publish ${o}`}}async function Qt(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
13
- `),display:"/audit"}}H();import{checkbox as ul}from"@inquirer/prompts";import{inventoryRepo as dl,readProjectConfig as br,writeProjectConfig as pl}from"document360-engine";function _o(e,t){let o=br(e);o&&(o.authoritativeSourceFiles=t,pl(o,e))}function Zt(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 vr(e,t){let o=t?.cwd??process.cwd();if(!br(o))return console.log($("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=dl(o);if(n.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(R("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(Zt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>ul({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${Zt(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"};_o(o,r),console.log(E(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}H();import{confirm as kl}from"@inquirer/prompts";import{applyPull as Rr,computeSyncStatus as jr,planPull as yl,D360AuthError as wl}from"document360-engine";H();var Cr=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:$,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:R,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:$,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:$,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:Q,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:Q,mark:"\xB7"}];function fl(e){return e.path?e.path:`${e.title??"(untitled)"} ${Q(`[${e.articleId}]`)}`}function eo(e){let t=[];for(let c of Cr){let d=e.entries.filter(g=>g.status===c.status);if(d.length!==0){t.push(""),t.push(Pe(c.header));for(let g of d)t.push(` ${c.paint(c.mark)} ${c.paint(fl(g))}${g.detail?Q(` (${g.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?E("\u2713 everything in sync"):[o>0?E(`\u2713 ${o} in sync`):null,...Cr.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(Q(" \xB7 "));return t.push(s+Q(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as ml}from"diff";import Pr from"picocolors";var Sr=80,gl="\x1B[48;2;74;28;28m",hl="\x1B[48;2;24;66;24m",Tr="\x1B[49m",No=e=>String(e).padStart(5);function Fe(e,t,o){let n=b=>{let C=b.replace(/\r\n/g,`
10
+ `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let n of t){let o=e.servers[n];if(o.type==="stdio")console.log(` ${R(n)} ${f("(stdio)")} ${o.command} ${(o.args??[]).join(" ")}`);else{let r=Object.keys(o.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${R(n)} ${f(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function da(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=In(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let d=1;d<o.length;d++)if(o[d]==="-H"||o[d]==="--header"){let g=o[++d],k=g?.match(/^([^:=]+)[:=](.+)$/);if(!k){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[k[1].trim()]=k[2].trim()}else{console.log(x(`Unexpected argument: ${o[d]}. 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,xr(r),console.log(""),console.log(E(`\u2713 Registered "${t}" (${n})`)),console.log(L(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function pa(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=In();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],xr(n),console.log(E(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}H();import{select as fa}from"@inquirer/prompts";import{computeSyncStatus as $r}from"document360-engine";function Kt(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
11
+ `)}var br={"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 br).map(t=>({path:t.path,label:br[t.status]}))}function yt(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
12
+ `)}async function vr(e,t){let n=e[0];if(n==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let o=Jt((await $r({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(E("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:yt(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 $r({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(E("\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(()=>fa({message:"Publish which article?",choices:[...o.length>1?[{name:`All ${o.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...o.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(n==="--all")return{kind:"forward-to-agent",prompt:yt(o.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Kt(n),display:`/publish ${n}`}}async function Qt(){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(`
13
+ `),display:"/audit"}}H();import{checkbox as ma}from"@inquirer/prompts";import{inventoryRepo as ga,readProjectConfig as Cr,writeProjectConfig as ha}from"document360-engine";function Nn(e,t){let n=Cr(e);n&&(n.authoritativeSourceFiles=t,ha(n,e))}function Zt(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 Pr(e,t){let n=t?.cwd??process.cwd();if(!Cr(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=ga(n);if(o.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(R("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(Zt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>ma({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${Zt(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"};Nn(n,r),console.log(E(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}H();import{confirm as $a}from"@inquirer/prompts";import{applyPull as Ar,computeSyncStatus as Er,planPull as ba,D360AuthError as va}from"document360-engine";H();var Sr=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:R,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:x,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:x,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:Q,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:Q,mark:"\xB7"}];function ka(e){return e.path?e.path:`${e.title??"(untitled)"} ${Q(`[${e.articleId}]`)}`}function en(e){let t=[];for(let c of Sr){let d=e.entries.filter(g=>g.status===c.status);if(d.length!==0){t.push(""),t.push(Pe(c.header));for(let g of d)t.push(` ${c.paint(c.mark)} ${c.paint(ka(g))}${g.detail?Q(` (${g.detail})`):""}`)}}let n=e.counts["in-sync"]??0,o=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=o===n?E("\u2713 everything in sync"):[n>0?E(`\u2713 ${n} in sync`):null,...Sr.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(Q(" \xB7 "));return t.push(s+Q(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as wa}from"diff";import Tr from"picocolors";var Rr=80,ya="\x1B[48;2;74;28;28m",xa="\x1B[48;2;24;66;24m",jr="\x1B[49m",_n=e=>String(e).padStart(5);function Fe(e,t,n){let o=b=>{let C=b.replace(/\r\n/g,`
14
14
  `);return C.endsWith(`
15
15
  `)||C===""?C:C+`
16
- `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),d=ml("a","b",r,s,"","",{context:3}),g=0,k=0,w=[];d.hunks.forEach((b,C)=>{C>0&&w.push(Pr.gray(" \u2026"));let x=b.oldStart,_=b.newStart;for(let z of b.lines){let G=z[0],ae=z.slice(1).slice(0,c);G==="-"?(k++,w.push(`${gl}${No(x++)} - ${ae}${Tr}`)):G==="+"?(g++,w.push(`${hl}${No(_++)} + ${ae}${Tr}`)):(w.push(Pr.gray(No(_))+" "+ae),x++,_++)}});let I=w.slice(0,Sr);return{added:g,removed:k,lines:I,hidden:Math.max(0,w.length-Sr)}}async function Ar(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await $l(t.cwd),{kind:"continue"};if(o==="pull")return await xl(t,e.slice(1)),{kind:"continue"};console.log($(`Unknown subcommand: /sync ${o}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof wl?console.log($(n.message)):console.log($(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function $l(e){console.log(f("Checking Document360 for drift\u2026"));let t=await jr({cwd:e});for(let o of eo(t))console.log(o);console.log("")}async function xl(e,t){let o=t[0];if(!o){console.log($("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),n=(await jr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(E("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${Pe(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await yl({cwd:e.cwd,relPath:r});console.log(""),console.log(`${R("\u25CF")} ${Pe(s.title)} ${Q(`(${s.path})`)}`);for(let k of s.notes)console.log(L(` \u26A0 ${k}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Fe(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.")),Rr({cwd:e.cwd},s);continue}let d=k=>k===1?"":"s";console.log(Q(` \u23BF Added ${c.added} line${d(c.added)}, removed ${c.removed} line${d(c.removed)}`));for(let k of c.lines)console.log(k);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>kl({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}Rr({cwd:e.cwd},s),console.log(E(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}H();import{statSync as Dl}from"node:fs";import{resolve as Il}from"node:path";import{estimateBulkCost as _l,planPartitions as Nl,readProjectConfig as Ml,resolveModelForOperation as Ll,runPartitioned as Ol,trackedArticlePaths as Ul}from"document360-engine";import q from"picocolors";import Mr from"wrap-ansi";import Fo from"string-width";H();import Ee from"picocolors";import Mo from"wrap-ansi";import Lo from"string-width";var Er=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Dr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Ir=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function bl(e){let t=e.replace(/\r/g,"").split(`
17
- `),o=[],n=0;for(;n<t.length;){let r=t[n];if(/^\s*```/.test(r)){let d=[];for(n++;n<t.length&&!/^\s*```/.test(t[n]);)d.push(t[n++]);n++,o.push({kind:"code",lines:d});continue}if(Er(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&Dr(t[n+1])){let d=Ir(r);n+=2;let g=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)g.push(Ir(t[n++]));o.push({kind:"table",header:d,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let d=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)d.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:d});continue}if(r.trim()===""){n++;continue}let c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!Er(t[n])&&!(t[n].includes("|")&&n+1<t.length&&Dr(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Oo(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ee.bold(t.slice(2,-2)):t.startsWith("`")?R(t.slice(1,-1)):Ee.italic(t.slice(1,-1)))}var vl=(e,t)=>e+" ".repeat(Math.max(0,t-Lo(e)));function Cl(e,t,o){let n=e.length,r=e.map((x,_)=>Math.max(Lo(x),...t.map(z=>Lo(z[_]??"")))),s=Math.max(24,o),c=3*n+1,d=[...r],g=()=>d.reduce((x,_)=>x+_,0)+c,k=0;for(;g()>s&&k++<1e4;){let x=-1,_=6;for(let z=0;z<n;z++)d[z]>_&&(_=d[z],x=z);if(x===-1)break;d[x]-=1}let w=(x,_,z)=>Ee.gray(x+d.map(G=>"\u2500".repeat(G+2)).join(_)+z),I=Ee.gray("\u2502"),b=(x,_)=>{let z=d.map((me,we)=>{let ge=x[we]??"",he=_?Ee.bold(ge):Oo(ge);return Mo(he,me,{hard:!0}).split(`
18
- `)}),G=Math.max(...z.map(me=>me.length)),ae=[];for(let me=0;me<G;me++)ae.push(d.map((we,ge)=>`${I} ${vl(z[ge][me]??"",we)} `).join("")+I);return ae.join(`
19
- `)},C=[w("\u250C","\u252C","\u2510"),b(e,!0)];return C.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach((x,_)=>{C.push(b(x,!1)),C.push(_===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),C.join(`
20
- `)}function Pl(e,t){switch(e.kind){case"heading":return Ee.bold(e.text);case"hr":return Ee.gray("\u2500".repeat(t));case"para":return Mo(Oo(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=Mo(Oo(o),Math.max(10,t-4)).split(`
21
- `);return" \u2022 "+n+r.map(s=>`
16
+ `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),d=wa("a","b",r,s,"","",{context:3}),g=0,k=0,y=[];d.hunks.forEach((b,C)=>{C>0&&y.push(Tr.gray(" \u2026"));let $=b.oldStart,N=b.newStart;for(let G of b.lines){let z=G[0],le=G.slice(1).slice(0,c);z==="-"?(k++,y.push(`${ya}${_n($++)} - ${le}${jr}`)):z==="+"?(g++,y.push(`${xa}${_n(N++)} + ${le}${jr}`)):(y.push(Tr.gray(_n(N))+" "+le),$++,N++)}});let I=y.slice(0,Rr);return{added:g,removed:k,lines:I,hidden:Math.max(0,y.length-Rr)}}async function Dr(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await Ca(t.cwd),{kind:"continue"};if(n==="pull")return await Pa(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 va?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function Ca(e){console.log(f("Checking Document360 for drift\u2026"));let t=await Er({cwd:e});for(let n of en(t))console.log(n);console.log("")}async function Pa(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 Er({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(E("\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 ba({cwd:e.cwd,relPath:r});console.log(""),console.log(`${R("\u25CF")} ${Pe(s.title)} ${Q(`(${s.path})`)}`);for(let k of s.notes)console.log(L(` \u26A0 ${k}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Fe(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.")),Ar({cwd:e.cwd},s);continue}let d=k=>k===1?"":"s";console.log(Q(` \u23BF Added ${c.added} line${d(c.added)}, removed ${c.removed} line${d(c.removed)}`));for(let k of c.lines)console.log(k);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>$a({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}Ar({cwd:e.cwd},s),console.log(E(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}H();import{statSync as Ma}from"node:fs";import{resolve as La}from"node:path";import{estimateBulkCost as Oa,planPartitions as Ua,readProjectConfig as Wa,resolveModelForOperation as Fa,runPartitioned as Ha,trackedArticlePaths as Ba}from"document360-engine";import q from"picocolors";import Or from"wrap-ansi";import Fn from"string-width";H();import Ee from"picocolors";import Mn from"wrap-ansi";import Ln from"string-width";var Ir=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Nr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),_r=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Sa(e){let t=e.replace(/\r/g,"").split(`
17
+ `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let d=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)d.push(t[o++]);o++,n.push({kind:"code",lines:d});continue}if(Ir(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Nr(t[o+1])){let d=_r(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(_r(t[o++]));n.push({kind:"table",header:d,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 d=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)d.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:d});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])&&!Ir(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Nr(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function On(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ee.bold(t.slice(2,-2)):t.startsWith("`")?R(t.slice(1,-1)):Ee.italic(t.slice(1,-1)))}var Ta=(e,t)=>e+" ".repeat(Math.max(0,t-Ln(e)));function Ra(e,t,n){let o=e.length,r=e.map(($,N)=>Math.max(Ln($),...t.map(G=>Ln(G[N]??"")))),s=Math.max(24,n),c=3*o+1,d=[...r],g=()=>d.reduce(($,N)=>$+N,0)+c,k=0;for(;g()>s&&k++<1e4;){let $=-1,N=6;for(let G=0;G<o;G++)d[G]>N&&(N=d[G],$=G);if($===-1)break;d[$]-=1}let y=($,N,G)=>Ee.gray($+d.map(z=>"\u2500".repeat(z+2)).join(N)+G),I=Ee.gray("\u2502"),b=($,N)=>{let G=d.map((me,ye)=>{let ge=$[ye]??"",he=N?Ee.bold(ge):On(ge);return Mn(he,me,{hard:!0}).split(`
18
+ `)}),z=Math.max(...G.map(me=>me.length)),le=[];for(let me=0;me<z;me++)le.push(d.map((ye,ge)=>`${I} ${Ta(G[ge][me]??"",ye)} `).join("")+I);return le.join(`
19
+ `)},C=[y("\u250C","\u252C","\u2510"),b(e,!0)];return C.push(t.length===0?y("\u2514","\u2534","\u2518"):y("\u251C","\u253C","\u2524")),t.forEach(($,N)=>{C.push(b($,!1)),C.push(N===t.length-1?y("\u2514","\u2534","\u2518"):y("\u251C","\u253C","\u2524"))}),C.join(`
20
+ `)}function ja(e,t){switch(e.kind){case"heading":return Ee.bold(e.text);case"hr":return Ee.gray("\u2500".repeat(t));case"para":return Mn(On(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=Mn(On(n),Math.max(10,t-4)).split(`
21
+ `);return" \u2022 "+o+r.map(s=>`
22
22
  `+s).join("")}).join(`
23
- `);case"code":return e.lines.map(o=>Ee.gray(" "+o)).join(`
24
- `);case"table":return Cl(e.header,e.rows,t)}}function Uo(e,t){let o=Math.max(20,t);return bl(e).map(n=>Pl(n,o)).join(`
23
+ `);case"code":return e.lines.map(n=>Ee.gray(" "+n)).join(`
24
+ `);case"table":return Ra(e.header,e.rows,t)}}function Un(e,t){let n=Math.max(20,t);return Sa(e).map(o=>ja(o,n)).join(`
25
25
 
26
- `)}H();import Sl from"picocolors";var Tl=[127,86,217],Rl=[22,38,43],_r={T:Tl,E:Rl},$t=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Wo=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,jl=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function Nr(){if(!Sl.isColorSupported)return[];let e=$t[0].length,t=[];for(let o=0;o<$t.length;o+=2){let n="";for(let r=0;r<e;r++){let s=_r[$t[o][r]],c=o+1<$t.length?_r[$t[o+1][r]]:void 0;s&&c?n+=`${Wo(s)}${jl(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${Wo(s)}\u2580\x1B[39m`:c?n+=`${Wo(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function xt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function fe(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function rt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var nt=(e,t)=>` ${e.padEnd(13)}${t}`;function Al(e,t){let o=[mt("\u270E document360-writer")+q.gray(` v${e.version}`),q.gray(" Reads your code, writes your docs."),"",nt("Claude:",`${e.claude}${q.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),nt("Document360:",e.configured?e.loggedOut?q.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?q.gray(` (${e.sessionHint})`):""}`:q.yellow("not set up \u2014 run /init")),nt("Profile:",e.configured?`${e.profile}${q.gray(` (${e.apiUrl})`)}${e.prod?q.bold(q.yellow(" \u26A0 PRODUCTION")):""}`:q.gray("\u2014 (run /init)")),nt("Project:",e.project),nt("Mode:",q.gray(e.mode)),nt("cwd:",q.gray(e.cwd))],n=Nr();if(n.length===0)return o.join(`
27
- `);let r=2,s=3,c=Fo(n[0]);if(!(t>=r+c+s+Math.max(...o.map(w=>Fo(w)))))return[...n.map(w=>" "+w),...o].join(`
28
- `);let g=Math.max(0,Math.floor((o.length-n.length)/2)),k=[];for(let w=0;w<Math.max(n.length+g,o.length);w++){let I=n[w-g]??" ".repeat(c);k.push((" ".repeat(r)+I+" ".repeat(s)+(o[w]??"")).trimEnd())}return k.join(`
29
- `)}var El={error:q.red,warn:q.yellow,ok:q.green,info:q.gray};function Ho(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Al(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,d=e.text.split(`
30
- `).flatMap(k=>Mr(k,s,{hard:!0}).split(`
26
+ `)}H();import Aa from"picocolors";var Ea=[127,86,217],Da=[22,38,43],Mr={T:Ea,E:Da},xt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Wn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,Ia=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Lr(){if(!Aa.isColorSupported)return[];let e=xt[0].length,t=[];for(let n=0;n<xt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Mr[xt[n][r]],c=n+1<xt.length?Mr[xt[n+1][r]]:void 0;s&&c?o+=`${Wn(s)}${Ia(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${Wn(s)}\u2580\x1B[39m`:c?o+=`${Wn(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function $t(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function fe(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function rt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var ot=(e,t)=>` ${e.padEnd(13)}${t}`;function Na(e,t){let n=[mt("\u270E document360-writer")+q.gray(` v${e.version}`),q.gray(" Reads your code, writes your docs."),"",ot("Claude:",`${e.claude}${q.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),ot("Document360:",e.configured?e.loggedOut?q.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?q.gray(` (${e.sessionHint})`):""}`:q.yellow("not set up \u2014 run /init")),ot("Profile:",e.configured?`${e.profile}${q.gray(` (${e.apiUrl})`)}${e.prod?q.bold(q.yellow(" \u26A0 PRODUCTION")):""}`:q.gray("\u2014 (run /init)")),ot("Project:",e.project),ot("Mode:",q.gray(e.mode)),ot("cwd:",q.gray(e.cwd))],o=Lr();if(o.length===0)return n.join(`
27
+ `);let r=2,s=3,c=Fn(o[0]);if(!(t>=r+c+s+Math.max(...n.map(y=>Fn(y)))))return[...o.map(y=>" "+y),...n].join(`
28
+ `);let g=Math.max(0,Math.floor((n.length-o.length)/2)),k=[];for(let y=0;y<Math.max(o.length+g,n.length);y++){let I=o[y-g]??" ".repeat(c);k.push((" ".repeat(r)+I+" ".repeat(s)+(n[y]??"")).trimEnd())}return k.join(`
29
+ `)}var _a={error:q.red,warn:q.yellow,ok:q.green,info:q.gray};function Hn(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return Na(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,d=e.text.split(`
30
+ `).flatMap(k=>Or(k,s,{hard:!0}).split(`
31
31
  `)),g=Math.max(0,d.length-c);return g>0&&(d=[...d.slice(0,c),q.dim(`\u2026 +${g} more lines`)]),`
32
- `+d.map((k,w)=>n+(w===0?R(" \u276F "):" ")+k+" ".repeat(Math.max(0,s-Fo(k))+1)+r).join(`
32
+ `+d.map((k,y)=>o+(y===0?R(" \u276F "):" ")+k+" ".repeat(Math.max(0,s-Fn(k))+1)+r).join(`
33
33
  `)}case"assistant":return`
34
- `+Uo(e.text,o);case"tool":{let n=e.arg!==null?q.gray(`${e.sep}(${e.arg})`):"";return`
35
- `+Mr(q.green("\u25CF ")+q.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?q.red:q.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(q.dim(` \u2026 +${e.hidden} lines`)),r.join(`
36
- `)}case"diff":{let n=c=>c===1?"":"s",s=[q.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&s.push(q.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
37
- `)}case"link":return e.lines.map(n=>R(ht(` \u2B95 ${n}`))).join(`
34
+ `+Un(e.text,n);case"tool":{let o=e.arg!==null?q.gray(`${e.sep}(${e.arg})`):"";return`
35
+ `+Or(q.green("\u25CF ")+q.bold(e.title)+o,n)}case"tool-result":{let o=e.isError?q.red:q.gray,r=e.lines.map((s,c)=>o((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(q.dim(` \u2026 +${e.hidden} lines`)),r.join(`
36
+ `)}case"diff":{let o=c=>c===1?"":"s",s=[q.gray(` \u23BF Added ${e.added} line${o(e.added)}, removed ${e.removed} line${o(e.removed)}`),...e.lines];return e.hidden>0&&s.push(q.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
37
+ `)}case"link":return e.lines.map(o=>R(ht(` \u2B95 ${o}`))).join(`
38
38
  `);case"preview":return`
39
39
  `+mt(`\u25A3 Preview \u2014 ${e.name}`)+`
40
40
 
41
- `+Uo(e.text,o);case"note":return`
42
- `+El[e.tone](ht(e.text));case"done":return`
43
- `+(e.ok?q.magenta("\u2736 "):q.red("\u2736 "))+q.gray(`Cooked for ${xt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${fe(e.costUsd)}`:""))}}function Lr(e,t){return e.map(o=>Ho(o,t)).join(`
44
- `)}var Bo=3;function qo(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function zo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Go(e,t){return t.map(o=>{let n=0;try{n=Dl(Il(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Yo(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
41
+ `+Un(e.text,n);case"note":return`
42
+ `+_a[e.tone](ht(e.text));case"done":return`
43
+ `+(e.ok?q.magenta("\u2736 "):q.red("\u2736 "))+q.gray(`Cooked for ${$t(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${fe(e.costUsd)}`:""))}}function Ur(e,t){return e.map(n=>Hn(n,t)).join(`
44
+ `)}var Bn=3;function qn(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 Gn(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function zn(e,t){return t.map(n=>{let o=0;try{o=Ma(La(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function Yn(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
45
45
  - Read ONLY the article files listed below. Do NOT read source code, other articles, or fetch the live portal \u2014 you already have everything you need.
46
46
  - Convert in place: wrap callouts, FAQs, tabs, accordions, and media embeds per the d360-markdown skill. Preserve all wording, headings, and structure; change only GFM\u2192DFM syntax.
47
47
  - Publish each as a draft with a single update call. Do NOT re-read, re-verify, or polish after converting, and skip articles already in DFM.
48
48
  - Work only on these files:
49
49
  `+e.paths.map(t=>`- ${t}`).join(`
50
- `)}function Xo(e,t,o){let n=e.reduce((d,g)=>d+g.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${fe(r)}\u2013${fe(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function Vo(e,t,o){let n=x=>t[x.index]?.paths.length??0,r=e.filter(x=>x.ok),s=e.filter(x=>!x.ok),c=e.reduce((x,_)=>x+n(_),0),d=r.reduce((x,_)=>x+n(_),0),g=e.reduce((x,_)=>x+_.costUsd,0),k=e.reduce((x,_)=>x+_.outputTokens,0),w=o==="api"?`${fe(g)} total`:rt(k),I=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=x=>`${x} article${x===1?"":"s"}`,C=[];if(s.length===0)C.push(`Converted ${b(c)} (${I}) successfully \xB7 ${w}.`);else{C.push(`Converted ${d}/${b(c)} (${I}) \xB7 ${w}.`),C.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let x of s)C.push(` \u2717 ${x.label}${x.error?` \u2014 ${x.error}`:""}`)}return C}async function Or(e,t){if(!Ml(t.cwd))return console.log($("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=qo(e),r=Ul(t.cwd,t.profileName);if(r.length===0)return console.log($("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=zo(r,o);if(s.length===0)return console.log($(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Nl(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:g,forced:k}=Ll(t.cwd,"light");if(!n){let w=_l({files:Go(t.cwd,s),op:"convert",model:g});o&&console.log(f(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let I of Xo(c,w,Bo))console.log(I);return console.log(f(`Model: ${g}${k?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Bo} agents at once) on ${g}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let w of Ol({cwd:t.cwd,partitions:c,promptFor:Yo,concurrency:Bo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if(w.type==="partition_status")w.status==="running"?console.log(f(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(E(` \u2713 ${w.label}`)):console.log($(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let I=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let b of Vo(w.results,c,I))console.log(w.ok?E(b):L(b))}}catch(w){console.log($(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}H();import{statSync as Ur}from"node:fs";import{resolve as Wr}from"node:path";import{estimateBulkCost as Wl,planPartitions as Fl,readDocsPlan as Hl,readProjectConfig as Fr,resolveModelForOperation as Bl,runPartitioned as ql}from"document360-engine";var Jo=5;function Ko(e){let t,o,n=!1;for(let s=0;s<e.length;s++){let c=e[s];if(c==="--run"||c==="--yes")n=!0;else{if(c==="--all")continue;c==="--scope"?t=e[++s]:c?.startsWith("--scope=")?t=c.slice(8):c&&!c.startsWith("--")&&(o=c)}}let r=s=>s?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0;return{scope:r(t),path:r(o),run:n}}function zl(e,t){let o=e.replace(/\\/g,"/").replace(/\/+$/,""),n=`${t.replace(/\/+$/,"")}/`;return o.startsWith(n)?o.slice(n.length):o}function Gl(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Yl(e,t,o){return o.filter(n=>{try{return Ur(Wr(e,t,n)).size===0}catch{return!0}})}function Qo(e,t,o){let n=new Map(t.map(r=>[r.path,r]));return o.map(r=>{let s=1;for(let c of n.get(r)?.sources??[])try{s+=Ur(Wr(e,c)).size}catch{}return{path:r,bytes:s}})}function Zo(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
50
+ `)}function Vn(e,t,n){let o=e.reduce((d,g)=>d+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(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${fe(r)}\u2013${fe(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function Xn(e,t,n){let o=$=>t[$.index]?.paths.length??0,r=e.filter($=>$.ok),s=e.filter($=>!$.ok),c=e.reduce(($,N)=>$+o(N),0),d=r.reduce(($,N)=>$+o(N),0),g=e.reduce(($,N)=>$+N.costUsd,0),k=e.reduce(($,N)=>$+N.outputTokens,0),y=n==="api"?`${fe(g)} total`:rt(k),I=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=$=>`${$} article${$===1?"":"s"}`,C=[];if(s.length===0)C.push(`Converted ${b(c)} (${I}) successfully \xB7 ${y}.`);else{C.push(`Converted ${d}/${b(c)} (${I}) \xB7 ${y}.`),C.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let $ of s)C.push(` \u2717 ${$.label}${$.error?` \u2014 ${$.error}`:""}`)}return C}async function Wr(e,t){if(!Wa(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=qn(e),r=Ba(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=Gn(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Ua(s),d=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:k}=Fa(t.cwd,"light");if(!o){let y=Oa({files:zn(t.cwd,s),op:"convert",model:g});n&&console.log(f(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let I of Vn(c,y,Bn))console.log(I);return console.log(f(`Model: ${g}${k?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Bn} 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 y of Ha({cwd:t.cwd,partitions:c,promptFor:Yn,concurrency:Bn,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if(y.type==="partition_status")y.status==="running"?console.log(f(` \u25B8 ${y.label} \u2014 converting\u2026`)):y.status==="done"?console.log(E(` \u2713 ${y.label}`)):console.log(x(` \u2717 ${y.label}`));else if(y.type==="run_done"){console.log("");let I=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let b of Xn(y.results,c,I))console.log(y.ok?E(b):L(b))}}catch(y){console.log(x(`Convert run failed: ${y.message}`))}return console.log(""),{kind:"continue"}}H();import{statSync as Fr}from"node:fs";import{resolve as Hr}from"node:path";import{estimateBulkCost as qa,planPartitions as Ga,readDocsPlan as za,readProjectConfig as Br,resolveModelForOperation as Ya,runPartitioned as Va}from"document360-engine";var Jn=5;function Kn(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 Xa(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function Ja(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Ka(e,t,n){return n.filter(o=>{try{return Fr(Hr(e,t,o)).size===0}catch{return!0}})}function Qn(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+=Fr(Hr(e,c)).size}catch{}return{path:r,bytes:s}})}function Zn(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
51
51
  - It is relative to ${t}/. Look up its entry (matching "path") in .d360-writer/plan.json for its purpose and the source files it draws from (AGENT-PLAN.md has the same table).
52
52
  - Read ONLY those source files plus any shared context you genuinely need, and ground every statement in them. Never invent UI labels, routes, or behaviour \u2014 if the source does not say it, omit it.
53
53
  - Write the finished article to ${t}/<path>, following the write-article structure and the project terminologyGlossary. Add the \`sources:\` frontmatter listing the files you actually read.
54
54
  - Emit \`<!-- SCREENSHOT -->\` placeholders where a visual helps, but do NOT author capture specs now.
55
55
  - Each article is independent. Do NOT write, edit, or publish any article not in this list, and do NOT publish to Document360 \u2014 these are local drafts.
56
56
  Articles:
57
- `+e.paths.map(o=>`- ${o}`).join(`
58
- `)}function en(e,t,o){let n=e.reduce((c,d)=>c+d.paths.length,0),[r,s]=t.usd;return[`Write ${n} article${n===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${fe(r)}\u2013${fe(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function tn(e,t,o){let n=x=>t[x.index]?.paths.length??0,r=e.filter(x=>x.ok),s=e.filter(x=>!x.ok),c=e.reduce((x,_)=>x+n(_),0),d=r.reduce((x,_)=>x+n(_),0),g=e.reduce((x,_)=>x+_.costUsd,0),k=e.reduce((x,_)=>x+_.outputTokens,0),w=o==="api"?`${fe(g)} total`:rt(k),I=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=x=>`${x} article${x===1?"":"s"}`,C=[];if(s.length===0)C.push(`Wrote ${b(c)} (${I}) successfully \xB7 ${w}.`),C.push("Review them in the docs tree, then /publish when ready.");else{C.push(`Wrote ${d}/${b(c)} (${I}) \xB7 ${w}.`),C.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let x of s)C.push(` \u2717 ${x.label}${x.error?` \u2014 ${x.error}`:""}`)}return C}function on(e,t,o){let n=(Fr(e)?.docsDir??"user-docs").replace(/\/+$/,""),r=t.map(d=>d.path);if(o.path){let d=zl(o.path,n);return r.includes(d)?{docsDir:n,targets:[d],planCount:r.length}:{docsDir:n,targets:[],planCount:r.length,reason:`"${d}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Gl(r,o.scope);if(o.scope&&s.length===0)return{docsDir:n,targets:[],planCount:r.length,reason:`No planned articles under "${o.scope}".`};let c=Yl(e,n,s);if(c.length===0){let d=o.scope?` under "${o.scope}"`:"";return{docsDir:n,targets:[],planCount:r.length,reason:`All planned articles${d} are already written. Use /write <path> to rewrite one.`}}return{docsDir:n,targets:c,planCount:r.length}}async function Hr(e,t){if(!Fr(t.cwd))return console.log($("No d360-writer config here. Run /init first.")),{kind:"continue"};let o=Hl(t.cwd);if(o.length===0)return console.log($("No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first.")),{kind:"continue"};let{scope:n,path:r,run:s}=Ko(e),{docsDir:c,targets:d,reason:g}=on(t.cwd,o,{scope:n,path:r});if(d.length===0)return console.log(g?L(g):L("Nothing to write.")),{kind:"continue"};let k=Fl(d),{model:w,forced:I}=Bl(t.cwd,"standard");if(!s&&!!!r){let C=Wl({files:Qo(t.cwd,o,d),op:"write",model:w});n&&console.log(f(`Scope: ${n} (${d.length} pending).`));for(let x of en(k,C,Jo))console.log(x);return console.log(f(`Model: ${w}${I?" (forced)":" \u2014 authoring; /model to override"}.`)),console.log(f(`Run /write${n?` --scope ${n}`:" --all"} --run to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Writing ${d.length} article${d.length===1?"":"s"} across ${k.length} partition${k.length===1?"":"s"} (\u2264${Jo} agents at once) on ${w}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let C of ql({cwd:t.cwd,partitions:k,promptFor:x=>Zo(x,c),concurrency:Jo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:w}))if(C.type==="partition_status")C.status==="running"?console.log(f(` \u25B8 ${C.label} \u2014 writing\u2026`)):C.status==="done"?console.log(E(` \u2713 ${C.label}`)):console.log($(` \u2717 ${C.label}`));else if(C.type==="run_done"){console.log("");let x=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let _ of tn(C.results,k,x))console.log(C.ok?E(_):L(_))}}catch(C){console.log($(`Write run failed: ${C.message}`))}return console.log(""),{kind:"continue"}}H();import{search as Xl}from"@inquirer/prompts";import{findByName as Vl,getSession as Jl,listSessions as Kl,relativeTime as qr}from"document360-engine";async function zr(e,t){let o=Kl(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(f("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=Vl(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log($(`No session matches "${n}".`)),Br(o),{kind:"continue"})}if(!process.stdin.isTTY)return Br(o),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>Xl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(d=>!c||d.name.toLowerCase().includes(c)||d.firstPrompt.toLowerCase().includes(c)).map(d=>({name:`${d.name} ${qr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=Jl(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Br(e){console.log("");for(let t of e.slice(0,15))console.log(` ${R(t.name)} ${f(qr(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as Ql}from"document360-engine";function nn(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 Gr(e,t){let o=nn(e.join(" "));if(!o)return console.log($("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(Ql(n,o)?console.log(E(`\u2713 Session renamed to "${o}"`)):console.log($("Could not find the current session record.")),{kind:"continue"}):(console.log($("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Yr,readProjectConfig as Zl,writeProjectConfig as ea}from"document360-engine";H();function rn(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=Zl(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let r=o??t;return Yr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},ea(n,e),null):`Unknown environment "${r}". Known: ${Yr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Xr(e,t){let o=e[0];if(!o)return qt(t.cwd),{kind:"continue"};if(o==="add"){let n=rn(t.cwd,e[1],e[2]);return n?(console.log($(n)),{kind:"continue"}):(console.log(E(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${R(`/profile ${e[1]}`)} then ${R("/login")}`),{kind:"continue"})}return zt(t.cwd,o),Gt(t.cwd,o),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as ta}from"@inquirer/prompts";import{readProjectConfig as oa,readUserConfig as Vr,resolveModelSetting as sn,writeUserConfig as Jr}from"document360-engine";var ye=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function to(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=ye.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function na(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 bt(e,t){let o=()=>{let s=sn(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Vr();return s.defaultModel?(delete s.defaultModel,Jr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${sn(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:o()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Jr({...Vr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=oa(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function Kr(e,t){let o=e[0]?.trim();if(!o){let c=sn(t.cwd);if(!process.stdin.isTTY)return console.log(`${Pe("Model:")} ${R(c.model??"Claude Code default")}`),console.log(f(` source: ${na(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=to(c),g;try{g=await t.withPausedInput(()=>ta({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ye[d].value,choices:ye.map((b,C)=>({name:`${b.label}${C===d?" \u2714":""}`,value:b.value,description:b.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:k,changed:w,effective:I}=bt(t.cwd,g??"default");for(let b of k)console.log(b.startsWith("\u26A0")?L(b):b.startsWith("\u2713")?E(b):f(b));return w&&await t.setModel(I),{kind:"continue"}}let{lines:n,changed:r,effective:s}=bt(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?E(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}oo();async function os(e,t){return await t.withPausedInput(()=>tt(t.cwd)),{kind:"clear"}}H();import{select as ma}from"@inquirer/prompts";import{resolveActiveProfile as ga,setProfileProject as ha,listProjects as ka}from"document360-engine";async function an(e,t){let o=ga(e,t),n={profile:o.name,connection:o.connection};return{projects:await ka(n),profile:o.name,environment:o.connection.name,current:o.project.projectId}}var ns=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function rs(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function cn(e,t,o){ha(e,t,{projectId:o,workspaceId:void 0,workspaceName:void 0})}async function ss(e,t){let o;try{o=await an(e,t)}catch(g){console.log($(`Could not list projects: ${g.message}`));return}let{projects:n,profile:r,current:s}=o;if(n.length===0){console.log(f("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===s?R("\u25CF"):" "} ${ns(g)} ${f(g.id)}`);console.log(f("Run: d360-writer project use <name>"));return}let c=await ma({message:"Select the Document360 project for this repo:",choices:n.map(g=>({name:`${ns(g)}${g.id===s?" (current)":""}`,value:g.id}))});cn(e,r,c);let d=n.find(g=>g.id===c);console.log(E(`\u2713 Project set to "${d?.name??c}" for profile "${r}". Next: pick a workspace.`))}async function is(e,t){return await t.withPausedInput(()=>ss(t.cwd,t.profileName)),{kind:"clear"}}H();import{resolveActiveProfile as ya}from"document360-engine";async function ls(e,t){let o=!1;try{o=ya(t.cwd).production}catch{}return o?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}H();var as=async(e,t)=>{try{await t.withPausedInput(()=>Bt({}))}catch(o){console.log($(`Login failed: ${o.message}`))}return{kind:"continue"}};H();import{resolveActiveProfile as wa,clearTokens as $a,clearProfileProject as xa}from"document360-engine";async function cs(e,t){let o;try{o=wa(t.cwd,t.profileName).name}catch(r){return console.log($(r.message)),{kind:"continue"}}let n=$a(o);return xa(t.cwd,o),console.log(n?E(`\u2713 Signed out of "${o}" and cleared its project/workspace selection.`):f(`Profile "${o}" 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 us}from"node:fs";import{isAbsolute as ba,join as va,resolve as Ca}from"node:path";import{readProjectConfig as Pa,screenshotPlaceholderIds as Sa}from"document360-engine";var ds=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function un(e){let t=!e.includes("--no-setup"),o=e.filter(r=>r!=="--no-setup");if(o[0]==="--list")return{mode:"list",scope:o[1]?ds(o[1]):void 0};let n=o[0];return!n||n==="--all"?{mode:"all",setup:t}:/[\\/]/.test(n)||n.endsWith(".md")?{mode:"scope",scope:ds(n),setup:t}:{mode:"single",id:n}}function dn(e,t){let o=Pa(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>ba(c)?va(c,d):Ca(e,c,d);return Sa(e,{scope:t}).map(({id:c,file:d})=>{let g=us(s(r,`${c}.png`))?"captured":us(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:g}})}var Ta={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Ra={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function pn(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(d=>d.file===s))r.push(` ${Ta[c.state]} ${c.id.padEnd(34)} ${Ra[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function ps(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(`
57
+ `+e.paths.map(n=>`- ${n}`).join(`
58
+ `)}function eo(e,t,n){let o=e.reduce((c,d)=>c+d.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: ${fe(r)}\u2013${fe(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function to(e,t,n){let o=$=>t[$.index]?.paths.length??0,r=e.filter($=>$.ok),s=e.filter($=>!$.ok),c=e.reduce(($,N)=>$+o(N),0),d=r.reduce(($,N)=>$+o(N),0),g=e.reduce(($,N)=>$+N.costUsd,0),k=e.reduce(($,N)=>$+N.outputTokens,0),y=n==="api"?`${fe(g)} total`:rt(k),I=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=$=>`${$} article${$===1?"":"s"}`,C=[];if(s.length===0)C.push(`Wrote ${b(c)} (${I}) successfully \xB7 ${y}.`),C.push("Review them in the docs tree, then /publish when ready.");else{C.push(`Wrote ${d}/${b(c)} (${I}) \xB7 ${y}.`),C.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let $ of s)C.push(` \u2717 ${$.label}${$.error?` \u2014 ${$.error}`:""}`)}return C}function no(e,t,n){let o=(Br(e)?.docsDir??"user-docs").replace(/\/+$/,""),r=t.map(d=>d.path);if(n.path){let d=Xa(n.path,o);return r.includes(d)?{docsDir:o,targets:[d],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${d}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Ja(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=Ka(e,o,s);if(c.length===0){let d=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${d} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function qr(e,t){if(!Br(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=za(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}=Kn(e),{docsDir:c,targets:d,reason:g}=no(t.cwd,n,{scope:o,path:r});if(d.length===0)return console.log(g?L(g):L("Nothing to write.")),{kind:"continue"};let k=Ga(d),{model:y,forced:I}=Ya(t.cwd,"standard");if(!s&&!!!r){let C=qa({files:Qn(t.cwd,n,d),op:"write",model:y});o&&console.log(f(`Scope: ${o} (${d.length} pending).`));for(let $ of eo(k,C,Jn))console.log($);return console.log(f(`Model: ${y}${I?" (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 ${d.length} article${d.length===1?"":"s"} across ${k.length} partition${k.length===1?"":"s"} (\u2264${Jn} agents at once) on ${y}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let C of Va({cwd:t.cwd,partitions:k,promptFor:$=>Zn($,c),concurrency:Jn,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:y}))if(C.type==="partition_status")C.status==="running"?console.log(f(` \u25B8 ${C.label} \u2014 writing\u2026`)):C.status==="done"?console.log(E(` \u2713 ${C.label}`)):console.log(x(` \u2717 ${C.label}`));else if(C.type==="run_done"){console.log("");let $=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let N of to(C.results,k,$))console.log(C.ok?E(N):L(N))}}catch(C){console.log(x(`Write run failed: ${C.message}`))}return console.log(""),{kind:"continue"}}H();import{search as Qa}from"@inquirer/prompts";import{findByName as Za,getSession as el,listSessions as tl,relativeTime as zr}from"document360-engine";async function Yr(e,t){let n=tl(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=Za(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),Gr(n),{kind:"continue"})}if(!process.stdin.isTTY)return Gr(n),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Qa({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(d=>!c||d.name.toLowerCase().includes(c)||d.firstPrompt.toLowerCase().includes(c)).map(d=>({name:`${d.name} ${zr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=el(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Gr(e){console.log("");for(let t of e.slice(0,15))console.log(` ${R(t.name)} ${f(zr(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as nl}from"document360-engine";function oo(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 Vr(e,t){let n=oo(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(nl(o,n)?console.log(E(`\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 Xr,readProjectConfig as ol,writeProjectConfig as rl}from"document360-engine";H();function ro(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=ol(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 Xr().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},rl(o,e),null):`Unknown environment "${r}". Known: ${Xr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Jr(e,t){let n=e[0];if(!n)return qt(t.cwd),{kind:"continue"};if(n==="add"){let o=ro(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(E(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${R(`/profile ${e[1]}`)} then ${R("/login")}`),{kind:"continue"})}return Gt(t.cwd,n),zt(t.cwd,n),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as sl}from"@inquirer/prompts";import{readProjectConfig as il,readUserConfig as Kr,resolveModelSetting as so,writeUserConfig as Qr}from"document360-engine";var we=[{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 tn(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=we.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function al(e){switch(e.source){case"project":return".d360-writer.json defaultModel (team setting)";case"user":return"~/.document360-writer/config.json (your /model setting)";case"env":return"ANTHROPIC_MODEL environment variable";case"claude-settings":return"Claude Code's own settings (~/.claude/settings.json)";case"claude-default":return"Claude Code default (no override configured)"}}function bt(e,t){let n=()=>{let s=so(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Kr();return s.defaultModel?(delete s.defaultModel,Qr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${so(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:n()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Qr({...Kr(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=il(e)?.defaultModel;return r&&o.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:o,changed:!0,effective:n()}}async function Zr(e,t){let n=e[0]?.trim();if(!n){let c=so(t.cwd);if(!process.stdin.isTTY)return console.log(`${Pe("Model:")} ${R(c.model??"Claude Code default")}`),console.log(f(` source: ${al(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=tn(c),g;try{g=await t.withPausedInput(()=>sl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:we[d].value,choices:we.map((b,C)=>({name:`${b.label}${C===d?" \u2714":""}`,value:b.value,description:b.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:k,changed:y,effective:I}=bt(t.cwd,g??"default");for(let b of k)console.log(b.startsWith("\u26A0")?L(b):b.startsWith("\u2713")?E(b):f(b));return y&&await t.setModel(I),{kind:"continue"}}let{lines:o,changed:r,effective:s}=bt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?E(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}nn();async function rs(e,t){return await t.withPausedInput(()=>tt(t.cwd)),{kind:"clear"}}H();import{select as wl}from"@inquirer/prompts";import{resolveActiveProfile as yl,setProfileProject as xl,listProjects as $l}from"document360-engine";async function ao(e,t){let n=yl(e,t),o={profile:n.name,connection:n.connection};return{projects:await $l(o),profile:n.name,environment:n.connection.name,current:n.project.projectId}}var ss=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function is(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 lo(e,t,n){xl(e,t,{projectId:n,workspaceId:void 0,workspaceName:void 0})}async function as(e,t){let n;try{n=await ao(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?R("\u25CF"):" "} ${ss(g)} ${f(g.id)}`);console.log(f("Run: d360-writer project use <name>"));return}let c=await wl({message:"Select the Document360 project for this repo:",choices:o.map(g=>({name:`${ss(g)}${g.id===s?" (current)":""}`,value:g.id}))});lo(e,r,c);let d=o.find(g=>g.id===c);console.log(E(`\u2713 Project set to "${d?.name??c}" for profile "${r}". Next: pick a workspace.`))}async function ls(e,t){return await t.withPausedInput(()=>as(t.cwd,t.profileName)),{kind:"clear"}}H();import{resolveActiveProfile as bl}from"document360-engine";async function cs(e,t){let n=!1;try{n=bl(t.cwd).production}catch{}return n?(console.log(L("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}H();var us=async(e,t)=>{try{await t.withPausedInput(()=>Bt({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};H();import{resolveActiveProfile as vl,clearTokens as Cl,clearProfileProject as Pl}from"document360-engine";async function ds(e,t){let n;try{n=vl(t.cwd,t.profileName).name}catch(r){return console.log(x(r.message)),{kind:"continue"}}let o=Cl(n);return Pl(t.cwd,n),console.log(o?E(`\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 ps}from"node:fs";import{isAbsolute as Sl,join as Tl,resolve as Rl}from"node:path";import{readProjectConfig as jl,screenshotPlaceholderIds as Al}from"document360-engine";var fs=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function co(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?fs(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:fs(o),setup:t}:{mode:"single",id:o}}function uo(e,t){let n=jl(e),o=n?.captureDir??"user-docs/_capture",r=n?.outputDir??"user-docs/_screenshots",s=(c,d)=>Sl(c)?Tl(c,d):Rl(e,c,d);return Al(e,{scope:t}).map(({id:c,file:d})=>{let g=ps(s(r,`${c}.png`))?"captured":ps(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:g}})}var El={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Dl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function po(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(d=>d.file===s))r.push(` ${El[c.state]} ${c.id.padEnd(34)} ${Dl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function ms(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(`
59
59
  `),"","For each: locate its <!-- SCREENSHOT --> block in user-docs/**/*.md; read the product source for","EXACT routes + stable data-testid selectors (never guess); enter the prepared context via","captureScope(); guard data prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts","(skip-with-TODO if a stable selector is missing). Do NOT run other skills. Report the specs you","wrote and any TODO data-testids."].join(`
60
- `)}async function no(e,t){let o=un(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let d of pn(dn(c,o.scope),o.scope))console.log(d);return{kind:"continue"}}if(o.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${o.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${o.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
61
- `),display:`/screenshot ${o.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${o.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${o.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${o.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
62
- `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as ja,readFileSync as Aa}from"node:fs";import{isAbsolute as Ea,join as Da,resolve as Ia}from"node:path";import{readProjectConfig as _a}from"document360-engine";async function Ct(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
63
- `),display:"/capture-setup"}}var fs="-".repeat(64);function fn(e){let t="user-docs/_capture";try{let w=_a(e);w?.captureDir&&(t=w.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let o=Ea(t)?Da(t,"CAPTURE-TESTID-REQUESTS.json"):Ia(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!ja(o))return null;let n;try{let w=JSON.parse(Aa(o,"utf8").replace(/^/,""));n=Array.isArray(w?.requests)?w.requests:[]}catch{return null}if(n.length===0)return null;let r=new Set(n.map(w=>w.file).filter(Boolean)).size,s=n.length,c=`${s} data-testid attribute${s===1?"":"s"}`,d=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,k=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${d}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",fs,`Read ${g} and ${k}. 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.",fs]}H();import{existsSync as Na,rmSync as Ma}from"node:fs";import{basename as ms,resolve as gs}from"node:path";import{input as La}from"@inquirer/prompts";import{readProjectConfig as Oa}from"document360-engine";var Ua=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function mn(e){let t=null;try{t=Oa(e)}catch{t=null}let o=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),n=o(t?.docsDir)??"user-docs",r=new Set([n]);for(let s of[o(t?.captureDir),o(t?.outputDir)])s&&s!==n&&!s.startsWith(`${n}/`)&&r.add(s);return[...r,...Ua].filter(s=>Na(gs(e,s)))}function gn(e,t){let o=[],n=[];for(let r of t)try{Ma(gs(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function hn(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${ms(e)}`]}async function hs(e,t){let o=mn(t.cwd);for(let d of hn(t.cwd,o))console.log(o.length===0?f(d):d);if(o.length===0)return{kind:"continue"};let n=ms(t.cwd);if((await t.withPausedInput(()=>La({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(L("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=gn(t.cwd,o);console.log(E(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let d of c)console.log($(` \u2717 ${d.path}: ${d.error}`));return console.log(f("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var ks={help:Eo,"?":Eo,clear:cr,exit:Do,quit:Do,init:gr,mcp:Vt,publish:xr,audit:Qt,scope:vr,sync:Ar,convert:Or,write:Hr,resume:zr,rename:Gr,profile:Xr,model:Kr,doctor:vt,workspace:os,project:is,"allow-prod":ls,login:as,logout:cs,screenshot:no,"capture-setup":Ct,reset:hs};function ys(e){let t=e.trim();if(!t.startsWith("/"))return null;let o=t.slice(1).split(/\s+/),n=(o[0]??"").toLowerCase();return n?{name:n,args:o.slice(1)}:null}H();var Wa={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"},$s=160,yn=200,xs=40;function se(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function ws(e){let t=Object.entries(e).filter(([,n])=>n!=null&&n!=="");if(t.length===0)return null;let o=t.slice(0,4).map(([n,r])=>`${n}: ${se(typeof r=="string"?r:JSON.stringify(r),xs)}`);return t.length>4&&o.push("\u2026"),se(o.join(", "),$s)}var De=e=>typeof e=="string"&&e?e:null,kn=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Fa(e){let t=De(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function Ha(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=De(t.title);if(!n)return null;let r=Fa(t.local_path);return o("Create article",`"${se(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=De(t.title),r=kn(t.article_id);return o("Update article",n?`"${se(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",kn(t.article_id)?`id ${kn(t.article_id)}\u2026`:null);case"d360_publish_article":{let n=t.version_number;return o("Publish article LIVE",typeof n=="number"?`v${n}`:null)}case"d360_unpublish_article":return o("Unpublish article",null);case"d360_create_category":return o("Create category",De(t.name)?`"${se(De(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=De(t.file_path);return o("Upload image",n?se(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function ro(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let k=Ha(c,t);if(k)return k}let d=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${d}`,sep:" ",arg:ws(t)}}let o=Wa[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:se(n,$s)}:{title:e,sep:"",arg:ws(t)}}function Ba(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let o of["name","title","slug","id"])if(typeof t[o]=="string"&&t[o])return t[o];return null}function qa(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(Ba).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>se(r,yn))]}if(t!==null&&typeof t=="object"){let o=Object.entries(t).filter(([,n])=>n!==null&&(typeof n=="string"||typeof n=="number"||typeof n=="boolean")).slice(0,6).map(([n,r])=>`${n}: ${se(String(r),xs)}`);return o.length>0?[se(o.join(" \xB7 "),yn)]:null}return null}function za(e){let t=De(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function Ga(e,t,o){switch(e){case"d360_create_article":{let n=za(t?.local_path);return[`\u2713 draft created${n?` \xB7 ${n}`:""}`]}case"d360_update_article":return["\u2713 draft updated"];case"d360_fork_article":return["\u2713 forked to a new draft"];case"d360_publish_article":return["\u2713 PUBLISHED LIVE \u2014 visible to readers"];case"d360_unpublish_article":return["\u2713 reverted to draft (removed from readers)"];case"d360_create_category":{let n=De(t?.name);return[`\u2713 category created${n?` \xB7 "${n}"`:""}`]}case"d360_upload_drive_file":{let n=o.match(/https?:\/\/\S+/);return[`\u2713 uploaded${n?` \xB7 ${se(n[0],120)}`:""}`]}default:return null}}function so(e,t=4,o,n){let r=e.replace(/\r\n/g,`
64
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=Ga(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=qa(r)??r.split(`
65
- `);return{lines:s.slice(0,t).map(c=>se(c,yn)),hidden:Math.max(0,s.length-t)}}function io(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function lo(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),r=(Array.isArray(o)?o[0]:o)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function ao(e){try{let t=JSON.parse(e),n=(Array.isArray(t)?t[0]:t)?.url;return typeof n=="string"&&/^https?:\/\//.test(n)?n:null}catch{return null}}var rc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function sc(e,t,o,n){if(rc.test(e))try{let r=Ps(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=lo(t,o),d=ao(o);e.endsWith("publish_article")&&d&&console.log(R(` \u2B95 Live: ${d}`)),c&&s&&console.log(R(` \u2B95 Preview: ${io(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Ss(e=process.cwd(),t="auto",o){let n=Xa(t);n.kind==="none"&&(console.error(""),console.error($("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${R("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${R('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),ic(e,o),n.kind==="subscription"&&(n.stored?console.log(f(" Using your Claude subscription (no API key set).")):console.log(f(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=wn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=Ya({input:process.stdin,output:process.stdout}),g=[],k=null,w=!1;d.on("line",C=>{if(k){let x=k;k=null,x(C)}else g.push(C)}),d.on("close",()=>{if(w=!0,k){let C=k;k=null,C(null)}});function I(){return g.length>0?Promise.resolve(g.shift()):w?Promise.resolve(null):(process.stdout.write(R("> ")),new Promise(C=>{k=C}))}let b={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=wn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async C=>s.setModel(C),withPausedInput:async C=>{d.pause();try{return await C()}finally{d.resume()}}};try{for(;;){let C=await I();if(C===null)break;let x=C.trim();if(x){if(x.startsWith("/")){let _=ys(x);if(!_)continue;let z=ks[_.name];if(!z){console.log($(`Unknown command: /${_.name}`)),console.log(f("Type /help for the list."));continue}let G=await z(_.args,b);if(G.kind==="exit")break;if(G.kind==="clear"){b.restartAgent();continue}if(G.kind==="allow-prod"){r=!0,b.restartAgent(),console.log(E("\u2713 Production writes authorized for this session.")),console.log("");continue}if(G.kind==="resume"){s.close(),s=wn({cwd:e,resume:G.uuid,profileName:o,allowProdWrites:r});let ae=Va(G.uuid);c={uuid:G.uuid,firstPrompt:ae?.firstPrompt??null,titleFired:!0},Cs(G.uuid),console.log(E(`\u2713 Resumed "${G.name}"`)),console.log("");continue}G.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=G.display??G.prompt),await vs(s,G.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=x),await vs(s,x,n,c,e)}}}finally{s.close(),d.close()}}function ic(e,t){console.log(""),console.log(mt("document360-writer")),console.log(f(` cwd: ${e}`));let o=ec(e);console.log(f(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(lc(e,t)),tc(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function lc(e,t){try{let o=Ps(e,t),n=o.production?L(" \u26A0 PRODUCTION"):"",r=nc(o.name);if(!r)return f(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...bs(r.idToken)??{},...bs(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return oc(r)&&!r.refreshToken?L(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return f(` Document360: ${o.message.split(".")[0]}`)}}function ac(){console.error(""),console.error(`Sign in with your Claude subscription: run ${R("claude")} once, then retry.`),console.error(` (No Claude Code? ${R("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${R("https://console.anthropic.com/settings/keys")}`)}function cc(e,t,o){e.uuid=t;let n=new Date().toISOString();Qa({uuid:t,name:Ka(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function uc(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||Za(n,t).then(r=>{r&&Ja(o,r)}).catch(()=>{})}async function vs(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))dc(c,n,r,o,s)}function dc(e,t,o,n,r){switch(e.type){case"session":t.uuid||cc(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=ro(e.name,e.input);s&&(process.stdout.write(`
60
+ `)}async function on(e,t){let n=co(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let d of po(uo(c,n.scope),n.scope))console.log(d);return{kind:"continue"}}if(n.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${n.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${n.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
61
+ `),display:`/screenshot ${n.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${n.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${n.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${n.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
62
+ `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as Il,readFileSync as Nl}from"node:fs";import{isAbsolute as _l,join as Ml,resolve as Ll}from"node:path";import{readProjectConfig as Ol}from"document360-engine";async function Ct(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
63
+ `),display:"/capture-setup"}}var gs="-".repeat(64);function fo(e){let t="user-docs/_capture";try{let y=Ol(e);y?.captureDir&&(t=y.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=_l(t)?Ml(t,"CAPTURE-TESTID-REQUESTS.json"):Ll(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!Il(n))return null;let o;try{let y=JSON.parse(Nl(n,"utf8").replace(/^/,""));o=Array.isArray(y?.requests)?y.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map(y=>y.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,d=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,k=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${d}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",gs,`Read ${g} and ${k}. 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.",gs]}import{writeFileSync as Ul}from"node:fs";import{devHintsGuidePath as Wl,hintsDir as Fl,ensureDir as Hl}from"document360-engine";var hs="-".repeat(64),Bl=1;function mo(){return`# Dev \u2192 Docs hints \u2014 protocol guide
66
64
 
67
- `),console.log(`${E("\u25CF")} ${Pe(s.title)}${s.arg!==null?Q(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Fe(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(Q(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let d of s.lines)console.log(d);s.hidden>0&&console.log(f(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=so(e.output,4,e.isError?void 0:s.name,s.input),d=e.isError?$:Q;c.lines.forEach((g,k)=>console.log(d((k===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||sc(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
68
- `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Cs(t.uuid),t.titleFired||uc(t,o));break;case"error":console.error(""),console.error($(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&ac();break}}import{render as iu}from"ink";import{resolveAuth as lu}from"document360-engine";import{useCallback as X,useEffect as _e,useMemo as mo,useRef as K,useState as F}from"react";import{Box as J,Text as v,useApp as xc,useInput as bc,useStdout as vc}from"ink";import{existsSync as Hs,readFileSync as Bs,readdirSync as Zs}from"node:fs";import{basename as go,isAbsolute as Cc,join as St}from"node:path";import{createSession as qs,loginPkce as Pc,toStoredTokens as Sc,saveTokens as Tc,getAccessToken as Rc,resolveActiveProfile as le,resolveProjectId as jc,getArticle as Ac,decodeJwtClaims as st,isExpired as Ie,loadTokens as He,clearTokens as Ec,clearProfileProject as Dc,setTitle as Ic,slugify as _c,touchSession as bn,upsertSession as Nc,generateTitle as zs,findByName as Mc,listSessions as Lc,renameSession as Oc,suggestNextAction as Uc,readProjectConfig as ie,writeProjectConfig as Wc,resolveModelSetting as Tn,loadProfileMap as Gs,applyPull as Fc,computeSyncStatus as vn,planPull as Hc,inventoryRepo as Bc,knownEnvironments as qc,resolveEnvironment as zc,planPartitions as Ys,partitionEvenly as Gc,screenshotPlaceholderIds as Yc,trackedArticlePaths as Xc,runPartitioned as Cn,estimateBulkCost as Xs,resolveModelForOperation as Pn,readDocsPlan as Vc}from"document360-engine";import{existsSync as pc,mkdirSync as fc,readFileSync as mc,writeFileSync as gc}from"node:fs";import{join as Ts}from"node:path";import{writerDir as hc}from"document360-engine";function Rs(e){return Ts(hc(e),".sessions")}function js(e,t){return Ts(Rs(e),`${t}.json`)}function As(e,t,o){try{fc(Rs(e),{recursive:!0});let n=o.filter(r=>r.kind!=="banner");gc(js(e,t),JSON.stringify({v:1,items:n}),"utf8")}catch{}}function $n(e,t){try{let o=js(e,t);if(!pc(o))return[];let n=JSON.parse(mc(o,"utf8"));return Array.isArray(n.items)?n.items:[]}catch{return[]}}var kc=e=>`\x1B]0;${e}\x07`,yc=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function Es(e){process.stdout.isTTY&&process.stdout.write(e)}function Pt(e){Es(kc(e))}function co(e){Es(yc(e))}var uo=["\xB7 "," \xB7"],xn="\u{1F7E3}";oo();var wc=/^(?:\d+[.)]|[-*•])\s+/;function Ds(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
69
- `)){let c=s.trim().replace(wc,""),d=c.match(/^`([^`]+)`$/);d&&(c=d[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!n.has(g[1].toLowerCase()))continue;let k=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(k)||r.push(k),r.length>=o)break}return r}H();var $c=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Is(e){return e.replace(/\r\n?/g,`
70
- `)}function _s(e){return e.includes(`
71
- `)||e.length>200}function Ns(e,t){let o=t.split(`
72
- `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function Ms(e,t){return e.replace($c,o=>t.get(o)??o)}function Ls(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function po(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
73
- `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function fo(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function Os(e,t,o){let n=fo(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function Us(e,t,o){let n=e[fo(e,t)];return o==="start"?n.start:n.end}function Ws(e,t,o){let n=Math.max(1,o),r=e.split(`
74
- `),s=0;for(let c=r.length-1;c>=0;c--){let d=r[c];if(s+=Math.max(1,Math.ceil(d.length/n)),s>=t){let g=s-t;return{text:[g>0?d.slice(g*n):d,...r.slice(c+1)].join(`
75
- `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function Fs(e){let t=!1,o=0,n=0;for(let r of e.split(`
76
- `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as Sn,jsx as S,jsxs as U}from"react/jsx-runtime";var Jc={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},Vs=` 1. /init \u2014 pick your Document360 environment & scaffold config
65
+ <!-- Guide version: ${Bl} \xB7 maintained by document360-writer (/devhints). -->
66
+
67
+ This repo's user documentation is written by **document360-writer**, a separate documentation agent.
68
+ It can read your code, but it can't read your *intent* \u2014 why a change matters to an end user, the
69
+ gotchas, the role-gating. You (the coding agent building features here) know that at the moment you
70
+ build it. A **doc hint** captures it so the docs agent can update the help center later.
71
+
72
+ ## When to write a hint
73
+
74
+ Write one when you ship a change with a **user-facing effect**:
75
+
76
+ - a new or changed screen, button, menu, flow, dialog, or setting
77
+ - a new/changed/removed API field, endpoint, CLI flag, or permission
78
+ - a behavior change a user would notice, or a **deprecation/removal**
79
+
80
+ **Do NOT** write one for internal-only work (refactors, test changes, build/CI, dependency bumps,
81
+ performance tweaks with no visible effect). One hint per shippable user-facing surface. When unsure,
82
+ write it \u2014 a spurious hint is cheap; a missed feature is an undocumented feature.
83
+
84
+ ## How to write a hint
85
+
86
+ Create **one new file** under \`.d360-writer/hints/\` named
87
+ \`<YYYY-MM-DD>-<short-slug>-<random>.md\` (e.g. \`2026-06-17-export-csv-button-a1b2c3.md\`).
88
+ One file per hint \u2014 never append to an existing one (distinct files merge cleanly across branches).
89
+ The file is markdown with a YAML frontmatter header:
90
+
91
+ \`\`\`markdown
92
+ ---
93
+ id: a1b2c3 # any short unique token
94
+ created: 2026-06-17T10:22:00Z # UTC now
95
+ author: <your agent / git user>
96
+ branch: <current git branch>
97
+ commit: <current HEAD short sha> # the docs agent verifies your change against this
98
+ kind: new # new | update | deprecate
99
+ sources: # REQUIRED \u2014 repo-relative files this change touched
100
+ - packages/api/src/export/csv.ts
101
+ - packages/web/src/components/ExportButton.tsx
102
+ surface: "Export CSV button on the run-detail toolbar" # the user-facing thing, in plain words
103
+ target: "" # OPTIONAL free-text hint at the article; NEVER guess a file path
104
+ status: open # always 'open' \u2014 the docs agent flips this to 'consumed'
105
+ ---
106
+ Prose: what you built and the gotchas a code diff would miss \u2014 e.g. "the button is admin-only",
107
+ "the toast fires only on the second save", "CSV uses the workspace's locale, not the user's".
108
+ \`\`\`
109
+
110
+ ### Field rules
111
+ - \`sources\` is **required** and must be real repo-relative paths \u2014 the docs agent verifies the
112
+ change against them, so a wrong path means your hint gets flagged, not trusted.
113
+ - \`commit\` = your current HEAD. \`kind\`: \`new\` (new feature/article), \`update\` (changed existing
114
+ surface), \`deprecate\` (removed/sunset).
115
+ - \`target\` is a free-text nudge only ("probably the Exports guide"). **Never invent an article path** \u2014
116
+ you can't see the docs tree; the docs agent resolves the right article from \`sources\`.
117
+ - Leave \`status: open\` and omit the \`consumed_*\` fields \u2014 the docs agent fills those when it
118
+ documents your hint.
119
+ - Keep the body about user-facing intent, not implementation. Don't propose docs structure.
120
+
121
+ That's it \u2014 drop the file and keep building. The docs agent picks it up on its next analysis run.
122
+ `}async function ks(e,t){Hl(Fl(t.cwd)),Ul(Wl(t.cwd),mo());for(let n of go())console.log(n);return{kind:"continue"}}function go(){return["\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md and created the .d360-writer/hints/ inbox.","","\u{1F4CB} One-time setup \u2014 connect this repo's coding agent to the docs agent."," Add the block below to your SOURCE repo's agent memory (CLAUDE.md / AGENTS.md), then commit"," .d360-writer/DEV-HINTS-GUIDE.md so teammates' agents get the same instruction:","",hs,"## Documentation hints (for the docs agent)","","When you ship a user-facing change (new/changed/removed screen, button, flow, setting, API field,","CLI flag, or permission), record a doc hint so the documentation team's agent can update the help","center. How: read `.d360-writer/DEV-HINTS-GUIDE.md` and follow it \u2014 create one markdown file under","`.d360-writer/hints/`. Do this as part of finishing the feature. Skip it only for changes with no","user-visible effect (refactors, tests, build, deps).",hs,"","Then keep building as usual. On its next analysis run (/audit), the docs agent reads open hints,","verifies each against your code, writes/updates the docs, and marks the hint consumed."]}H();import{existsSync as ql,rmSync as Gl}from"node:fs";import{basename as ws,resolve as ys}from"node:path";import{execFileSync as zl}from"node:child_process";import{input as Yl}from"@inquirer/prompts";import{readProjectConfig as Vl}from"document360-engine";var Xl=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function ho(e){let t=null;try{t=Vl(e)}catch{t=null}let n=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),o=n(t?.docsDir)??"user-docs",r=new Set([o]);for(let s of[n(t?.captureDir),n(t?.outputDir)])s&&s!==o&&!s.startsWith(`${o}/`)&&r.add(s);return[...r,...Xl].filter(s=>ql(ys(e,s)))}function ko(e,t){let n=[],o=[];for(let r of t)try{Gl(ys(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}return{removed:n,failed:o}}function Jl(e){try{return zl("git",["status","--porcelain","--",".d360-writer/hints"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
123
+ `).filter(n=>n.trim()&&/\.md\s*$/.test(n)).length}catch{return 0}}function wo(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let n=Jl(e);return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"",...n>0?[`\u26A0 ${n} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${ws(e)}`]}async function xs(e,t){let n=ho(t.cwd);for(let d of wo(t.cwd,n))console.log(n.length===0?f(d):d);if(n.length===0)return{kind:"continue"};let o=ws(t.cwd);if((await t.withPausedInput(()=>Yl({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(L("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=ko(t.cwd,n);console.log(E(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let d of c)console.log(x(` \u2717 ${d.path}: ${d.error}`));return console.log(f("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var $s={help:En,"?":En,clear:dr,exit:Dn,quit:Dn,init:kr,mcp:Xt,publish:vr,audit:Qt,scope:Pr,sync:Dr,convert:Wr,write:qr,resume:Yr,rename:Vr,profile:Jr,model:Zr,doctor:vt,workspace:rs,project:ls,"allow-prod":cs,login:us,logout:ds,screenshot:on,"capture-setup":Ct,devhints:ks,reset:xs};function bs(e){let t=e.trim();if(!t.startsWith("/"))return null;let n=t.slice(1).split(/\s+/),o=(n[0]??"").toLowerCase();return o?{name:o,args:n.slice(1)}:null}H();var Kl={Bash:"command",PowerShell:"command",Read:"file_path",Write:"file_path",Edit:"file_path",NotebookEdit:"notebook_path",Glob:"pattern",Grep:"pattern",WebFetch:"url",WebSearch:"query",Agent:"description",Task:"description",Skill:"skill"},Cs=160,xo=200,Ps=40;function ie(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function vs(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}: ${ie(typeof r=="string"?r:JSON.stringify(r),Ps)}`);return t.length>4&&n.push("\u2026"),ie(n.join(", "),Cs)}var De=e=>typeof e=="string"&&e?e:null,yo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Ql(e){let t=De(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 Zl(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=De(t.title);if(!o)return null;let r=Ql(t.local_path);return n("Create article",`"${ie(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=De(t.title),r=yo(t.article_id);return n("Update article",o?`"${ie(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",yo(t.article_id)?`id ${yo(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",De(t.name)?`"${ie(De(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=De(t.file_path);return n("Upload image",o?ie(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 k=Zl(c,t);if(k)return k}let d=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${d}`,sep:" ",arg:vs(t)}}let n=Kl[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:ie(o,Cs)}:{title:e,sep:"",arg:vs(t)}}function ec(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 tc(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(ec).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>ie(r,xo))]}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}: ${ie(String(r),Ps)}`);return n.length>0?[ie(n.join(" \xB7 "),xo)]:null}return null}function nc(e){let t=De(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function oc(e,t,n){switch(e){case"d360_create_article":{let o=nc(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=De(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 ${ie(o[0],120)}`:""}`]}default:return null}}function sn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
124
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=oc(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=tc(r)??r.split(`
125
+ `);return{lines:s.slice(0,t).map(c=>ie(c,xo)),hidden:Math.max(0,s.length-t)}}function an(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function ln(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 cn(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 gc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function hc(e,t,n,o){if(gc.test(e))try{let r=js(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=ln(t,n),d=cn(n);e.endsWith("publish_article")&&d&&console.log(R(` \u2B95 Live: ${d}`)),c&&s&&console.log(R(` \u2B95 Preview: ${an(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function As(e=process.cwd(),t="auto",n){let o=sc(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${R("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${R('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),kc(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=$o({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=rc({input:process.stdin,output:process.stdout}),g=[],k=null,y=!1;d.on("line",C=>{if(k){let $=k;k=null,$(C)}else g.push(C)}),d.on("close",()=>{if(y=!0,k){let C=k;k=null,C(null)}});function I(){return g.length>0?Promise.resolve(g.shift()):y?Promise.resolve(null):(process.stdout.write(R("> ")),new Promise(C=>{k=C}))}let b={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=$o({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async C=>s.setModel(C),withPausedInput:async C=>{d.pause();try{return await C()}finally{d.resume()}}};try{for(;;){let C=await I();if(C===null)break;let $=C.trim();if($){if($.startsWith("/")){let N=bs($);if(!N)continue;let G=$s[N.name];if(!G){console.log(x(`Unknown command: /${N.name}`)),console.log(f("Type /help for the list."));continue}let z=await G(N.args,b);if(z.kind==="exit")break;if(z.kind==="clear"){b.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,b.restartAgent(),console.log(E("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=$o({cwd:e,resume:z.uuid,profileName:n,allowProdWrites:r});let le=ic(z.uuid);c={uuid:z.uuid,firstPrompt:le?.firstPrompt??null,titleFired:!0},Rs(z.uuid),console.log(E(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await Ts(s,z.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=$),await Ts(s,$,o,c,e)}}}finally{s.close(),d.close()}}function kc(e,t){console.log(""),console.log(mt("document360-writer")),console.log(f(` cwd: ${e}`));let n=dc(e);console.log(f(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(wc(e,t)),pc(e)||console.log(L(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function wc(e,t){try{let n=js(e,t),o=n.production?L(" \u26A0 PRODUCTION"):"",r=mc(n.name);if(!r)return f(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...Ss(r.idToken)??{},...Ss(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return fc(r)&&!r.refreshToken?L(` Document360: profile "${n.name}"${o} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${n.name}"${o}`)}catch(n){return f(` Document360: ${n.message.split(".")[0]}`)}}function yc(){console.error(""),console.error(`Sign in with your Claude subscription: run ${R("claude")} once, then retry.`),console.error(` (No Claude Code? ${R("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${R("https://console.anthropic.com/settings/keys")}`)}function xc(e,t,n){e.uuid=t;let o=new Date().toISOString();cc({uuid:t,name:lc(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function $c(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||uc(o,t).then(r=>{r&&ac(n,r)}).catch(()=>{})}async function Ts(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))bc(c,o,r,n,s)}function bc(e,t,n,o,r){switch(e.type){case"session":t.uuid||xc(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(`
126
+
127
+ `),console.log(`${E("\u25CF")} ${Pe(s.title)}${s.arg!==null?Q(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Fe(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(Q(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let d of s.lines)console.log(d);s.hidden>0&&console.log(f(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=sn(e.output,4,e.isError?void 0:s.name,s.input),d=e.isError?x:Q;c.lines.forEach((g,k)=>console.log(d((k===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||hc(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
128
+ `),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&&(Rs(t.uuid),t.titleFired||$c(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&yc();break}}import{render as $u}from"ink";import{resolveAuth as bu}from"document360-engine";import{useCallback as V,useEffect as Ne,useMemo as mn,useRef as K,useState as F}from"react";import{Box as J,Text as v,useApp as Dc,useInput as Ic,useStdout as Nc}from"ink";import{existsSync as zs,readFileSync as Ys,readdirSync as oi,writeFileSync as _c}from"node:fs";import{basename as gn,isAbsolute as Mc,join as St}from"node:path";import{createSession as Vs,loginPkce as Lc,toStoredTokens as Oc,saveTokens as Uc,getAccessToken as Wc,resolveActiveProfile as ae,resolveProjectId as Fc,getArticle as Hc,decodeJwtClaims as st,isExpired as Ie,loadTokens as He,clearTokens as Bc,clearProfileProject as qc,setTitle as Gc,slugify as zc,touchSession as Co,upsertSession as Yc,generateTitle as Xs,findByName as Vc,listSessions as Xc,renameSession as Jc,suggestNextAction as Kc,readProjectConfig as ne,writeProjectConfig as Qc,resolveModelSetting as jo,loadProfileMap as Js,applyPull as Zc,computeSyncStatus as Po,planPull as eu,inventoryRepo as tu,knownEnvironments as nu,resolveEnvironment as ou,planPartitions as Ks,partitionEvenly as ru,screenshotPlaceholderIds as su,trackedArticlePaths as iu,runPartitioned as So,estimateBulkCost as Qs,resolveModelForOperation as To,readDocsPlan as au,ensureDir as lu,hintsDir as cu,devHintsGuidePath as uu}from"document360-engine";import{existsSync as vc,mkdirSync as Cc,readFileSync as Pc,writeFileSync as Sc}from"node:fs";import{join as Es}from"node:path";import{writerDir as Tc}from"document360-engine";function Ds(e){return Es(Tc(e),".sessions")}function Is(e,t){return Es(Ds(e),`${t}.json`)}function Ns(e,t,n){try{Cc(Ds(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");Sc(Is(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function bo(e,t){try{let n=Is(e,t);if(!vc(n))return[];let o=JSON.parse(Pc(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var Rc=e=>`\x1B]0;${e}\x07`,jc=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function _s(e){process.stdout.isTTY&&process.stdout.write(e)}function Pt(e){_s(Rc(e))}function un(e){_s(jc(e))}var dn=["\xB7 "," \xB7"],vo="\u{1F7E3}";nn();var Ac=/^(?:\d+[.)]|[-*•])\s+/;function Ms(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
129
+ `)){let c=s.trim().replace(Ac,""),d=c.match(/^`([^`]+)`$/);d&&(c=d[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let k=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(k)||r.push(k),r.length>=n)break}return r}H();var Ec=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Ls(e){return e.replace(/\r\n?/g,`
130
+ `)}function Os(e){return e.includes(`
131
+ `)||e.length>200}function Us(e,t){let n=t.split(`
132
+ `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function Ws(e,t){return e.replace(Ec,n=>t.get(n)??n)}function Fs(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function pn(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
133
+ `)){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 fn(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function Hs(e,t,n){let o=fn(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 Bs(e,t,n){let o=e[fn(e,t)];return n==="start"?o.start:o.end}function qs(e,t,n){let o=Math.max(1,n),r=e.split(`
134
+ `),s=0;for(let c=r.length-1;c>=0;c--){let d=r[c];if(s+=Math.max(1,Math.ceil(d.length/o)),s>=t){let g=s-t;return{text:[g>0?d.slice(g*o):d,...r.slice(c+1)].join(`
135
+ `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function Gs(e){let t=!1,n=0,o=0;for(let r of e.split(`
136
+ `)){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 Ro,jsx as S,jsxs as U}from"react/jsx-runtime";var du={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},Zs=` 1. /init \u2014 pick your Document360 environment & scaffold config
77
137
  2. /login signs in to that environment
78
138
  3. /project then /workspace \u2014 pick the project and where articles go
79
- Press 1 to start.`;function Kc(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=Tn(e),c=ie(e),d=(c?.docsDir??"user-docs").replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${d}/ + config`,k={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:Jc[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:g};if(c===null)return k;try{let w=le(e,n);k.profile=w.name,k.apiUrl=w.connection.apiUrl,k.prod=w.production,k.project=w.project.projectId??"(chosen at login)";let I=He(w.name);if(I){let b={...st(I.idToken)??{},...st(I.accessToken)??{}},C=b.email??b.preferred_username??"signed in";Ie(I)?I.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(I.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return k}function Qc(e,t){try{let o=le(e,t),n=He(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...st(n.idToken)??{},...st(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Ie(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var Js=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],ko=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Zc="Ask me to write or update an article\u2026";function ho({ch:e,dim:t}){let[o,n]=F(!0);return _e(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),S(v,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var eu=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Ks(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var tu=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Qs(e){try{return Zs(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function ou(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>Qs(St(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&Qs(St(e,n)))}function nu({startTime:e,chars:t}){let[o,n]=F(0);_e(()=>{let g=setInterval(()=>n(k=>k+1),120);return()=>clearInterval(g)},[]);let r=ko[o%ko.length],s=Js[Math.floor(o/16)%Js.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return U(J,{children:[S(v,{color:B,children:` ${r} ${s}\u2026 `}),S(v,{color:"gray",children:`(${xt(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var ru=12e4;function su({p:e}){let[t,o]=F(0);_e(()=>{let g=setInterval(()=>o(k=>k+1),150);return()=>clearInterval(g)},[]);let n=ko[t%ko.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),d=Date.now()-e.lastAt>=ru;return U(J,{flexDirection:"column",children:[U(J,{children:[S(v,{color:B,children:` ${n} ${e.verb??"Converting"} `}),S(v,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${xt(r)} \xB7 esc to stop`})]}),e.active.length>0&&S(v,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),d&&S(v,{color:"yellow",children:` \u26A0 no activity for ${xt(s)} \u2014 press esc to stop if it's wedged`})]})}function ei({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=xc(),[s,c]=F(o),[d,g]=F(null),[k,w]=F({text:"",pos:0}),I=k.text,b=X(a=>{w(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),C=X(a=>{w(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),x=X(()=>{w(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=Ls(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[_,z]=F(!1),[G,ae]=F(!1),[me,we]=F(0),[ge,he]=F(null),[it,V]=F([]),Tt=K(0),[ri,Rt]=F(!1),[si,lt]=F(0),Be=K(0),[at,qe]=F(0),[ze,jt]=F(null),An=K(new Map),ii=K(0),Ge=K([]),ce=K(null);ce.current===null&&(ce.current=qs({cwd:e,profileName:s,allowProdWrites:!1}));let $e=K({uuid:null,firstPrompt:null,titleFired:!1}),At=K(new Map),ct=K(!1),yo=K([]),Te=K([]),wo=K(null),[En,Ye]=F(null),[Ne,Me]=F(null),[xe,Xe]=F(null),[Re,Ve]=F(null),[oe,Je]=F(null),[ue,Ke]=F(null),[be,Qe]=F(null),[de,Le]=F(null),[ne,Ze]=F(null),[Dn,$o]=F([]),Oe=K([]),Et=K(!1),ve=K(null),[In,Z]=F(null),Dt=K(!1),It=K(null),[pe,Ue]=F(null),[_n,ut]=F(0),[Nn,dt]=F(0),[li,Mn]=F(0),xo=mo(()=>{try{return Tn(e).model??"auto"}catch{return null}},[e,li]),{stdout:ke}=vc(),[,ai]=F(0),Ln=K(`${ke.columns??80}x${ke.rows??24}`),je=X(()=>Math.max(20,(ke.columns??80)-1),[ke]),u=X(a=>{Ge.current.push(a),console.log(Ho(a,je()))},[je]);_e(()=>{let a=`d360-writer \xB7 ${En??go(e)}`;if(!_){co(0),Pt(`${xn} ${a}`);return}co(3);let l=0;Pt(`${uo[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%uo.length,Pt(`${uo[l]} ${a}`)},400);return()=>clearInterval(i)},[_,e,En]),_e(()=>()=>co(0),[]),_e(()=>{if(u({kind:"banner",info:Kc(e,n,t,s)}),!ie(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:
80
- ${Vs}`}),V(["/init"]);return}try{let a=le(e,s),l=He(a.name);l&&Ie(l)&&l.refreshToken?Rc({profile:a.name,connection:a.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),we(i=>i+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),V(["/login"])}):(!l||Ie(l))&&(u({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),V(["/login"]))}catch{}try{let a=ie(e),l=le(e,s),i=He(l.name);a&&i&&!Ie(i)&&ou(e,a.authoritativeSourceFiles)&&(u({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),V(["/scope"]))}catch{}},[]),_e(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let h=`${ke.columns??80}x${ke.rows??24}`;h!==Ln.current&&(Ln.current=h,ai(p=>p+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Lr(Ge.current,je())+"\x1B[?2026l")},80))},400)};return ke.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ke.off("resize",i)}},[ke,je]);let We=Math.max(20,(ke.columns??80)-1),On=mo(()=>Qc(e,s),[e,s,me]),bo=mo(()=>{let a=ie(e);if(!a)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let h=le(e,s),p=He(h.name);if(!(!!p&&!(Ie(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let l=St(e,(a.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return Hs(l)&&Zs(l).length>0}catch{return!1}})()?{text:Zc,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,me]),pt=lr(I),Un=pt.length>0&&!_,vo=d!==null?Ws(d,8,We):null,_t=de?de.paths.filter(a=>!de.query||a.toLowerCase().includes(de.query.toLowerCase())).slice(0,8):[],Nt=pe?pe.sessions.filter(a=>{let l=pe.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],re=X((a,l)=>{ce.current?.close(),ce.current=qs({cwd:e,resume:a,profileName:l??s,allowProdWrites:ct.current}),a||($e.current={uuid:null,firstPrompt:null,titleFired:!1},Ye(null)),ut(0),dt(0)},[e,s]),Wn=X((a,l,i)=>{if(eu.test(a))try{let h=le(e,s),p=typeof l.project_id=="string"&&l.project_id||h.project.projectId,m=lo(l,i),y=[],T=ao(i);a.endsWith("publish_article")&&T&&y.push(`Live: ${T}`),m&&p&&y.push(`Preview: ${io(h.connection.portalUrl,p,m,h.project.languageCode??"en")}`),y.length>0&&u({kind:"link",lines:y})}catch{}},[e,s,u]),Ce=X(async(a,l)=>{Rt(!0),he(null),V([]);let i=++Tt.current;u({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let h=$e.current;h.firstPrompt||(h.firstPrompt=l?.display??a),Be.current=Date.now(),lt(0),z(!0),At.current.clear();let p="",m="",y=null,T=()=>{y||(y=setTimeout(()=>{y=null,g(m.length>0?m:null)},60))},D=()=>{y&&clearTimeout(y),y=null,g(null)},M=()=>{if(m.trim()){let A=m.trimEnd();u({kind:"assistant",text:A})}m="",D()};try{for await(let A of ce.current.send(a))if(A.type==="session"){if(!h.uuid){h.uuid=A.sessionId;let N=new Date().toISOString(),P=_c(h.firstPrompt??"session");Nc({uuid:A.sessionId,name:P,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:N,updatedAt:N}),Ye(P)}}else if(A.type==="text"){m+=A.delta,p+=A.delta;let N=Fs(m);if(N>0){let P=m.slice(0,N).trimEnd();P&&u({kind:"assistant",text:P}),m=m.slice(N)}T(),lt(P=>P+A.delta.length)}else if(A.type==="tool"){let N=ro(A.name,A.input);N&&(M(),u({kind:"tool",title:N.title,sep:N.sep,arg:N.arg}),At.current.set(A.id,{name:A.name,input:A.input}))}else if(A.type==="article_diff"){let N=Fe(A.oldContent,A.newContent,je());N&&(M(),u({kind:"diff",added:N.added,removed:N.removed,lines:N.lines,hidden:N.hidden}))}else if(A.type==="tool_result"){A.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(A.output)&&(Dt.current=!0);let N=At.current.get(A.id);if(N){At.current.delete(A.id),M();let P=so(A.output,4,A.isError?void 0:N.name,N.input);u({kind:"tool-result",lines:P.lines,hidden:P.hidden,isError:A.isError}),A.isError||Wn(N.name,N.input,A.output)}}else if(A.type==="result"){M(),ut(P=>P+A.outputTokens),dt(P=>P+A.costUsd),u({kind:"done",seconds:Math.round((Date.now()-Be.current)/1e3),tokens:A.outputTokens,costUsd:A.costUsd,ok:A.ok});let N=A.ok?Ds(p,kt.map(P=>P.name)):[];if(N.length>0?V(N):A.ok&&p.trim()&&Uc(a,p,e).then(P=>{P&&Tt.current===i&&he(P)}).catch(()=>{}),h.uuid&&(bn(h.uuid),!h.titleFired)){h.titleFired=!0;let P=h.uuid,j=h.firstPrompt;j&&zs(j,e).then(W=>{W&&(Ic(P,W),Ye(W))}).catch(()=>{})}}else A.type==="error"&&(M(),A.kind==="auth"&&(Dt.current=!0),u({kind:"note",text:`agent error: ${A.message}`,tone:"error"}))}finally{z(!1),D(),$e.current.uuid&&As(e,$e.current.uuid,Ge.current),Dt.current&&(Dt.current=!1,It.current=l?.display??a,V(["/login"])),Et.current&&(Et.current=!1,Oe.current.length>0&&(u({kind:"note",tone:"info",text:`(${Oe.current.length} queued message(s) discarded)`}),Oe.current=[],$o([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,Wn,je]),Co=X(a=>{let l=He(a);if(!l||Ie(l)&&!l.refreshToken)return null;let i={...st(l.idToken)??{},...st(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),Po=X((a,l)=>{if(l){let i=ie(e);i&&(i.defaultProfile=a,Wc(i,e))}c(a),re(void 0,a),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Co(a)||(u({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),V(["/login"])),we(i=>i+1)},[e,u,re,Co]),Mt=X((a,l,i)=>{Wt(e,a,l,i.id,i.name),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),re(),Gs(e,a)||(u({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),he("analyze this repo and propose a docs structure"))},[e,u,re]),So=X(async a=>{let l;try{l=await gt(e,a)}catch{u({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),V(["/workspace"]);return}if(l.workspaces.length===0){u({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let i=l.workspaces.filter(p=>(p.workspace_type??"").toLowerCase()!=="apidocumentation"),h=i.length>0?i:l.workspaces;if(h.length===1){let p=h[0],m=l.workspaces.length-h.length;u({kind:"note",tone:"ok",text:`Selected the "${p.name??p.id}" workspace${m>0?" (skipped the API-documentation workspace)":""}.`}),Mt(l.profile,l.projectId,p);return}u({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),V(["/workspace"])},[e,u,Mt]),Fn=X((a,l)=>{cn(e,a,l.id),u({kind:"note",tone:"ok",text:`Project set to "${l.name??l.id}" (agent restarted).`}),re(),So(a)},[e,u,re,So]),Lt=X(()=>{let a=Te.current[0];if(!a)return;u({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Fe(a.oldContent,a.newContent,je());u(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),u({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[u,je]),Hn=X(a=>{if(Te.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=Te.current.shift();try{Fc({cwd:e,profileName:s},i),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),Te.current.length===0&&V(h=>h.length>0?h:["/sync"])}catch(h){u({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(l==="n"||l==="no"){let i=Te.current.shift();u({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=Te.current.length;return Te.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return Lt(),!0},[e,s,Lt,u]),Bn=X(a=>{let l=wo.current;if(!l)return!1;if(wo.current=null,a.trim()!==l.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:h}=gn(e,l.targets);u({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let p of h)u({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return u({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
81
- ${Vs}`}),V(["/init"]),Rt(!1),!0},[e,u]),qn=X(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),h=l.slice(1);switch(Rt(!0),i){case"help":u({kind:"note",tone:"info",text:Yt().join(`
82
- `)});return;case"exit":case"quit":ce.current?.close(),r();return;case"clear":re(),Ge.current=[],Rt(!1),he(null),Tt.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=le(e,s)}catch(m){u({kind:"note",tone:"error",text:m.message});return}u({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),ae(!0);try{let m=await Pc(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},D=>u({kind:"note",tone:"info",text:D})),y=Sc(p.name,m);Tc(y),Ao(y,p.name,D=>u({kind:"note",tone:"info",text:D})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Ht(y)}`});let T=(()=>{try{return le(e,s)}catch{return p}})();T.project.workspaceId||(T.project.projectId?await So(T.name):(u({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),V(["/project"]))),It.current&&(he(It.current),It.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{ae(!1),we(m=>m+1)}return}case"allow-prod":{let p=!1;try{p=le(e,s).production}catch{}if(!p){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}ct.current=!0,re(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=nn(h.join(" ")),m=$e.current.uuid;if(!m){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=$e.current.firstPrompt??"";zs(y,e).then(T=>{T?(he(`/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}Oc(m,p),Ye(p),Pt(`${xn} d360-writer \xB7 ${p}`),u({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=h[0],m=ie(e);if(!p){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(([A,N])=>({name:A,env:N.connection?.environment??"custom",prod:N.production===!0,who:Co(A)})),D=s??m?.defaultProfile,M=Math.max(0,T.findIndex(A=>A.name===D));Xe({cursor:M,current:M,rows:T});return}if(p==="add"){let y=rn(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]}).`}),Po(h[1],!1);return}if(!m?.profiles?.[p]){u({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}Po(p,!0);return}case"doctor":await vt(h,{cwd:e});return;case"mcp":await Vt(h);return;case"init":{if(ie(e)){u({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let p=qc().map(m=>({name:m,apiUrl:zc(m).apiUrl}));Ve({cursor:0,rows:p});return}case"resume":{let p=h.join(" ").trim(),m=Lc(e).filter(D=>D.uuid!==$e.current.uuid);if(!p){if(!m.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Ue({query:"",cursor:0,sessions:m});return}let y=Mc(e,p);if(!y){u({kind:"note",tone:"error",text:`No session matches "${p}".`});return}re(y.uuid),$e.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Ye(y.name),bn(y.uuid),Ge.current=[];let T=$n(e,y.uuid);for(let D of T)u(D);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 p=h.join(" ").trim(),m;try{m=await gt(e,s)}catch(T){u({kind:"note",tone:"error",text:`Could not list workspaces: ${T.message}`});return}if(!p){let T=m.workspaces.map(M=>({id:M.id,name:M.name??M.id,type:M.workspace_type}));if(T.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let D=Math.max(0,T.findIndex(M=>M.id===m.current));Je({cursor:D,current:D,rows:T,profile:m.profile,projectId:m.projectId,environment:m.environment});return}let y=jo(m.workspaces,p);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}Mt(m.profile,m.projectId,y);return}case"project":{let p=h.join(" ").trim(),m;try{m=await an(e,s)}catch(T){u({kind:"note",tone:"error",text:`Could not list projects: ${T.message} (signed in? try /login)`});return}if(!p){let T=m.projects.map(M=>({id:M.id,name:M.name??M.id,sub:M.sub_domain}));if(T.length===0){u({kind:"note",tone:"info",text:"No projects found for this identity."});return}let D=T.findIndex(M=>M.id===m.current);Ke({cursor:Math.max(0,D),current:D,rows:T,profile:m.profile,environment:m.environment});return}let y=rs(m.projects,p);if(!y){u({kind:"note",tone:"error",text:`No project matches "${p}". Available: ${m.projects.map(T=>T.name??T.id).join(", ")}`});return}Fn(m.profile,y);return}case"logout":{let p;try{p=le(e,s).name}catch(y){u({kind:"note",tone:"error",text:y.message});return}let m=Ec(p);Dc(e,p),re(),we(y=>y+1),u({kind:"note",tone:m?"ok":"info",text:m?`\u2713 Signed out of "${p}" and cleared its project/workspace selection.`:`Profile "${p}" was not signed in. Cleared any project/workspace selection.`}),u({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),V(["/login"]);return}case"publish":{if(h[0]&&h[0]!=="--all"){await Ce(Kt(h[0]),{display:`/publish ${h[0]}`,echoDisplay:!0});return}let p=h[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let m=await vn({cwd:e,profileName:s}),y=Jt(m.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let D=m.counts["unknown-base"]??0;D>0&&u({kind:"note",tone:"info",text:`(${D} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await Ce(wt(y.map(D=>D.path)),{display:"/publish --all",echoDisplay:!0});return}let T=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;Qe({cursor:0,rows:T})}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 p=h.join(" ").trim();if(!p){let y=[];try{y=Object.keys(Gs(e,le(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}Le({query:"",cursor:0,paths:y});return}let m=Cc(p)?p:St(e,p);if(Hs(m)){try{u({kind:"preview",name:go(m),text:Ks(Bs(m,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${m}: ${y.message}`})}return}if(tu.test(p)){try{let y=le(e,s),T={profile:y.name,connection:y.connection},D=y.project.projectId??jc(T),M=await Ac(T,D,p);u({kind:"preview",name:M.title??p,text:M.content??"*(article has no content)*"})}catch(y){u({kind:"note",tone:"error",text:`Could not fetch article: ${y.message}`})}return}u({kind:"note",tone:"error",text:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=h[0]?.trim();if(!p){let D=to(Tn(e));Me({cursor:D,current:D});return}let{lines:m,changed:y,effective:T}=bt(e,p);for(let D of m)u({kind:"note",tone:D.startsWith("\u26A0")?"warn":D.startsWith("\u2713")?"ok":"info",text:D});y&&(Mn(D=>D+1),ce.current?.setModel(T));return}case"convert":{if(!ie(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:m}=qo(h),y=Xc(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=zo(y,p);if(T.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${y.length} are tracked overall.)`});return}let D=Ys(T),M=3,A=`/convert${p?` --scope ${p}`:""} --run`,N=Pn(e,"light");if(!m){let j=Xs({files:Go(e,T),op:"convert",model:N.model}),W=p?`Scope: ${p} (${T.length} of ${y.length} tracked)
139
+ Press 1 to start.`;function pu(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",s=jo(e),c=ne(e),d=(c?.docsDir??"user-docs").replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${d}/ + config`,k={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:du[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:g};if(c===null)return k;try{let y=ae(e,o);k.profile=y.name,k.apiUrl=y.connection.apiUrl,k.prod=y.production,k.project=y.project.projectId??"(chosen at login)";let I=He(y.name);if(I){let b={...st(I.idToken)??{},...st(I.accessToken)??{}},C=b.email??b.preferred_username??"signed in";Ie(I)?I.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(I.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return k}function fu(e,t){try{let n=ae(e,t),o=He(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...st(o.idToken)??{},...st(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Ie(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 ei=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],kn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],mu="Ask me to write or update an article\u2026";function hn({ch:e,dim:t}){let[n,o]=F(!0);return Ne(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),S(v,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var gu=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ti(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var hu=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function ni(e){try{return oi(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function ku(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>ni(St(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&ni(St(e,o)))}function wu({startTime:e,chars:t}){let[n,o]=F(0);Ne(()=>{let g=setInterval(()=>o(k=>k+1),120);return()=>clearInterval(g)},[]);let r=kn[n%kn.length],s=ei[Math.floor(n/16)%ei.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return U(J,{children:[S(v,{color:B,children:` ${r} ${s}\u2026 `}),S(v,{color:"gray",children:`(${$t(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var yu=12e4;function xu({p:e}){let[t,n]=F(0);Ne(()=>{let g=setInterval(()=>n(k=>k+1),150);return()=>clearInterval(g)},[]);let o=kn[t%kn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),d=Date.now()-e.lastAt>=yu;return U(J,{flexDirection:"column",children:[U(J,{children:[S(v,{color:B,children:` ${o} ${e.verb??"Converting"} `}),S(v,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${$t(r)} \xB7 esc to stop`})]}),e.active.length>0&&S(v,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),d&&S(v,{color:"yellow",children:` \u26A0 no activity for ${$t(s)} \u2014 press esc to stop if it's wedged`})]})}function ri({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=Dc(),[s,c]=F(n),[d,g]=F(null),[k,y]=F({text:"",pos:0}),I=k.text,b=V(l=>{y(a=>{let i=typeof l=="function"?l(a.text):l;return{text:i,pos:i.length}})},[]),C=V(l=>{y(a=>({text:a.text.slice(0,a.pos)+l+a.text.slice(a.pos),pos:a.pos+l.length}))},[]),$=V(()=>{y(l=>{if(l.pos===0)return l;let a=l.text.slice(0,l.pos),i=Fs(a)??a.slice(0,-1);return{text:i+l.text.slice(l.pos),pos:i.length}})},[]),[N,G]=F(!1),[z,le]=F(!1),[me,ye]=F(0),[ge,he]=F(null),[it,X]=F([]),Tt=K(0),[li,Rt]=F(!1),[ci,at]=F(0),Be=K(0),[lt,qe]=F(0),[Ge,jt]=F(null),Do=K(new Map),ui=K(0),ze=K([]),ce=K(null);ce.current===null&&(ce.current=Vs({cwd:e,profileName:s,allowProdWrites:!1}));let xe=K({uuid:null,firstPrompt:null,titleFired:!1}),At=K(new Map),ct=K(!1),wn=K([]),Te=K([]),yn=K(null),[Io,Ye]=F(null),[_e,Me]=F(null),[$e,Ve]=F(null),[Re,Xe]=F(null),[oe,Je]=F(null),[ue,Ke]=F(null),[be,Qe]=F(null),[de,Le]=F(null),[re,Ze]=F(null),[No,xn]=F([]),Oe=K([]),Et=K(!1),ve=K(null),[_o,Z]=F(null),Dt=K(!1),It=K(null),[pe,Ue]=F(null),[Mo,ut]=F(0),[Lo,dt]=F(0),[di,Oo]=F(0),$n=mn(()=>{try{return jo(e).model??"auto"}catch{return null}},[e,di]),{stdout:ke}=Nc(),[,pi]=F(0),Uo=K(`${ke.columns??80}x${ke.rows??24}`),je=V(()=>Math.max(20,(ke.columns??80)-1),[ke]),u=V(l=>{ze.current.push(l),console.log(Hn(l,je()))},[je]);Ne(()=>{let l=`d360-writer \xB7 ${Io??gn(e)}`;if(!N){un(0),Pt(`${vo} ${l}`);return}un(3);let a=0;Pt(`${dn[0]} ${l}`);let i=setInterval(()=>{a=(a+1)%dn.length,Pt(`${dn[a]} ${l}`)},400);return()=>clearInterval(i)},[N,e,Io]),Ne(()=>()=>un(0),[]),Ne(()=>{if(u({kind:"banner",info:pu(e,o,t,s)}),!ne(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:
140
+ ${Zs}`}),X(["/init"]);return}try{let l=ae(e,s),a=He(l.name);a&&Ie(a)&&a.refreshToken?Wc({profile:l.name,connection:l.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),ye(i=>i+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),X(["/login"])}):(!a||Ie(a))&&(u({kind:"note",tone:"warn",text:`Profile "${l.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"]))}catch{}try{let l=ne(e),a=ae(e,s),i=He(a.name);l&&i&&!Ie(i)&&ku(e,l.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."}),X(["/scope"]))}catch{}},[]),Ne(()=>{let l=null,a=null,i=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let h=`${ke.columns??80}x${ke.rows??24}`;h!==Uo.current&&(Uo.current=h,pi(p=>p+1),a&&clearTimeout(a),a=setTimeout(()=>{a=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Ur(ze.current,je())+"\x1B[?2026l")},80))},400)};return ke.on("resize",i),()=>{l&&clearTimeout(l),a&&clearTimeout(a),ke.off("resize",i)}},[ke,je]);let We=Math.max(20,(ke.columns??80)-1),Wo=mn(()=>fu(e,s),[e,s,me]),bn=mn(()=>{let l=ne(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let h=ae(e,s),p=He(h.name);if(!(!!p&&!(Ie(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let a=St(e,(l.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return zs(a)&&oi(a).length>0}catch{return!1}})()?{text:mu,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,me]),pt=cr(I),Fo=pt.length>0&&!N,vn=d!==null?qs(d,8,We):null,Nt=de?de.paths.filter(l=>!de.query||l.toLowerCase().includes(de.query.toLowerCase())).slice(0,8):[],_t=pe?pe.sessions.filter(l=>{let a=pe.query.toLowerCase();return!a||l.name.toLowerCase().includes(a)||l.firstPrompt.toLowerCase().includes(a)}).slice(0,8):[],se=V((l,a)=>{ce.current?.close(),ce.current=Vs({cwd:e,resume:l,profileName:a??s,allowProdWrites:ct.current}),l||(xe.current={uuid:null,firstPrompt:null,titleFired:!1},Ye(null)),ut(0),dt(0)},[e,s]),Ho=V((l,a,i)=>{if(gu.test(l))try{let h=ae(e,s),p=typeof a.project_id=="string"&&a.project_id||h.project.projectId,m=ln(a,i),w=[],T=cn(i);l.endsWith("publish_article")&&T&&w.push(`Live: ${T}`),m&&p&&w.push(`Preview: ${an(h.connection.portalUrl,p,m,h.project.languageCode??"en")}`),w.length>0&&u({kind:"link",lines:w})}catch{}},[e,s,u]),Ce=V(async(l,a)=>{Rt(!0),he(null),X([]);let i=++Tt.current;u({kind:"user",text:a?.echoDisplay&&a.display?a.display:l});let h=xe.current;h.firstPrompt||(h.firstPrompt=a?.display??l),Be.current=Date.now(),at(0),G(!0),At.current.clear();let p="",m="",w=null,T=()=>{w||(w=setTimeout(()=>{w=null,g(m.length>0?m:null)},60))},D=()=>{w&&clearTimeout(w),w=null,g(null)},M=()=>{if(m.trim()){let A=m.trimEnd();u({kind:"assistant",text:A})}m="",D()};try{for await(let A of ce.current.send(l))if(A.type==="session"){if(!h.uuid){h.uuid=A.sessionId;let _=new Date().toISOString(),P=zc(h.firstPrompt??"session");Yc({uuid:A.sessionId,name:P,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:_,updatedAt:_}),Ye(P)}}else if(A.type==="text"){m+=A.delta,p+=A.delta;let _=Gs(m);if(_>0){let P=m.slice(0,_).trimEnd();P&&u({kind:"assistant",text:P}),m=m.slice(_)}T(),at(P=>P+A.delta.length)}else if(A.type==="tool"){let _=rn(A.name,A.input);_&&(M(),u({kind:"tool",title:_.title,sep:_.sep,arg:_.arg}),At.current.set(A.id,{name:A.name,input:A.input}))}else if(A.type==="article_diff"){let _=Fe(A.oldContent,A.newContent,je());_&&(M(),u({kind:"diff",added:_.added,removed:_.removed,lines:_.lines,hidden:_.hidden}))}else if(A.type==="tool_result"){A.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(A.output)&&(Dt.current=!0);let _=At.current.get(A.id);if(_){At.current.delete(A.id),M();let P=sn(A.output,4,A.isError?void 0:_.name,_.input);u({kind:"tool-result",lines:P.lines,hidden:P.hidden,isError:A.isError}),A.isError||Ho(_.name,_.input,A.output)}}else if(A.type==="result"){M(),ut(P=>P+A.outputTokens),dt(P=>P+A.costUsd),u({kind:"done",seconds:Math.round((Date.now()-Be.current)/1e3),tokens:A.outputTokens,costUsd:A.costUsd,ok:A.ok});let _=A.ok?Ms(p,kt.map(P=>P.name)):[];if(_.length>0?X(_):A.ok&&p.trim()&&Kc(l,p,e).then(P=>{P&&Tt.current===i&&he(P)}).catch(()=>{}),h.uuid&&(Co(h.uuid),!h.titleFired)){h.titleFired=!0;let P=h.uuid,j=h.firstPrompt;j&&Xs(j,e).then(W=>{W&&(Gc(P,W),Ye(W))}).catch(()=>{})}}else A.type==="error"&&(M(),A.kind==="auth"&&(Dt.current=!0),u({kind:"note",text:`agent error: ${A.message}`,tone:"error"}))}finally{G(!1),D(),xe.current.uuid&&Ns(e,xe.current.uuid,ze.current),Dt.current&&(Dt.current=!1,It.current=a?.display??l,X(["/login"])),Et.current&&(Et.current=!1,Oe.current.length>0&&(u({kind:"note",tone:"info",text:`(${Oe.current.length} queued message(s) discarded)`}),Oe.current=[],xn([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,Ho,je]),Cn=V(l=>{let a=He(l);if(!a||Ie(a)&&!a.refreshToken)return null;let i={...st(a.idToken)??{},...st(a.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),Pn=V((l,a)=>{if(a){let i=ne(e);i&&(i.defaultProfile=l,Qc(i,e))}c(l),se(void 0,l),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${a?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Cn(l)||(u({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"])),ye(i=>i+1)},[e,u,se,Cn]),Mt=V((l,a,i)=>{Wt(e,l,a,i.id,i.name),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),se(),Js(e,l)||(u({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),he("analyze this repo and propose a docs structure"))},[e,u,se]),Sn=V(async l=>{let a;try{a=await gt(e,l)}catch{u({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),X(["/workspace"]);return}if(a.workspaces.length===0){u({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let i=a.workspaces.filter(p=>(p.workspace_type??"").toLowerCase()!=="apidocumentation"),h=i.length>0?i:a.workspaces;if(h.length===1){let p=h[0],m=a.workspaces.length-h.length;u({kind:"note",tone:"ok",text:`Selected the "${p.name??p.id}" workspace${m>0?" (skipped the API-documentation workspace)":""}.`}),Mt(a.profile,a.projectId,p);return}u({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),X(["/workspace"])},[e,u,Mt]),Bo=V((l,a)=>{lo(e,l,a.id),u({kind:"note",tone:"ok",text:`Project set to "${a.name??a.id}" (agent restarted).`}),se(),Sn(l)},[e,u,se,Sn]),Lt=V(()=>{let l=Te.current[0];if(!l)return;u({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let i of l.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${i}`});l.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let a=Fe(l.oldContent,l.newContent,je());u(a?{kind:"diff",added:a.added,removed:a.removed,lines:a.lines,hidden:a.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]),qo=V(l=>{if(Te.current.length===0)return!1;let a=l.trim().toLowerCase();if(a==="y"||a==="yes"){let i=Te.current.shift();try{Zc({cwd:e,profileName:s},i),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),Te.current.length===0&&X(h=>h.length>0?h:["/sync"])}catch(h){u({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(a==="n"||a==="no"){let i=Te.current.shift();u({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=Te.current.length;return Te.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return Lt(),!0},[e,s,Lt,u]),Go=V(l=>{let a=yn.current;if(!a)return!1;if(yn.current=null,l.trim()!==a.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:h}=ko(e,a.targets);u({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let p of h)u({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return u({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
141
+ ${Zs}`}),X(["/init"]),Rt(!1),!0},[e,u]),zo=V(async l=>{let a=l.slice(1).trim().split(/\s+/),i=(a[0]??"").toLowerCase(),h=a.slice(1);switch(Rt(!0),i){case"help":u({kind:"note",tone:"info",text:Yt().join(`
142
+ `)});return;case"exit":case"quit":ce.current?.close(),r();return;case"clear":se(),ze.current=[],Rt(!1),he(null),Tt.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=ae(e,s)}catch(m){u({kind:"note",tone:"error",text:m.message});return}u({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),le(!0);try{let m=await Lc(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},D=>u({kind:"note",tone:"info",text:D})),w=Oc(p.name,m);Uc(w),An(w,p.name,D=>u({kind:"note",tone:"info",text:D})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Ht(w)}`});let T=(()=>{try{return ae(e,s)}catch{return p}})();T.project.workspaceId||(T.project.projectId?await Sn(T.name):(u({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),X(["/project"]))),It.current&&(he(It.current),It.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{le(!1),ye(m=>m+1)}return}case"allow-prod":{let p=!1;try{p=ae(e,s).production}catch{}if(!p){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}ct.current=!0,se(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=oo(h.join(" ")),m=xe.current.uuid;if(!m){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let w=xe.current.firstPrompt??"";Xs(w,e).then(T=>{T?(he(`/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}Jc(m,p),Ye(p),Pt(`${vo} d360-writer \xB7 ${p}`),u({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=h[0],m=ne(e);if(!p){let w=Object.entries(m?.profiles??{});if(w.length===0){u({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let T=w.map(([A,_])=>({name:A,env:_.connection?.environment??"custom",prod:_.production===!0,who:Cn(A)})),D=s??m?.defaultProfile,M=Math.max(0,T.findIndex(A=>A.name===D));Ve({cursor:M,current:M,rows:T});return}if(p==="add"){let w=ro(e,h[1],h[2]);if(w){u({kind:"note",tone:"error",text:w});return}u({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),Pn(h[1],!1);return}if(!m?.profiles?.[p]){u({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}Pn(p,!0);return}case"doctor":await vt(h,{cwd:e});return;case"mcp":await Xt(h);return;case"init":{if(ne(e)){u({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let p=nu().map(m=>({name:m,apiUrl:ou(m).apiUrl}));Xe({cursor:0,rows:p});return}case"resume":{let p=h.join(" ").trim(),m=Xc(e).filter(D=>D.uuid!==xe.current.uuid);if(!p){if(!m.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Ue({query:"",cursor:0,sessions:m});return}let w=Vc(e,p);if(!w){u({kind:"note",tone:"error",text:`No session matches "${p}".`});return}se(w.uuid),xe.current={uuid:w.uuid,firstPrompt:w.firstPrompt,titleFired:!0},Ye(w.name),Co(w.uuid),ze.current=[];let T=bo(e,w.uuid);for(let D of T)u(D);u({kind:"note",tone:"ok",text:T.length?`Resumed "${w.name}" \u2014 restored ${T.length} message(s); continue where you left off.`:`Resumed "${w.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let p=h.join(" ").trim(),m;try{m=await gt(e,s)}catch(T){u({kind:"note",tone:"error",text:`Could not list workspaces: ${T.message}`});return}if(!p){let T=m.workspaces.map(M=>({id:M.id,name:M.name??M.id,type:M.workspace_type}));if(T.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let D=Math.max(0,T.findIndex(M=>M.id===m.current));Je({cursor:D,current:D,rows:T,profile:m.profile,projectId:m.projectId,environment:m.environment});return}let w=jn(m.workspaces,p);if(!w){u({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}Mt(m.profile,m.projectId,w);return}case"project":{let p=h.join(" ").trim(),m;try{m=await ao(e,s)}catch(T){u({kind:"note",tone:"error",text:`Could not list projects: ${T.message} (signed in? try /login)`});return}if(!p){let T=m.projects.map(M=>({id:M.id,name:M.name??M.id,sub:M.sub_domain}));if(T.length===0){u({kind:"note",tone:"info",text:"No projects found for this identity."});return}let D=T.findIndex(M=>M.id===m.current);Ke({cursor:Math.max(0,D),current:D,rows:T,profile:m.profile,environment:m.environment});return}let w=is(m.projects,p);if(!w){u({kind:"note",tone:"error",text:`No project matches "${p}". Available: ${m.projects.map(T=>T.name??T.id).join(", ")}`});return}Bo(m.profile,w);return}case"logout":{let p;try{p=ae(e,s).name}catch(w){u({kind:"note",tone:"error",text:w.message});return}let m=Bc(p);qc(e,p),se(),ye(w=>w+1),u({kind:"note",tone:m?"ok":"info",text:m?`\u2713 Signed out of "${p}" and cleared its project/workspace selection.`:`Profile "${p}" was not signed in. Cleared any project/workspace selection.`}),u({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),X(["/login"]);return}case"publish":{if(h[0]&&h[0]!=="--all"){await Ce(Kt(h[0]),{display:`/publish ${h[0]}`,echoDisplay:!0});return}let p=h[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let m=await Po({cwd:e,profileName:s}),w=Jt(m.entries);if(w.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let D=m.counts["unknown-base"]??0;D>0&&u({kind:"note",tone:"info",text:`(${D} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await Ce(yt(w.map(D=>D.path)),{display:"/publish --all",echoDisplay:!0});return}let T=w.length>1?[{path:"--all",label:`publish all ${w.length} candidates in one run`},...w]:w;Qe({cursor:0,rows:T})}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 p=h.join(" ").trim();if(!p){let w=[];try{w=Object.keys(Js(e,ae(e,s).name)?.articles??{})}catch{}if(w.length===0){u({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}Le({query:"",cursor:0,paths:w});return}let m=Mc(p)?p:St(e,p);if(zs(m)){try{u({kind:"preview",name:gn(m),text:ti(Ys(m,"utf8"))})}catch(w){u({kind:"note",tone:"error",text:`Could not read ${m}: ${w.message}`})}return}if(hu.test(p)){try{let w=ae(e,s),T={profile:w.name,connection:w.connection},D=w.project.projectId??Fc(T),M=await Hc(T,D,p);u({kind:"preview",name:M.title??p,text:M.content??"*(article has no content)*"})}catch(w){u({kind:"note",tone:"error",text:`Could not fetch article: ${w.message}`})}return}u({kind:"note",tone:"error",text:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=h[0]?.trim();if(!p){let D=tn(jo(e));Me({cursor:D,current:D});return}let{lines:m,changed:w,effective:T}=bt(e,p);for(let D of m)u({kind:"note",tone:D.startsWith("\u26A0")?"warn":D.startsWith("\u2713")?"ok":"info",text:D});w&&(Oo(D=>D+1),ce.current?.setModel(T));return}case"convert":{if(!ne(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:m}=qn(h),w=iu(e,s);if(w.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=Gn(w,p);if(T.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${w.length} are tracked overall.)`});return}let D=Ks(T),M=3,A=`/convert${p?` --scope ${p}`:""} --run`,_=To(e,"light");if(!m){let j=Qs({files:zn(e,T),op:"convert",model:_.model}),W=p?`Scope: ${p} (${T.length} of ${w.length} tracked)
83
143
  `:"",ee=`
84
- Model: ${N.model}${N.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:W+Xo(D,j,M).join(`
85
- `)+ee}),V([A]);return}Be.current=Date.now(),lt(0),z(!0);let P=new AbortController;ve.current=P,Z({total:D.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${T.length} articles across ${D.length} partitions (\u2264${M} agents at once) on ${N.model}\u2026 (esc to stop)`});try{for await(let j of Cn({cwd:e,partitions:D,promptFor:Yo,concurrency:M,profileName:s,allowProdWrites:ct.current,model:N.model,signal:P.signal}))if(j.type==="partition_status")j.status==="running"?(Z(W=>W&&{...W,active:[...W.active,j.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${j.label} \u2014 converting\u2026`})):(Z(W=>W&&{...W,active:W.active.filter(ee=>ee!==j.label),done:W.done+1,lastAt:Date.now()}),u({kind:"note",tone:j.status==="done"?"ok":"error",text:` ${j.status==="done"?"\u2713":"\u2717"} ${j.label}`}));else if(j.type==="partition_event")Z(W=>{if(!W)return W;let ee={...W,lastAt:Date.now()};return j.event.type==="tool"?ee.tools=W.tools+1:j.event.type==="text"&&(ee.chars=W.chars+j.event.delta.length),ee});else if(j.type==="run_done"){dt(O=>O+j.totalCostUsd),ut(O=>O+j.results.reduce((Y,te)=>Y+te.outputTokens,0));let W=j.aborted?"Stopped. ":"",ee=t.kind==="api"?"api":"subscription";u({kind:"note",tone:j.aborted?"warn":j.ok?"ok":"warn",text:W+Vo(j.results,D,ee).join(`
86
- `)})}}catch(j){u({kind:"note",tone:"error",text:`Convert run failed: ${j.message}`})}finally{ve.current=null,Z(null),z(!1)}return}case"write":{if(!ie(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Vc(e);if(p.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}=Ko(h),{docsDir:D,targets:M,reason:A}=on(e,p,{scope:m,path:y});if(M.length===0){u({kind:"note",tone:"warn",text:A??"Nothing to write."});return}let N=Ys(M),P=5,j=Pn(e,"standard");if(!T&&!!!y){let O=Xs({files:Qo(e,p,M),op:"write",model:j.model}),Y=m?`Scope: ${m} (${M.length} pending)
144
+ Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:W+Vn(D,j,M).join(`
145
+ `)+ee}),X([A]);return}Be.current=Date.now(),at(0),G(!0);let P=new AbortController;ve.current=P,Z({total:D.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${T.length} articles across ${D.length} partitions (\u2264${M} agents at once) on ${_.model}\u2026 (esc to stop)`});try{for await(let j of So({cwd:e,partitions:D,promptFor:Yn,concurrency:M,profileName:s,allowProdWrites:ct.current,model:_.model,signal:P.signal}))if(j.type==="partition_status")j.status==="running"?(Z(W=>W&&{...W,active:[...W.active,j.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${j.label} \u2014 converting\u2026`})):(Z(W=>W&&{...W,active:W.active.filter(ee=>ee!==j.label),done:W.done+1,lastAt:Date.now()}),u({kind:"note",tone:j.status==="done"?"ok":"error",text:` ${j.status==="done"?"\u2713":"\u2717"} ${j.label}`}));else if(j.type==="partition_event")Z(W=>{if(!W)return W;let ee={...W,lastAt:Date.now()};return j.event.type==="tool"?ee.tools=W.tools+1:j.event.type==="text"&&(ee.chars=W.chars+j.event.delta.length),ee});else if(j.type==="run_done"){dt(O=>O+j.totalCostUsd),ut(O=>O+j.results.reduce((Y,te)=>Y+te.outputTokens,0));let W=j.aborted?"Stopped. ":"",ee=t.kind==="api"?"api":"subscription";u({kind:"note",tone:j.aborted?"warn":j.ok?"ok":"warn",text:W+Xn(j.results,D,ee).join(`
146
+ `)})}}catch(j){u({kind:"note",tone:"error",text:`Convert run failed: ${j.message}`})}finally{ve.current=null,Z(null),G(!1)}return}case"write":{if(!ne(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=au(e);if(p.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:w,run:T}=Kn(h),{docsDir:D,targets:M,reason:A}=no(e,p,{scope:m,path:w});if(M.length===0){u({kind:"note",tone:"warn",text:A??"Nothing to write."});return}let _=Ks(M),P=5,j=To(e,"standard");if(!T&&!!!w){let O=Qs({files:Qn(e,p,M),op:"write",model:j.model}),Y=m?`Scope: ${m} (${M.length} pending)
87
147
  `:"",te=`
88
- Model: ${j.model}${j.forced?" (forced)":" \u2014 authoring; /model to override"}`;u({kind:"note",tone:"info",text:Y+en(N,O,P).join(`
89
- `)+te}),V([`/write${m?` --scope ${m}`:" --all"} --run`]);return}Be.current=Date.now(),lt(0),z(!0);let ee=new AbortController;ve.current=ee,Z({verb:"Writing",total:N.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Writing ${M.length} article${M.length===1?"":"s"} across ${N.length} partition${N.length===1?"":"s"} (\u2264${P} agents at once) on ${j.model}${j.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let O of Cn({cwd:e,partitions:N,promptFor:Y=>Zo(Y,D),concurrency:P,profileName:s,allowProdWrites:ct.current,model:j.model,signal:ee.signal}))if(O.type==="partition_status")O.status==="running"?(Z(Y=>Y&&{...Y,active:[...Y.active,O.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${O.label} \u2014 writing\u2026`})):(Z(Y=>Y&&{...Y,active:Y.active.filter(te=>te!==O.label),done:Y.done+1,lastAt:Date.now()}),u({kind:"note",tone:O.status==="done"?"ok":"error",text:` ${O.status==="done"?"\u2713":"\u2717"} ${O.label}`}));else if(O.type==="partition_event")Z(Y=>{if(!Y)return Y;let te={...Y,lastAt:Date.now()};return O.event.type==="tool"?te.tools=Y.tools+1:O.event.type==="text"&&(te.chars=Y.chars+O.event.delta.length),te});else if(O.type==="run_done"){dt(Ro=>Ro+O.totalCostUsd),ut(Ro=>Ro+O.results.reduce((di,pi)=>di+pi.outputTokens,0));let Y=O.aborted?"Stopped. ":"",te=t.kind==="api"?"api":"subscription";u({kind:"note",tone:O.aborted?"warn":O.ok?"ok":"warn",text:Y+tn(O.results,N,te).join(`
90
- `)})}}catch(O){u({kind:"note",tone:"error",text:`Write run failed: ${O.message}`})}finally{ve.current=null,Z(null),z(!1)}return}case"sync":{let p=(h[0]??"status").toLowerCase();try{if(p==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let m=await vn({cwd:e,profileName:s});u({kind:"note",tone:"info",text:eo(m).join(`
91
- `)});return}if(p==="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 vn({cwd:e,profileName:s})).entries.filter(M=>M.status==="remote-ahead"&&M.path).map(M=>M.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 D of y)T.push(await Hc({cwd:e,profileName:s,relPath:D}));Te.current=T,Lt();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(m){u({kind:"note",tone:"error",text:`Sync failed: ${m.message}`})}return}case"scope":{if(!ie(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Bc(e);if(p.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ze({cursor:0,rows:p.map(m=>({...m,checked:m.recommended}))});return}case"audit":case"capture-setup":{let m=await(i==="audit"?Qt:Ct)(h,void 0);if(m.kind==="forward-to-agent"&&m.prompt&&await Ce(m.prompt,{display:m.display,echoDisplay:!0}),i==="capture-setup"){let y=fn(e);y&&u({kind:"note",tone:"info",text:y.join(`
92
- `)})}return}case"screenshot":{let p=un(h);if(p.mode==="list"){if(!ie(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:pn(dn(e,p.scope),p.scope).join(`
93
- `)});return}if(p.mode==="single"){let P=await no(h);P.kind==="forward-to-agent"&&P.prompt&&await Ce(P.prompt,{display:P.display,echoDisplay:!0});return}if(!ie(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let m=p.mode==="scope"?p.scope:void 0,y=Yc(e,{scope:m}).map(P=>P.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,D=Gc(y,3),M=Pn(e,"standard");Be.current=Date.now(),lt(0),z(!0);let A=new AbortController;ve.current=A,Z({verb:"Authoring",total:D.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${D.length} partition${D.length===1?"":"s"} (\u2264${T} agents at once) on ${M.model}${M.forced?" (forced)":""}\u2026 (esc to stop)`});let N=!1;try{for await(let P of Cn({cwd:e,partitions:D,promptFor:ps,concurrency:T,profileName:s,allowProdWrites:ct.current,model:M.model,signal:A.signal}))if(P.type==="partition_status")P.status==="running"?(Z(j=>j&&{...j,active:[...j.active,P.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${P.label} \u2014 authoring\u2026`})):(Z(j=>j&&{...j,active:j.active.filter(W=>W!==P.label),done:j.done+1,lastAt:Date.now()}),u({kind:"note",tone:P.status==="done"?"ok":"error",text:` ${P.status==="done"?"\u2713":"\u2717"} ${P.label}`}));else if(P.type==="partition_event")Z(j=>{if(!j)return j;let W={...j,lastAt:Date.now()};return P.event.type==="tool"?W.tools=j.tools+1:P.event.type==="text"&&(W.chars=j.chars+P.event.delta.length),W});else if(P.type==="run_done"){dt(O=>O+P.totalCostUsd),ut(O=>O+P.results.reduce((Y,te)=>Y+te.outputTokens,0));let j=P.results.filter(O=>O.ok).length,W=P.aborted?"Stopped. ":"";N=!P.aborted&&p.setup;let ee=P.aborted?"":N?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";u({kind:"note",tone:P.aborted?"warn":P.ok?"ok":"warn",text:`${W}Authored specs \u2014 ${j}/${P.results.length} batches ok.${ee}`})}}catch(P){u({kind:"note",tone:"error",text:`Spec authoring failed: ${P.message}`})}finally{ve.current=null,Z(null),z(!1)}if(N){let P=await Ct();P.kind==="forward-to-agent"&&P.prompt&&await Ce(P.prompt,{display:P.display,echoDisplay:!0});let j=fn(e);j&&u({kind:"note",tone:"info",text:j.join(`
94
- `)})}return}case"reset":{let p=mn(e);if(p.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}wo.current={repoName:go(e),targets:p},u({kind:"note",tone:"warn",text:hn(e,p).join(`
95
- `)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Lt,u,re,Ce]),Ot=X(a=>{let l=(a??I).trim();if(b(""),jt(null),qe(0),l.startsWith("/")){let h=m=>m.trim().split(/\s+/).slice(1).join(" "),p=h(l);V(m=>m.filter(y=>y.trim()!==l&&!(p&&h(y)===p)))}if(!l||Bn(l)||Hn(l))return;yo.current.push(l);let i=Ms(l,An.current);l.startsWith("/")?qn(i):Ce(i,{display:l})},[I,Bn,Hn,qn,Ce]),zn=X(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=Is(a);if(_s(l)){let i=Ns(++ii.current,l);An.current.set(i,l),C(i)}else C(l);return}C(a)},[C]),et=Math.max(10,We-6),Gn=mo(()=>po(k.text,et),[k.text,et]),To=X(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Ut=X(a=>w(l=>({...l,pos:Os(po(l.text,et),l.pos,a)})),[et]),ft=X(a=>w(l=>({...l,pos:Us(po(l.text,et),l.pos,a)})),[et]),ci=["\x1B[H","\x1B[1~","\x1BOH"],ui=["\x1B[F","\x1B[4~","\x1BOF"],Yn=X((a,l)=>l.leftArrow?(To(-1),!0):l.rightArrow?(To(1),!0):a&&ci.includes(a)?(ft("start"),!0):a&&ui.includes(a)?(ft("end"),!0):l.ctrl&&a==="a"?(ft("start"),!0):l.ctrl&&a==="e"?(ft("end"),!0):!1,[To,ft]);return bc((a,l)=>{if(l.ctrl&&a==="c"){ce.current?.close(),r();return}if(!G){if(_){if(l.escape){if(ve.current){ve.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),ve.current.abort());return}Et.current||(Et.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),ce.current?.interrupt());return}if(l.return){let i=I.trim();if(!i)return;Oe.current.push(i),$o([...Oe.current]),b("");return}if(Yn(a,l))return;if(l.upArrow){Ut(-1);return}if(l.downArrow){Ut(1);return}if(l.backspace||l.delete){x();return}a&&!l.ctrl&&!l.meta&&zn(a);return}if(Ne){if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(ye.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ye.length){Me(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ye[Ne.cursor];Me(null);let{lines:h,changed:p,effective:m}=bt(e,i.value??"default");for(let y of h)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(Mn(y=>y+1),ce.current?.setModel(m));return}if(a==="s"){let i=ye[Ne.cursor];Me(null),ce.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Me(null);return}return}if(xe){if(l.upArrow){Xe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Xe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=xe.rows.length){Xe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=xe.rows[xe.cursor];Xe(null),Po(i.name,l.return===!0);return}if(l.escape){Xe(null);return}return}if(Re){if(l.upArrow){Ve(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ve(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=Re.rows.length){Ve(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=Re.rows[Re.cursor].name;if(Ve(null),!fr(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let m=He(i);p=!!m&&!(Ie(m)&&!m.refreshToken)}catch{}p?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),V(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),V(["/login"])),we(m=>m+1);return}if(l.escape){Ve(null);return}return}if(oe){if(l.upArrow){Je(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Je(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=oe.rows.length){Je(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=oe.rows[oe.cursor],{profile:h,projectId:p}=oe;Je(null),Mt(h,p,i);return}if(l.escape){Je(null);return}return}if(ue){if(l.upArrow){Ke(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ke(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ue.rows.length){Ke(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ue.rows[ue.cursor],{profile:h}=ue;Ke(null),Fn(h,i);return}if(l.escape){Ke(null);return}return}if(ne){if(l.upArrow){Ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Ze(i=>i&&{...i,rows:i.rows.map((h,p)=>p===i.cursor?{...h,checked:!h.checked}:h)});return}if(l.return){let i=ne.rows.filter(h=>h.checked).map(h=>h.path);if(Ze(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}_o(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of i)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(l.escape){Ze(null);return}return}if(be){if(l.upArrow){Qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=be.rows.length){Qe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=be.rows[be.cursor],h=be.rows.filter(p=>p.path!=="--all").map(p=>p.path);Qe(null),Ce(i.path==="--all"?wt(h):Kt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Qe(null);return}return}if(de){if(l.escape){Le(null);return}if(l.upArrow){Le(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Le(i=>i&&{...i,cursor:Math.min(Math.max(0,_t.length-1),i.cursor+1)});return}if(l.return){let i=_t[de.cursor];if(i){Le(null);try{u({kind:"preview",name:go(i),text:Ks(Bs(St(e,i),"utf8"))})}catch(h){u({kind:"note",tone:"error",text:`Could not read ${i}: ${h.message}`})}}return}if(l.backspace||l.delete){Le(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Le(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(pe){if(l.escape){Ue(null);return}if(l.upArrow){Ue(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ue(i=>i&&{...i,cursor:Math.min(Math.max(0,Nt.length-1),i.cursor+1)});return}if(l.return){let i=Nt[pe.cursor];if(i){Ue(null),re(i.uuid),$e.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Ye(i.name),bn(i.uuid),Ge.current=[];let h=$n(e,i.uuid);for(let p of h)u(p);u({kind:"note",tone:"ok",text:h.length?`Resumed "${i.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(l.backspace||l.delete){Ue(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ue(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!I&&ge){b(ge),he(null),Tt.current++;return}if(!I&&it.length>0&&a&&/^[1-9]$/.test(a)){let i=it[Number(a)-1];if(i){b(i);return}}if(!Yn(a,l)){if(Un){if(l.upArrow){qe(i=>Math.max(0,i-1));return}if(l.downArrow){qe(i=>Math.min(pt.length-1,i+1));return}if(l.tab){b("/"+(pt[at]?.name??"")+" "),qe(0);return}if(l.return){let i=pt[at];if(i){let h=I.trim().slice(1).split(/\s+/).slice(1).join(" ");if(ar(i.usage)&&!h){b("/"+i.name+" "),qe(0);return}Ot("/"+i.name+(h?" "+h:""));return}}}else{if(l.upArrow){if(I!==""&&ze===null){Ut(-1);return}let i=yo.current;if(!i.length)return;let h=ze===null?i.length-1:Math.max(0,ze-1);jt(h),b(i[h]??"");return}if(l.downArrow){if(I!==""&&ze===null){Ut(1);return}let i=yo.current;if(ze===null)return;let h=ze+1;h>=i.length?(jt(null),b("")):(jt(h),b(i[h]??""));return}}if(l.return){Ot();return}if(l.backspace||l.delete){x();return}if(l.escape){b(""),qe(0),he(null),V([]);return}a&&!l.ctrl&&!l.meta&&zn(a)}}}),_e(()=>{if(_||G)return;let a=Oe.current.shift();a!==void 0&&($o([...Oe.current]),Ot(a))},[_,G,Ot]),U(J,{flexDirection:"column",width:We,children:[vo!==null&&U(J,{marginTop:1,flexDirection:"column",children:[vo.truncated&&S(v,{dimColor:!0,children:"\u2026"}),S(v,{children:vo.text})]}),_&&(In?S(su,{p:In}):S(nu,{startTime:Be.current,chars:si})),S(J,{borderStyle:"round",borderColor:On.prod?"yellow":B,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:I?Gn.map((a,l)=>{let i=k.text.slice(a.start,a.end),h=l===fo(Gn,k.pos),p=Math.min(k.pos,a.end)-a.start;return U(v,{children:[S(v,{color:B,children:l===0?"> ":" "}),h?U(Sn,{children:[i.slice(0,p),S(ho,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):U(v,{children:[S(v,{color:B,children:"> "}),ge&&!_?U(Sn,{children:[S(ho,{ch:ge[0],dim:!0}),S(v,{color:"gray",children:ge.slice(1)}),S(v,{dimColor:!0,children:" (tab)"})]}):bo.isSetup||!ri?U(Sn,{children:[S(ho,{ch:bo.text[0],dim:!0}),S(v,{color:"gray",children:bo.text.slice(1)})]}):S(ho,{ch:" "})]})}),Dn.length>0&&S(J,{flexDirection:"column",paddingX:1,children:Dn.map((a,l)=>S(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),Ne?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Select model"}),S(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ye.map((a,l)=>U(v,{color:l===Ne.cursor?B:void 0,children:[l===Ne.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===Ne.current?" \u2714":""}`.padEnd(16),S(v,{color:"gray",children:a.desc})]},a.label)),S(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):xe?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Switch connection profile"}),xe.rows.map((a,l)=>U(v,{color:l===xe.cursor?B:void 0,children:[l===xe.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===xe.current?" \u2714":""}`.padEnd(20),S(v,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?S(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),S(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Re?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Pick your Document360 environment"}),Re.rows.map((a,l)=>U(v,{color:l===Re.cursor?B:void 0,children:[l===Re.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),S(v,{color:"gray",children:a.apiUrl})]},a.name)),S(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):oe?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Switch workspace"}),S(v,{dimColor:!0,children:`environment ${oe.environment} \xB7 project ${oe.projectId.slice(0,8)}\u2026`}),oe.rows.map((a,l)=>U(v,{color:l===oe.cursor?B:void 0,children:[l===oe.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===oe.current?" \u2714":""}`.padEnd(30),S(v,{color:"gray",children:a.type??""})]},a.id)),S(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):ue?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Choose project"}),S(v,{dimColor:!0,children:`environment ${ue.environment}`}),ue.rows.map((a,l)=>U(v,{color:l===ue.cursor?B:void 0,children:[l===ue.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ue.current?" \u2714":""}`.padEnd(30),S(v,{color:"gray",children:a.sub??""})]},a.id)),S(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ne?(()=>{let l=Math.min(Math.max(0,ne.cursor-Math.floor(7)),Math.max(0,ne.rows.length-14)),i=ne.rows.slice(l,l+14),h=ne.rows.filter(p=>p.checked).length;return U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:`Which folders back the user docs? (${h} selected of ${ne.rows.length})`}),l>0?S(v,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,m)=>{let y=l+m;return U(v,{color:y===ne.cursor?B:void 0,children:[y===ne.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,We-34)),S(v,{color:"gray",children:Zt(p)})]},p.path)}),l+14<ne.rows.length?S(v,{dimColor:!0,children:` \u2193 ${ne.rows.length-l-14} more`}):null,S(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():be?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Publish which article?"}),be.rows.map((a,l)=>U(v,{color:l===be.cursor?B:void 0,children:[l===be.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,We-30)),S(v,{color:"gray",children:a.label})]},a.path)),S(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):de?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:`Preview article${de.query?` \u2014 filter: ${de.query}`:" (type to filter)"}`}),_t.length===0?S(v,{color:"gray",children:"no articles match"}):_t.map((a,l)=>U(v,{color:l===de.cursor?B:void 0,children:[l===de.cursor?"\u276F ":" ",a]},a)),S(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):pe?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:`Resume session${pe.query?` \u2014 filter: ${pe.query}`:" (type to filter)"}`}),Nt.length===0?S(v,{color:"gray",children:"no sessions match"}):Nt.map((a,l)=>U(v,{color:l===pe.cursor?B:void 0,children:[l===pe.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),S(v,{color:l===pe.cursor?B:"gray",children:a.firstPrompt.slice(0,Math.max(10,We-40))})]},a.uuid)),S(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Un?S(J,{flexDirection:"column",children:pt.map((a,l)=>U(v,{color:l===at?B:void 0,children:[l===at?"\u276F ":" ",a.usage.padEnd(22)," ",S(v,{color:l===at?B:"gray",children:a.name==="model"&&xo?`${a.desc} (currently ${xo})`:a.desc})]},a.name))}):!I&&it.length>0?U(J,{flexDirection:"column",paddingX:1,children:[it.map((a,l)=>U(v,{children:[S(v,{color:B,children:l+1})," ",a.slice(0,Math.max(20,We-5))]},a)),S(v,{dimColor:!0,children:`press 1-${it.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:U(v,{color:"gray",children:[On.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${xo??"model n/a"}${t.kind==="api"?Nn>0?` \xB7 ${fe(Nn)}`:"":_n>0?` \xB7 ${rt(_n)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as au}from"react/jsx-runtime";async function ti(e=process.cwd(),t="auto",o,n="0.0.0"){let r=lu(t);r.kind==="none"&&(console.error(""),console.error($("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=iu(au(ei,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
96
- `),process.exit(0)}var pu=uu(import.meta.url),oi=pu("../package.json"),Se=new cu;function Rn(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=>{Rn(e),await Bt({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=>{Rn(e),await nr({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=>{Rn(e),await or({profile:e.profile})});var jn=Se.command("profile").description("Manage connection profiles for the current repo");jn.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>qt(process.cwd()));jn.command("use <name>").description("Set the default profile for this repo").action(e=>zt(process.cwd(),e));jn.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Gt(process.cwd(),e));var ni=Se.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");ni.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>tt(process.cwd(),e.profile));ni.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await Kn(process.cwd(),e,t.profile)});Se.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>sr());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(()=>(oo(),ts)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});Se.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(oi.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(du.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 ir(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 Ss(e.cwd,e.auth,e.profile):await ti(e.cwd,e.auth,e.profile,oi.version)});Se.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
148
+ Model: ${j.model}${j.forced?" (forced)":" \u2014 authoring; /model to override"}`;u({kind:"note",tone:"info",text:Y+eo(_,O,P).join(`
149
+ `)+te}),X([`/write${m?` --scope ${m}`:" --all"} --run`]);return}Be.current=Date.now(),at(0),G(!0);let ee=new AbortController;ve.current=ee,Z({verb:"Writing",total:_.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Writing ${M.length} article${M.length===1?"":"s"} across ${_.length} partition${_.length===1?"":"s"} (\u2264${P} agents at once) on ${j.model}${j.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let O of So({cwd:e,partitions:_,promptFor:Y=>Zn(Y,D),concurrency:P,profileName:s,allowProdWrites:ct.current,model:j.model,signal:ee.signal}))if(O.type==="partition_status")O.status==="running"?(Z(Y=>Y&&{...Y,active:[...Y.active,O.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${O.label} \u2014 writing\u2026`})):(Z(Y=>Y&&{...Y,active:Y.active.filter(te=>te!==O.label),done:Y.done+1,lastAt:Date.now()}),u({kind:"note",tone:O.status==="done"?"ok":"error",text:` ${O.status==="done"?"\u2713":"\u2717"} ${O.label}`}));else if(O.type==="partition_event")Z(Y=>{if(!Y)return Y;let te={...Y,lastAt:Date.now()};return O.event.type==="tool"?te.tools=Y.tools+1:O.event.type==="text"&&(te.chars=Y.chars+O.event.delta.length),te});else if(O.type==="run_done"){dt(Rn=>Rn+O.totalCostUsd),ut(Rn=>Rn+O.results.reduce((gi,hi)=>gi+hi.outputTokens,0));let Y=O.aborted?"Stopped. ":"",te=t.kind==="api"?"api":"subscription";u({kind:"note",tone:O.aborted?"warn":O.ok?"ok":"warn",text:Y+to(O.results,_,te).join(`
150
+ `)})}}catch(O){u({kind:"note",tone:"error",text:`Write run failed: ${O.message}`})}finally{ve.current=null,Z(null),G(!1)}return}case"sync":{let p=(h[0]??"status").toLowerCase();try{if(p==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let m=await Po({cwd:e,profileName:s});u({kind:"note",tone:"info",text:en(m).join(`
151
+ `)});return}if(p==="pull"){let m=h[1];if(!m){u({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let w;if(m==="--all"){if(u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),w=(await Po({cwd:e,profileName:s})).entries.filter(M=>M.status==="remote-ahead"&&M.path).map(M=>M.path),w.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 w=[m.replace(/\\/g,"/")];let T=[];for(let D of w)T.push(await eu({cwd:e,profileName:s,relPath:D}));Te.current=T,Lt();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(m){u({kind:"note",tone:"error",text:`Sync failed: ${m.message}`})}return}case"scope":{if(!ne(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=tu(e);if(p.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ze({cursor:0,rows:p.map(m=>({...m,checked:m.recommended}))});return}case"audit":case"capture-setup":{let m=await(i==="audit"?Qt:Ct)(h,void 0);if(m.kind==="forward-to-agent"&&m.prompt&&await Ce(m.prompt,{display:m.display,echoDisplay:!0}),i==="capture-setup"){let w=fo(e);w&&u({kind:"note",tone:"info",text:w.join(`
152
+ `)})}return}case"devhints":{if(!ne(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}lu(cu(e)),_c(uu(e),mo()),u({kind:"note",tone:"info",text:go().join(`
153
+ `)});return}case"screenshot":{let p=co(h);if(p.mode==="list"){if(!ne(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:po(uo(e,p.scope),p.scope).join(`
154
+ `)});return}if(p.mode==="single"){let P=await on(h);P.kind==="forward-to-agent"&&P.prompt&&await Ce(P.prompt,{display:P.display,echoDisplay:!0});return}if(!ne(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let m=p.mode==="scope"?p.scope:void 0,w=su(e,{scope:m}).map(P=>P.id);if(w.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,D=ru(w,3),M=To(e,"standard");Be.current=Date.now(),at(0),G(!0);let A=new AbortController;ve.current=A,Z({verb:"Authoring",total:D.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Authoring ${w.length} screenshot spec${w.length===1?"":"s"} across ${D.length} partition${D.length===1?"":"s"} (\u2264${T} agents at once) on ${M.model}${M.forced?" (forced)":""}\u2026 (esc to stop)`});let _=!1;try{for await(let P of So({cwd:e,partitions:D,promptFor:ms,concurrency:T,profileName:s,allowProdWrites:ct.current,model:M.model,signal:A.signal}))if(P.type==="partition_status")P.status==="running"?(Z(j=>j&&{...j,active:[...j.active,P.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${P.label} \u2014 authoring\u2026`})):(Z(j=>j&&{...j,active:j.active.filter(W=>W!==P.label),done:j.done+1,lastAt:Date.now()}),u({kind:"note",tone:P.status==="done"?"ok":"error",text:` ${P.status==="done"?"\u2713":"\u2717"} ${P.label}`}));else if(P.type==="partition_event")Z(j=>{if(!j)return j;let W={...j,lastAt:Date.now()};return P.event.type==="tool"?W.tools=j.tools+1:P.event.type==="text"&&(W.chars=j.chars+P.event.delta.length),W});else if(P.type==="run_done"){dt(O=>O+P.totalCostUsd),ut(O=>O+P.results.reduce((Y,te)=>Y+te.outputTokens,0));let j=P.results.filter(O=>O.ok).length,W=P.aborted?"Stopped. ":"";_=!P.aborted&&p.setup;let ee=P.aborted?"":_?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";u({kind:"note",tone:P.aborted?"warn":P.ok?"ok":"warn",text:`${W}Authored specs \u2014 ${j}/${P.results.length} batches ok.${ee}`})}}catch(P){u({kind:"note",tone:"error",text:`Spec authoring failed: ${P.message}`})}finally{ve.current=null,Z(null),G(!1)}if(_){let P=await Ct();P.kind==="forward-to-agent"&&P.prompt&&await Ce(P.prompt,{display:P.display,echoDisplay:!0});let j=fo(e);j&&u({kind:"note",tone:"info",text:j.join(`
155
+ `)})}return}case"reset":{let p=ho(e);if(p.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}yn.current={repoName:gn(e),targets:p},u({kind:"note",tone:"warn",text:wo(e,p).join(`
156
+ `)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Lt,u,se,Ce]),Ot=V(l=>{let a=(l??I).trim();if(b(""),jt(null),qe(0),a.startsWith("/")){let h=m=>m.trim().split(/\s+/).slice(1).join(" "),p=h(a);X(m=>m.filter(w=>w.trim()!==a&&!(p&&h(w)===p)))}if(!a||Go(a)||qo(a))return;wn.current.push(a);let i=Ws(a,Do.current);a.startsWith("/")?zo(i):Ce(i,{display:a})},[I,Go,qo,zo,Ce]),Yo=V(l=>{if(l.length>1){if(l.includes("\x1B"))return;let a=Ls(l);if(Os(a)){let i=Us(++ui.current,a);Do.current.set(i,a),C(i)}else C(a);return}C(l)},[C]),et=Math.max(10,We-6),Vo=mn(()=>pn(k.text,et),[k.text,et]),Tn=V(l=>y(a=>({...a,pos:Math.max(0,Math.min(a.text.length,a.pos+l))})),[]),Ut=V(l=>y(a=>({...a,pos:Hs(pn(a.text,et),a.pos,l)})),[et]),ft=V(l=>y(a=>({...a,pos:Bs(pn(a.text,et),a.pos,l)})),[et]),fi=["\x1B[H","\x1B[1~","\x1BOH"],mi=["\x1B[F","\x1B[4~","\x1BOF"],Xo=V((l,a)=>a.leftArrow?(Tn(-1),!0):a.rightArrow?(Tn(1),!0):l&&fi.includes(l)?(ft("start"),!0):l&&mi.includes(l)?(ft("end"),!0):a.ctrl&&l==="a"?(ft("start"),!0):a.ctrl&&l==="e"?(ft("end"),!0):!1,[Tn,ft]);return Ic((l,a)=>{if(a.ctrl&&l==="c"){ce.current?.close(),r();return}if(!z){if(N){if(a.escape){if(ve.current){ve.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),ve.current.abort());return}Et.current||(Et.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),ce.current?.interrupt());return}if(a.return){let i=I.trim();if(!i)return;Oe.current.push(i),xn([...Oe.current]),b("");return}if(Xo(l,a))return;if(a.upArrow){Ut(-1);return}if(a.downArrow){Ut(1);return}if(a.backspace||a.delete){$();return}l&&!a.ctrl&&!a.meta&&Yo(l);return}if(_e){if(a.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Me(i=>i&&{...i,cursor:Math.min(we.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=we.length){Me(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=we[_e.cursor];Me(null);let{lines:h,changed:p,effective:m}=bt(e,i.value??"default");for(let w of h)u({kind:"note",tone:w.startsWith("\u26A0")?"warn":w.startsWith("\u2713")?"ok":"info",text:w});p&&(Oo(w=>w+1),ce.current?.setModel(m));return}if(l==="s"){let i=we[_e.cursor];Me(null),ce.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(a.escape){Me(null);return}return}if($e){if(a.upArrow){Ve(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Ve(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=$e.rows.length){Ve(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return||l==="s"){let i=$e.rows[$e.cursor];Ve(null),Pn(i.name,a.return===!0);return}if(a.escape){Ve(null);return}return}if(Re){if(a.upArrow){Xe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Xe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Re.rows.length){Xe(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=Re.rows[Re.cursor].name;if(Xe(null),!gr(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let m=He(i);p=!!m&&!(Ie(m)&&!m.refreshToken)}catch{}p?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),X(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),X(["/login"])),ye(m=>m+1);return}if(a.escape){Xe(null);return}return}if(oe){if(a.upArrow){Je(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Je(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=oe.rows.length){Je(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=oe.rows[oe.cursor],{profile:h,projectId:p}=oe;Je(null),Mt(h,p,i);return}if(a.escape){Je(null);return}return}if(ue){if(a.upArrow){Ke(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Ke(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ue.rows.length){Ke(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=ue.rows[ue.cursor],{profile:h}=ue;Ke(null),Bo(h,i);return}if(a.escape){Ke(null);return}return}if(re){if(a.upArrow){Ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l===" "){Ze(i=>i&&{...i,rows:i.rows.map((h,p)=>p===i.cursor?{...h,checked:!h.checked}:h)});return}if(a.return){let i=re.rows.filter(h=>h.checked).map(h=>h.path);if(Ze(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}Nn(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of i)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(a.escape){Ze(null);return}return}if(be){if(a.upArrow){Qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=be.rows.length){Qe(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=be.rows[be.cursor],h=be.rows.filter(p=>p.path!=="--all").map(p=>p.path);Qe(null),Ce(i.path==="--all"?yt(h):Kt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(a.escape){Qe(null);return}return}if(de){if(a.escape){Le(null);return}if(a.upArrow){Le(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Le(i=>i&&{...i,cursor:Math.min(Math.max(0,Nt.length-1),i.cursor+1)});return}if(a.return){let i=Nt[de.cursor];if(i){Le(null);try{u({kind:"preview",name:gn(i),text:ti(Ys(St(e,i),"utf8"))})}catch(h){u({kind:"note",tone:"error",text:`Could not read ${i}: ${h.message}`})}}return}if(a.backspace||a.delete){Le(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(l&&!a.ctrl&&!a.meta&&l.length===1){Le(i=>i&&{...i,query:i.query+l,cursor:0});return}return}if(pe){if(a.escape){Ue(null);return}if(a.upArrow){Ue(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Ue(i=>i&&{...i,cursor:Math.min(Math.max(0,_t.length-1),i.cursor+1)});return}if(a.return){let i=_t[pe.cursor];if(i){Ue(null),se(i.uuid),xe.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Ye(i.name),Co(i.uuid),ze.current=[];let h=bo(e,i.uuid);for(let p of h)u(p);u({kind:"note",tone:"ok",text:h.length?`Resumed "${i.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(a.backspace||a.delete){Ue(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(l&&!a.ctrl&&!a.meta&&l.length===1){Ue(i=>i&&{...i,query:i.query+l,cursor:0});return}return}if(a.tab&&!I&&ge){b(ge),he(null),Tt.current++;return}if(!I&&it.length>0&&l&&/^[1-9]$/.test(l)){let i=it[Number(l)-1];if(i){b(i);return}}if(!Xo(l,a)){if(Fo){if(a.upArrow){qe(i=>Math.max(0,i-1));return}if(a.downArrow){qe(i=>Math.min(pt.length-1,i+1));return}if(a.tab){b("/"+(pt[lt]?.name??"")+" "),qe(0);return}if(a.return){let i=pt[lt];if(i){let h=I.trim().slice(1).split(/\s+/).slice(1).join(" ");if(ur(i.usage)&&!h){b("/"+i.name+" "),qe(0);return}Ot("/"+i.name+(h?" "+h:""));return}}}else{if(a.upArrow){if(I!==""&&Ge===null){Ut(-1);return}let i=wn.current;if(!i.length)return;let h=Ge===null?i.length-1:Math.max(0,Ge-1);jt(h),b(i[h]??"");return}if(a.downArrow){if(I!==""&&Ge===null){Ut(1);return}let i=wn.current;if(Ge===null)return;let h=Ge+1;h>=i.length?(jt(null),b("")):(jt(h),b(i[h]??""));return}}if(a.return){Ot();return}if(a.backspace||a.delete){$();return}if(a.escape){b(""),qe(0),he(null),X([]);return}l&&!a.ctrl&&!a.meta&&Yo(l)}}}),Ne(()=>{if(N||z)return;let l=Oe.current.shift();l!==void 0&&(xn([...Oe.current]),Ot(l))},[N,z,Ot]),U(J,{flexDirection:"column",width:We,children:[vn!==null&&U(J,{marginTop:1,flexDirection:"column",children:[vn.truncated&&S(v,{dimColor:!0,children:"\u2026"}),S(v,{children:vn.text})]}),N&&(_o?S(xu,{p:_o}):S(wu,{startTime:Be.current,chars:ci})),S(J,{borderStyle:"round",borderColor:Wo.prod?"yellow":B,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:I?Vo.map((l,a)=>{let i=k.text.slice(l.start,l.end),h=a===fn(Vo,k.pos),p=Math.min(k.pos,l.end)-l.start;return U(v,{children:[S(v,{color:B,children:a===0?"> ":" "}),h?U(Ro,{children:[i.slice(0,p),S(hn,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${a}-${l.start}`)}):U(v,{children:[S(v,{color:B,children:"> "}),ge&&!N?U(Ro,{children:[S(hn,{ch:ge[0],dim:!0}),S(v,{color:"gray",children:ge.slice(1)}),S(v,{dimColor:!0,children:" (tab)"})]}):bn.isSetup||!li?U(Ro,{children:[S(hn,{ch:bn.text[0],dim:!0}),S(v,{color:"gray",children:bn.text.slice(1)})]}):S(hn,{ch:" "})]})}),No.length>0&&S(J,{flexDirection:"column",paddingX:1,children:No.map((l,a)=>S(v,{color:"gray",children:`\u29D7 queued: ${l}`},`${a}-${l.slice(0,24)}`))}),_e?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Select model"}),S(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),we.map((l,a)=>U(v,{color:a===_e.cursor?B:void 0,children:[a===_e.cursor?"\u276F ":" ",`${a+1}. ${l.label}${a===_e.current?" \u2714":""}`.padEnd(16),S(v,{color:"gray",children:l.desc})]},l.label)),S(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):$e?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Switch connection profile"}),$e.rows.map((l,a)=>U(v,{color:a===$e.cursor?B:void 0,children:[a===$e.cursor?"\u276F ":" ",`${a+1}. ${l.name}${a===$e.current?" \u2714":""}`.padEnd(20),S(v,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?S(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),S(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Re?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Pick your Document360 environment"}),Re.rows.map((l,a)=>U(v,{color:a===Re.cursor?B:void 0,children:[a===Re.cursor?"\u276F ":" ",`${a+1}. ${l.name}`.padEnd(16),S(v,{color:"gray",children:l.apiUrl})]},l.name)),S(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):oe?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Switch workspace"}),S(v,{dimColor:!0,children:`environment ${oe.environment} \xB7 project ${oe.projectId.slice(0,8)}\u2026`}),oe.rows.map((l,a)=>U(v,{color:a===oe.cursor?B:void 0,children:[a===oe.cursor?"\u276F ":" ",`${a+1}. ${l.name}${a===oe.current?" \u2714":""}`.padEnd(30),S(v,{color:"gray",children:l.type??""})]},l.id)),S(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):ue?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Choose project"}),S(v,{dimColor:!0,children:`environment ${ue.environment}`}),ue.rows.map((l,a)=>U(v,{color:a===ue.cursor?B:void 0,children:[a===ue.cursor?"\u276F ":" ",`${a+1}. ${l.name}${a===ue.current?" \u2714":""}`.padEnd(30),S(v,{color:"gray",children:l.sub??""})]},l.id)),S(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):re?(()=>{let a=Math.min(Math.max(0,re.cursor-Math.floor(7)),Math.max(0,re.rows.length-14)),i=re.rows.slice(a,a+14),h=re.rows.filter(p=>p.checked).length;return U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:`Which folders back the user docs? (${h} selected of ${re.rows.length})`}),a>0?S(v,{dimColor:!0,children:` \u2191 ${a} more`}):null,i.map((p,m)=>{let w=a+m;return U(v,{color:w===re.cursor?B:void 0,children:[w===re.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,We-34)),S(v,{color:"gray",children:Zt(p)})]},p.path)}),a+14<re.rows.length?S(v,{dimColor:!0,children:` \u2193 ${re.rows.length-a-14} more`}):null,S(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():be?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:"Publish which article?"}),be.rows.map((l,a)=>U(v,{color:a===be.cursor?B:void 0,children:[a===be.cursor?"\u276F ":" ",`${a+1}. ${l.path}`.padEnd(Math.min(56,We-30)),S(v,{color:"gray",children:l.label})]},l.path)),S(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):de?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:`Preview article${de.query?` \u2014 filter: ${de.query}`:" (type to filter)"}`}),Nt.length===0?S(v,{color:"gray",children:"no articles match"}):Nt.map((l,a)=>U(v,{color:a===de.cursor?B:void 0,children:[a===de.cursor?"\u276F ":" ",l]},l)),S(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):pe?U(J,{flexDirection:"column",paddingX:1,children:[S(v,{color:B,bold:!0,children:`Resume session${pe.query?` \u2014 filter: ${pe.query}`:" (type to filter)"}`}),_t.length===0?S(v,{color:"gray",children:"no sessions match"}):_t.map((l,a)=>U(v,{color:a===pe.cursor?B:void 0,children:[a===pe.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),S(v,{color:a===pe.cursor?B:"gray",children:l.firstPrompt.slice(0,Math.max(10,We-40))})]},l.uuid)),S(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Fo?S(J,{flexDirection:"column",children:pt.map((l,a)=>U(v,{color:a===lt?B:void 0,children:[a===lt?"\u276F ":" ",l.usage.padEnd(22)," ",S(v,{color:a===lt?B:"gray",children:l.name==="model"&&$n?`${l.desc} (currently ${$n})`:l.desc})]},l.name))}):!I&&it.length>0?U(J,{flexDirection:"column",paddingX:1,children:[it.map((l,a)=>U(v,{children:[S(v,{color:B,children:a+1})," ",l.slice(0,Math.max(20,We-5))]},l)),S(v,{dimColor:!0,children:`press 1-${it.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:U(v,{color:"gray",children:[Wo.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${$n??"model n/a"}${t.kind==="api"?Lo>0?` \xB7 ${fe(Lo)}`:"":Mo>0?` \xB7 ${rt(Mo)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as vu}from"react/jsx-runtime";async function si(e=process.cwd(),t="auto",n,o="0.0.0"){let r=bu(t);r.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=$u(vu(ri,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
157
+ `),process.exit(0)}var Tu=Pu(import.meta.url),ii=Tu("../package.json"),Se=new Cu;function Ao(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=>{Ao(e),await Bt({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=>{Ao(e),await sr({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=>{Ao(e),await rr({profile:e.profile})});var Eo=Se.command("profile").description("Manage connection profiles for the current repo");Eo.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>qt(process.cwd()));Eo.command("use <name>").description("Set the default profile for this repo").action(e=>Gt(process.cwd(),e));Eo.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>zt(process.cwd(),e));var ai=Se.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");ai.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>tt(process.cwd(),e.profile));ai.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await Zo(process.cwd(),e,t.profile)});Se.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>ar());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(()=>(nn(),os)),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(ii.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(Su.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 lr(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 As(e.cwd,e.auth,e.profile):await si(e.cwd,e.auth,e.profile,ii.version)});Se.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -0,0 +1,20 @@
1
+ import type { SlashCommandResult } from './index.js';
2
+ import type { ReplContext } from '../repl.js';
3
+ /** Bump when the guide content changes — lets a future check detect a stale committed guide. */
4
+ export declare const DEV_HINTS_GUIDE_VERSION = 1;
5
+ /**
6
+ * The committed protocol guide the SOURCE repo's coding agent follows to write doc hints. It is
7
+ * self-contained because that agent has none of d360-writer's context (no tools, no system prompt) —
8
+ * everything it needs to produce a valid hint is here. The /devhints command writes this to
9
+ * `.d360-writer/DEV-HINTS-GUIDE.md`; the paste block (below) just points the source agent at it.
10
+ */
11
+ export declare function devHintsGuide(): string;
12
+ /** `/devhints` — scaffold the hint channel: write the committed guide + emit a paste-ready block the
13
+ user adds to their SOURCE repo's CLAUDE.md/AGENTS.md so its coding agent starts leaving hints. */
14
+ export declare function devhintsCommand(_args: string[], ctx: ReplContext): Promise<SlashCommandResult>;
15
+ /**
16
+ * The paste-ready block for the user to add to their SOURCE repo's agent memory (CLAUDE.md/AGENTS.md).
17
+ * A ONE-LINE pointer to the committed guide — not the protocol inline — so their memory stays lean and
18
+ * we can improve the guide without anyone re-pasting. Mirrors renderTestidHandoff's bracketed shape.
19
+ */
20
+ export declare function renderDevHintsHandoff(): string[];
@@ -10,6 +10,9 @@ export declare function performReset(cwd: string, targets: string[]): {
10
10
  error: string;
11
11
  }[];
12
12
  };
13
+ /** Count UNcommitted (untracked or modified) files under .d360-writer/hints/ — they'd be lost by a
14
+ reset with no git copy to restore. Read-only git call; 0 on any error (no git, etc.). */
15
+ export declare function uncommittedHintCount(cwd: string): number;
13
16
  /** The destructive preview shown before the typed-name confirmation. */
14
17
  export declare function resetPreviewLines(cwd: string, targets: string[]): string[];
15
18
  /** `/reset` [DANGER] — classic REPL: preview → typed-name confirmation → delete. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.42",
3
+ "version": "0.4.43",
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.26",
37
+ "document360-engine": "^0.2.27",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",