document360-writer 0.4.31 → 0.4.33

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,81 +1,83 @@
1
1
  #!/usr/bin/env node
2
- var Ns=Object.defineProperty;var An=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var Ls=(e,t)=>{for(var o in t)Ns(e,o,{get:t[o],enumerable:!0})};import Se from"picocolors";var H,Os,Us,Ws,En,st,f,x,N,E,S,V,we,B=An(()=>{"use strict";H="#7f56d9",[Os,Us,Ws]=[127,86,217],En=e=>Se.isColorSupported?`\x1B[38;2;${Os};${Us};${Ws}m${e}\x1B[39m`:e,st=e=>Se.bold(En(e)),f=e=>Se.dim(e),x=e=>Se.red(e),N=e=>Se.yellow(e),E=e=>Se.green(e),S=En,V=e=>Se.gray(e),we=e=>Se.bold(e)});var Ir={};Ls(Ir,{doctorCommand:()=>ft,renderDoctorChecks:()=>Dr,runDoctorChecks:()=>Er});import{existsSync as wl}from"node:fs";import{d360GetAll as xl,getAccessToken as $l,isExpired as Yo,loadProfileMap as bl,loadTokens as vl,packageSkillsDir as Ar,projectConfigPath as Cl,readProjectConfig as Sl,resolveActiveProfile as Pl,resolveAuth as Tl,resolveModelSetting as Rl}from"document360-engine";async function Er(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=Tl("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=Rl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Sl(e);if(!s)return t.push({level:"fail",label:`No ${Cl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Sources: docs grounded in ${d.length} path(s) (${d.slice(0,3).join(", ")}${d.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let h;try{h=Pl(e),t.push({level:"ok",label:`Profile: ${h.name} (${h.connection.name})${h.production?" \u26A0 PRODUCTION":""}`})}catch(R){return t.push({level:"fail",label:`Profile config: ${R.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let g=vl(h.name);g?Yo(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Yo(g)?t.push({level:"warn",label:"Document360: token expired \u2014 will auto-refresh on next call"}):t.push({level:"ok",label:`Document360: logged in (until ${new Date(g.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${h.name})`}),t.push(h.project.workspaceId?{level:"ok",label:`Workspace: ${h.project.workspaceName??h.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let w=bl(e,h.name);if(w?w.projectId&&h.project.projectId&&w.projectId!==h.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.projectId}) \u2260 profile projectId (${h.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${h.name}" (created on first /publish)`}),t.push(wl(Ar())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Ar()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!Yo(g)||g.refreshToken)){let R={profile:h.name,connection:h.connection};try{await $l(R);let $=await xl(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${h.connection.apiUrl}) \u2014 ${$.length} project(s) visible`})}catch($){t.push({level:"fail",label:`API call failed: ${$.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Dr(e){let t=[""];for(let r of e){let s=r.level==="ok"?E("\u2713"):r.level==="warn"?N("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${S(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?E(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):x(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function ft(e,t){console.log(f("Running checks\u2026"));for(let o of Dr(await Er(t.cwd)))console.log(o);return{kind:"continue"}}var Jt=An(()=>{"use strict";B()});import{Command as uc}from"commander";import{createRequire as dc}from"node:module";import{AUTH_MODES as pc}from"document360-engine";import{input as Js}from"@inquirer/prompts";import{loginPkce as Ks,refreshTokens as Qs,toStoredTokens as Mn,clearTokens as Zs,decodeJwtClaims as _n,isExpired as ei,loadTokens as ti,saveTokens as Nn,resolveActiveProfile as _t,setProfileProject as oi,readProjectConfig as ni}from"document360-engine";B();import{select as Fs}from"@inquirer/prompts";import{resolveActiveProfile as Bs,setProfileProject as Hs,resolveProjectId as qs,listWorkspaces as Gs}from"document360-engine";async function Dt(e,t){let o=Bs(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??qs(n);return{workspaces:await Gs(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var Dn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Co(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function It(e,t,o,n,r){Hs(e,t,{projectId:o,workspaceId:n,workspaceName:r})}async function In(e,t,o){let n;try{n=await Dt(e,o)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Co(n.workspaces,t);return r?(It(e,n.profile,n.projectId,r.id,r.name),console.log(E(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Je(e,t){let o;try{o=await Dt(e,t)}catch(g){console.log(x(`Could not list workspaces: ${g.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===c?S("\u25CF"):" "} ${Dn(g)} ${f(g.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let d=await Fs({message:"Select the Document360 workspace for this repo:",choices:n.map(g=>({name:`${Dn(g)}${g.id===c?" (current)":""}`,value:g.id}))}),h=n.find(g=>g.id===d);It(e,s,r,d,h?.name),console.log(E(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}B();import zs from"picocolors";function Xs(e=process.env){return e.FORCE_HYPERLINK==="0"||!zs.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 Ys(e,t=e,o){return Xs(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Vs=/https?:\/\/[^\s\x1b]+/g;function it(e,t){return e.replace(Vs,o=>Ys(o,o,t))}function Ln(e){return{..._n(e.idToken)??{},..._n(e.accessToken)??{}}}function Mt(e){let t=Ln(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function Nt(e){let t=_t(process.cwd(),e.profile),o=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Ks(o,{manual:e.manual,promptForRedirect:s=>Js({message:s})},s=>console.log(it(s))),r=Mn(t.name,n);if(Nn(r),So(r,t.name,s=>console.log(f(s))),console.log(""),console.log(E(`\u2713 Logged in to "${t.name}" as ${Mt(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{ni(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Je(process.cwd(),t.name))}catch{}}function So(e,t,o){let r=Ln(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(_t(process.cwd(),t).project.projectId)return;oi(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function On(e){let t=_t(process.cwd(),e.profile),o=t.connection,n=ti(t.name);if(!n){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${S(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Mt(n)}`),ei(n))if(n.refreshToken)try{let r=Mn(t.name,await Qs(o,n.refreshToken));Nn(r),console.log(E(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(N(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(N("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${n.expiresAt}`))}async function Un(e){let t=_t(process.cwd(),e.profile);Zs(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.`))}B();import{readProjectConfig as Wn,writeProjectConfig as ri,resolveActiveProfile as si,loadTokens as ii,isExpired as li}from"document360-engine";function Lt(e){let t=Wn(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?S("\u25CF "):" ",s=n.production?N(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=ii(o),h=d?li(d)&&!d.refreshToken?N("expired"):f("logged in"):f("not logged in");console.log(`${r}${S(o)} \u2192 ${c}${s} [${h}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Ot(e,t){let o=Wn(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,ri(o,e);let n=o.profiles[t].production?N(" \u26A0 PRODUCTION"):"";console.log(E(`\u2713 Default profile is now "${t}"${n}`))}function Ut(e,t){try{let o=si(e,t);console.log(""),console.log(`Profile ${S(o.name)}${o.production?N(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${o.connection.apiUrl}`)),console.log(f(` identity: ${o.connection.authorizationUrl}`)),console.log(f(` clientId: ${o.connection.clientId}`)),console.log(f(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(f(` project: ${o.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(x(o.message)),process.exitCode=1}}B();import{existsSync as ai,readdirSync as ci,statSync as ui}from"node:fs";import{join as di}from"node:path";import{apiLogDir as pi}from"document360-engine";function Fn(){let e=pi();if(console.log(""),console.log(`Document360 API logs: ${S(e)}`),!ai(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ci(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=(ui(di(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("")}B();import{createSession as fi,resolveAuth as mi,findByName as gi,slugify as hi,touchSession as ki,upsertSession as yi,resolveActiveProfile as wi}from"document360-engine";async function Bn(e,t,o,n,r,s){let c=mi(o);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let d=null;try{d=wi(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}d.production&&(console.error(N(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let h;if(n){let $=gi(e,n);$||(console.error(x(`No saved session matches "${n}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(f(`Resuming "${$.name}"`))}let g=fi({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),w=h??null,R=1;for await(let $ of g.send(t))switch($.type){case"session":if(!w){w=$.sessionId;let j=new Date().toISOString();yi({uuid:w,name:hi(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:j,updatedAt:j})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(V(` \u2699 ${$.name}`));break;case"result":R=$.ok?0:1,console.error(f(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${$.message}`)),process.exit(1)}w&&ki(w),process.stdout.write(`
3
- `),process.exit(R)}import{createInterface as Vl}from"node:readline/promises";import{createSession as nn,resolveAuth as Jl,getSession as Kl,setTitle as Ql,slugify as Zl,touchSession as Jr,upsertSession as ea,generateTitle as ta,resolveActiveProfile as Kr,resolveModelSetting as oa,readProjectConfig as na,decodeJwtClaims as Yr,isExpired as ra,loadTokens as sa}from"document360-engine";var lt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],xi=[{key:"start",label:"Start here"},{key:"docs",label:"Write & screenshots"},{key:"publish",label:"Publish & keep in sync"},{key:"setup",label:"Setup & health"},{key:"session",label:"Session"}];function Wt(){let e=Math.max(...lt.map(o=>o.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:o,label:n}of xi){t.push("",n);for(let r of lt.filter(s=>s.group===o))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function Hn(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return lt.filter(o=>o.name.startsWith(t))}function qn(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Po(){console.log("");for(let e of Wt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}B();import{getSession as $i}from"document360-engine";async function Gn(e,t){let o=t.currentUuid(),n=o?$i(o):void 0;return console.log(n?f(`
2
+ var Os=Object.defineProperty;var En=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var Us=(e,t)=>{for(var o in t)Os(e,o,{get:t[o],enumerable:!0})};import Se from"picocolors";var H,Ws,Fs,Bs,Dn,st,f,w,N,D,S,V,xe,B=En(()=>{"use strict";H="#7f56d9",[Ws,Fs,Bs]=[127,86,217],Dn=e=>Se.isColorSupported?`\x1B[38;2;${Ws};${Fs};${Bs}m${e}\x1B[39m`:e,st=e=>Se.bold(Dn(e)),f=e=>Se.dim(e),w=e=>Se.red(e),N=e=>Se.yellow(e),D=e=>Se.green(e),S=Dn,V=e=>Se.gray(e),xe=e=>Se.bold(e)});var _r={};Us(_r,{doctorCommand:()=>ft,renderDoctorChecks:()=>Ir,runDoctorChecks:()=>Dr});import{existsSync as $l}from"node:fs";import{d360GetAll as bl,getAccessToken as vl,isExpired as Yo,loadProfileMap as Cl,loadTokens as Sl,packageSkillsDir as Er,projectConfigPath as Tl,readProjectConfig as Pl,resolveActiveProfile as Rl,resolveAuth as Al,resolveModelSetting as jl}from"document360-engine";async function Dr(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=Al("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=jl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Pl(e);if(!s)return t.push({level:"fail",label:`No ${Tl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Sources: docs grounded in ${d.length} path(s) (${d.slice(0,3).join(", ")}${d.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let h;try{h=Rl(e),t.push({level:"ok",label:`Profile: ${h.name} (${h.connection.name})${h.production?" \u26A0 PRODUCTION":""}`})}catch(A){return t.push({level:"fail",label:`Profile config: ${A.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let g=Sl(h.name);g?Yo(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Yo(g)?t.push({level:"warn",label:"Document360: token expired \u2014 will auto-refresh on next call"}):t.push({level:"ok",label:`Document360: logged in (until ${new Date(g.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${h.name})`}),t.push(h.project.workspaceId?{level:"ok",label:`Workspace: ${h.project.workspaceName??h.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let x=Cl(e,h.name);if(x?x.projectId&&h.project.projectId&&x.projectId!==h.project.projectId?t.push({level:"fail",label:`Category map projectId (${x.projectId}) \u2260 profile projectId (${h.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(x.articles).length} articles, ${Object.keys(x.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${h.name}" (created on first /publish)`}),t.push($l(Er())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Er()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!Yo(g)||g.refreshToken)){let A={profile:h.name,connection:h.connection};try{await vl(A);let $=await bl(A,"/v3/projects");t.push({level:"ok",label:`API reachable (${h.connection.apiUrl}) \u2014 ${$.length} project(s) visible`})}catch($){t.push({level:"fail",label:`API call failed: ${$.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Ir(e){let t=[""];for(let r of e){let s=r.level==="ok"?D("\u2713"):r.level==="warn"?N("\u26A0"):w("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${S(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?D(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):w(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function ft(e,t){console.log(f("Running checks\u2026"));for(let o of Ir(await Dr(t.cwd)))console.log(o);return{kind:"continue"}}var Jt=En(()=>{"use strict";B()});import{Command as yc}from"commander";import{createRequire as xc}from"node:module";import{AUTH_MODES as wc}from"document360-engine";import{input as Qs}from"@inquirer/prompts";import{loginPkce as Zs,refreshTokens as ei,toStoredTokens as Nn,clearTokens as ti,decodeJwtClaims as Mn,isExpired as oi,loadTokens as ni,saveTokens as Ln,resolveActiveProfile as _t,setProfileProject as ri,readProjectConfig as si}from"document360-engine";B();import{select as Hs}from"@inquirer/prompts";import{resolveActiveProfile as qs,setProfileProject as zs,resolveProjectId as Gs,listWorkspaces as Xs}from"document360-engine";async function Dt(e,t){let o=qs(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??Gs(n);return{workspaces:await Xs(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var In=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Co(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function It(e,t,o,n,r){zs(e,t,{projectId:o,workspaceId:n,workspaceName:r})}async function _n(e,t,o){let n;try{n=await Dt(e,o)}catch(s){return console.log(w(`Could not list workspaces: ${s.message}`)),1}let r=Co(n.workspaces,t);return r?(It(e,n.profile,n.projectId,r.id,r.name),console.log(D(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(w(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Je(e,t){let o;try{o=await Dt(e,t)}catch(g){console.log(w(`Could not list workspaces: ${g.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===c?S("\u25CF"):" "} ${In(g)} ${f(g.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let d=await Hs({message:"Select the Document360 workspace for this repo:",choices:n.map(g=>({name:`${In(g)}${g.id===c?" (current)":""}`,value:g.id}))}),h=n.find(g=>g.id===d);It(e,s,r,d,h?.name),console.log(D(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}B();import Ys from"picocolors";function Vs(e=process.env){return e.FORCE_HYPERLINK==="0"||!Ys.isColorSupported?!1:e.FORCE_HYPERLINK?!0:!!(e.WT_SESSION||e.TERM_PROGRAM==="vscode"||e.TERM_PROGRAM==="iTerm.app"||e.TERM_PROGRAM==="WezTerm"||e.TERM_PROGRAM==="ghostty"||e.VTE_VERSION||e.KONSOLE_VERSION)}function Js(e,t=e,o){return Vs(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ks=/https?:\/\/[^\s\x1b]+/g;function it(e,t){return e.replace(Ks,o=>Js(o,o,t))}function On(e){return{...Mn(e.idToken)??{},...Mn(e.accessToken)??{}}}function Mt(e){let t=On(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function Nt(e){let t=_t(process.cwd(),e.profile),o=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Zs(o,{manual:e.manual,promptForRedirect:s=>Qs({message:s})},s=>console.log(it(s))),r=Nn(t.name,n);if(Ln(r),So(r,t.name,s=>console.log(f(s))),console.log(""),console.log(D(`\u2713 Logged in to "${t.name}" as ${Mt(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{si(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Je(process.cwd(),t.name))}catch{}}function So(e,t,o){let r=On(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(_t(process.cwd(),t).project.projectId)return;ri(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function Un(e){let t=_t(process.cwd(),e.profile),o=t.connection,n=ni(t.name);if(!n){console.log(w(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${S(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Mt(n)}`),oi(n))if(n.refreshToken)try{let r=Nn(t.name,await ei(o,n.refreshToken));Ln(r),console.log(D(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(N(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(N("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${n.expiresAt}`))}async function Wn(e){let t=_t(process.cwd(),e.profile);ti(t.name)?console.log(D(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}B();import{readProjectConfig as Fn,writeProjectConfig as ii,resolveActiveProfile as li,loadTokens as ai,isExpired as ci}from"document360-engine";function Lt(e){let t=Fn(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(w("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?S("\u25CF "):" ",s=n.production?N(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=ai(o),h=d?ci(d)&&!d.refreshToken?N("expired"):f("logged in"):f("not logged in");console.log(`${r}${S(o)} \u2192 ${c}${s} [${h}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Ot(e,t){let o=Fn(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(w(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,ii(o,e);let n=o.profiles[t].production?N(" \u26A0 PRODUCTION"):"";console.log(D(`\u2713 Default profile is now "${t}"${n}`))}function Ut(e,t){try{let o=li(e,t);console.log(""),console.log(`Profile ${S(o.name)}${o.production?N(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${o.connection.apiUrl}`)),console.log(f(` identity: ${o.connection.authorizationUrl}`)),console.log(f(` clientId: ${o.connection.clientId}`)),console.log(f(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(f(` project: ${o.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(w(o.message)),process.exitCode=1}}B();import{existsSync as ui,readdirSync as di,statSync as pi}from"node:fs";import{join as fi}from"node:path";import{apiLogDir as mi}from"document360-engine";function Bn(){let e=mi();if(console.log(""),console.log(`Document360 API logs: ${S(e)}`),!ui(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=di(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(pi(fi(e,o)).size/1024).toFixed(1);console.log(` ${o} ${f(`${n} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}B();import{createSession as gi,resolveAuth as hi,findByName as ki,slugify as yi,touchSession as xi,upsertSession as wi,resolveActiveProfile as $i}from"document360-engine";async function Hn(e,t,o,n,r,s){let c=hi(o);c.kind==="none"&&(console.error(""),console.error(w("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let d=null;try{d=$i(e,r)}catch($){console.error(w(`Document360 profile error: ${$.message}`)),process.exit(2)}d.production&&(console.error(N(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(w("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let h;if(n){let $=ki(e,n);$||(console.error(w(`No saved session matches "${n}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(f(`Resuming "${$.name}"`))}let g=gi({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),x=h??null,A=1;for await(let $ of g.send(t))switch($.type){case"session":if(!x){x=$.sessionId;let j=new Date().toISOString();wi({uuid:x,name:yi(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:j,updatedAt:j})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(V(` \u2699 ${$.name}`));break;case"result":A=$.ok?0:1,console.error(f(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(w("agent finished with an error result"));break;case"error":console.error(""),console.error(w(`agent error: ${$.message}`)),process.exit(1)}x&&xi(x),process.stdout.write(`
3
+ `),process.exit(A)}import{createInterface as na}from"node:readline/promises";import{createSession as rn,resolveAuth as ra,getSession as sa,setTitle as ia,slugify as la,touchSession as Qr,upsertSession as aa,generateTitle as ca,resolveActiveProfile as Zr,resolveModelSetting as ua,readProjectConfig as da,decodeJwtClaims as Jr,isExpired as pa,loadTokens as fa}from"document360-engine";var lt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],bi=[{key:"start",label:"Start here"},{key:"docs",label:"Write & screenshots"},{key:"publish",label:"Publish & keep in sync"},{key:"setup",label:"Setup & health"},{key:"session",label:"Session"}];function Wt(){let e=Math.max(...lt.map(o=>o.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:o,label:n}of bi){t.push("",n);for(let r of lt.filter(s=>s.group===o))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function qn(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return lt.filter(o=>o.name.startsWith(t))}function zn(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function To(){console.log("");for(let e of Wt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}B();import{getSession as vi}from"document360-engine";async function Gn(e,t){let o=t.currentUuid(),n=o?vi(o):void 0;return console.log(n?f(`
4
4
  (conversation reset \u2014 "${n.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 To(){return{kind:"exit"}}B();import{input as at,confirm as bi}from"@inquirer/prompts";import{basename as zn,join as Ft}from"node:path";import{existsSync as Ke,readFileSync as vi,readdirSync as Ci,writeFileSync as Si}from"node:fs";import{writeProjectConfig as Xn,readProjectConfig as Pi,projectConfigPath as Yn,writerDir as Ti}from"document360-engine";function Vn(e,t="berlin"){let o=Yn(e);if(Ke(o))return{created:!1,path:o,profileName:""};let n={projectId:Qn(e)??zn(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Zn(e)};return Xn(n,e),Jn(e),{created:!0,path:o,profileName:t}}function Jn(e){let t=Ft(Ti(e),".gitignore");Ke(t)||Si(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
- `),"utf8")}async function Kn(){let e=process.cwd(),t=Yn(e);if(Ke(t)&&!await bi({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let o=Qn(e)??zn(e),n=await at({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await at({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await at({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await at({message:"Default connection profile name:",default:"berlin"}),d=await at({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),h={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:d},production:!1}},defaultProfile:c,authoritativeSourceFiles:Zn(e)},g=Pi(e);return g?.terminologyGlossary&&(h.terminologyGlossary=g.terminologyGlossary),Xn(h,e),Jn(e),console.log(""),console.log(E(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${S(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Qn(e){let t=Ft(e,"package.json");if(!Ke(t))return null;try{return JSON.parse(vi(t,"utf8")).name??null}catch{return null}}function Zn(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])Ke(Ft(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=Ft(e,o);Ke(n)&&!Ri(n)&&t.push(o)}return t}function Ri(e){try{return Ci(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}B();import{readMcpConfig as Ro,writeMcpConfig as er}from"document360-engine";async function Bt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(ji(),{kind:"continue"}):t==="add"?(Ai(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ei(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 ji(){let e=Ro(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
7
+ `)),{kind:"clear"}}async function Po(){return{kind:"exit"}}B();import{input as at,confirm as Ci}from"@inquirer/prompts";import{basename as Xn,join as Ft}from"node:path";import{existsSync as Ke,readFileSync as Si,readdirSync as Ti,writeFileSync as Pi}from"node:fs";import{writeProjectConfig as Yn,readProjectConfig as Ri,projectConfigPath as Vn,writerDir as Ai}from"document360-engine";function Jn(e,t="berlin"){let o=Vn(e);if(Ke(o))return{created:!1,path:o,profileName:""};let n={projectId:Zn(e)??Xn(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:er(e)};return Yn(n,e),Kn(e),{created:!0,path:o,profileName:t}}function Kn(e){let t=Ft(Ai(e),".gitignore");Ke(t)||Pi(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
+ `),"utf8")}async function Qn(){let e=process.cwd(),t=Vn(e);if(Ke(t)&&!await Ci({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let o=Zn(e)??Xn(e),n=await at({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await at({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await at({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await at({message:"Default connection profile name:",default:"berlin"}),d=await at({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),h={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:d},production:!1}},defaultProfile:c,authoritativeSourceFiles:er(e)},g=Ri(e);return g?.terminologyGlossary&&(h.terminologyGlossary=g.terminologyGlossary),Yn(h,e),Kn(e),console.log(""),console.log(D(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${S(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Zn(e){let t=Ft(e,"package.json");if(!Ke(t))return null;try{return JSON.parse(Si(t,"utf8")).name??null}catch{return null}}function er(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])Ke(Ft(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=Ft(e,o);Ke(n)&&!ji(n)&&t.push(o)}return t}function ji(e){try{return Ti(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}B();import{readMcpConfig as Ro,writeMcpConfig as tr}from"document360-engine";async function Bt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Ei(),{kind:"continue"}):t==="add"?(Di(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ii(e.slice(1)),{kind:"continue"}):(console.log(w(`Unknown /mcp subcommand: ${t}`)),console.log(f("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Ei(){let e=Ro(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
9
9
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
10
- `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${S(o)} ${f("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${S(o)} ${f(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function Ai(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(x(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=Ro(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let d=1;d<n.length;d++)if(n[d]==="-H"||n[d]==="--header"){let h=n[++d],g=h?.match(/^([^:=]+)[:=](.+)$/);if(!g){console.log(x(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[g[1].trim()]=g[2].trim()}else{console.log(x(`Unexpected argument: ${n[d]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,er(r),console.log(""),console.log(E(`\u2713 Registered "${t}" (${o})`)),console.log(N(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ei(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let o=Ro();if(!o.servers[t]){console.log(x(`No server named "${t}".`));return}delete o.servers[t],er(o),console.log(E(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}B();import{select as Di}from"@inquirer/prompts";import{computeSyncStatus as tr}from"document360-engine";function qt(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
11
- `)}var or={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Ht(e){return e.filter(t=>t.path!==null&&t.status in or).map(t=>({path:t.path,label:or[t.status]}))}function ct(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
12
- `)}async function nr(e,t){let o=e[0];if(o==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let n=Ht((await tr({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:ct(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(x(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(f("Checking what needs publishing\u2026"));let n;try{n=Ht((await tr({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(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(()=>Di({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:ct(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:qt(o),display:`/publish ${o}`}}async function Gt(){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"}}B();import{checkbox as Ii}from"@inquirer/prompts";import{inventoryRepo as _i,readProjectConfig as rr,writeProjectConfig as Mi}from"document360-engine";function jo(e,t){let o=rr(e);o&&(o.authoritativeSourceFiles=t,Mi(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 sr(e,t){let o=t?.cwd??process.cwd();if(!rr(o))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=_i(o);if(n.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(S("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(zt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Ii({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"};jo(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"}}B();import{confirm as Wi}from"@inquirer/prompts";import{applyPull as ur,computeSyncStatus as dr,planPull as Fi,D360AuthError as Bi}from"document360-engine";B();var ir=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:N,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:S,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:x,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:x,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:V,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:V,mark:"\xB7"}];function Ni(e){return e.path?e.path:`${e.title??"(untitled)"} ${V(`[${e.articleId}]`)}`}function Xt(e){let t=[];for(let c of ir){let d=e.entries.filter(h=>h.status===c.status);if(d.length!==0){t.push(""),t.push(we(c.header));for(let h of d)t.push(` ${c.paint(c.mark)} ${c.paint(Ni(h))}${h.detail?V(` (${h.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?E("\u2713 everything in sync"):[o>0?E(`\u2713 ${o} in sync`):null,...ir.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(V(" \xB7 "));return t.push(s+V(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Li}from"diff";import lr from"picocolors";var ar=80,Oi="\x1B[48;2;74;28;28m",Ui="\x1B[48;2;24;66;24m",cr="\x1B[49m",Ao=e=>String(e).padStart(5);function Le(e,t,o){let n=$=>{let j=$.replace(/\r\n/g,`
10
+ `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${S(o)} ${f("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${S(o)} ${f(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function Di(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(w("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(w(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=Ro(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let d=1;d<n.length;d++)if(n[d]==="-H"||n[d]==="--header"){let h=n[++d],g=h?.match(/^([^:=]+)[:=](.+)$/);if(!g){console.log(w(`-H expects key:value (no spaces). Got: ${h??"(nothing)"}`));return}c[g[1].trim()]=g[2].trim()}else{console.log(w(`Unexpected argument: ${n[d]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,tr(r),console.log(""),console.log(D(`\u2713 Registered "${t}" (${o})`)),console.log(N(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ii(e){let t=e[0];if(!t){console.log(w("Usage: /mcp remove <name>"));return}let o=Ro();if(!o.servers[t]){console.log(w(`No server named "${t}".`));return}delete o.servers[t],tr(o),console.log(D(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}B();import{select as _i}from"@inquirer/prompts";import{computeSyncStatus as or}from"document360-engine";function qt(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
11
+ `)}var nr={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Ht(e){return e.filter(t=>t.path!==null&&t.status in nr).map(t=>({path:t.path,label:nr[t.status]}))}function ct(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
12
+ `)}async function rr(e,t){let o=e[0];if(o==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let n=Ht((await or({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:ct(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(w(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(f("Checking what needs publishing\u2026"));let n;try{n=Ht((await or({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(w(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of n)console.log(` ${r.path} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>_i({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:ct(n.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:qt(o),display:`/publish ${o}`}}async function zt(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
13
+ `),display:"/audit"}}B();import{checkbox as Mi}from"@inquirer/prompts";import{inventoryRepo as Ni,readProjectConfig as sr,writeProjectConfig as Li}from"document360-engine";function Ao(e,t){let o=sr(e);o&&(o.authoritativeSourceFiles=t,Li(o,e))}function Gt(e){let t=[`${e.fileCount}${e.fileCount>=5e3?"+":""} files`];return e.stacks.length&&t.push(e.stacks.join("+")),t.push(e.reason),t.join(" \xB7 ")}async function ir(e,t){let o=t?.cwd??process.cwd();if(!sr(o))return console.log(w("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=Ni(o);if(n.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(S("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(Gt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Mi({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${Gt(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(f("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};Ao(o,r),console.log(D(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}B();import{confirm as Bi}from"@inquirer/prompts";import{applyPull as dr,computeSyncStatus as pr,planPull as Hi,D360AuthError as qi}from"document360-engine";B();var lr=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:w,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:N,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:S,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:w,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:w,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:V,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:V,mark:"\xB7"}];function Oi(e){return e.path?e.path:`${e.title??"(untitled)"} ${V(`[${e.articleId}]`)}`}function Xt(e){let t=[];for(let c of lr){let d=e.entries.filter(h=>h.status===c.status);if(d.length!==0){t.push(""),t.push(xe(c.header));for(let h of d)t.push(` ${c.paint(c.mark)} ${c.paint(Oi(h))}${h.detail?V(` (${h.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?D("\u2713 everything in sync"):[o>0?D(`\u2713 ${o} in sync`):null,...lr.map(c=>{let d=e.counts[c.status]??0;return d>0?`${d} ${r[c.status]}`:null})].filter(Boolean).join(V(" \xB7 "));return t.push(s+V(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Ui}from"diff";import ar from"picocolors";var cr=80,Wi="\x1B[48;2;74;28;28m",Fi="\x1B[48;2;24;66;24m",ur="\x1B[49m",jo=e=>String(e).padStart(5);function Le(e,t,o){let n=$=>{let j=$.replace(/\r\n/g,`
14
14
  `);return j.endsWith(`
15
15
  `)||j===""?j:j+`
16
- `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),d=Li("a","b",r,s,"","",{context:3}),h=0,g=0,w=[];d.hunks.forEach(($,j)=>{j>0&&w.push(lr.gray(" \u2026"));let T=$.oldStart,M=$.newStart;for(let q of $.lines){let G=q[0],ee=q.slice(1).slice(0,c);G==="-"?(g++,w.push(`${Oi}${Ao(T++)} - ${ee}${cr}`)):G==="+"?(h++,w.push(`${Ui}${Ao(M++)} + ${ee}${cr}`)):(w.push(lr.gray(Ao(M))+" "+ee),T++,M++)}});let R=w.slice(0,ar);return{added:h,removed:g,lines:R,hidden:Math.max(0,w.length-ar)}}async function pr(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await Hi(t.cwd),{kind:"continue"};if(o==="pull")return await qi(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${o}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof Bi?console.log(x(n.message)):console.log(x(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function Hi(e){console.log(f("Checking Document360 for drift\u2026"));let t=await dr({cwd:e});for(let o of Xt(t))console.log(o);console.log("")}async function qi(e,t){let o=t[0];if(!o){console.log(x("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),n=(await dr({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(`${we(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await Fi({cwd:e.cwd,relPath:r});console.log(""),console.log(`${S("\u25CF")} ${we(s.title)} ${V(`(${s.path})`)}`);for(let g of s.notes)console.log(N(` \u26A0 ${g}`));s.overwritesLocalChanges&&console.log(N(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Le(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(f(" Local file already matches the remote content \u2014 advancing the sync base only.")),ur({cwd:e.cwd},s);continue}let d=g=>g===1?"":"s";console.log(V(` \u23BF Added ${c.added} line${d(c.added)}, removed ${c.removed} line${d(c.removed)}`));for(let g of c.lines)console.log(g);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>Wi({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}ur({cwd:e.cwd},s),console.log(E(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}B();import{statSync as tl}from"node:fs";import{resolve as ol}from"node:path";import{estimateBulkCost as nl,planPartitions as rl,readProjectConfig as sl,resolveModelForOperation as il,runPartitioned as ll,trackedArticlePaths as al}from"document360-engine";import F from"picocolors";import yr from"wrap-ansi";import No from"string-width";B();import Pe from"picocolors";import Eo from"wrap-ansi";import Do from"string-width";var fr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),mr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),gr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Gi(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(fr(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&mr(t[n+1])){let d=gr(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(gr(t[n++]));o.push({kind:"table",header:d,rows:h});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let d=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)d.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:d});continue}if(r.trim()===""){n++;continue}let c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!fr(t[n])&&!(t[n].includes("|")&&n+1<t.length&&mr(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Io(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Pe.bold(t.slice(2,-2)):t.startsWith("`")?S(t.slice(1,-1)):Pe.italic(t.slice(1,-1)))}var zi=(e,t)=>e+" ".repeat(Math.max(0,t-Do(e)));function Xi(e,t,o){let n=e.length,r=e.map((T,M)=>Math.max(Do(T),...t.map(q=>Do(q[M]??"")))),s=Math.max(24,o),c=3*n+1,d=[...r],h=()=>d.reduce((T,M)=>T+M,0)+c,g=0;for(;h()>s&&g++<1e4;){let T=-1,M=6;for(let q=0;q<n;q++)d[q]>M&&(M=d[q],T=q);if(T===-1)break;d[T]-=1}let w=(T,M,q)=>Pe.gray(T+d.map(G=>"\u2500".repeat(G+2)).join(M)+q),R=Pe.gray("\u2502"),$=(T,M)=>{let q=d.map((ie,$e)=>{let le=T[$e]??"",ae=M?Pe.bold(le):Io(le);return Eo(ae,ie,{hard:!0}).split(`
18
- `)}),G=Math.max(...q.map(ie=>ie.length)),ee=[];for(let ie=0;ie<G;ie++)ee.push(d.map(($e,le)=>`${R} ${zi(q[le][ie]??"",$e)} `).join("")+R);return ee.join(`
19
- `)},j=[w("\u250C","\u252C","\u2510"),$(e,!0)];return j.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach((T,M)=>{j.push($(T,!1)),j.push(M===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),j.join(`
20
- `)}function Yi(e,t){switch(e.kind){case"heading":return Pe.bold(e.text);case"hr":return Pe.gray("\u2500".repeat(t));case"para":return Eo(Io(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=Eo(Io(o),Math.max(10,t-4)).split(`
16
+ `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),d=Ui("a","b",r,s,"","",{context:3}),h=0,g=0,x=[];d.hunks.forEach(($,j)=>{j>0&&x.push(ar.gray(" \u2026"));let P=$.oldStart,M=$.newStart;for(let q of $.lines){let z=q[0],ee=q.slice(1).slice(0,c);z==="-"?(g++,x.push(`${Wi}${jo(P++)} - ${ee}${ur}`)):z==="+"?(h++,x.push(`${Fi}${jo(M++)} + ${ee}${ur}`)):(x.push(ar.gray(jo(M))+" "+ee),P++,M++)}});let A=x.slice(0,cr);return{added:h,removed:g,lines:A,hidden:Math.max(0,x.length-cr)}}async function fr(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await zi(t.cwd),{kind:"continue"};if(o==="pull")return await Gi(t,e.slice(1)),{kind:"continue"};console.log(w(`Unknown subcommand: /sync ${o}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof qi?console.log(w(n.message)):console.log(w(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function zi(e){console.log(f("Checking Document360 for drift\u2026"));let t=await pr({cwd:e});for(let o of Xt(t))console.log(o);console.log("")}async function Gi(e,t){let o=t[0];if(!o){console.log(w("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),n=(await pr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(D("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${xe(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await Hi({cwd:e.cwd,relPath:r});console.log(""),console.log(`${S("\u25CF")} ${xe(s.title)} ${V(`(${s.path})`)}`);for(let g of s.notes)console.log(N(` \u26A0 ${g}`));s.overwritesLocalChanges&&console.log(N(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=Le(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(f(" Local file already matches the remote content \u2014 advancing the sync base only.")),dr({cwd:e.cwd},s);continue}let d=g=>g===1?"":"s";console.log(V(` \u23BF Added ${c.added} line${d(c.added)}, removed ${c.removed} line${d(c.removed)}`));for(let g of c.lines)console.log(g);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>Bi({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}dr({cwd:e.cwd},s),console.log(D(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}B();import{statSync as nl}from"node:fs";import{resolve as rl}from"node:path";import{estimateBulkCost as sl,planPartitions as il,readProjectConfig as ll,resolveModelForOperation as al,runPartitioned as cl,trackedArticlePaths as ul}from"document360-engine";import F from"picocolors";import xr from"wrap-ansi";import No from"string-width";B();import Te from"picocolors";import Eo from"wrap-ansi";import Do from"string-width";var mr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),gr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),hr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Xi(e){let t=e.replace(/\r/g,"").split(`
17
+ `),o=[],n=0;for(;n<t.length;){let r=t[n];if(/^\s*```/.test(r)){let d=[];for(n++;n<t.length&&!/^\s*```/.test(t[n]);)d.push(t[n++]);n++,o.push({kind:"code",lines:d});continue}if(mr(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&gr(t[n+1])){let d=hr(r);n+=2;let h=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)h.push(hr(t[n++]));o.push({kind:"table",header:d,rows:h});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let d=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)d.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:d});continue}if(r.trim()===""){n++;continue}let c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!mr(t[n])&&!(t[n].includes("|")&&n+1<t.length&&gr(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Io(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Te.bold(t.slice(2,-2)):t.startsWith("`")?S(t.slice(1,-1)):Te.italic(t.slice(1,-1)))}var Yi=(e,t)=>e+" ".repeat(Math.max(0,t-Do(e)));function Vi(e,t,o){let n=e.length,r=e.map((P,M)=>Math.max(Do(P),...t.map(q=>Do(q[M]??"")))),s=Math.max(24,o),c=3*n+1,d=[...r],h=()=>d.reduce((P,M)=>P+M,0)+c,g=0;for(;h()>s&&g++<1e4;){let P=-1,M=6;for(let q=0;q<n;q++)d[q]>M&&(M=d[q],P=q);if(P===-1)break;d[P]-=1}let x=(P,M,q)=>Te.gray(P+d.map(z=>"\u2500".repeat(z+2)).join(M)+q),A=Te.gray("\u2502"),$=(P,M)=>{let q=d.map((ie,$e)=>{let le=P[$e]??"",ae=M?Te.bold(le):Io(le);return Eo(ae,ie,{hard:!0}).split(`
18
+ `)}),z=Math.max(...q.map(ie=>ie.length)),ee=[];for(let ie=0;ie<z;ie++)ee.push(d.map(($e,le)=>`${A} ${Yi(q[le][ie]??"",$e)} `).join("")+A);return ee.join(`
19
+ `)},j=[x("\u250C","\u252C","\u2510"),$(e,!0)];return j.push(t.length===0?x("\u2514","\u2534","\u2518"):x("\u251C","\u253C","\u2524")),t.forEach((P,M)=>{j.push($(P,!1)),j.push(M===t.length-1?x("\u2514","\u2534","\u2518"):x("\u251C","\u253C","\u2524"))}),j.join(`
20
+ `)}function Ji(e,t){switch(e.kind){case"heading":return Te.bold(e.text);case"hr":return Te.gray("\u2500".repeat(t));case"para":return Eo(Io(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=Eo(Io(o),Math.max(10,t-4)).split(`
21
21
  `);return" \u2022 "+n+r.map(s=>`
22
22
  `+s).join("")}).join(`
23
- `);case"code":return e.lines.map(o=>Pe.gray(" "+o)).join(`
24
- `);case"table":return Xi(e.header,e.rows,t)}}function _o(e,t){let o=Math.max(20,t);return Gi(e).map(n=>Yi(n,o)).join(`
23
+ `);case"code":return e.lines.map(o=>Te.gray(" "+o)).join(`
24
+ `);case"table":return Vi(e.header,e.rows,t)}}function _o(e,t){let o=Math.max(20,t);return Xi(e).map(n=>Ji(n,o)).join(`
25
25
 
26
- `)}B();import Vi from"picocolors";var Ji=[127,86,217],Ki=[22,38,43],hr={T:Ji,E:Ki},ut=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Mo=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,Qi=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function kr(){if(!Vi.isColorSupported)return[];let e=ut[0].length,t=[];for(let o=0;o<ut.length;o+=2){let n="";for(let r=0;r<e;r++){let s=hr[ut[o][r]],c=o+1<ut.length?hr[ut[o+1][r]]:void 0;s&&c?n+=`${Mo(s)}${Qi(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${Mo(s)}\u2580\x1B[39m`:c?n+=`${Mo(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function dt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Oe(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Yt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var Qe=(e,t)=>` ${e.padEnd(13)}${t}`;function Zi(e,t){let o=[st("\u270E document360-writer")+F.gray(` v${e.version}`),F.gray(" Reads your code, writes your docs."),"",Qe("Claude:",`${e.claude}${F.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Qe("Document360:",e.configured?e.loggedOut?F.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?F.gray(` (${e.sessionHint})`):""}`:F.yellow("not set up \u2014 run /init")),Qe("Profile:",e.configured?`${e.profile}${F.gray(` (${e.apiUrl})`)}${e.prod?F.bold(F.yellow(" \u26A0 PRODUCTION")):""}`:F.gray("\u2014 (run /init)")),Qe("Project:",e.project),Qe("Mode:",F.gray(e.mode)),Qe("cwd:",F.gray(e.cwd))],n=kr();if(n.length===0)return o.join(`
27
- `);let r=2,s=3,c=No(n[0]);if(!(t>=r+c+s+Math.max(...o.map(w=>No(w)))))return[...n.map(w=>" "+w),...o].join(`
28
- `);let h=Math.max(0,Math.floor((o.length-n.length)/2)),g=[];for(let w=0;w<Math.max(n.length+h,o.length);w++){let R=n[w-h]??" ".repeat(c);g.push((" ".repeat(r)+R+" ".repeat(s)+(o[w]??"")).trimEnd())}return g.join(`
29
- `)}var el={error:F.red,warn:F.yellow,ok:F.green,info:F.gray};function Lo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Zi(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,d=e.text.split(`
30
- `).flatMap(g=>yr(g,s,{hard:!0}).split(`
26
+ `)}B();import Ki from"picocolors";var Qi=[127,86,217],Zi=[22,38,43],kr={T:Qi,E:Zi},ut=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Mo=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,el=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function yr(){if(!Ki.isColorSupported)return[];let e=ut[0].length,t=[];for(let o=0;o<ut.length;o+=2){let n="";for(let r=0;r<e;r++){let s=kr[ut[o][r]],c=o+1<ut.length?kr[ut[o+1][r]]:void 0;s&&c?n+=`${Mo(s)}${el(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${Mo(s)}\u2580\x1B[39m`:c?n+=`${Mo(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function dt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Oe(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Yt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var Qe=(e,t)=>` ${e.padEnd(13)}${t}`;function tl(e,t){let o=[st("\u270E document360-writer")+F.gray(` v${e.version}`),F.gray(" Reads your code, writes your docs."),"",Qe("Claude:",`${e.claude}${F.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Qe("Document360:",e.configured?e.loggedOut?F.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?F.gray(` (${e.sessionHint})`):""}`:F.yellow("not set up \u2014 run /init")),Qe("Profile:",e.configured?`${e.profile}${F.gray(` (${e.apiUrl})`)}${e.prod?F.bold(F.yellow(" \u26A0 PRODUCTION")):""}`:F.gray("\u2014 (run /init)")),Qe("Project:",e.project),Qe("Mode:",F.gray(e.mode)),Qe("cwd:",F.gray(e.cwd))],n=yr();if(n.length===0)return o.join(`
27
+ `);let r=2,s=3,c=No(n[0]);if(!(t>=r+c+s+Math.max(...o.map(x=>No(x)))))return[...n.map(x=>" "+x),...o].join(`
28
+ `);let h=Math.max(0,Math.floor((o.length-n.length)/2)),g=[];for(let x=0;x<Math.max(n.length+h,o.length);x++){let A=n[x-h]??" ".repeat(c);g.push((" ".repeat(r)+A+" ".repeat(s)+(o[x]??"")).trimEnd())}return g.join(`
29
+ `)}var ol={error:F.red,warn:F.yellow,ok:F.green,info:F.gray};function Lo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return tl(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,d=e.text.split(`
30
+ `).flatMap(g=>xr(g,s,{hard:!0}).split(`
31
31
  `)),h=Math.max(0,d.length-c);return h>0&&(d=[...d.slice(0,c),F.dim(`\u2026 +${h} more lines`)]),`
32
- `+d.map((g,w)=>n+(w===0?S(" \u276F "):" ")+g+" ".repeat(Math.max(0,s-No(g))+1)+r).join(`
32
+ `+d.map((g,x)=>n+(x===0?S(" \u276F "):" ")+g+" ".repeat(Math.max(0,s-No(g))+1)+r).join(`
33
33
  `)}case"assistant":return`
34
34
  `+_o(e.text,o);case"tool":{let n=e.arg!==null?F.gray(`${e.sep}(${e.arg})`):"";return`
35
- `+yr(F.green("\u25CF ")+F.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?F.red:F.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(F.dim(` \u2026 +${e.hidden} lines`)),r.join(`
35
+ `+xr(F.green("\u25CF ")+F.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?F.red:F.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(F.dim(` \u2026 +${e.hidden} lines`)),r.join(`
36
36
  `)}case"diff":{let n=c=>c===1?"":"s",s=[F.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&s.push(F.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
37
37
  `)}case"link":return e.lines.map(n=>S(it(` \u2B95 ${n}`))).join(`
38
38
  `);case"preview":return`
39
39
  `+st(`\u25A3 Preview \u2014 ${e.name}`)+`
40
40
 
41
41
  `+_o(e.text,o);case"note":return`
42
- `+el[e.tone](it(e.text));case"done":return`
42
+ `+ol[e.tone](it(e.text));case"done":return`
43
43
  `+(e.ok?F.magenta("\u2736 "):F.red("\u2736 "))+F.gray(`Cooked for ${dt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Oe(e.costUsd)}`:""))}}function wr(e,t){return e.map(o=>Lo(o,t)).join(`
44
- `)}var Oo=3;function Uo(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function Wo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Fo(e,t){return t.map(o=>{let n=0;try{n=tl(ol(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Bo(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
44
+ `)}var Oo=3;function Uo(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function Wo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Fo(e,t){return t.map(o=>{let n=0;try{n=nl(rl(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function Bo(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 Ho(e,t,o){let n=e.reduce((d,h)=>d+h.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${Oe(r)}\u2013${Oe(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function qo(e,t,o){let n=T=>t[T.index]?.paths.length??0,r=e.filter(T=>T.ok),s=e.filter(T=>!T.ok),c=e.reduce((T,M)=>T+n(M),0),d=r.reduce((T,M)=>T+n(M),0),h=e.reduce((T,M)=>T+M.costUsd,0),g=e.reduce((T,M)=>T+M.outputTokens,0),w=o==="api"?`${Oe(h)} total`:Yt(g),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,$=T=>`${T} article${T===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${$(c)} (${R}) successfully \xB7 ${w}.`);else{j.push(`Converted ${d}/${$(c)} (${R}) \xB7 ${w}.`),j.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let T of s)j.push(` \u2717 ${T.label}${T.error?` \u2014 ${T.error}`:""}`)}return j}async function xr(e,t){if(!sl(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Uo(e),r=al(t.cwd,t.profileName);if(r.length===0)return console.log(x("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Wo(r,o);if(s.length===0)return console.log(x(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=rl(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:g}=il(t.cwd,"light");if(!n){let w=nl({files:Fo(t.cwd,s),op:"convert",model:h});o&&console.log(f(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let R of Ho(c,w,Oo))console.log(R);return console.log(f(`Model: ${h}${g?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Oo} agents at once) on ${h}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let w of ll({cwd:t.cwd,partitions:c,promptFor:Bo,concurrency:Oo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:h}))if(w.type==="partition_status")w.status==="running"?console.log(f(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(E(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of qo(w.results,c,R))console.log(w.ok?E($):N($))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}B();import{search as cl}from"@inquirer/prompts";import{findByName as ul,getSession as dl,listSessions as pl,relativeTime as br}from"document360-engine";async function vr(e,t){let o=pl(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=ul(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${n}".`)),$r(o),{kind:"continue"})}if(!process.stdin.isTTY)return $r(o),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>cl({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} ${br(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=dl(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function $r(e){console.log("");for(let t of e.slice(0,15))console.log(` ${S(t.name)} ${f(br(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}B();import{renameSession as fl}from"document360-engine";function Go(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 Cr(e,t){let o=Go(e.join(" "));if(!o)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(fl(n,o)?console.log(E(`\u2713 Session renamed to "${o}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Sr,readProjectConfig as ml,writeProjectConfig as gl}from"document360-engine";B();function zo(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=ml(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 Sr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},gl(n,e),null):`Unknown environment "${r}". Known: ${Sr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Pr(e,t){let o=e[0];if(!o)return Lt(t.cwd),{kind:"continue"};if(o==="add"){let n=zo(t.cwd,e[1],e[2]);return n?(console.log(x(n)),{kind:"continue"}):(console.log(E(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${S(`/profile ${e[1]}`)} then ${S("/login")}`),{kind:"continue"})}return Ot(t.cwd,o),Ut(t.cwd,o),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}B();import{select as hl}from"@inquirer/prompts";import{readProjectConfig as kl,readUserConfig as Tr,resolveModelSetting as Xo,writeUserConfig as Rr}from"document360-engine";var de=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function Vt(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=de.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function yl(e){switch(e.source){case"project":return".d360-writer.json defaultModel (team setting)";case"user":return"~/.document360-writer/config.json (your /model setting)";case"env":return"ANTHROPIC_MODEL environment variable";case"claude-settings":return"Claude Code's own settings (~/.claude/settings.json)";case"claude-default":return"Claude Code default (no override configured)"}}function pt(e,t){let o=()=>{let s=Xo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Tr();return s.defaultModel?(delete s.defaultModel,Rr(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Xo(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}}Rr({...Tr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=kl(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function jr(e,t){let o=e[0]?.trim();if(!o){let c=Xo(t.cwd);if(!process.stdin.isTTY)return console.log(`${we("Model:")} ${S(c.model??"Claude Code default")}`),console.log(f(` source: ${yl(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=Vt(c),h;try{h=await t.withPausedInput(()=>hl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:de[d].value,choices:de.map(($,j)=>({name:`${$.label}${j===d?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:g,changed:w,effective:R}=pt(t.cwd,h??"default");for(let $ of g)console.log($.startsWith("\u26A0")?N($):$.startsWith("\u2713")?E($):f($));return w&&await t.setModel(R),{kind:"continue"}}let{lines:n,changed:r,effective:s}=pt(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?N(c):c.startsWith("\u2713")?E(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Jt();async function _r(e,t){return await t.withPausedInput(()=>Je(t.cwd)),{kind:"clear"}}B();import{resolveActiveProfile as jl}from"document360-engine";async function Mr(e,t){let o=!1;try{o=jl(t.cwd).production}catch{}return o?(console.log(N("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}B();var Nr=async(e,t)=>{try{await t.withPausedInput(()=>Nt({}))}catch(o){console.log(x(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as Lr}from"node:fs";import{isAbsolute as Al,join as El,resolve as Dl}from"node:path";import{readProjectConfig as Il,screenshotPlaceholderIds as _l}from"document360-engine";var Or=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Vo(e){let t=!e.includes("--no-setup"),o=e.filter(r=>r!=="--no-setup");if(o[0]==="--list")return{mode:"list",scope:o[1]?Or(o[1]):void 0};let n=o[0];return!n||n==="--all"?{mode:"all",setup:t}:/[\\/]/.test(n)||n.endsWith(".md")?{mode:"scope",scope:Or(n),setup:t}:{mode:"single",id:n}}function Jo(e,t){let o=Il(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>Al(c)?El(c,d):Dl(e,c,d);return _l(e,{scope:t}).map(({id:c,file:d})=>{let h=Lr(s(r,`${c}.png`))?"captured":Lr(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var Ml={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Nl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Ko(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(d=>d.file===s))r.push(` ${Ml[c.state]} ${c.id.padEnd(34)} ${Nl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Ur(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
50
+ `)}function Ho(e,t,o){let n=e.reduce((d,h)=>d+h.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${Oe(r)}\u2013${Oe(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function qo(e,t,o){let n=P=>t[P.index]?.paths.length??0,r=e.filter(P=>P.ok),s=e.filter(P=>!P.ok),c=e.reduce((P,M)=>P+n(M),0),d=r.reduce((P,M)=>P+n(M),0),h=e.reduce((P,M)=>P+M.costUsd,0),g=e.reduce((P,M)=>P+M.outputTokens,0),x=o==="api"?`${Oe(h)} total`:Yt(g),A=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,$=P=>`${P} article${P===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${$(c)} (${A}) successfully \xB7 ${x}.`);else{j.push(`Converted ${d}/${$(c)} (${A}) \xB7 ${x}.`),j.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let P of s)j.push(` \u2717 ${P.label}${P.error?` \u2014 ${P.error}`:""}`)}return j}async function $r(e,t){if(!ll(t.cwd))return console.log(w("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Uo(e),r=ul(t.cwd,t.profileName);if(r.length===0)return console.log(w("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Wo(r,o);if(s.length===0)return console.log(w(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=il(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:g}=al(t.cwd,"light");if(!n){let x=sl({files:Fo(t.cwd,s),op:"convert",model:h});o&&console.log(f(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let A of Ho(c,x,Oo))console.log(A);return console.log(f(`Model: ${h}${g?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Oo} agents at once) on ${h}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let x of cl({cwd:t.cwd,partitions:c,promptFor:Bo,concurrency:Oo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:h}))if(x.type==="partition_status")x.status==="running"?console.log(f(` \u25B8 ${x.label} \u2014 converting\u2026`)):x.status==="done"?console.log(D(` \u2713 ${x.label}`)):console.log(w(` \u2717 ${x.label}`));else if(x.type==="run_done"){console.log("");let A=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of qo(x.results,c,A))console.log(x.ok?D($):N($))}}catch(x){console.log(w(`Convert run failed: ${x.message}`))}return console.log(""),{kind:"continue"}}B();import{search as dl}from"@inquirer/prompts";import{findByName as pl,getSession as fl,listSessions as ml,relativeTime as vr}from"document360-engine";async function Cr(e,t){let o=ml(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(f("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=pl(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(w(`No session matches "${n}".`)),br(o),{kind:"continue"})}if(!process.stdin.isTTY)return br(o),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>dl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(d=>!c||d.name.toLowerCase().includes(c)||d.firstPrompt.toLowerCase().includes(c)).map(d=>({name:`${d.name} ${vr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=fl(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function br(e){console.log("");for(let t of e.slice(0,15))console.log(` ${S(t.name)} ${f(vr(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}B();import{renameSession as gl}from"document360-engine";function zo(e){let t=e.trim();return t.length>=2&&(t[0]==='"'&&t.endsWith('"')||t[0]==="'"&&t.endsWith("'"))?t.slice(1,-1).trim():t}async function Sr(e,t){let o=zo(e.join(" "));if(!o)return console.log(w("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(gl(n,o)?console.log(D(`\u2713 Session renamed to "${o}"`)):console.log(w("Could not find the current session record.")),{kind:"continue"}):(console.log(w("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Tr,readProjectConfig as hl,writeProjectConfig as kl}from"document360-engine";B();function Go(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=hl(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let r=o??t;return Tr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},kl(n,e),null):`Unknown environment "${r}". Known: ${Tr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Pr(e,t){let o=e[0];if(!o)return Lt(t.cwd),{kind:"continue"};if(o==="add"){let n=Go(t.cwd,e[1],e[2]);return n?(console.log(w(n)),{kind:"continue"}):(console.log(D(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${S(`/profile ${e[1]}`)} then ${S("/login")}`),{kind:"continue"})}return Ot(t.cwd,o),Ut(t.cwd,o),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}B();import{select as yl}from"@inquirer/prompts";import{readProjectConfig as xl,readUserConfig as Rr,resolveModelSetting as Xo,writeUserConfig as Ar}from"document360-engine";var de=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function Vt(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=de.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function wl(e){switch(e.source){case"project":return".d360-writer.json defaultModel (team setting)";case"user":return"~/.document360-writer/config.json (your /model setting)";case"env":return"ANTHROPIC_MODEL environment variable";case"claude-settings":return"Claude Code's own settings (~/.claude/settings.json)";case"claude-default":return"Claude Code default (no override configured)"}}function pt(e,t){let o=()=>{let s=Xo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Rr();return s.defaultModel?(delete s.defaultModel,Ar(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Xo(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:o()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Ar({...Rr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=xl(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function jr(e,t){let o=e[0]?.trim();if(!o){let c=Xo(t.cwd);if(!process.stdin.isTTY)return console.log(`${xe("Model:")} ${S(c.model??"Claude Code default")}`),console.log(f(` source: ${wl(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let d=Vt(c),h;try{h=await t.withPausedInput(()=>yl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:de[d].value,choices:de.map(($,j)=>({name:`${$.label}${j===d?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:g,changed:x,effective:A}=pt(t.cwd,h??"default");for(let $ of g)console.log($.startsWith("\u26A0")?N($):$.startsWith("\u2713")?D($):f($));return x&&await t.setModel(A),{kind:"continue"}}let{lines:n,changed:r,effective:s}=pt(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?N(c):c.startsWith("\u2713")?D(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Jt();async function Mr(e,t){return await t.withPausedInput(()=>Je(t.cwd)),{kind:"clear"}}B();import{resolveActiveProfile as El}from"document360-engine";async function Nr(e,t){let o=!1;try{o=El(t.cwd).production}catch{}return o?(console.log(N("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}B();var Lr=async(e,t)=>{try{await t.withPausedInput(()=>Nt({}))}catch(o){console.log(w(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as Or}from"node:fs";import{isAbsolute as Dl,join as Il,resolve as _l}from"node:path";import{readProjectConfig as Ml,screenshotPlaceholderIds as Nl}from"document360-engine";var Ur=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Vo(e){let t=!e.includes("--no-setup"),o=e.filter(r=>r!=="--no-setup");if(o[0]==="--list")return{mode:"list",scope:o[1]?Ur(o[1]):void 0};let n=o[0];return!n||n==="--all"?{mode:"all",setup:t}:/[\\/]/.test(n)||n.endsWith(".md")?{mode:"scope",scope:Ur(n),setup:t}:{mode:"single",id:n}}function Jo(e,t){let o=Ml(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>Dl(c)?Il(c,d):_l(e,c,d);return Nl(e,{scope:t}).map(({id:c,file:d})=>{let h=Or(s(r,`${c}.png`))?"captured":Or(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var Ll={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Ol={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Ko(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(d=>d.file===s))r.push(` ${Ll[c.state]} ${c.id.padEnd(34)} ${Ol[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Wr(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
51
51
  `),"","For each: locate its <!-- SCREENSHOT --> block in user-docs/**/*.md; read the product source for","EXACT routes + stable data-testid selectors (never guess); enter the prepared context via","captureScope(); guard data prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts","(skip-with-TODO if a stable selector is missing). Do NOT run other skills. Report the specs you","wrote and any TODO data-testids."].join(`
52
52
  `)}async function Kt(e,t){let o=Vo(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let d of Ko(Jo(c,o.scope),o.scope))console.log(d);return{kind:"continue"}}if(o.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${o.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${o.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
53
53
  `),display:`/screenshot ${o.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${o.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${o.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${o.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
54
- `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}async function mt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
55
- `),display:"/capture-setup"}}B();import{existsSync as Ll,rmSync as Ol}from"node:fs";import{basename as Wr,resolve as Fr}from"node:path";import{input as Ul}from"@inquirer/prompts";import{readProjectConfig as Wl}from"document360-engine";var Fl=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function Qo(e){let t=null;try{t=Wl(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,...Fl].filter(s=>Ll(Fr(e,s)))}function Zo(e,t){let o=[],n=[];for(let r of t)try{Ol(Fr(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function en(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: ${Wr(e)}`]}async function Br(e,t){let o=Qo(t.cwd);for(let d of en(t.cwd,o))console.log(o.length===0?f(d):d);if(o.length===0)return{kind:"continue"};let n=Wr(t.cwd);if((await t.withPausedInput(()=>Ul({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(N("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=Zo(t.cwd,o);console.log(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{kind:"continue"}}var Hr={help:Po,"?":Po,clear:Gn,exit:To,quit:To,init:Kn,mcp:Bt,publish:nr,audit:Gt,scope:sr,sync:pr,convert:xr,resume:vr,rename:Cr,profile:Pr,model:jr,doctor:ft,workspace:_r,"allow-prod":Mr,login:Nr,screenshot:Kt,"capture-setup":mt,reset:Br};function qr(e){let t=e.trim();if(!t.startsWith("/"))return null;let o=t.slice(1).split(/\s+/),n=(o[0]??"").toLowerCase();return n?{name:n,args:o.slice(1)}:null}B();var Bl={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"},zr=160,on=200,Xr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Gr(e){let t=Object.entries(e).filter(([,n])=>n!=null&&n!=="");if(t.length===0)return null;let o=t.slice(0,4).map(([n,r])=>`${n}: ${Z(typeof r=="string"?r:JSON.stringify(r),Xr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),zr)}var Te=e=>typeof e=="string"&&e?e:null,tn=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Hl(e){let t=Te(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function ql(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=Te(t.title);if(!n)return null;let r=Hl(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Te(t.title),r=tn(t.article_id);return o("Update article",n?`"${Z(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",tn(t.article_id)?`id ${tn(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",Te(t.name)?`"${Z(Te(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=Te(t.file_path);return o("Upload image",n?Z(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function Qt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let g=ql(c,t);if(g)return g}let d=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${d}`,sep:" ",arg:Gr(t)}}let o=Bl[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,zr)}:{title:e,sep:"",arg:Gr(t)}}function Gl(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 zl(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(Gl).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>Z(r,on))]}if(t!==null&&typeof t=="object"){let o=Object.entries(t).filter(([,n])=>n!==null&&(typeof n=="string"||typeof n=="number"||typeof n=="boolean")).slice(0,6).map(([n,r])=>`${n}: ${Z(String(r),Xr)}`);return o.length>0?[Z(o.join(" \xB7 "),on)]:null}return null}function Xl(e){let t=Te(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function Yl(e,t,o){switch(e){case"d360_create_article":{let n=Xl(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=Te(t?.name);return[`\u2713 category created${n?` \xB7 "${n}"`:""}`]}case"d360_upload_drive_file":{let n=o.match(/https?:\/\/\S+/);return[`\u2713 uploaded${n?` \xB7 ${Z(n[0],120)}`:""}`]}default:return null}}function Zt(e,t=4,o,n){let r=e.replace(/\r\n/g,`
56
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=Yl(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=zl(r)??r.split(`
57
- `);return{lines:s.slice(0,t).map(c=>Z(c,on)),hidden:Math.max(0,s.length-t)}}function eo(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function to(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),r=(Array.isArray(o)?o[0]:o)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function oo(e){try{let t=JSON.parse(e),n=(Array.isArray(t)?t[0]:t)?.url;return typeof n=="string"&&/^https?:\/\//.test(n)?n:null}catch{return null}}var ia=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function la(e,t,o,n){if(ia.test(e))try{let r=Kr(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=to(t,o),d=oo(o);e.endsWith("publish_article")&&d&&console.log(S(` \u2B95 Live: ${d}`)),c&&s&&console.log(S(` \u2B95 Preview: ${eo(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Qr(e=process.cwd(),t="auto",o){let n=Jl(t);n.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${S("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${S('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),aa(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=nn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=Vl({input:process.stdin,output:process.stdout}),h=[],g=null,w=!1;d.on("line",j=>{if(g){let T=g;g=null,T(j)}else h.push(j)}),d.on("close",()=>{if(w=!0,g){let j=g;g=null,j(null)}});function R(){return h.length>0?Promise.resolve(h.shift()):w?Promise.resolve(null):(process.stdout.write(S("> ")),new Promise(j=>{g=j}))}let $={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=nn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async j=>s.setModel(j),withPausedInput:async j=>{d.pause();try{return await j()}finally{d.resume()}}};try{for(;;){let j=await R();if(j===null)break;let T=j.trim();if(T){if(T.startsWith("/")){let M=qr(T);if(!M)continue;let q=Hr[M.name];if(!q){console.log(x(`Unknown command: /${M.name}`)),console.log(f("Type /help for the list."));continue}let G=await q(M.args,$);if(G.kind==="exit")break;if(G.kind==="clear"){$.restartAgent();continue}if(G.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(E("\u2713 Production writes authorized for this session.")),console.log("");continue}if(G.kind==="resume"){s.close(),s=nn({cwd:e,resume:G.uuid,profileName:o,allowProdWrites:r});let ee=Kl(G.uuid);c={uuid:G.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Jr(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 Vr(s,G.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=T),await Vr(s,T,n,c,e)}}}finally{s.close(),d.close()}}function aa(e,t){console.log(""),console.log(st("document360-writer")),console.log(f(` cwd: ${e}`));let o=oa(e);console.log(f(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(ca(e,t)),na(e)||console.log(N(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function ca(e,t){try{let o=Kr(e,t),n=o.production?N(" \u26A0 PRODUCTION"):"",r=sa(o.name);if(!r)return f(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...Yr(r.idToken)??{},...Yr(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return ra(r)&&!r.refreshToken?N(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return f(` Document360: ${o.message.split(".")[0]}`)}}function ua(){console.error(""),console.error(`Sign in with your Claude subscription: run ${S("claude")} once, then retry.`),console.error(` (No Claude Code? ${S("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${S("https://console.anthropic.com/settings/keys")}`)}function da(e,t,o){e.uuid=t;let n=new Date().toISOString();ea({uuid:t,name:Zl(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function pa(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||ta(n,t).then(r=>{r&&Ql(o,r)}).catch(()=>{})}async function Vr(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))fa(c,n,r,o,s)}function fa(e,t,o,n,r){switch(e.type){case"session":t.uuid||da(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Qt(e.name,e.input);s&&(process.stdout.write(`
54
+ `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as Ul,readFileSync as Wl}from"node:fs";import{isAbsolute as Fl,join as Bl,resolve as Hl}from"node:path";import{readProjectConfig as ql}from"document360-engine";async function mt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
55
+ `),display:"/capture-setup"}}var Fr="-".repeat(64);function Qo(e){let t="user-docs/_capture";try{let x=ql(e);x?.captureDir&&(t=x.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let o=Fl(t)?Bl(t,"CAPTURE-TESTID-REQUESTS.json"):Hl(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!Ul(o))return null;let n;try{let x=JSON.parse(Wl(o,"utf8").replace(/^/,""));n=Array.isArray(x?.requests)?x.requests:[]}catch{return null}if(n.length===0)return null;let r=new Set(n.map(x=>x.file).filter(Boolean)).size,s=n.length,c=`${s} data-testid attribute${s===1?"":"s"}`,d=`${r} source file${r===1?"":"s"}`,h=`${t}/CAPTURE-TESTID-REQUESTS.md`,g=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} needed 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:","",Fr,`Read ${h} and ${g}. They list ${c} that need to be added to source files in this repo for screenshot automation.`,"","For each request in the JSON:",'- Open the file named in "file".','- Find the element described by "element" and "anchor" (use the anchor if the line number has drifted).','- Add data-testid="<testid>" to that element and no other element.',"- Do not rename, restructure, reformat, or change behaviour. The only change per file is adding the attribute.","","After making all changes, list each file you edited and confirm the attribute you added. If you cannot find an element by its anchor, say so for that item and skip it \u2014 do not guess.",Fr]}B();import{existsSync as zl,rmSync as Gl}from"node:fs";import{basename as Br,resolve as Hr}from"node:path";import{input as Xl}from"@inquirer/prompts";import{readProjectConfig as Yl}from"document360-engine";var Vl=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function Zo(e){let t=null;try{t=Yl(e)}catch{t=null}let o=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),n=o(t?.docsDir)??"user-docs",r=new Set([n]);for(let s of[o(t?.captureDir),o(t?.outputDir)])s&&s!==n&&!s.startsWith(`${n}/`)&&r.add(s);return[...r,...Vl].filter(s=>zl(Hr(e,s)))}function en(e,t){let o=[],n=[];for(let r of t)try{Gl(Hr(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function tn(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Br(e)}`]}async function qr(e,t){let o=Zo(t.cwd);for(let d of tn(t.cwd,o))console.log(o.length===0?f(d):d);if(o.length===0)return{kind:"continue"};let n=Br(t.cwd);if((await t.withPausedInput(()=>Xl({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(N("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=en(t.cwd,o);console.log(D(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let d of c)console.log(w(` \u2717 ${d.path}: ${d.error}`));return{kind:"continue"}}var zr={help:To,"?":To,clear:Gn,exit:Po,quit:Po,init:Qn,mcp:Bt,publish:rr,audit:zt,scope:ir,sync:fr,convert:$r,resume:Cr,rename:Sr,profile:Pr,model:jr,doctor:ft,workspace:Mr,"allow-prod":Nr,login:Lr,screenshot:Kt,"capture-setup":mt,reset:qr};function Gr(e){let t=e.trim();if(!t.startsWith("/"))return null;let o=t.slice(1).split(/\s+/),n=(o[0]??"").toLowerCase();return n?{name:n,args:o.slice(1)}:null}B();var Jl={Bash:"command",PowerShell:"command",Read:"file_path",Write:"file_path",Edit:"file_path",NotebookEdit:"notebook_path",Glob:"pattern",Grep:"pattern",WebFetch:"url",WebSearch:"query",Agent:"description",Task:"description",Skill:"skill"},Yr=160,nn=200,Vr=40;function Z(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Xr(e){let t=Object.entries(e).filter(([,n])=>n!=null&&n!=="");if(t.length===0)return null;let o=t.slice(0,4).map(([n,r])=>`${n}: ${Z(typeof r=="string"?r:JSON.stringify(r),Vr)}`);return t.length>4&&o.push("\u2026"),Z(o.join(", "),Yr)}var Pe=e=>typeof e=="string"&&e?e:null,on=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Kl(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function Ql(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=Pe(t.title);if(!n)return null;let r=Kl(t.local_path);return o("Create article",`"${Z(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=Pe(t.title),r=on(t.article_id);return o("Update article",n?`"${Z(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",on(t.article_id)?`id ${on(t.article_id)}\u2026`:null);case"d360_publish_article":{let n=t.version_number;return o("Publish article LIVE",typeof n=="number"?`v${n}`:null)}case"d360_unpublish_article":return o("Unpublish article",null);case"d360_create_category":return o("Create category",Pe(t.name)?`"${Z(Pe(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=Pe(t.file_path);return o("Upload image",n?Z(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function Qt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let g=Ql(c,t);if(g)return g}let d=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${d}`,sep:" ",arg:Xr(t)}}let o=Jl[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:Z(n,Yr)}:{title:e,sep:"",arg:Xr(t)}}function Zl(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let o of["name","title","slug","id"])if(typeof t[o]=="string"&&t[o])return t[o];return null}function ea(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(Zl).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>Z(r,nn))]}if(t!==null&&typeof t=="object"){let o=Object.entries(t).filter(([,n])=>n!==null&&(typeof n=="string"||typeof n=="number"||typeof n=="boolean")).slice(0,6).map(([n,r])=>`${n}: ${Z(String(r),Vr)}`);return o.length>0?[Z(o.join(" \xB7 "),nn)]:null}return null}function ta(e){let t=Pe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function oa(e,t,o){switch(e){case"d360_create_article":{let n=ta(t?.local_path);return[`\u2713 draft created${n?` \xB7 ${n}`:""}`]}case"d360_update_article":return["\u2713 draft updated"];case"d360_fork_article":return["\u2713 forked to a new draft"];case"d360_publish_article":return["\u2713 PUBLISHED LIVE \u2014 visible to readers"];case"d360_unpublish_article":return["\u2713 reverted to draft (removed from readers)"];case"d360_create_category":{let n=Pe(t?.name);return[`\u2713 category created${n?` \xB7 "${n}"`:""}`]}case"d360_upload_drive_file":{let n=o.match(/https?:\/\/\S+/);return[`\u2713 uploaded${n?` \xB7 ${Z(n[0],120)}`:""}`]}default:return null}}function Zt(e,t=4,o,n){let r=e.replace(/\r\n/g,`
56
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=oa(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=ea(r)??r.split(`
57
+ `);return{lines:s.slice(0,t).map(c=>Z(c,nn)),hidden:Math.max(0,s.length-t)}}function eo(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function to(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),r=(Array.isArray(o)?o[0]:o)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function oo(e){try{let t=JSON.parse(e),n=(Array.isArray(t)?t[0]:t)?.url;return typeof n=="string"&&/^https?:\/\//.test(n)?n:null}catch{return null}}var ma=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ga(e,t,o,n){if(ma.test(e))try{let r=Zr(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=to(t,o),d=oo(o);e.endsWith("publish_article")&&d&&console.log(S(` \u2B95 Live: ${d}`)),c&&s&&console.log(S(` \u2B95 Preview: ${eo(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function es(e=process.cwd(),t="auto",o){let n=ra(t);n.kind==="none"&&(console.error(""),console.error(w("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${S("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${S('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),ha(e,o),n.kind==="subscription"&&(n.stored?console.log(f(" Using your Claude subscription (no API key set).")):console.log(f(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=rn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},d=na({input:process.stdin,output:process.stdout}),h=[],g=null,x=!1;d.on("line",j=>{if(g){let P=g;g=null,P(j)}else h.push(j)}),d.on("close",()=>{if(x=!0,g){let j=g;g=null,j(null)}});function A(){return h.length>0?Promise.resolve(h.shift()):x?Promise.resolve(null):(process.stdout.write(S("> ")),new Promise(j=>{g=j}))}let $={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=rn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async j=>s.setModel(j),withPausedInput:async j=>{d.pause();try{return await j()}finally{d.resume()}}};try{for(;;){let j=await A();if(j===null)break;let P=j.trim();if(P){if(P.startsWith("/")){let M=Gr(P);if(!M)continue;let q=zr[M.name];if(!q){console.log(w(`Unknown command: /${M.name}`)),console.log(f("Type /help for the list."));continue}let z=await q(M.args,$);if(z.kind==="exit")break;if(z.kind==="clear"){$.restartAgent();continue}if(z.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(D("\u2713 Production writes authorized for this session.")),console.log("");continue}if(z.kind==="resume"){s.close(),s=rn({cwd:e,resume:z.uuid,profileName:o,allowProdWrites:r});let ee=sa(z.uuid);c={uuid:z.uuid,firstPrompt:ee?.firstPrompt??null,titleFired:!0},Qr(z.uuid),console.log(D(`\u2713 Resumed "${z.name}"`)),console.log("");continue}z.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=z.display??z.prompt),await Kr(s,z.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=P),await Kr(s,P,n,c,e)}}}finally{s.close(),d.close()}}function ha(e,t){console.log(""),console.log(st("document360-writer")),console.log(f(` cwd: ${e}`));let o=ua(e);console.log(f(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(ka(e,t)),da(e)||console.log(N(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function ka(e,t){try{let o=Zr(e,t),n=o.production?N(" \u26A0 PRODUCTION"):"",r=fa(o.name);if(!r)return f(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...Jr(r.idToken)??{},...Jr(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return pa(r)&&!r.refreshToken?N(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return f(` Document360: ${o.message.split(".")[0]}`)}}function ya(){console.error(""),console.error(`Sign in with your Claude subscription: run ${S("claude")} once, then retry.`),console.error(` (No Claude Code? ${S("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${S("https://console.anthropic.com/settings/keys")}`)}function xa(e,t,o){e.uuid=t;let n=new Date().toISOString();aa({uuid:t,name:la(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function wa(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||ca(n,t).then(r=>{r&&ia(o,r)}).catch(()=>{})}async function Kr(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))$a(c,n,r,o,s)}function $a(e,t,o,n,r){switch(e.type){case"session":t.uuid||xa(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=Qt(e.name,e.input);s&&(process.stdout.write(`
58
58
 
59
- `),console.log(`${E("\u25CF")} ${we(s.title)}${s.arg!==null?V(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Le(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(V(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let d of s.lines)console.log(d);s.hidden>0&&console.log(f(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=Zt(e.output,4,e.isError?void 0:s.name,s.input),d=e.isError?x:V;c.lines.forEach((h,g)=>console.log(d((g===0?" \u23BF ":" ")+h))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||la(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
60
- `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Jr(t.uuid),t.titleFired||pa(t,o));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&ua();break}}import{render as lc}from"ink";import{resolveAuth as ac}from"document360-engine";import{useCallback as z,useEffect as je,useMemo as lo,useRef as X,useState as U}from"react";import{Box as Y,Text as v,useApp as va,useInput as Ca,useStdout as Sa}from"ink";import{existsSync as ms,readFileSync as gs,readdirSync as Cs}from"node:fs";import{basename as ao,isAbsolute as Pa,join as ht}from"node:path";import{createSession as hs,loginPkce as Ta,toStoredTokens as Ra,saveTokens as ja,getAccessToken as Aa,resolveActiveProfile as pe,resolveProjectId as Ea,getArticle as Da,decodeJwtClaims as Ze,isExpired as Re,loadTokens as Ue,setTitle as Ia,slugify as _a,touchSession as sn,upsertSession as Ma,generateTitle as ks,findByName as Na,listSessions as La,renameSession as Oa,suggestNextAction as Ua,readProjectConfig as se,writeProjectConfig as Wa,resolveModelSetting as cn,loadProfileMap as ys,applyPull as Fa,computeSyncStatus as ln,planPull as Ba,inventoryRepo as Ha,knownEnvironments as qa,resolveEnvironment as Ga,planPartitions as za,partitionEvenly as Xa,screenshotPlaceholderIds as Ya,trackedArticlePaths as Va,runPartitioned as ws,estimateBulkCost as Ja,resolveModelForOperation as xs}from"document360-engine";import{existsSync as ma,mkdirSync as ga,readFileSync as ha,writeFileSync as ka}from"node:fs";import{join as Zr}from"node:path";import{writerDir as ya}from"document360-engine";function es(e){return Zr(ya(e),".sessions")}function ts(e,t){return Zr(es(e),`${t}.json`)}function os(e,t,o){try{ga(es(e),{recursive:!0});let n=o.filter(r=>r.kind!=="banner");ka(ts(e,t),JSON.stringify({v:1,items:n}),"utf8")}catch{}}function rn(e,t){try{let o=ts(e,t);if(!ma(o))return[];let n=JSON.parse(ha(o,"utf8"));return Array.isArray(n.items)?n.items:[]}catch{return[]}}var wa=e=>`\x1B]0;${e}\x07`,xa=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function ns(e){process.stdout.isTTY&&process.stdout.write(e)}function gt(e){ns(wa(e))}function no(e){ns(xa(e))}var ro=["\xB7 ","\xB7\xB7 ","\xB7\xB7\xB7"];Jt();var $a=/^(?:\d+[.)]|[-*•])\s+/;function rs(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
61
- `)){let c=s.trim().replace($a,""),d=c.match(/^`([^`]+)`$/);d&&(c=d[1].trim());let h=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!h||!n.has(h[1].toLowerCase()))continue;let g=`/${h[1].toLowerCase()}${h[2]?` ${h[2]}`:""}`;if(r.includes(g)||r.push(g),r.length>=o)break}return r}B();var ba=/\[Pasted text #\d+ \+\d+ lines?\]/g;function ss(e){return e.replace(/\r\n?/g,`
62
- `)}function is(e){return e.includes(`
63
- `)||e.length>200}function ls(e,t){let o=t.split(`
64
- `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function as(e,t){return e.replace(ba,o=>t.get(o)??o)}function cs(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function so(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
65
- `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function io(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function us(e,t,o){let n=io(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function ds(e,t,o){let n=e[io(e,t)];return o==="start"?n.start:n.end}function ps(e,t,o){let n=Math.max(1,o),r=e.split(`
59
+ `),console.log(`${D("\u25CF")} ${xe(s.title)}${s.arg!==null?V(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=Le(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=d=>d===1?"":"s";console.log(V(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let d of s.lines)console.log(d);s.hidden>0&&console.log(f(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=Zt(e.output,4,e.isError?void 0:s.name,s.input),d=e.isError?w:V;c.lines.forEach((h,g)=>console.log(d((g===0?" \u23BF ":" ")+h))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||ga(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
60
+ `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Qr(t.uuid),t.titleFired||wa(t,o));break;case"error":console.error(""),console.error(w(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&ya();break}}import{render as gc}from"ink";import{resolveAuth as hc}from"document360-engine";import{useCallback as G,useEffect as Ae,useMemo as lo,useRef as X,useState as U}from"react";import{Box as Y,Text as v,useApp as Ea,useInput as Da,useStdout as Ia}from"ink";import{existsSync as hs,readFileSync as ks,readdirSync as Ts}from"node:fs";import{basename as ao,isAbsolute as _a,join as ht}from"node:path";import{createSession as ys,loginPkce as Ma,toStoredTokens as Na,saveTokens as La,getAccessToken as Oa,resolveActiveProfile as pe,resolveProjectId as Ua,getArticle as Wa,decodeJwtClaims as Ze,isExpired as Re,loadTokens as Ue,setTitle as Fa,slugify as Ba,touchSession as ln,upsertSession as Ha,generateTitle as xs,findByName as qa,listSessions as za,renameSession as Ga,suggestNextAction as Xa,readProjectConfig as se,writeProjectConfig as Ya,resolveModelSetting as un,loadProfileMap as ws,applyPull as Va,computeSyncStatus as an,planPull as Ja,inventoryRepo as Ka,knownEnvironments as Qa,resolveEnvironment as Za,planPartitions as ec,partitionEvenly as tc,screenshotPlaceholderIds as oc,trackedArticlePaths as nc,runPartitioned as $s,estimateBulkCost as rc,resolveModelForOperation as bs}from"document360-engine";import{existsSync as ba,mkdirSync as va,readFileSync as Ca,writeFileSync as Sa}from"node:fs";import{join as ts}from"node:path";import{writerDir as Ta}from"document360-engine";function os(e){return ts(Ta(e),".sessions")}function ns(e,t){return ts(os(e),`${t}.json`)}function rs(e,t,o){try{va(os(e),{recursive:!0});let n=o.filter(r=>r.kind!=="banner");Sa(ns(e,t),JSON.stringify({v:1,items:n}),"utf8")}catch{}}function sn(e,t){try{let o=ns(e,t);if(!ba(o))return[];let n=JSON.parse(Ca(o,"utf8"));return Array.isArray(n.items)?n.items:[]}catch{return[]}}var Pa=e=>`\x1B]0;${e}\x07`,Ra=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function ss(e){process.stdout.isTTY&&process.stdout.write(e)}function gt(e){ss(Pa(e))}function no(e){ss(Ra(e))}var ro=["\xB7 ","\xB7\xB7 ","\xB7\xB7\xB7"];Jt();var Aa=/^(?:\d+[.)]|[-*•])\s+/;function is(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
61
+ `)){let c=s.trim().replace(Aa,""),d=c.match(/^`([^`]+)`$/);d&&(c=d[1].trim());let h=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!h||!n.has(h[1].toLowerCase()))continue;let g=`/${h[1].toLowerCase()}${h[2]?` ${h[2]}`:""}`;if(r.includes(g)||r.push(g),r.length>=o)break}return r}B();var ja=/\[Pasted text #\d+ \+\d+ lines?\]/g;function ls(e){return e.replace(/\r\n?/g,`
62
+ `)}function as(e){return e.includes(`
63
+ `)||e.length>200}function cs(e,t){let o=t.split(`
64
+ `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function us(e,t){return e.replace(ja,o=>t.get(o)??o)}function ds(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function so(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
65
+ `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function io(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function ps(e,t,o){let n=io(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function fs(e,t,o){let n=e[io(e,t)];return o==="start"?n.start:n.end}function ms(e,t,o){let n=Math.max(1,o),r=e.split(`
66
66
  `),s=0;for(let c=r.length-1;c>=0;c--){let d=r[c];if(s+=Math.max(1,Math.ceil(d.length/n)),s>=t){let h=s-t;return{text:[h>0?d.slice(h*n):d,...r.slice(c+1)].join(`
67
- `),truncated:c>0||h>0}}}return{text:e,truncated:!1}}function fs(e){let t=!1,o=0,n=0;for(let r of e.split(`
68
- `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as an,jsx as C,jsxs as W}from"react/jsx-runtime";var Ka={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function Qa(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=cn(e),c=se(e),d=(c?.docsDir??"user-docs").replace(/\/+$/,""),h=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${d}/ + config`,g={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:Ka[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:h};if(c===null)return g;try{let w=pe(e,n);g.profile=w.name,g.apiUrl=w.connection.apiUrl,g.prod=w.production,g.project=w.project.projectId??"(chosen at login)";let R=Ue(w.name);if(R){let $={...Ze(R.idToken)??{},...Ze(R.accessToken)??{}},j=$.email??$.preferred_username??"signed in";Re(R)?R.refreshToken&&(g.who=j,g.loggedOut=!1,g.sessionHint="session expired \u2014 refreshing\u2026"):(g.who=j,g.loggedOut=!1,g.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return g}function Za(e,t){try{let o=pe(e,t),n=Ue(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...Ze(n.idToken)??{},...Ze(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Re(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var $s=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],uo=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],ec="Ask me to write or update an article\u2026";function co({ch:e,dim:t}){let[o,n]=U(!0);return je(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),C(v,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var tc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function bs(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var oc=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function vs(e){try{return Cs(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function nc(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>vs(ht(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&vs(ht(e,n)))}function rc({startTime:e,chars:t}){let[o,n]=U(0);je(()=>{let h=setInterval(()=>n(g=>g+1),120);return()=>clearInterval(h)},[]);let r=uo[o%uo.length],s=$s[Math.floor(o/16)%$s.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return W(Y,{children:[C(v,{color:H,children:` ${r} ${s}\u2026 `}),C(v,{color:"gray",children:`(${dt(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var sc=12e4;function ic({p:e}){let[t,o]=U(0);je(()=>{let h=setInterval(()=>o(g=>g+1),150);return()=>clearInterval(h)},[]);let n=uo[t%uo.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),d=Date.now()-e.lastAt>=sc;return W(Y,{flexDirection:"column",children:[W(Y,{children:[C(v,{color:H,children:` ${n} ${e.verb??"Converting"} `}),C(v,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${dt(r)} \xB7 esc to stop`})]}),e.active.length>0&&C(v,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),d&&C(v,{color:"yellow",children:` \u26A0 no activity for ${dt(s)} \u2014 press esc to stop if it's wedged`})]})}function Ss({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=va(),[s,c]=U(o),[d,h]=U(null),[g,w]=U({text:"",pos:0}),R=g.text,$=z(a=>{w(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),j=z(a=>{w(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),T=z(()=>{w(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=cs(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[M,q]=U(!1),[G,ee]=U(!1),[ie,$e]=U(0),[le,ae]=U(null),[et,J]=U([]),kt=X(0),[js,po]=U(!1),[As,yt]=U(0),tt=X(0),[ot,We]=U(0),[Fe,wt]=U(null),pn=X(new Map),Es=X(0),Be=X([]),te=X(null);te.current===null&&(te.current=hs({cwd:e,profileName:s,allowProdWrites:!1}));let fe=X({uuid:null,firstPrompt:null,titleFired:!1}),xt=X(new Map),$t=X(!1),fo=X([]),be=X([]),mo=X(null),[fn,He]=U(null),[Ae,Ee]=U(null),[me,qe]=U(null),[ve,Ge]=U(null),[K,ze]=U(null),[ge,Xe]=U(null),[oe,De]=U(null),[Q,Ye]=U(null),[mn,go]=U([]),Ie=X([]),bt=X(!1),_e=X(null),[gn,ce]=U(null),vt=X(!1),Ct=X(null),[ne,Me]=U(null),[hn,St]=U(0),[kn,Pt]=U(0),[Ds,yn]=U(0),ho=lo(()=>{try{return cn(e).model??"auto"}catch{return null}},[e,Ds]),{stdout:ue}=Sa(),[,Is]=U(0),wn=X(`${ue.columns??80}x${ue.rows??24}`),Ce=z(()=>Math.max(20,(ue.columns??80)-1),[ue]),u=z(a=>{Be.current.push(a),console.log(Lo(a,Ce()))},[Ce]);je(()=>{let a=`d360-writer \xB7 ${fn??ao(e)}`;if(!M){no(1),gt(a);return}no(3);let l=0;gt(`${ro[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%ro.length,gt(`${ro[l]} ${a}`)},450);return()=>clearInterval(i)},[M,e,fn]),je(()=>()=>no(0),[]),je(()=>{if(u({kind:"banner",info:Qa(e,n,t,s)}),!se(e)){u({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
67
+ `),truncated:c>0||h>0}}}return{text:e,truncated:!1}}function gs(e){let t=!1,o=0,n=0;for(let r of e.split(`
68
+ `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as cn,jsx as C,jsxs as W}from"react/jsx-runtime";var sc={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function ic(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=un(e),c=se(e),d=(c?.docsDir??"user-docs").replace(/\/+$/,""),h=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${d}/ + config`,g={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:sc[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:h};if(c===null)return g;try{let x=pe(e,n);g.profile=x.name,g.apiUrl=x.connection.apiUrl,g.prod=x.production,g.project=x.project.projectId??"(chosen at login)";let A=Ue(x.name);if(A){let $={...Ze(A.idToken)??{},...Ze(A.accessToken)??{}},j=$.email??$.preferred_username??"signed in";Re(A)?A.refreshToken&&(g.who=j,g.loggedOut=!1,g.sessionHint="session expired \u2014 refreshing\u2026"):(g.who=j,g.loggedOut=!1,g.sessionHint=`session valid until ${new Date(A.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return g}function lc(e,t){try{let o=pe(e,t),n=Ue(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...Ze(n.idToken)??{},...Ze(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Re(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var vs=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],uo=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],ac="Ask me to write or update an article\u2026";function co({ch:e,dim:t}){let[o,n]=U(!0);return Ae(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),C(v,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var cc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Cs(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var uc=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Ss(e){try{return Ts(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function dc(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>Ss(ht(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&Ss(ht(e,n)))}function pc({startTime:e,chars:t}){let[o,n]=U(0);Ae(()=>{let h=setInterval(()=>n(g=>g+1),120);return()=>clearInterval(h)},[]);let r=uo[o%uo.length],s=vs[Math.floor(o/16)%vs.length],c=Math.floor((Date.now()-e)/1e3),d=Math.round(t/4);return W(Y,{children:[C(v,{color:H,children:` ${r} ${s}\u2026 `}),C(v,{color:"gray",children:`(${dt(c)} \xB7 ~${d} tokens \xB7 esc to interrupt)`})]})}var fc=12e4;function mc({p:e}){let[t,o]=U(0);Ae(()=>{let h=setInterval(()=>o(g=>g+1),150);return()=>clearInterval(h)},[]);let n=uo[t%uo.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),d=Date.now()-e.lastAt>=fc;return W(Y,{flexDirection:"column",children:[W(Y,{children:[C(v,{color:H,children:` ${n} ${e.verb??"Converting"} `}),C(v,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${dt(r)} \xB7 esc to stop`})]}),e.active.length>0&&C(v,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),d&&C(v,{color:"yellow",children:` \u26A0 no activity for ${dt(s)} \u2014 press esc to stop if it's wedged`})]})}function Ps({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=Ea(),[s,c]=U(o),[d,h]=U(null),[g,x]=U({text:"",pos:0}),A=g.text,$=G(a=>{x(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),j=G(a=>{x(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),P=G(()=>{x(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=ds(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[M,q]=U(!1),[z,ee]=U(!1),[ie,$e]=U(0),[le,ae]=U(null),[et,J]=U([]),kt=X(0),[Es,po]=U(!1),[Ds,yt]=U(0),tt=X(0),[ot,We]=U(0),[Fe,xt]=U(null),fn=X(new Map),Is=X(0),Be=X([]),te=X(null);te.current===null&&(te.current=ys({cwd:e,profileName:s,allowProdWrites:!1}));let fe=X({uuid:null,firstPrompt:null,titleFired:!1}),wt=X(new Map),$t=X(!1),fo=X([]),be=X([]),mo=X(null),[mn,He]=U(null),[je,Ee]=U(null),[me,qe]=U(null),[ve,ze]=U(null),[K,Ge]=U(null),[ge,Xe]=U(null),[oe,De]=U(null),[Q,Ye]=U(null),[gn,go]=U([]),Ie=X([]),bt=X(!1),_e=X(null),[hn,ce]=U(null),vt=X(!1),Ct=X(null),[ne,Me]=U(null),[kn,St]=U(0),[yn,Tt]=U(0),[_s,xn]=U(0),ho=lo(()=>{try{return un(e).model??"auto"}catch{return null}},[e,_s]),{stdout:ue}=Ia(),[,Ms]=U(0),wn=X(`${ue.columns??80}x${ue.rows??24}`),Ce=G(()=>Math.max(20,(ue.columns??80)-1),[ue]),u=G(a=>{Be.current.push(a),console.log(Lo(a,Ce()))},[Ce]);Ae(()=>{let a=`d360-writer \xB7 ${mn??ao(e)}`;if(!M){no(1),gt(a);return}no(3);let l=0;gt(`${ro[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%ro.length,gt(`${ro[l]} ${a}`)},450);return()=>clearInterval(i)},[M,e,mn]),Ae(()=>()=>no(0),[]),Ae(()=>{if(u({kind:"banner",info:ic(e,n,t,s)}),!se(e)){u({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
69
69
  1. /init \u2014 pick your Document360 environment & scaffold config
70
70
  2. /login signs in to that environment
71
71
  3. /workspace picks where articles go
72
- Press 1 to start.`}),J(["/init"]);return}try{let a=pe(e,s),l=Ue(a.name);l&&Re(l)&&l.refreshToken?Aa({profile:a.name,connection:a.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),$e(i=>i+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),J(["/login"])}):(!l||Re(l))&&(u({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),J(["/login"]))}catch{}try{let a=se(e),l=pe(e,s),i=Ue(l.name);a&&i&&!Re(i)&&nc(e,a.authoritativeSourceFiles)&&(u({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),J(["/scope"]))}catch{}},[]),je(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let m=`${ue.columns??80}x${ue.rows??24}`;m!==wn.current&&(wn.current=m,Is(p=>p+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+wr(Be.current,Ce())+"\x1B[?2026l")},80))},400)};return ue.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ue.off("resize",i)}},[ue,Ce]);let Ne=Math.max(20,(ue.columns??80)-1),xn=lo(()=>Za(e,s),[e,s,ie]),ko=lo(()=>{let a=se(e);if(!a)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let m=pe(e,s),p=Ue(m.name);if(!(!!p&&!(Re(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${m.name}")\u2026`,isSetup:!0};if(!m.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let l=ht(e,(a.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return ms(l)&&Cs(l).length>0}catch{return!1}})()?{text:ec,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,ie]),nt=Hn(R),$n=nt.length>0&&!M,yo=d!==null?ps(d,8,Ne):null,Tt=oe?oe.paths.filter(a=>!oe.query||a.toLowerCase().includes(oe.query.toLowerCase())).slice(0,8):[],Rt=ne?ne.sessions.filter(a=>{let l=ne.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],he=z((a,l)=>{te.current?.close(),te.current=hs({cwd:e,resume:a,profileName:l??s,allowProdWrites:$t.current}),a||(fe.current={uuid:null,firstPrompt:null,titleFired:!1},He(null)),St(0),Pt(0)},[e,s]),bn=z((a,l,i)=>{if(tc.test(a))try{let m=pe(e,s),p=typeof l.project_id=="string"&&l.project_id||m.project.projectId,k=to(l,i),y=[],A=oo(i);a.endsWith("publish_article")&&A&&y.push(`Live: ${A}`),k&&p&&y.push(`Preview: ${eo(m.connection.portalUrl,p,k,m.project.languageCode??"en")}`),y.length>0&&u({kind:"link",lines:y})}catch{}},[e,s,u]),ke=z(async(a,l)=>{po(!0),ae(null),J([]);let i=++kt.current;u({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let m=fe.current;m.firstPrompt||(m.firstPrompt=l?.display??a),tt.current=Date.now(),yt(0),q(!0),xt.current.clear();let p="",k="",y=null,A=()=>{y||(y=setTimeout(()=>{y=null,h(k.length>0?k:null)},60))},I=()=>{y&&clearTimeout(y),y=null,h(null)},L=()=>{if(k.trim()){let P=k.trimEnd();u({kind:"assistant",text:P})}k="",I()};try{for await(let P of te.current.send(a))if(P.type==="session"){if(!m.uuid){m.uuid=P.sessionId;let _=new Date().toISOString(),b=_a(m.firstPrompt??"session");Ma({uuid:P.sessionId,name:b,renamed:!1,titled:!1,cwd:e,firstPrompt:m.firstPrompt??"",createdAt:_,updatedAt:_}),He(b)}}else if(P.type==="text"){k+=P.delta,p+=P.delta;let _=fs(k);if(_>0){let b=k.slice(0,_).trimEnd();b&&u({kind:"assistant",text:b}),k=k.slice(_)}A(),yt(b=>b+P.delta.length)}else if(P.type==="tool"){let _=Qt(P.name,P.input);_&&(L(),u({kind:"tool",title:_.title,sep:_.sep,arg:_.arg}),xt.current.set(P.id,{name:P.name,input:P.input}))}else if(P.type==="article_diff"){let _=Le(P.oldContent,P.newContent,Ce());_&&(L(),u({kind:"diff",added:_.added,removed:_.removed,lines:_.lines,hidden:_.hidden}))}else if(P.type==="tool_result"){P.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(P.output)&&(vt.current=!0);let _=xt.current.get(P.id);if(_){xt.current.delete(P.id),L();let b=Zt(P.output,4,P.isError?void 0:_.name,_.input);u({kind:"tool-result",lines:b.lines,hidden:b.hidden,isError:P.isError}),P.isError||bn(_.name,_.input,P.output)}}else if(P.type==="result"){L(),St(b=>b+P.outputTokens),Pt(b=>b+P.costUsd),u({kind:"done",seconds:Math.round((Date.now()-tt.current)/1e3),tokens:P.outputTokens,costUsd:P.costUsd,ok:P.ok});let _=P.ok?rs(p,lt.map(b=>b.name)):[];if(_.length>0?J(_):P.ok&&p.trim()&&Ua(a,p,e).then(b=>{b&&kt.current===i&&ae(b)}).catch(()=>{}),m.uuid&&(sn(m.uuid),!m.titleFired)){m.titleFired=!0;let b=m.uuid,D=m.firstPrompt;D&&ks(D,e).then(O=>{O&&(Ia(b,O),He(O))}).catch(()=>{})}}else P.type==="error"&&(L(),P.kind==="auth"&&(vt.current=!0),u({kind:"note",text:`agent error: ${P.message}`,tone:"error"}))}finally{q(!1),I(),fe.current.uuid&&os(e,fe.current.uuid,Be.current),vt.current&&(vt.current=!1,Ct.current=l?.display??a,J(["/login"])),bt.current&&(bt.current=!1,Ie.current.length>0&&(u({kind:"note",tone:"info",text:`(${Ie.current.length} queued message(s) discarded)`}),Ie.current=[],go([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,bn,Ce]),wo=z(a=>{let l=Ue(a);if(!l||Re(l)&&!l.refreshToken)return null;let i={...Ze(l.idToken)??{},...Ze(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),xo=z((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,Wa(i,e))}c(a),he(void 0,a),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),wo(a)||(u({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),J(["/login"])),$e(i=>i+1)},[e,u,he,wo]),vn=z((a,l,i)=>{It(e,a,l,i.id,i.name),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),he(),ys(e,a)||(u({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),ae("analyze this repo and propose a docs structure"))},[e,u,he]),jt=z(()=>{let a=be.current[0];if(!a)return;u({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Le(a.oldContent,a.newContent,Ce());u(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),u({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[u,Ce]),Cn=z(a=>{if(be.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=be.current.shift();try{Fa({cwd:e,profileName:s},i),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),be.current.length===0&&J(m=>m.length>0?m:["/sync"])}catch(m){u({kind:"note",tone:"error",text:`Pull failed: ${m.message}`})}}else if(l==="n"||l==="no"){let i=be.current.shift();u({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=be.current.length;return be.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return jt(),!0},[e,s,jt,u]),Sn=z(a=>{let l=mo.current;if(!l)return!1;if(mo.current=null,a.trim()!==l.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:m}=Zo(e,l.targets);u({kind:"note",tone:m.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let p of m)u({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return!0},[e,u]),Pn=z(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),m=l.slice(1);switch(po(!0),i){case"help":u({kind:"note",tone:"info",text:Wt().join(`
73
- `)});return;case"exit":case"quit":te.current?.close(),r();return;case"clear":he(),Be.current=[],po(!1),ae(null),kt.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=pe(e,s)}catch(k){u({kind:"note",tone:"error",text:k.message});return}u({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let k=await Ta(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},A=>u({kind:"note",tone:"info",text:A})),y=Ra(p.name,k);ja(y),So(y,p.name,A=>u({kind:"note",tone:"info",text:A})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Mt(y)}`}),p.project.workspaceId||(u({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),J(["/workspace"])),Ct.current&&(ae(Ct.current),Ct.current=null,u({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(k){u({kind:"note",tone:"error",text:`Login failed: ${k.message}`})}finally{ee(!1),$e(k=>k+1)}return}case"allow-prod":{let p=!1;try{p=pe(e,s).production}catch{}if(!p){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}$t.current=!0,he(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=Go(m.join(" ")),k=fe.current.uuid;if(!k){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=fe.current.firstPrompt??"";ks(y,e).then(A=>{A?(ae(`/rename ${A}`),u({kind:"note",tone:"info",text:`Suggestion: "${A}" \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}Oa(k,p),He(p),gt(`d360-writer \xB7 ${p}`),u({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=m[0],k=se(e);if(!p){let y=Object.entries(k?.profiles??{});if(y.length===0){u({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let A=y.map(([P,_])=>({name:P,env:_.connection?.environment??"custom",prod:_.production===!0,who:wo(P)})),I=s??k?.defaultProfile,L=Math.max(0,A.findIndex(P=>P.name===I));qe({cursor:L,current:L,rows:A});return}if(p==="add"){let y=zo(e,m[1],m[2]);if(y){u({kind:"note",tone:"error",text:y});return}u({kind:"note",tone:"ok",text:`\u2713 Profile "${m[1]}" created (environment: ${m[2]??m[1]}).`}),xo(m[1],!1);return}if(!k?.profiles?.[p]){u({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}xo(p,!0);return}case"doctor":await ft(m,{cwd:e});return;case"mcp":await Bt(m);return;case"init":{if(se(e)){u({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let p=qa().map(k=>({name:k,apiUrl:Ga(k).apiUrl}));Ge({cursor:0,rows:p});return}case"resume":{let p=m.join(" ").trim(),k=La(e).filter(I=>I.uuid!==fe.current.uuid);if(!p){if(!k.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Me({query:"",cursor:0,sessions:k});return}let y=Na(e,p);if(!y){u({kind:"note",tone:"error",text:`No session matches "${p}".`});return}he(y.uuid),fe.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},He(y.name),sn(y.uuid),Be.current=[];let A=rn(e,y.uuid);for(let I of A)u(I);u({kind:"note",tone:"ok",text:A.length?`Resumed "${y.name}" \u2014 restored ${A.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let p=m.join(" ").trim(),k;try{k=await Dt(e,s)}catch(A){u({kind:"note",tone:"error",text:`Could not list workspaces: ${A.message}`});return}if(!p){let A=k.workspaces.map(L=>({id:L.id,name:L.name??L.id,type:L.workspace_type}));if(A.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let I=Math.max(0,A.findIndex(L=>L.id===k.current));ze({cursor:I,current:I,rows:A,profile:k.profile,projectId:k.projectId,environment:k.environment});return}let y=Co(k.workspaces,p);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}vn(k.profile,k.projectId,y);return}case"publish":{if(m[0]&&m[0]!=="--all"){await ke(qt(m[0]),{display:`/publish ${m[0]}`,echoDisplay:!0});return}let p=m[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let k=await ln({cwd:e,profileName:s}),y=Ht(k.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let I=k.counts["unknown-base"]??0;I>0&&u({kind:"note",tone:"info",text:`(${I} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await ke(ct(y.map(I=>I.path)),{display:"/publish --all",echoDisplay:!0});return}let A=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;Xe({cursor:0,rows:A})}catch(k){u({kind:"note",tone:"error",text:`Could not compute sync status: ${k.message}`}),u({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let p=m.join(" ").trim();if(!p){let y=[];try{y=Object.keys(ys(e,pe(e,s).name)?.articles??{})}catch{}if(y.length===0){u({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}De({query:"",cursor:0,paths:y});return}let k=Pa(p)?p:ht(e,p);if(ms(k)){try{u({kind:"preview",name:ao(k),text:bs(gs(k,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${k}: ${y.message}`})}return}if(oc.test(p)){try{let y=pe(e,s),A={profile:y.name,connection:y.connection},I=y.project.projectId??Ea(A),L=await Da(A,I,p);u({kind:"preview",name:L.title??p,text:L.content??"*(article has no content)*"})}catch(y){u({kind:"note",tone:"error",text:`Could not fetch article: ${y.message}`})}return}u({kind:"note",tone:"error",text:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=m[0]?.trim();if(!p){let I=Vt(cn(e));Ee({cursor:I,current:I});return}let{lines:k,changed:y,effective:A}=pt(e,p);for(let I of k)u({kind:"note",tone:I.startsWith("\u26A0")?"warn":I.startsWith("\u2713")?"ok":"info",text:I});y&&(yn(I=>I+1),te.current?.setModel(A));return}case"convert":{if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:k}=Uo(m),y=Va(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 A=Wo(y,p);if(A.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${y.length} are tracked overall.)`});return}let I=za(A),L=3,P=`/convert${p?` --scope ${p}`:""} --run`,_=xs(e,"light");if(!k){let D=Ja({files:Fo(e,A),op:"convert",model:_.model}),O=p?`Scope: ${p} (${A.length} of ${y.length} tracked)
72
+ Press 1 to start.`}),J(["/init"]);return}try{let a=pe(e,s),l=Ue(a.name);l&&Re(l)&&l.refreshToken?Oa({profile:a.name,connection:a.connection}).then(()=>{u({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),$e(i=>i+1)}).catch(()=>{u({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),J(["/login"])}):(!l||Re(l))&&(u({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),J(["/login"]))}catch{}try{let a=se(e),l=pe(e,s),i=Ue(l.name);a&&i&&!Re(i)&&dc(e,a.authoritativeSourceFiles)&&(u({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),J(["/scope"]))}catch{}},[]),Ae(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let m=`${ue.columns??80}x${ue.rows??24}`;m!==wn.current&&(wn.current=m,Ms(p=>p+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+wr(Be.current,Ce())+"\x1B[?2026l")},80))},400)};return ue.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ue.off("resize",i)}},[ue,Ce]);let Ne=Math.max(20,(ue.columns??80)-1),$n=lo(()=>lc(e,s),[e,s,ie]),ko=lo(()=>{let a=se(e);if(!a)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let m=pe(e,s),p=Ue(m.name);if(!(!!p&&!(Re(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${m.name}")\u2026`,isSetup:!0};if(!m.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let l=ht(e,(a.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return hs(l)&&Ts(l).length>0}catch{return!1}})()?{text:ac,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,ie]),nt=qn(A),bn=nt.length>0&&!M,yo=d!==null?ms(d,8,Ne):null,Pt=oe?oe.paths.filter(a=>!oe.query||a.toLowerCase().includes(oe.query.toLowerCase())).slice(0,8):[],Rt=ne?ne.sessions.filter(a=>{let l=ne.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],he=G((a,l)=>{te.current?.close(),te.current=ys({cwd:e,resume:a,profileName:l??s,allowProdWrites:$t.current}),a||(fe.current={uuid:null,firstPrompt:null,titleFired:!1},He(null)),St(0),Tt(0)},[e,s]),vn=G((a,l,i)=>{if(cc.test(a))try{let m=pe(e,s),p=typeof l.project_id=="string"&&l.project_id||m.project.projectId,k=to(l,i),y=[],E=oo(i);a.endsWith("publish_article")&&E&&y.push(`Live: ${E}`),k&&p&&y.push(`Preview: ${eo(m.connection.portalUrl,p,k,m.project.languageCode??"en")}`),y.length>0&&u({kind:"link",lines:y})}catch{}},[e,s,u]),ke=G(async(a,l)=>{po(!0),ae(null),J([]);let i=++kt.current;u({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let m=fe.current;m.firstPrompt||(m.firstPrompt=l?.display??a),tt.current=Date.now(),yt(0),q(!0),wt.current.clear();let p="",k="",y=null,E=()=>{y||(y=setTimeout(()=>{y=null,h(k.length>0?k:null)},60))},I=()=>{y&&clearTimeout(y),y=null,h(null)},L=()=>{if(k.trim()){let T=k.trimEnd();u({kind:"assistant",text:T})}k="",I()};try{for await(let T of te.current.send(a))if(T.type==="session"){if(!m.uuid){m.uuid=T.sessionId;let _=new Date().toISOString(),b=Ba(m.firstPrompt??"session");Ha({uuid:T.sessionId,name:b,renamed:!1,titled:!1,cwd:e,firstPrompt:m.firstPrompt??"",createdAt:_,updatedAt:_}),He(b)}}else if(T.type==="text"){k+=T.delta,p+=T.delta;let _=gs(k);if(_>0){let b=k.slice(0,_).trimEnd();b&&u({kind:"assistant",text:b}),k=k.slice(_)}E(),yt(b=>b+T.delta.length)}else if(T.type==="tool"){let _=Qt(T.name,T.input);_&&(L(),u({kind:"tool",title:_.title,sep:_.sep,arg:_.arg}),wt.current.set(T.id,{name:T.name,input:T.input}))}else if(T.type==="article_diff"){let _=Le(T.oldContent,T.newContent,Ce());_&&(L(),u({kind:"diff",added:_.added,removed:_.removed,lines:_.lines,hidden:_.hidden}))}else if(T.type==="tool_result"){T.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(T.output)&&(vt.current=!0);let _=wt.current.get(T.id);if(_){wt.current.delete(T.id),L();let b=Zt(T.output,4,T.isError?void 0:_.name,_.input);u({kind:"tool-result",lines:b.lines,hidden:b.hidden,isError:T.isError}),T.isError||vn(_.name,_.input,T.output)}}else if(T.type==="result"){L(),St(b=>b+T.outputTokens),Tt(b=>b+T.costUsd),u({kind:"done",seconds:Math.round((Date.now()-tt.current)/1e3),tokens:T.outputTokens,costUsd:T.costUsd,ok:T.ok});let _=T.ok?is(p,lt.map(b=>b.name)):[];if(_.length>0?J(_):T.ok&&p.trim()&&Xa(a,p,e).then(b=>{b&&kt.current===i&&ae(b)}).catch(()=>{}),m.uuid&&(ln(m.uuid),!m.titleFired)){m.titleFired=!0;let b=m.uuid,R=m.firstPrompt;R&&xs(R,e).then(O=>{O&&(Fa(b,O),He(O))}).catch(()=>{})}}else T.type==="error"&&(L(),T.kind==="auth"&&(vt.current=!0),u({kind:"note",text:`agent error: ${T.message}`,tone:"error"}))}finally{q(!1),I(),fe.current.uuid&&rs(e,fe.current.uuid,Be.current),vt.current&&(vt.current=!1,Ct.current=l?.display??a,J(["/login"])),bt.current&&(bt.current=!1,Ie.current.length>0&&(u({kind:"note",tone:"info",text:`(${Ie.current.length} queued message(s) discarded)`}),Ie.current=[],go([])),u({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,u,vn,Ce]),xo=G(a=>{let l=Ue(a);if(!l||Re(l)&&!l.refreshToken)return null;let i={...Ze(l.idToken)??{},...Ze(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),wo=G((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,Ya(i,e))}c(a),he(void 0,a),u({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),xo(a)||(u({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),J(["/login"])),$e(i=>i+1)},[e,u,he,xo]),Cn=G((a,l,i)=>{It(e,a,l,i.id,i.name),u({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),he(),ws(e,a)||(u({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),ae("analyze this repo and propose a docs structure"))},[e,u,he]),At=G(()=>{let a=be.current[0];if(!a)return;u({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)u({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&u({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Le(a.oldContent,a.newContent,Ce());u(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),u({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[u,Ce]),Sn=G(a=>{if(be.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=be.current.shift();try{Va({cwd:e,profileName:s},i),u({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),be.current.length===0&&J(m=>m.length>0?m:["/sync"])}catch(m){u({kind:"note",tone:"error",text:`Pull failed: ${m.message}`})}}else if(l==="n"||l==="no"){let i=be.current.shift();u({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=be.current.length;return be.current=[],u({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return At(),!0},[e,s,At,u]),Tn=G(a=>{let l=mo.current;if(!l)return!1;if(mo.current=null,a.trim()!==l.repoName)return u({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:m}=en(e,l.targets);u({kind:"note",tone:m.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let p of m)u({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return!0},[e,u]),Pn=G(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),m=l.slice(1);switch(po(!0),i){case"help":u({kind:"note",tone:"info",text:Wt().join(`
73
+ `)});return;case"exit":case"quit":te.current?.close(),r();return;case"clear":he(),Be.current=[],po(!1),ae(null),kt.current++,u({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=pe(e,s)}catch(k){u({kind:"note",tone:"error",text:k.message});return}u({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),ee(!0);try{let k=await Ma(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},E=>u({kind:"note",tone:"info",text:E})),y=Na(p.name,k);La(y),So(y,p.name,E=>u({kind:"note",tone:"info",text:E})),u({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Mt(y)}`}),p.project.workspaceId||(u({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),J(["/workspace"])),Ct.current&&(ae(Ct.current),Ct.current=null,u({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(k){u({kind:"note",tone:"error",text:`Login failed: ${k.message}`})}finally{ee(!1),$e(k=>k+1)}return}case"allow-prod":{let p=!1;try{p=pe(e,s).production}catch{}if(!p){u({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}$t.current=!0,he(),u({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=zo(m.join(" ")),k=fe.current.uuid;if(!k){u({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){u({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=fe.current.firstPrompt??"";xs(y,e).then(E=>{E?(ae(`/rename ${E}`),u({kind:"note",tone:"info",text:`Suggestion: "${E}" \u2014 press tab to accept, or type /rename <your name>.`})):u({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>u({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}Ga(k,p),He(p),gt(`d360-writer \xB7 ${p}`),u({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=m[0],k=se(e);if(!p){let y=Object.entries(k?.profiles??{});if(y.length===0){u({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let E=y.map(([T,_])=>({name:T,env:_.connection?.environment??"custom",prod:_.production===!0,who:xo(T)})),I=s??k?.defaultProfile,L=Math.max(0,E.findIndex(T=>T.name===I));qe({cursor:L,current:L,rows:E});return}if(p==="add"){let y=Go(e,m[1],m[2]);if(y){u({kind:"note",tone:"error",text:y});return}u({kind:"note",tone:"ok",text:`\u2713 Profile "${m[1]}" created (environment: ${m[2]??m[1]}).`}),wo(m[1],!1);return}if(!k?.profiles?.[p]){u({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}wo(p,!0);return}case"doctor":await ft(m,{cwd:e});return;case"mcp":await Bt(m);return;case"init":{if(se(e)){u({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let p=Qa().map(k=>({name:k,apiUrl:Za(k).apiUrl}));ze({cursor:0,rows:p});return}case"resume":{let p=m.join(" ").trim(),k=za(e).filter(I=>I.uuid!==fe.current.uuid);if(!p){if(!k.length){u({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Me({query:"",cursor:0,sessions:k});return}let y=qa(e,p);if(!y){u({kind:"note",tone:"error",text:`No session matches "${p}".`});return}he(y.uuid),fe.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},He(y.name),ln(y.uuid),Be.current=[];let E=sn(e,y.uuid);for(let I of E)u(I);u({kind:"note",tone:"ok",text:E.length?`Resumed "${y.name}" \u2014 restored ${E.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let p=m.join(" ").trim(),k;try{k=await Dt(e,s)}catch(E){u({kind:"note",tone:"error",text:`Could not list workspaces: ${E.message}`});return}if(!p){let E=k.workspaces.map(L=>({id:L.id,name:L.name??L.id,type:L.workspace_type}));if(E.length===0){u({kind:"note",tone:"info",text:"No workspaces in this project."});return}let I=Math.max(0,E.findIndex(L=>L.id===k.current));Ge({cursor:I,current:I,rows:E,profile:k.profile,projectId:k.projectId,environment:k.environment});return}let y=Co(k.workspaces,p);if(!y){u({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}Cn(k.profile,k.projectId,y);return}case"publish":{if(m[0]&&m[0]!=="--all"){await ke(qt(m[0]),{display:`/publish ${m[0]}`,echoDisplay:!0});return}let p=m[0]==="--all";u({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let k=await an({cwd:e,profileName:s}),y=Ht(k.entries);if(y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let I=k.counts["unknown-base"]??0;I>0&&u({kind:"note",tone:"info",text:`(${I} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await ke(ct(y.map(I=>I.path)),{display:"/publish --all",echoDisplay:!0});return}let E=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;Xe({cursor:0,rows:E})}catch(k){u({kind:"note",tone:"error",text:`Could not compute sync status: ${k.message}`}),u({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let p=m.join(" ").trim();if(!p){let y=[];try{y=Object.keys(ws(e,pe(e,s).name)?.articles??{})}catch{}if(y.length===0){u({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}De({query:"",cursor:0,paths:y});return}let k=_a(p)?p:ht(e,p);if(hs(k)){try{u({kind:"preview",name:ao(k),text:Cs(ks(k,"utf8"))})}catch(y){u({kind:"note",tone:"error",text:`Could not read ${k}: ${y.message}`})}return}if(uc.test(p)){try{let y=pe(e,s),E={profile:y.name,connection:y.connection},I=y.project.projectId??Ua(E),L=await Wa(E,I,p);u({kind:"preview",name:L.title??p,text:L.content??"*(article has no content)*"})}catch(y){u({kind:"note",tone:"error",text:`Could not fetch article: ${y.message}`})}return}u({kind:"note",tone:"error",text:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=m[0]?.trim();if(!p){let I=Vt(un(e));Ee({cursor:I,current:I});return}let{lines:k,changed:y,effective:E}=pt(e,p);for(let I of k)u({kind:"note",tone:I.startsWith("\u26A0")?"warn":I.startsWith("\u2713")?"ok":"info",text:I});y&&(xn(I=>I+1),te.current?.setModel(E));return}case"convert":{if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:k}=Uo(m),y=nc(e,s);if(y.length===0){u({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let E=Wo(y,p);if(E.length===0){u({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${y.length} are tracked overall.)`});return}let I=ec(E),L=3,T=`/convert${p?` --scope ${p}`:""} --run`,_=bs(e,"light");if(!k){let R=rc({files:Fo(e,E),op:"convert",model:_.model}),O=p?`Scope: ${p} (${E.length} of ${y.length} tracked)
74
74
  `:"",re=`
75
- Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:O+Ho(I,D,L).join(`
76
- `)+re}),J([P]);return}tt.current=Date.now(),yt(0),q(!0);let b=new AbortController;_e.current=b,ce({total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${A.length} articles across ${I.length} partitions (\u2264${L} agents at once) on ${_.model}\u2026 (esc to stop)`});try{for await(let D of ws({cwd:e,partitions:I,promptFor:Bo,concurrency:L,profileName:s,allowProdWrites:$t.current,model:_.model,signal:b.signal}))if(D.type==="partition_status")D.status==="running"?(ce(O=>O&&{...O,active:[...O.active,D.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${D.label} \u2014 converting\u2026`})):(ce(O=>O&&{...O,active:O.active.filter(re=>re!==D.label),done:O.done+1,lastAt:Date.now()}),u({kind:"note",tone:D.status==="done"?"ok":"error",text:` ${D.status==="done"?"\u2713":"\u2717"} ${D.label}`}));else if(D.type==="partition_event")ce(O=>{if(!O)return O;let re={...O,lastAt:Date.now()};return D.event.type==="tool"?re.tools=O.tools+1:D.event.type==="text"&&(re.chars=O.chars+D.event.delta.length),re});else if(D.type==="run_done"){Pt(ye=>ye+D.totalCostUsd),St(ye=>ye+D.results.reduce((bo,vo)=>bo+vo.outputTokens,0));let O=D.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";u({kind:"note",tone:D.aborted?"warn":D.ok?"ok":"warn",text:O+qo(D.results,I,re).join(`
77
- `)})}}catch(D){u({kind:"note",tone:"error",text:`Convert run failed: ${D.message}`})}finally{_e.current=null,ce(null),q(!1)}return}case"sync":{let p=(m[0]??"status").toLowerCase();try{if(p==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let k=await ln({cwd:e,profileName:s});u({kind:"note",tone:"info",text:Xt(k).join(`
78
- `)});return}if(p==="pull"){let k=m[1];if(!k){u({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(k==="--all"){if(u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await ln({cwd:e,profileName:s})).entries.filter(L=>L.status==="remote-ahead"&&L.path).map(L=>L.path),y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else y=[k.replace(/\\/g,"/")];let A=[];for(let I of y)A.push(await Ba({cwd:e,profileName:s,relPath:I}));be.current=A,jt();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(k){u({kind:"note",tone:"error",text:`Sync failed: ${k.message}`})}return}case"scope":{if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Ha(e);if(p.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ye({cursor:0,rows:p.map(k=>({...k,checked:k.recommended}))});return}case"audit":case"capture-setup":{let k=await(i==="audit"?Gt:mt)(m,void 0);k.kind==="forward-to-agent"&&k.prompt&&await ke(k.prompt,{display:k.display,echoDisplay:!0});return}case"screenshot":{let p=Vo(m);if(p.mode==="list"){if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:Ko(Jo(e,p.scope),p.scope).join(`
79
- `)});return}if(p.mode==="single"){let b=await Kt(m);b.kind==="forward-to-agent"&&b.prompt&&await ke(b.prompt,{display:b.display,echoDisplay:!0});return}if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let k=p.mode==="scope"?p.scope:void 0,y=Ya(e,{scope:k}).map(b=>b.id);if(y.length===0){u({kind:"note",tone:"info",text:k?`No screenshot placeholders under ${k}.`:"No screenshot placeholders found in the docs."});return}let A=6,I=Xa(y,3),L=xs(e,"standard");tt.current=Date.now(),yt(0),q(!0);let P=new AbortController;_e.current=P,ce({verb:"Authoring",total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${I.length} partition${I.length===1?"":"s"} (\u2264${A} agents at once) on ${L.model}${L.forced?" (forced)":""}\u2026 (esc to stop)`});let _=!1;try{for await(let b of ws({cwd:e,partitions:I,promptFor:Ur,concurrency:A,profileName:s,allowProdWrites:$t.current,model:L.model,signal:P.signal}))if(b.type==="partition_status")b.status==="running"?(ce(D=>D&&{...D,active:[...D.active,b.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${b.label} \u2014 authoring\u2026`})):(ce(D=>D&&{...D,active:D.active.filter(O=>O!==b.label),done:D.done+1,lastAt:Date.now()}),u({kind:"note",tone:b.status==="done"?"ok":"error",text:` ${b.status==="done"?"\u2713":"\u2717"} ${b.label}`}));else if(b.type==="partition_event")ce(D=>{if(!D)return D;let O={...D,lastAt:Date.now()};return b.event.type==="tool"?O.tools=D.tools+1:b.event.type==="text"&&(O.chars=D.chars+b.event.delta.length),O});else if(b.type==="run_done"){Pt(ye=>ye+b.totalCostUsd),St(ye=>ye+b.results.reduce((bo,vo)=>bo+vo.outputTokens,0));let D=b.results.filter(ye=>ye.ok).length,O=b.aborted?"Stopped. ":"";_=!b.aborted&&p.setup;let re=b.aborted?"":_?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";u({kind:"note",tone:b.aborted?"warn":b.ok?"ok":"warn",text:`${O}Authored specs \u2014 ${D}/${b.results.length} batches ok.${re}`})}}catch(b){u({kind:"note",tone:"error",text:`Spec authoring failed: ${b.message}`})}finally{_e.current=null,ce(null),q(!1)}if(_){let b=await mt();b.kind==="forward-to-agent"&&b.prompt&&await ke(b.prompt,{display:b.display,echoDisplay:!0})}return}case"reset":{let p=Qo(e);if(p.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}mo.current={repoName:ao(e),targets:p},u({kind:"note",tone:"warn",text:en(e,p).join(`
80
- `)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,jt,u,he,ke]),At=z(a=>{let l=(a??R).trim();if($(""),wt(null),We(0),l.startsWith("/")){let m=k=>k.trim().split(/\s+/).slice(1).join(" "),p=m(l);J(k=>k.filter(y=>y.trim()!==l&&!(p&&m(y)===p)))}if(!l||Sn(l)||Cn(l))return;fo.current.push(l);let i=as(l,pn.current);l.startsWith("/")?Pn(i):ke(i,{display:l})},[R,Sn,Cn,Pn,ke]),Tn=z(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=ss(a);if(is(l)){let i=ls(++Es.current,l);pn.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),Ve=Math.max(10,Ne-6),Rn=lo(()=>so(g.text,Ve),[g.text,Ve]),$o=z(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Et=z(a=>w(l=>({...l,pos:us(so(l.text,Ve),l.pos,a)})),[Ve]),rt=z(a=>w(l=>({...l,pos:ds(so(l.text,Ve),l.pos,a)})),[Ve]),_s=["\x1B[H","\x1B[1~","\x1BOH"],Ms=["\x1B[F","\x1B[4~","\x1BOF"],jn=z((a,l)=>l.leftArrow?($o(-1),!0):l.rightArrow?($o(1),!0):a&&_s.includes(a)?(rt("start"),!0):a&&Ms.includes(a)?(rt("end"),!0):l.ctrl&&a==="a"?(rt("start"),!0):l.ctrl&&a==="e"?(rt("end"),!0):!1,[$o,rt]);return Ca((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!G){if(M){if(l.escape){if(_e.current){_e.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),_e.current.abort());return}bt.current||(bt.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),te.current?.interrupt());return}if(l.return){let i=R.trim();if(!i)return;Ie.current.push(i),go([...Ie.current]),$("");return}if(jn(a,l))return;if(l.upArrow){Et(-1);return}if(l.downArrow){Et(1);return}if(l.backspace||l.delete){T();return}a&&!l.ctrl&&!l.meta&&Tn(a);return}if(Ae){if(l.upArrow){Ee(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ee(i=>i&&{...i,cursor:Math.min(de.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=de.length){Ee(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=de[Ae.cursor];Ee(null);let{lines:m,changed:p,effective:k}=pt(e,i.value??"default");for(let y of m)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(yn(y=>y+1),te.current?.setModel(k));return}if(a==="s"){let i=de[Ae.cursor];Ee(null),te.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Ee(null);return}return}if(me){if(l.upArrow){qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=me.rows.length){qe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=me.rows[me.cursor];qe(null),xo(i.name,l.return===!0);return}if(l.escape){qe(null);return}return}if(ve){if(l.upArrow){Ge(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ge(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ve.rows.length){Ge(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ve.rows[ve.cursor].name;if(Ge(null),!Vn(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let k=Ue(i);p=!!k&&!(Re(k)&&!k.refreshToken)}catch{}p?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),J(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),J(["/login"])),$e(k=>k+1);return}if(l.escape){Ge(null);return}return}if(K){if(l.upArrow){ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=K.rows.length){ze(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=K.rows[K.cursor],{profile:m,projectId:p}=K;ze(null),vn(m,p,i);return}if(l.escape){ze(null);return}return}if(Q){if(l.upArrow){Ye(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ye(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Ye(i=>i&&{...i,rows:i.rows.map((m,p)=>p===i.cursor?{...m,checked:!m.checked}:m)});return}if(l.return){let i=Q.rows.filter(m=>m.checked).map(m=>m.path);if(Ye(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}jo(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let m of i)u({kind:"note",tone:"info",text:` ${m}`});u({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Ye(null);return}return}if(ge){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)<=ge.rows.length){Xe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge.rows[ge.cursor],m=ge.rows.filter(p=>p.path!=="--all").map(p=>p.path);Xe(null),ke(i.path==="--all"?ct(m):qt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Xe(null);return}return}if(oe){if(l.escape){De(null);return}if(l.upArrow){De(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){De(i=>i&&{...i,cursor:Math.min(Math.max(0,Tt.length-1),i.cursor+1)});return}if(l.return){let i=Tt[oe.cursor];if(i){De(null);try{u({kind:"preview",name:ao(i),text:bs(gs(ht(e,i),"utf8"))})}catch(m){u({kind:"note",tone:"error",text:`Could not read ${i}: ${m.message}`})}}return}if(l.backspace||l.delete){De(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){De(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){Me(null);return}if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(Math.max(0,Rt.length-1),i.cursor+1)});return}if(l.return){let i=Rt[ne.cursor];if(i){Me(null),he(i.uuid),fe.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},He(i.name),sn(i.uuid),Be.current=[];let m=rn(e,i.uuid);for(let p of m)u(p);u({kind:"note",tone:"ok",text:m.length?`Resumed "${i.name}" \u2014 restored ${m.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(l.backspace||l.delete){Me(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Me(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!R&&le){$(le),ae(null),kt.current++;return}if(!R&&et.length>0&&a&&/^[1-9]$/.test(a)){let i=et[Number(a)-1];if(i){$(i);return}}if(!jn(a,l)){if($n){if(l.upArrow){We(i=>Math.max(0,i-1));return}if(l.downArrow){We(i=>Math.min(nt.length-1,i+1));return}if(l.tab){$("/"+(nt[ot]?.name??"")+" "),We(0);return}if(l.return){let i=nt[ot];if(i){let m=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(qn(i.usage)&&!m){$("/"+i.name+" "),We(0);return}At("/"+i.name+(m?" "+m:""));return}}}else{if(l.upArrow){if(R!==""&&Fe===null){Et(-1);return}let i=fo.current;if(!i.length)return;let m=Fe===null?i.length-1:Math.max(0,Fe-1);wt(m),$(i[m]??"");return}if(l.downArrow){if(R!==""&&Fe===null){Et(1);return}let i=fo.current;if(Fe===null)return;let m=Fe+1;m>=i.length?(wt(null),$("")):(wt(m),$(i[m]??""));return}}if(l.return){At();return}if(l.backspace||l.delete){T();return}if(l.escape){$(""),We(0),ae(null),J([]);return}a&&!l.ctrl&&!l.meta&&Tn(a)}}}),je(()=>{if(M||G)return;let a=Ie.current.shift();a!==void 0&&(go([...Ie.current]),At(a))},[M,G,At]),W(Y,{flexDirection:"column",width:Ne,children:[yo!==null&&W(Y,{marginTop:1,flexDirection:"column",children:[yo.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:yo.text})]}),M&&(gn?C(ic,{p:gn}):C(rc,{startTime:tt.current,chars:As})),C(Y,{borderStyle:"round",borderColor:xn.prod?"yellow":H,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?Rn.map((a,l)=>{let i=g.text.slice(a.start,a.end),m=l===io(Rn,g.pos),p=Math.min(g.pos,a.end)-a.start;return W(v,{children:[C(v,{color:H,children:l===0?"> ":" "}),m?W(an,{children:[i.slice(0,p),C(co,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):W(v,{children:[C(v,{color:H,children:"> "}),le&&!M?W(an,{children:[C(co,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):ko.isSetup||!js?W(an,{children:[C(co,{ch:ko.text[0],dim:!0}),C(v,{color:"gray",children:ko.text.slice(1)})]}):C(co,{ch:" "})]})}),mn.length>0&&C(Y,{flexDirection:"column",paddingX:1,children:mn.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),Ae?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Select model"}),C(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),de.map((a,l)=>W(v,{color:l===Ae.cursor?H:void 0,children:[l===Ae.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===Ae.current?" \u2714":""}`.padEnd(16),C(v,{color:"gray",children:a.desc})]},a.label)),C(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):me?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>W(v,{color:l===me.cursor?H:void 0,children:[l===me.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===me.current?" \u2714":""}`.padEnd(20),C(v,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):ve?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Pick your Document360 environment"}),ve.rows.map((a,l)=>W(v,{color:l===ve.cursor?H:void 0,children:[l===ve.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(v,{color:"gray",children:a.apiUrl})]},a.name)),C(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):K?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Switch workspace"}),C(v,{dimColor:!0,children:`environment ${K.environment} \xB7 project ${K.projectId.slice(0,8)}\u2026`}),K.rows.map((a,l)=>W(v,{color:l===K.cursor?H:void 0,children:[l===K.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===K.current?" \u2714":""}`.padEnd(30),C(v,{color:"gray",children:a.type??""})]},a.id)),C(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):Q?(()=>{let l=Math.min(Math.max(0,Q.cursor-Math.floor(7)),Math.max(0,Q.rows.length-14)),i=Q.rows.slice(l,l+14),m=Q.rows.filter(p=>p.checked).length;return W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Which folders back the user docs? (${m} selected of ${Q.rows.length})`}),l>0?C(v,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,k)=>{let y=l+k;return W(v,{color:y===Q.cursor?H:void 0,children:[y===Q.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,Ne-34)),C(v,{color:"gray",children:zt(p)})]},p.path)}),l+14<Q.rows.length?C(v,{dimColor:!0,children:` \u2193 ${Q.rows.length-l-14} more`}):null,C(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ge?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Publish which article?"}),ge.rows.map((a,l)=>W(v,{color:l===ge.cursor?H:void 0,children:[l===ge.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Ne-30)),C(v,{color:"gray",children:a.label})]},a.path)),C(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),Tt.length===0?C(v,{color:"gray",children:"no articles match"}):Tt.map((a,l)=>W(v,{color:l===oe.cursor?H:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),Rt.length===0?C(v,{color:"gray",children:"no sessions match"}):Rt.map((a,l)=>W(v,{color:l===ne.cursor?H:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?H:"gray",children:a.firstPrompt.slice(0,Math.max(10,Ne-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):$n?C(Y,{flexDirection:"column",children:nt.map((a,l)=>W(v,{color:l===ot?H:void 0,children:[l===ot?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===ot?H:"gray",children:a.name==="model"&&ho?`${a.desc} (currently ${ho})`:a.desc})]},a.name))}):!R&&et.length>0?W(Y,{flexDirection:"column",paddingX:1,children:[et.map((a,l)=>W(v,{children:[C(v,{color:H,children:l+1})," ",a.slice(0,Math.max(20,Ne-5))]},a)),C(v,{dimColor:!0,children:`press 1-${et.length} to fill the command \xB7 esc dismiss`})]}):C(Y,{paddingX:1,children:W(v,{color:"gray",children:[xn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${ho??"model n/a"}${t.kind==="api"?kn>0?` \xB7 ${Oe(kn)}`:"":hn>0?` \xB7 ${Yt(hn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}B();import{jsx as cc}from"react/jsx-runtime";async function Ps(e=process.cwd(),t="auto",o,n="0.0.0"){let r=ac(t);r.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=lc(cc(Ss,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
81
- `),process.exit(0)}var fc=dc(import.meta.url),Ts=fc("../package.json"),xe=new uc;function un(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}xe.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{un(e),await Nt({profile:e.profile,manual:e.manual})});xe.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{un(e),await Un({profile:e.profile})});xe.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{un(e),await On({profile:e.profile})});var dn=xe.command("profile").description("Manage connection profiles for the current repo");dn.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Lt(process.cwd()));dn.command("use <name>").description("Set the default profile for this repo").action(e=>Ot(process.cwd(),e));dn.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ut(process.cwd(),e));var Rs=xe.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Rs.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Je(process.cwd(),e.profile));Rs.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await In(process.cwd(),e,t.profile)});xe.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Fn());xe.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(Jt(),Ir)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});xe.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(Ts.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(pc.includes(e.auth)||(console.error(`\u2717 Invalid --auth mode: ${e.auth}`),console.error("Run: d360-writer --auth auto | api | subscription"),process.exit(2)),e.prompt){await Bn(e.cwd,e.prompt,e.auth,e.resume,e.profile,e.yes);return}e.resume&&(console.error("\u2717 --resume requires --prompt. In the interactive REPL, use /resume instead."),process.exit(2)),e.classic||!process.stdin.isTTY?await Qr(e.cwd,e.auth,e.profile):await Ps(e.cwd,e.auth,e.profile,Ts.version)});xe.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
75
+ Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;u({kind:"note",tone:"info",text:O+Ho(I,R,L).join(`
76
+ `)+re}),J([T]);return}tt.current=Date.now(),yt(0),q(!0);let b=new AbortController;_e.current=b,ce({total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Converting ${E.length} articles across ${I.length} partitions (\u2264${L} agents at once) on ${_.model}\u2026 (esc to stop)`});try{for await(let R of $s({cwd:e,partitions:I,promptFor:Bo,concurrency:L,profileName:s,allowProdWrites:$t.current,model:_.model,signal:b.signal}))if(R.type==="partition_status")R.status==="running"?(ce(O=>O&&{...O,active:[...O.active,R.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${R.label} \u2014 converting\u2026`})):(ce(O=>O&&{...O,active:O.active.filter(re=>re!==R.label),done:O.done+1,lastAt:Date.now()}),u({kind:"note",tone:R.status==="done"?"ok":"error",text:` ${R.status==="done"?"\u2713":"\u2717"} ${R.label}`}));else if(R.type==="partition_event")ce(O=>{if(!O)return O;let re={...O,lastAt:Date.now()};return R.event.type==="tool"?re.tools=O.tools+1:R.event.type==="text"&&(re.chars=O.chars+R.event.delta.length),re});else if(R.type==="run_done"){Tt(ye=>ye+R.totalCostUsd),St(ye=>ye+R.results.reduce((bo,vo)=>bo+vo.outputTokens,0));let O=R.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";u({kind:"note",tone:R.aborted?"warn":R.ok?"ok":"warn",text:O+qo(R.results,I,re).join(`
77
+ `)})}}catch(R){u({kind:"note",tone:"error",text:`Convert run failed: ${R.message}`})}finally{_e.current=null,ce(null),q(!1)}return}case"sync":{let p=(m[0]??"status").toLowerCase();try{if(p==="status"){u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let k=await an({cwd:e,profileName:s});u({kind:"note",tone:"info",text:Xt(k).join(`
78
+ `)});return}if(p==="pull"){let k=m[1];if(!k){u({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(k==="--all"){if(u({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await an({cwd:e,profileName:s})).entries.filter(L=>L.status==="remote-ahead"&&L.path).map(L=>L.path),y.length===0){u({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else y=[k.replace(/\\/g,"/")];let E=[];for(let I of y)E.push(await Ja({cwd:e,profileName:s,relPath:I}));be.current=E,At();return}u({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(k){u({kind:"note",tone:"error",text:`Sync failed: ${k.message}`})}return}case"scope":{if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Ka(e);if(p.length===0){u({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ye({cursor:0,rows:p.map(k=>({...k,checked:k.recommended}))});return}case"audit":case"capture-setup":{let k=await(i==="audit"?zt:mt)(m,void 0);if(k.kind==="forward-to-agent"&&k.prompt&&await ke(k.prompt,{display:k.display,echoDisplay:!0}),i==="capture-setup"){let y=Qo(e);y&&u({kind:"note",tone:"info",text:y.join(`
79
+ `)})}return}case"screenshot":{let p=Vo(m);if(p.mode==="list"){if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}u({kind:"note",tone:"info",text:Ko(Jo(e,p.scope),p.scope).join(`
80
+ `)});return}if(p.mode==="single"){let b=await Kt(m);b.kind==="forward-to-agent"&&b.prompt&&await ke(b.prompt,{display:b.display,echoDisplay:!0});return}if(!se(e)){u({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let k=p.mode==="scope"?p.scope:void 0,y=oc(e,{scope:k}).map(b=>b.id);if(y.length===0){u({kind:"note",tone:"info",text:k?`No screenshot placeholders under ${k}.`:"No screenshot placeholders found in the docs."});return}let E=6,I=tc(y,3),L=bs(e,"standard");tt.current=Date.now(),yt(0),q(!0);let T=new AbortController;_e.current=T,ce({verb:"Authoring",total:I.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),u({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${I.length} partition${I.length===1?"":"s"} (\u2264${E} agents at once) on ${L.model}${L.forced?" (forced)":""}\u2026 (esc to stop)`});let _=!1;try{for await(let b of $s({cwd:e,partitions:I,promptFor:Wr,concurrency:E,profileName:s,allowProdWrites:$t.current,model:L.model,signal:T.signal}))if(b.type==="partition_status")b.status==="running"?(ce(R=>R&&{...R,active:[...R.active,b.label],lastAt:Date.now()}),u({kind:"note",tone:"info",text:` \u25B8 ${b.label} \u2014 authoring\u2026`})):(ce(R=>R&&{...R,active:R.active.filter(O=>O!==b.label),done:R.done+1,lastAt:Date.now()}),u({kind:"note",tone:b.status==="done"?"ok":"error",text:` ${b.status==="done"?"\u2713":"\u2717"} ${b.label}`}));else if(b.type==="partition_event")ce(R=>{if(!R)return R;let O={...R,lastAt:Date.now()};return b.event.type==="tool"?O.tools=R.tools+1:b.event.type==="text"&&(O.chars=R.chars+b.event.delta.length),O});else if(b.type==="run_done"){Tt(ye=>ye+b.totalCostUsd),St(ye=>ye+b.results.reduce((bo,vo)=>bo+vo.outputTokens,0));let R=b.results.filter(ye=>ye.ok).length,O=b.aborted?"Stopped. ":"";_=!b.aborted&&p.setup;let re=b.aborted?"":_?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";u({kind:"note",tone:b.aborted?"warn":b.ok?"ok":"warn",text:`${O}Authored specs \u2014 ${R}/${b.results.length} batches ok.${re}`})}}catch(b){u({kind:"note",tone:"error",text:`Spec authoring failed: ${b.message}`})}finally{_e.current=null,ce(null),q(!1)}if(_){let b=await mt();b.kind==="forward-to-agent"&&b.prompt&&await ke(b.prompt,{display:b.display,echoDisplay:!0});let R=Qo(e);R&&u({kind:"note",tone:"info",text:R.join(`
81
+ `)})}return}case"reset":{let p=Zo(e);if(p.length===0){u({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}mo.current={repoName:ao(e),targets:p},u({kind:"note",tone:"warn",text:tn(e,p).join(`
82
+ `)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,At,u,he,ke]),jt=G(a=>{let l=(a??A).trim();if($(""),xt(null),We(0),l.startsWith("/")){let m=k=>k.trim().split(/\s+/).slice(1).join(" "),p=m(l);J(k=>k.filter(y=>y.trim()!==l&&!(p&&m(y)===p)))}if(!l||Tn(l)||Sn(l))return;fo.current.push(l);let i=us(l,fn.current);l.startsWith("/")?Pn(i):ke(i,{display:l})},[A,Tn,Sn,Pn,ke]),Rn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=ls(a);if(as(l)){let i=cs(++Is.current,l);fn.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),Ve=Math.max(10,Ne-6),An=lo(()=>so(g.text,Ve),[g.text,Ve]),$o=G(a=>x(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Et=G(a=>x(l=>({...l,pos:ps(so(l.text,Ve),l.pos,a)})),[Ve]),rt=G(a=>x(l=>({...l,pos:fs(so(l.text,Ve),l.pos,a)})),[Ve]),Ns=["\x1B[H","\x1B[1~","\x1BOH"],Ls=["\x1B[F","\x1B[4~","\x1BOF"],jn=G((a,l)=>l.leftArrow?($o(-1),!0):l.rightArrow?($o(1),!0):a&&Ns.includes(a)?(rt("start"),!0):a&&Ls.includes(a)?(rt("end"),!0):l.ctrl&&a==="a"?(rt("start"),!0):l.ctrl&&a==="e"?(rt("end"),!0):!1,[$o,rt]);return Da((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!z){if(M){if(l.escape){if(_e.current){_e.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),_e.current.abort());return}bt.current||(bt.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),te.current?.interrupt());return}if(l.return){let i=A.trim();if(!i)return;Ie.current.push(i),go([...Ie.current]),$("");return}if(jn(a,l))return;if(l.upArrow){Et(-1);return}if(l.downArrow){Et(1);return}if(l.backspace||l.delete){P();return}a&&!l.ctrl&&!l.meta&&Rn(a);return}if(je){if(l.upArrow){Ee(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ee(i=>i&&{...i,cursor:Math.min(de.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=de.length){Ee(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=de[je.cursor];Ee(null);let{lines:m,changed:p,effective:k}=pt(e,i.value??"default");for(let y of m)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(xn(y=>y+1),te.current?.setModel(k));return}if(a==="s"){let i=de[je.cursor];Ee(null),te.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Ee(null);return}return}if(me){if(l.upArrow){qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=me.rows.length){qe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=me.rows[me.cursor];qe(null),wo(i.name,l.return===!0);return}if(l.escape){qe(null);return}return}if(ve){if(l.upArrow){ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ve.rows.length){ze(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ve.rows[ve.cursor].name;if(ze(null),!Jn(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let k=Ue(i);p=!!k&&!(Re(k)&&!k.refreshToken)}catch{}p?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),J(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),J(["/login"])),$e(k=>k+1);return}if(l.escape){ze(null);return}return}if(K){if(l.upArrow){Ge(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ge(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=K.rows.length){Ge(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=K.rows[K.cursor],{profile:m,projectId:p}=K;Ge(null),Cn(m,p,i);return}if(l.escape){Ge(null);return}return}if(Q){if(l.upArrow){Ye(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ye(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Ye(i=>i&&{...i,rows:i.rows.map((m,p)=>p===i.cursor?{...m,checked:!m.checked}:m)});return}if(l.return){let i=Q.rows.filter(m=>m.checked).map(m=>m.path);if(Ye(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}Ao(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let m of i)u({kind:"note",tone:"info",text:` ${m}`});u({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Ye(null);return}return}if(ge){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)<=ge.rows.length){Xe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge.rows[ge.cursor],m=ge.rows.filter(p=>p.path!=="--all").map(p=>p.path);Xe(null),ke(i.path==="--all"?ct(m):qt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Xe(null);return}return}if(oe){if(l.escape){De(null);return}if(l.upArrow){De(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){De(i=>i&&{...i,cursor:Math.min(Math.max(0,Pt.length-1),i.cursor+1)});return}if(l.return){let i=Pt[oe.cursor];if(i){De(null);try{u({kind:"preview",name:ao(i),text:Cs(ks(ht(e,i),"utf8"))})}catch(m){u({kind:"note",tone:"error",text:`Could not read ${i}: ${m.message}`})}}return}if(l.backspace||l.delete){De(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){De(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){Me(null);return}if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(Math.max(0,Rt.length-1),i.cursor+1)});return}if(l.return){let i=Rt[ne.cursor];if(i){Me(null),he(i.uuid),fe.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},He(i.name),ln(i.uuid),Be.current=[];let m=sn(e,i.uuid);for(let p of m)u(p);u({kind:"note",tone:"ok",text:m.length?`Resumed "${i.name}" \u2014 restored ${m.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(l.backspace||l.delete){Me(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Me(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!A&&le){$(le),ae(null),kt.current++;return}if(!A&&et.length>0&&a&&/^[1-9]$/.test(a)){let i=et[Number(a)-1];if(i){$(i);return}}if(!jn(a,l)){if(bn){if(l.upArrow){We(i=>Math.max(0,i-1));return}if(l.downArrow){We(i=>Math.min(nt.length-1,i+1));return}if(l.tab){$("/"+(nt[ot]?.name??"")+" "),We(0);return}if(l.return){let i=nt[ot];if(i){let m=A.trim().slice(1).split(/\s+/).slice(1).join(" ");if(zn(i.usage)&&!m){$("/"+i.name+" "),We(0);return}jt("/"+i.name+(m?" "+m:""));return}}}else{if(l.upArrow){if(A!==""&&Fe===null){Et(-1);return}let i=fo.current;if(!i.length)return;let m=Fe===null?i.length-1:Math.max(0,Fe-1);xt(m),$(i[m]??"");return}if(l.downArrow){if(A!==""&&Fe===null){Et(1);return}let i=fo.current;if(Fe===null)return;let m=Fe+1;m>=i.length?(xt(null),$("")):(xt(m),$(i[m]??""));return}}if(l.return){jt();return}if(l.backspace||l.delete){P();return}if(l.escape){$(""),We(0),ae(null),J([]);return}a&&!l.ctrl&&!l.meta&&Rn(a)}}}),Ae(()=>{if(M||z)return;let a=Ie.current.shift();a!==void 0&&(go([...Ie.current]),jt(a))},[M,z,jt]),W(Y,{flexDirection:"column",width:Ne,children:[yo!==null&&W(Y,{marginTop:1,flexDirection:"column",children:[yo.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:yo.text})]}),M&&(hn?C(mc,{p:hn}):C(pc,{startTime:tt.current,chars:Ds})),C(Y,{borderStyle:"round",borderColor:$n.prod?"yellow":H,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:A?An.map((a,l)=>{let i=g.text.slice(a.start,a.end),m=l===io(An,g.pos),p=Math.min(g.pos,a.end)-a.start;return W(v,{children:[C(v,{color:H,children:l===0?"> ":" "}),m?W(cn,{children:[i.slice(0,p),C(co,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):W(v,{children:[C(v,{color:H,children:"> "}),le&&!M?W(cn,{children:[C(co,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):ko.isSetup||!Es?W(cn,{children:[C(co,{ch:ko.text[0],dim:!0}),C(v,{color:"gray",children:ko.text.slice(1)})]}):C(co,{ch:" "})]})}),gn.length>0&&C(Y,{flexDirection:"column",paddingX:1,children:gn.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),je?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Select model"}),C(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),de.map((a,l)=>W(v,{color:l===je.cursor?H:void 0,children:[l===je.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===je.current?" \u2714":""}`.padEnd(16),C(v,{color:"gray",children:a.desc})]},a.label)),C(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):me?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>W(v,{color:l===me.cursor?H:void 0,children:[l===me.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===me.current?" \u2714":""}`.padEnd(20),C(v,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):ve?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Pick your Document360 environment"}),ve.rows.map((a,l)=>W(v,{color:l===ve.cursor?H:void 0,children:[l===ve.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(v,{color:"gray",children:a.apiUrl})]},a.name)),C(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):K?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Switch workspace"}),C(v,{dimColor:!0,children:`environment ${K.environment} \xB7 project ${K.projectId.slice(0,8)}\u2026`}),K.rows.map((a,l)=>W(v,{color:l===K.cursor?H:void 0,children:[l===K.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===K.current?" \u2714":""}`.padEnd(30),C(v,{color:"gray",children:a.type??""})]},a.id)),C(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):Q?(()=>{let l=Math.min(Math.max(0,Q.cursor-Math.floor(7)),Math.max(0,Q.rows.length-14)),i=Q.rows.slice(l,l+14),m=Q.rows.filter(p=>p.checked).length;return W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Which folders back the user docs? (${m} selected of ${Q.rows.length})`}),l>0?C(v,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,k)=>{let y=l+k;return W(v,{color:y===Q.cursor?H:void 0,children:[y===Q.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,Ne-34)),C(v,{color:"gray",children:Gt(p)})]},p.path)}),l+14<Q.rows.length?C(v,{dimColor:!0,children:` \u2193 ${Q.rows.length-l-14} more`}):null,C(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ge?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:"Publish which article?"}),ge.rows.map((a,l)=>W(v,{color:l===ge.cursor?H:void 0,children:[l===ge.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Ne-30)),C(v,{color:"gray",children:a.label})]},a.path)),C(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),Pt.length===0?C(v,{color:"gray",children:"no articles match"}):Pt.map((a,l)=>W(v,{color:l===oe.cursor?H:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?W(Y,{flexDirection:"column",paddingX:1,children:[C(v,{color:H,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),Rt.length===0?C(v,{color:"gray",children:"no sessions match"}):Rt.map((a,l)=>W(v,{color:l===ne.cursor?H:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?H:"gray",children:a.firstPrompt.slice(0,Math.max(10,Ne-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):bn?C(Y,{flexDirection:"column",children:nt.map((a,l)=>W(v,{color:l===ot?H:void 0,children:[l===ot?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===ot?H:"gray",children:a.name==="model"&&ho?`${a.desc} (currently ${ho})`:a.desc})]},a.name))}):!A&&et.length>0?W(Y,{flexDirection:"column",paddingX:1,children:[et.map((a,l)=>W(v,{children:[C(v,{color:H,children:l+1})," ",a.slice(0,Math.max(20,Ne-5))]},a)),C(v,{dimColor:!0,children:`press 1-${et.length} to fill the command \xB7 esc dismiss`})]}):C(Y,{paddingX:1,children:W(v,{color:"gray",children:[$n.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${ho??"model n/a"}${t.kind==="api"?yn>0?` \xB7 ${Oe(yn)}`:"":kn>0?` \xB7 ${Yt(kn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}B();import{jsx as kc}from"react/jsx-runtime";async function Rs(e=process.cwd(),t="auto",o,n="0.0.0"){let r=hc(t);r.kind==="none"&&(console.error(""),console.error(w("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=gc(kc(Ps,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
83
+ `),process.exit(0)}var $c=xc(import.meta.url),As=$c("../package.json"),we=new yc;function dn(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}we.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{dn(e),await Nt({profile:e.profile,manual:e.manual})});we.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{dn(e),await Wn({profile:e.profile})});we.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{dn(e),await Un({profile:e.profile})});var pn=we.command("profile").description("Manage connection profiles for the current repo");pn.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Lt(process.cwd()));pn.command("use <name>").description("Set the default profile for this repo").action(e=>Ot(process.cwd(),e));pn.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ut(process.cwd(),e));var js=we.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");js.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Je(process.cwd(),e.profile));js.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await _n(process.cwd(),e,t.profile)});we.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Bn());we.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(Jt(),_r)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});we.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(As.version,"-v, --version").option("-p, --prompt <text>","One-shot prompt mode (non-interactive)").option("-r, --resume <name>","Resume a saved session (with --prompt; use /resume inside the REPL)").option("-C, --cwd <dir>","Working directory",process.cwd()).option("--auth <mode>","Auth mode: auto | api | subscription","auto").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--yes","Skip the production write confirmation (for one-shot/CI)").option("--classic","Use the plain readline REPL instead of the Ink TUI").action(async e=>{if(wc.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 Hn(e.cwd,e.prompt,e.auth,e.resume,e.profile,e.yes);return}e.resume&&(console.error("\u2717 --resume requires --prompt. In the interactive REPL, use /resume instead."),process.exit(2)),e.classic||!process.stdin.isTTY?await es(e.cwd,e.auth,e.profile):await Rs(e.cwd,e.auth,e.profile,As.version)});we.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -2,3 +2,11 @@ import type { SlashCommandResult } from './index.js';
2
2
  /** `/capture-setup` — synthesize the "stage this data" checklist from every article's
3
3
  SCREENSHOT prerequisites. Forwards to the agent's capture-setup-checklist skill. */
4
4
  export declare function captureSetupCommand(): Promise<SlashCommandResult>;
5
+ /**
6
+ * After capture-setup writes `CAPTURE-TESTID-REQUESTS.json`, build a paste-ready hand-off the user
7
+ * runs in THEIR OWN code repo (a different terminal) — d360-writer never edits product source, so
8
+ * the test-ids are *requested*, not added. The prompt body is bracketed by `---` rules so the user
9
+ * can copy exactly the part to paste into Claude Code. Returns null when there are no requests
10
+ * (nothing to hand off) or the file is missing/unreadable.
11
+ */
12
+ export declare function renderTestidHandoff(cwd: string): string[] | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.31",
3
+ "version": "0.4.33",
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.19",
37
+ "document360-engine": "^0.2.21",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",