document360-writer 0.4.9 → 0.4.11

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,68 +1,68 @@
1
1
  #!/usr/bin/env node
2
- var zr=Object.defineProperty;var Vn=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var Gr=(e,t)=>{for(var n in t)zr(e,n,{get:t[n],enumerable:!0})};import we from"picocolors";var W,Xr,Yr,Vr,Jn,Ye,c,x,E,S,y,z,ne,N=Vn(()=>{"use strict";W="#7f56d9",[Xr,Yr,Vr]=[127,86,217],Jn=e=>we.isColorSupported?`\x1B[38;2;${Xr};${Yr};${Vr}m${e}\x1B[39m`:e,Ye=e=>we.bold(Jn(e)),c=e=>we.dim(e),x=e=>we.red(e),E=e=>we.yellow(e),S=e=>we.green(e),y=Jn,z=e=>we.gray(e),ne=e=>we.bold(e)});var Bo={};Gr(Bo,{doctorCommand:()=>et,renderDoctorChecks:()=>Fo,runDoctorChecks:()=>Wo});import{existsSync as li}from"node:fs";import{d360GetAll as ai,getAccessToken as ci,isExpired as mn,loadProfileMap as ui,loadTokens as di,packageSkillsDir as Uo,projectConfigPath as pi,readProjectConfig as mi,resolveActiveProfile as fi,resolveAuth as gi,resolveModelSetting as hi}from"document360-engine";async function Wo(e){let t=[],n=Number(process.versions.node.split(".")[0]);t.push(n>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let o=gi("auto");t.push(o.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${o.kind==="api"?"API key":"subscription"}`});let r=hi(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let i=mi(e);if(!i)return t.push({level:"fail",label:`No ${pi(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${i.projectId}`});let u=(i.docsDir??"user-docs").replace(/\/+$/,"");t.push(i.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${u}/, markdown, capture specs, and d360 config`});let p=i.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Docs scope: ${p.length} folder(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Docs scope not set (authoritativeSourceFiles empty)",fix:"Run: /scope to choose which folders back the docs"});let k;try{k=fi(e),t.push({level:"ok",label:`Profile: ${k.name} (${k.connection.name})${k.production?" \u26A0 PRODUCTION":""}`})}catch(j){return t.push({level:"fail",label:`Profile config: ${j.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let g=di(k.name);g?mn(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):mn(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 ${k.name})`}),t.push(k.project.workspaceId?{level:"ok",label:`Workspace: ${k.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let v=ui(e,k.name);if(v?v.projectId&&k.project.projectId&&v.projectId!==k.project.projectId?t.push({level:"fail",label:`Category map projectId (${v.projectId}) \u2260 profile projectId (${k.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(v.articles).length} articles, ${Object.keys(v.categories).length} categories`}):t.push({level:"warn",label:`No d360-category-map.json section for "${k.name}" (created on first /publish)`}),t.push(li(Uo())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Uo()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!mn(g)||g.refreshToken)){let j={profile:k.name,connection:k.connection};try{await ci(j);let $=await ai(j,"/v3/projects");t.push({level:"ok",label:`API reachable (${k.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 Fo(e){let t=[""];for(let r of e){let i=r.level==="ok"?S("\u2713"):r.level==="warn"?E("\u26A0"):x("\u2717");t.push(` ${i} ${r.label}${r.detail?c(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${c("fix:")} ${y(r.fix)}`)}let n=e.filter(r=>r.level==="fail").length,o=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(n===0?S(`\u2713 ${o===0?"All checks passed":`Healthy (${o} warning${o===1?"":"s"})`}`):x(`\u2717 ${n} problem${n===1?"":"s"} found`)),t.push(""),t}async function et(e,t){console.log(c("Running checks\u2026"));for(let n of Fo(await Wo(t.cwd)))console.log(n);return{kind:"continue"}}var It=Vn(()=>{"use strict";N()});import{Command as ql}from"commander";import{createRequire as zl}from"node:module";import{AUTH_MODES as Gl}from"document360-engine";import{input as ss}from"@inquirer/prompts";import{loginPkce as is,refreshTokens as ls,toStoredTokens as eo,clearTokens as as,decodeJwtClaims as Zn,isExpired as cs,loadTokens as us,saveTokens as to,resolveActiveProfile as wt,setProfileProject as ds,readProjectConfig as ps}from"document360-engine";N();import{select as Jr}from"@inquirer/prompts";import{resolveActiveProfile as Kr,setProfileProject as Qr,resolveProjectId as Zr,listWorkspaces as es}from"document360-engine";async function ht(e,t){let n=Kr(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??Zr(o);return{workspaces:await es(o,r),projectId:r,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var Kn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function nn(e,t){let n=t.toLowerCase();return e.find(o=>(o.name??"").toLowerCase()===n)??e.find(o=>(o.name??"").toLowerCase().startsWith(n))??e.find(o=>o.id.startsWith(t))}function kt(e,t,n,o){Qr(e,t,{projectId:n,workspaceId:o})}async function Qn(e,t,n){let o;try{o=await ht(e,n)}catch(i){return console.log(x(`Could not list workspaces: ${i.message}`)),1}let r=nn(o.workspaces,t);return r?(kt(e,o.profile,o.projectId,r.id),console.log(S(`\u2713 Workspace set to "${r.name??r.id}" for profile "${o.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${o.workspaces.map(i=>i.name??i.id).join(", ")}`)),1)}async function Fe(e,t){let n;try{n=await ht(e,t)}catch(g){console.log(x(`Could not list workspaces: ${g.message}`));return}let{workspaces:o,projectId:r,profile:i,current:u}=n;if(o.length===0){console.log(c("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let g of o)console.log(` ${g.id===u?y("\u25CF"):" "} ${Kn(g)} ${c(g.id)}`);console.log(c("Run: d360-writer workspace use <name>"));return}let p=await Jr({message:"Select the Document360 workspace for this repo:",choices:o.map(g=>({name:`${Kn(g)}${g.id===u?" (current)":""}`,value:g.id}))});kt(e,i,r,p);let k=o.find(g=>g.id===p);console.log(S(`\u2713 Workspace set to "${k?.name??p}" for profile "${i}".`))}N();import ts from"picocolors";function ns(e=process.env){return e.FORCE_HYPERLINK==="0"||!ts.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 os(e,t=e,n){return ns(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var rs=/https?:\/\/[^\s\x1b]+/g;function Ve(e,t){return e.replace(rs,n=>os(n,n,t))}function no(e){return{...Zn(e.idToken)??{},...Zn(e.accessToken)??{}}}function yt(e){let t=no(e),n=t.email??t.preferred_username??t.sub??"unknown",o=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${n}${o}`}async function xt(e){let t=wt(process.cwd(),e.profile),n=t.connection;console.log(c(`Profile "${t.name}" \u2192 ${n.name} (${n.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let o=await is(n,{manual:e.manual,promptForRedirect:i=>ss({message:i})},i=>console.log(Ve(i))),r=eo(t.name,o);if(to(r),on(r,t.name,i=>console.log(c(i))),console.log(""),console.log(S(`\u2713 Logged in to "${t.name}" as ${yt(r)}`)),console.log(c(` access token expires: ${r.expiresAt}`)),console.log(c(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{ps(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Fe(process.cwd(),t.name))}catch{}}function on(e,t,n){let r=no(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(wt(process.cwd(),t).project.projectId)return;ds(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function oo(e){let t=wt(process.cwd(),e.profile),n=t.connection,o=us(t.name);if(!o){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(c(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${y(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${yt(o)}`),cs(o))if(o.refreshToken)try{let r=eo(t.name,await ls(n,o.refreshToken));to(r),console.log(S(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(E(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(c(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(E("Session expired (no refresh token).")),console.log(c(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(c(` expires: ${o.expiresAt}`))}async function ro(e){let t=wt(process.cwd(),e.profile);as(t.name)?console.log(S(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(c(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}N();import{readProjectConfig as so,writeProjectConfig as ms,resolveActiveProfile as fs,loadTokens as gs,isExpired as hs}from"document360-engine";function $t(e){let t=so(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[n,o]of Object.entries(t.profiles)){let r=n===t.defaultProfile?y("\u25CF "):" ",i=o.production?E(" \u26A0 PRODUCTION"):"",u=o.connection.environment??"(inline)",p=gs(n),k=p?hs(p)&&!p.refreshToken?E("expired"):c("logged in"):c("not logged in");console.log(`${r}${y(n)} \u2192 ${u}${i} [${k}]`)}console.log(""),console.log(c("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function bt(e,t){let n=so(e);if(!n?.profiles?.[t]){let r=n?.profiles?Object.keys(n.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}n.defaultProfile=t,ms(n,e);let o=n.profiles[t].production?E(" \u26A0 PRODUCTION"):"";console.log(S(`\u2713 Default profile is now "${t}"${o}`))}function vt(e,t){try{let n=fs(e,t);console.log(""),console.log(`Profile ${y(n.name)}${n.production?E(" \u26A0 PRODUCTION"):""}`),console.log(c(` api: ${n.connection.apiUrl}`)),console.log(c(` identity: ${n.connection.authorizationUrl}`)),console.log(c(` clientId: ${n.connection.clientId}`)),console.log(c(` scopes: ${n.connection.scopes.join(" ")}`)),console.log(c(` project: ${n.project.projectId??"(set at login)"}`)),console.log(c(` workspace:${n.project.workspaceId?" "+n.project.workspaceId:" (none)"}`)),console.log("")}catch(n){console.log(x(n.message)),process.exitCode=1}}N();import{existsSync as ks,readdirSync as ws,statSync as ys}from"node:fs";import{join as xs}from"node:path";import{apiLogDir as $s}from"document360-engine";function io(){let e=$s();if(console.log(""),console.log(`Document360 API logs: ${y(e)}`),!ks(e)){console.log(c(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ws(e).filter(n=>n.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(c(" No logs yet \u2014 they appear after the first Document360 API call."));for(let n of t.slice(0,14)){let o=(ys(xs(e,n)).size/1024).toFixed(1);console.log(` ${n} ${c(`${o} KB`)}`)}console.log(""),console.log(c("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(c("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}N();import{createSession as bs,resolveAuth as vs,findByName as Cs,slugify as Ps,touchSession as Ts,upsertSession as Ss,resolveActiveProfile as Rs}from"document360-engine";async function lo(e,t,n,o,r,i){let u=vs(n);u.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),process.exit(2)),u.kind==="subscription"&&console.error(c("Using your Claude subscription (no API key set)."));let p=null;try{p=Rs(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}p.production&&(console.error(E(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),i||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(c(" --yes given \u2014 proceeding against production.")));let k;if(o){let $=Cs(e,o);$||(console.error(x(`No saved session matches "${o}" in this repo.`)),console.error(c("List sessions inside the REPL with /resume.")),process.exit(2)),k=$.uuid,console.error(c(`Resuming "${$.name}"`))}let g=bs({cwd:e,resume:k,profileName:r,allowProdWrites:i===!0}),v=k??null,j=1;for await(let $ of g.send(t))switch($.type){case"session":if(!v){v=$.sessionId;let R=new Date().toISOString();Ss({uuid:v,name:Ps(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:R,updatedAt:R})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(z(` \u2699 ${$.name}`));break;case"result":j=$.ok?0:1,console.error(c(`(${$.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)}v&&Ts(v),process.stdout.write(`
3
- `),process.exit(j)}import{createInterface as Pi}from"node:readline/promises";import{createSession as hn,resolveAuth as Ti,getSession as Si,setTitle as Ri,slugify as ji,touchSession as Zo,upsertSession as Ai,generateTitle as Ii,resolveActiveProfile as er,resolveModelSetting as Di,readProjectConfig as Ei,decodeJwtClaims as Ko,isExpired as Mi,loadTokens as _i}from"document360-engine";N();async function rn(){let e=["",ne("document360-writer \u2014 slash commands"),"",` ${y("/help")} ${c("Show this help")}`,` ${y("/init")} ${c("Pick a Document360 environment & scaffold .d360-writer.json")}`,` ${y("/mcp add <name> <type> <ref>")} ${c("Register an MCP server (stdio|http|sse; -H key:value for auth)")}`,` ${y("/mcp list")} ${c("Show registered MCP servers")}`,` ${y("/mcp remove <name>")} ${c("Unregister an MCP server")}`,` ${y("/publish [path|--all]")} ${c("Publish to Document360 (no arg: pick; --all: every candidate)")}`,` ${y("/sync")} ${c("Drift report: local docs vs Document360 (no tokens)")}`,` ${y("/sync pull <path>|--all")} ${c("Pull portal edits into local markdown (diff + confirm)")}`,` ${y("/scope")} ${c("Choose which repo folders back the docs (analyses + recommends)")}`,` ${y("/audit")} ${c("Gap analysis: code vs docs vs Document360 (incremental)")}`,` ${y("/screenshot <id>")} ${c("Emit a document360-capture-compatible spec")}`,` ${y("/resume [name]")} ${c("Resume a session (no arg: searchable picker)")}`,` ${y("/rename <name>")} ${c("Name the current session")}`,` ${y("/login")} ${c("Sign in to Document360 (browser) without leaving the session")}`,` ${y("/profile [name|add <name> [env]]")} ${c("Switch connection profile, or create one from a preset")}`,` ${y("/model [name|default]")} ${c("Show or set the Claude model (personal setting)")}`,` ${y("/doctor")} ${c("Health-check: auth, profile, workspace, map, API")}`,` ${y("/workspace [name]")} ${c("List or switch the Document360 workspace")}`,` ${y("/allow-prod")} ${c("Authorize writes to a production profile (this session)")}`,` ${y("/clear")} ${c("Reset conversation")}`,` ${y("/exit")} ${c("Quit")}`,"",c("Anything not starting with / is sent to the agent."),c("Reporting a problem? Run `d360-writer logs` from your shell for the API log files."),""];for(let t of e)console.log(t);return{kind:"continue"}}N();import{getSession as js}from"document360-engine";async function ao(e,t){let n=t.currentUuid(),o=n?js(n):void 0;return console.log(o?c(`
4
- (conversation reset \u2014 "${o.name}" is still available via /resume)
2
+ var Vr=Object.defineProperty;var tn=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var Xr=(e,t)=>{for(var o in t)Vr(e,o,{get:t[o],enumerable:!0})};import we from"picocolors";var W,Jr,Kr,Qr,on,Xe,c,x,E,T,y,G,oe,_=tn(()=>{"use strict";W="#7f56d9",[Jr,Kr,Qr]=[127,86,217],on=e=>we.isColorSupported?`\x1B[38;2;${Jr};${Kr};${Qr}m${e}\x1B[39m`:e,Xe=e=>we.bold(on(e)),c=e=>we.dim(e),x=e=>we.red(e),E=e=>we.yellow(e),T=e=>we.green(e),y=on,G=e=>we.gray(e),oe=e=>we.bold(e)});var or={};Xr(or,{doctorCommand:()=>ot,renderDoctorChecks:()=>tr,runDoctorChecks:()=>er});import{existsSync as Si}from"node:fs";import{d360GetAll as ji,getAccessToken as Ai,isExpired as Ro,loadProfileMap as Ii,loadTokens as Ei,packageSkillsDir as Zn,projectConfigPath as Di,readProjectConfig as Mi,resolveActiveProfile as _i,resolveAuth as Li,resolveModelSetting as Ni}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=Li("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 s=Ni(e);t.push({level:"ok",label:`Model: ${s.model??"Claude Code default"} (${s.source})`});let r=Mi(e);if(!r)return t.push({level:"fail",label:`No ${Di(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${r.projectId}`});let u=(r.docsDir??"user-docs").replace(/\/+$/,"");t.push(r.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${u}/, markdown, capture specs, and d360 config`});let p=r.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Docs scope: ${p.length} folder(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Docs scope not set (authoritativeSourceFiles empty)",fix:"Run: /scope to choose which folders back the docs"});let k;try{k=_i(e),t.push({level:"ok",label:`Profile: ${k.name} (${k.connection.name})${k.production?" \u26A0 PRODUCTION":""}`})}catch(j){return t.push({level:"fail",label:`Profile config: ${j.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let g=Ei(k.name);g?Ro(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Ro(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 ${k.name})`}),t.push(k.project.workspaceId?{level:"ok",label:`Workspace: ${k.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let v=Ii(e,k.name);if(v?v.projectId&&k.project.projectId&&v.projectId!==k.project.projectId?t.push({level:"fail",label:`Category map projectId (${v.projectId}) \u2260 profile projectId (${k.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(v.articles).length} articles, ${Object.keys(v.categories).length} categories`}):t.push({level:"warn",label:`No d360-category-map.json section for "${k.name}" (created on first /publish)`}),t.push(Si(Zn())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Zn()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!Ro(g)||g.refreshToken)){let j={profile:k.name,connection:k.connection};try{await Ai(j);let $=await ji(j,"/v3/projects");t.push({level:"ok",label:`API reachable (${k.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 tr(e){let t=[""];for(let s of e){let r=s.level==="ok"?T("\u2713"):s.level==="warn"?E("\u26A0"):x("\u2717");t.push(` ${r} ${s.label}${s.detail?c(` \u2014 ${s.detail}`):""}`),s.fix&&t.push(` ${c("fix:")} ${y(s.fix)}`)}let o=e.filter(s=>s.level==="fail").length,n=e.filter(s=>s.level==="warn").length;return t.push(""),t.push(o===0?T(`\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 ot(e,t){console.log(c("Running checks\u2026"));for(let o of tr(await er(t.cwd)))console.log(o);return{kind:"continue"}}var Et=tn(()=>{"use strict";_()});import{Command as Zl}from"commander";import{createRequire as ea}from"node:module";import{AUTH_MODES as ta}from"document360-engine";import{input as as}from"@inquirer/prompts";import{loginPkce as cs,refreshTokens as us,toStoredTokens as ln,clearTokens as ds,decodeJwtClaims as sn,isExpired as ps,loadTokens as ms,saveTokens as an,resolveActiveProfile as yt,setProfileProject as fs,readProjectConfig as gs}from"document360-engine";_();import{select as Zr}from"@inquirer/prompts";import{resolveActiveProfile as es,setProfileProject as ts,resolveProjectId as os,listWorkspaces as ns}from"document360-engine";async function kt(e,t){let o=es(e,t),n={profile:o.name,connection:o.connection},s=o.project.projectId??os(n);return{workspaces:await ns(n,s),projectId:s,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var nn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function no(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function wt(e,t,o,n){ts(e,t,{projectId:o,workspaceId:n})}async function rn(e,t,o){let n;try{n=await kt(e,o)}catch(r){return console.log(x(`Could not list workspaces: ${r.message}`)),1}let s=no(n.workspaces,t);return s?(wt(e,n.profile,n.projectId,s.id),console.log(T(`\u2713 Workspace set to "${s.name??s.id}" for profile "${n.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${n.workspaces.map(r=>r.name??r.id).join(", ")}`)),1)}async function Fe(e,t){let o;try{o=await kt(e,t)}catch(g){console.log(x(`Could not list workspaces: ${g.message}`));return}let{workspaces:n,projectId:s,profile:r,current:u}=o;if(n.length===0){console.log(c("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===u?y("\u25CF"):" "} ${nn(g)} ${c(g.id)}`);console.log(c("Run: d360-writer workspace use <name>"));return}let p=await Zr({message:"Select the Document360 workspace for this repo:",choices:n.map(g=>({name:`${nn(g)}${g.id===u?" (current)":""}`,value:g.id}))});wt(e,r,s,p);let k=n.find(g=>g.id===p);console.log(T(`\u2713 Workspace set to "${k?.name??p}" for profile "${r}".`))}_();import rs from"picocolors";function ss(e=process.env){return e.FORCE_HYPERLINK==="0"||!rs.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 is(e,t=e,o){return ss(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var ls=/https?:\/\/[^\s\x1b]+/g;function Je(e,t){return e.replace(ls,o=>is(o,o,t))}function cn(e){return{...sn(e.idToken)??{},...sn(e.accessToken)??{}}}function xt(e){let t=cn(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 $t(e){let t=yt(process.cwd(),e.profile),o=t.connection;console.log(c(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await cs(o,{manual:e.manual,promptForRedirect:r=>as({message:r})},r=>console.log(Je(r))),s=ln(t.name,n);if(an(s),ro(s,t.name,r=>console.log(c(r))),console.log(""),console.log(T(`\u2713 Logged in to "${t.name}" as ${xt(s)}`)),console.log(c(` access token expires: ${s.expiresAt}`)),console.log(c(` refresh token: ${s.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{gs(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Fe(process.cwd(),t.name))}catch{}}function ro(e,t,o){let s=cn(e).doc360_project_id;if(!(typeof s!="string"||!s))try{if(yt(process.cwd(),t).project.projectId)return;fs(process.cwd(),t,{projectId:s}),o(` Project ${s} written to profile "${t}".`)}catch{}}async function un(e){let t=yt(process.cwd(),e.profile),o=t.connection,n=ms(t.name);if(!n){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(c(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${y(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${xt(n)}`),ps(n))if(n.refreshToken)try{let s=ln(t.name,await us(o,n.refreshToken));an(s),console.log(T(`\u2713 Session refreshed \u2014 expires ${s.expiresAt}`))}catch(s){console.log(E(`Session expired and refresh failed (${s.message.slice(0,120)})`)),console.log(c(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(E("Session expired (no refresh token).")),console.log(c(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(c(` expires: ${n.expiresAt}`))}async function dn(e){let t=yt(process.cwd(),e.profile);ds(t.name)?console.log(T(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(c(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}_();import{readProjectConfig as pn,writeProjectConfig as hs,resolveActiveProfile as ks,loadTokens as ws,isExpired as ys}from"document360-engine";function bt(e){let t=pn(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 s=o===t.defaultProfile?y("\u25CF "):" ",r=n.production?E(" \u26A0 PRODUCTION"):"",u=n.connection.environment??"(inline)",p=ws(o),k=p?ys(p)&&!p.refreshToken?E("expired"):c("logged in"):c("not logged in");console.log(`${s}${y(o)} \u2192 ${u}${r} [${k}]`)}console.log(""),console.log(c("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function vt(e,t){let o=pn(e);if(!o?.profiles?.[t]){let s=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${s}`)),process.exitCode=1;return}o.defaultProfile=t,hs(o,e);let n=o.profiles[t].production?E(" \u26A0 PRODUCTION"):"";console.log(T(`\u2713 Default profile is now "${t}"${n}`))}function Ct(e,t){try{let o=ks(e,t);console.log(""),console.log(`Profile ${y(o.name)}${o.production?E(" \u26A0 PRODUCTION"):""}`),console.log(c(` api: ${o.connection.apiUrl}`)),console.log(c(` identity: ${o.connection.authorizationUrl}`)),console.log(c(` clientId: ${o.connection.clientId}`)),console.log(c(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(c(` project: ${o.project.projectId??"(set at login)"}`)),console.log(c(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(x(o.message)),process.exitCode=1}}_();import{existsSync as xs,readdirSync as $s,statSync as bs}from"node:fs";import{join as vs}from"node:path";import{apiLogDir as Cs}from"document360-engine";function mn(){let e=Cs();if(console.log(""),console.log(`Document360 API logs: ${y(e)}`),!xs(e)){console.log(c(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=$s(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(c(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(bs(vs(e,o)).size/1024).toFixed(1);console.log(` ${o} ${c(`${n} KB`)}`)}console.log(""),console.log(c("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(c("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}_();import{createSession as Ps,resolveAuth as Ts,findByName as Rs,slugify as Ss,touchSession as js,upsertSession as As,resolveActiveProfile as Is}from"document360-engine";async function fn(e,t,o,n,s,r){let u=Ts(o);u.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),process.exit(2)),u.kind==="subscription"&&console.error(c("Using your Claude subscription (no API key set)."));let p=null;try{p=Is(e,s)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}p.production&&(console.error(E(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),r||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(c(" --yes given \u2014 proceeding against production.")));let k;if(n){let $=Rs(e,n);$||(console.error(x(`No saved session matches "${n}" in this repo.`)),console.error(c("List sessions inside the REPL with /resume.")),process.exit(2)),k=$.uuid,console.error(c(`Resuming "${$.name}"`))}let g=Ps({cwd:e,resume:k,profileName:s,allowProdWrites:r===!0}),v=k??null,j=1;for await(let $ of g.send(t))switch($.type){case"session":if(!v){v=$.sessionId;let S=new Date().toISOString();As({uuid:v,name:Ss(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:S,updatedAt:S})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(G(` \u2699 ${$.name}`));break;case"result":j=$.ok?0:1,console.error(c(`(${$.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)}v&&js(v),process.stdout.write(`
3
+ `),process.exit(j)}import{createInterface as Gi}from"node:readline/promises";import{createSession as Ao,resolveAuth as Yi,getSession as Vi,setTitle as Xi,slugify as Ji,touchSession as mr,upsertSession as Ki,generateTitle as Qi,resolveActiveProfile as fr,resolveModelSetting as Zi,readProjectConfig as el,decodeJwtClaims as dr,isExpired as tl,loadTokens as ol}from"document360-engine";_();async function so(){let e=["",oe("document360-writer \u2014 slash commands"),"",` ${y("/help")} ${c("Show this help")}`,` ${y("/init")} ${c("Pick a Document360 environment & scaffold .d360-writer.json")}`,` ${y("/mcp add <name> <type> <ref>")} ${c("Register an MCP server (stdio|http|sse; -H key:value for auth)")}`,` ${y("/mcp list")} ${c("Show registered MCP servers")}`,` ${y("/mcp remove <name>")} ${c("Unregister an MCP server")}`,` ${y("/publish [path|--all]")} ${c("Publish to Document360 (no arg: pick; --all: every candidate)")}`,` ${y("/sync")} ${c("Drift report: local docs vs Document360 (no tokens)")}`,` ${y("/sync pull <path>|--all")} ${c("Pull portal edits into local markdown (diff + confirm)")}`,` ${y("/convert [--run]")} ${c("Convert all tracked articles to DFM across parallel agents (preview; --run to start)")}`,` ${y("/scope")} ${c("Choose which repo folders back the docs (analyses + recommends)")}`,` ${y("/audit")} ${c("Gap analysis: code vs docs vs Document360 (incremental)")}`,` ${y("/screenshot <id>")} ${c("Emit a document360-capture-compatible spec")}`,` ${y("/resume [name]")} ${c("Resume a session (no arg: searchable picker)")}`,` ${y("/rename <name>")} ${c("Name the current session")}`,` ${y("/login")} ${c("Sign in to Document360 (browser) without leaving the session")}`,` ${y("/profile [name|add <name> [env]]")} ${c("Switch connection profile, or create one from a preset")}`,` ${y("/model [name|default]")} ${c("Show or set the Claude model (personal setting)")}`,` ${y("/doctor")} ${c("Health-check: auth, profile, workspace, map, API")}`,` ${y("/workspace [name]")} ${c("List or switch the Document360 workspace")}`,` ${y("/allow-prod")} ${c("Authorize writes to a production profile (this session)")}`,` ${y("/clear")} ${c("Reset conversation")}`,` ${y("/exit")} ${c("Quit")}`,"",c("Anything not starting with / is sent to the agent."),c("Reporting a problem? Run `d360-writer logs` from your shell for the API log files."),""];for(let t of e)console.log(t);return{kind:"continue"}}_();import{getSession as Es}from"document360-engine";async function gn(e,t){let o=t.currentUuid(),n=o?Es(o):void 0;return console.log(n?c(`
4
+ (conversation reset \u2014 "${n.name}" is still available via /resume)
5
5
  `):c(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function sn(){return{kind:"exit"}}N();import{input as Je,confirm as As}from"@inquirer/prompts";import{basename as co,join as ln}from"node:path";import{existsSync as Ke,readFileSync as Is,readdirSync as Ds}from"node:fs";import{writeProjectConfig as uo,readProjectConfig as Es,projectConfigPath as po}from"document360-engine";function mo(e,t="berlin"){let n=po(e);if(Ke(n))return{created:!1,path:n,profileName:""};let o={projectId:go(e)??co(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:ho(e)};return uo(o,e),{created:!0,path:n,profileName:t}}async function fo(){let e=process.cwd(),t=po(e);if(Ke(t)&&!await As({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(c("init cancelled.")),{kind:"continue"};let n=go(e)??co(e),o=await Je({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await Je({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),i=await Je({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),u=await Je({message:"Default connection profile name:",default:"berlin"}),p=await Je({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),k={projectId:o,captureDir:r,outputDir:i,profiles:{[u]:{connection:{environment:p},production:!1}},defaultProfile:u,authoritativeSourceFiles:ho(e)},g=Es(e);return g?.terminologyGlossary&&(k.terminologyGlossary=g.terminologyGlossary),uo(k,e),console.log(""),console.log(S(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${y(`d360-writer login --profile ${u}`)} ${c("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function go(e){let t=ln(e,"package.json");if(!Ke(t))return null;try{return JSON.parse(Is(t,"utf8")).name??null}catch{return null}}function ho(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])Ke(ln(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=ln(e,n);Ke(o)&&!Ms(o)&&t.push(n)}return t}function Ms(e){try{return Ds(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}N();import{readMcpConfig as an,writeMcpConfig as ko}from"document360-engine";async function Ct(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(_s(),{kind:"continue"}):t==="add"?(Ls(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ns(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(c("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function _s(){let e=an(),t=Object.keys(e.servers);if(t.length===0){console.log(c(`
7
+ `)),{kind:"clear"}}async function io(){return{kind:"exit"}}_();import{input as Ke,confirm as Ds}from"@inquirer/prompts";import{basename as hn,join as lo}from"node:path";import{existsSync as Qe,readFileSync as Ms,readdirSync as _s}from"node:fs";import{writeProjectConfig as kn,readProjectConfig as Ls,projectConfigPath as wn}from"document360-engine";function yn(e,t="berlin"){let o=wn(e);if(Qe(o))return{created:!1,path:o,profileName:""};let n={projectId:$n(e)??hn(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:bn(e)};return kn(n,e),{created:!0,path:o,profileName:t}}async function xn(){let e=process.cwd(),t=wn(e);if(Qe(t)&&!await Ds({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(c("init cancelled.")),{kind:"continue"};let o=$n(e)??hn(e),n=await Ke({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),s=await Ke({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),r=await Ke({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),u=await Ke({message:"Default connection profile name:",default:"berlin"}),p=await Ke({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),k={projectId:n,captureDir:s,outputDir:r,profiles:{[u]:{connection:{environment:p},production:!1}},defaultProfile:u,authoritativeSourceFiles:bn(e)},g=Ls(e);return g?.terminologyGlossary&&(k.terminologyGlossary=g.terminologyGlossary),kn(k,e),console.log(""),console.log(T(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${y(`d360-writer login --profile ${u}`)} ${c("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function $n(e){let t=lo(e,"package.json");if(!Qe(t))return null;try{return JSON.parse(Ms(t,"utf8")).name??null}catch{return null}}function bn(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])Qe(lo(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=lo(e,o);Qe(n)&&!Ns(n)&&t.push(o)}return t}function Ns(e){try{return _s(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}_();import{readMcpConfig as ao,writeMcpConfig as vn}from"document360-engine";async function Pt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Us(),{kind:"continue"}):t==="add"?(Os(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ws(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(c("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Us(){let e=ao(),t=Object.keys(e.servers);if(t.length===0){console.log(c(`
8
8
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
9
- `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let n of t){let o=e.servers[n];if(o.type==="stdio")console.log(` ${y(n)} ${c("(stdio)")} ${o.command} ${(o.args??[]).join(" ")}`);else{let r=Object.keys(o.headers??{}),i=r.length>0?c(` [headers: ${r.join(", ")}]`):"";console.log(` ${y(n)} ${c(`(${o.type})`)} ${o.url}${i}`)}}console.log("")}function Ls(e){let[t,n,...o]=e;if(!t||!n||o.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(n!=="stdio"&&n!=="http"&&n!=="sse"){console.log(x(`Unknown transport: ${n}. Use stdio, http, or sse.`));return}let r=an(),i;if(n==="stdio")i={type:"stdio",command:o[0],args:o.slice(1)};else{let u={};for(let p=1;p<o.length;p++)if(o[p]==="-H"||o[p]==="--header"){let k=o[++p],g=k?.match(/^([^:=]+)[:=](.+)$/);if(!g){console.log(x(`-H expects key:value (no spaces). Got: ${k??"(nothing)"}`));return}u[g[1].trim()]=g[2].trim()}else{console.log(x(`Unexpected argument: ${o[p]}. After the URL, only -H key:value is allowed.`));return}i={type:n,url:o[0],headers:Object.keys(u).length>0?u:void 0}}r.servers[t]=i,ko(r),console.log(""),console.log(S(`\u2713 Registered "${t}" (${n})`)),console.log(E(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(c(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ns(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=an();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],ko(n),console.log(S(`\u2713 Removed "${t}".`)),console.log(c(" Run /clear to drop it from the current session immediately."))}N();import{select as Os}from"@inquirer/prompts";import{computeSyncStatus as wo}from"document360-engine";function Tt(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(`
10
- `)}var yo={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Pt(e){return e.filter(t=>t.path!==null&&t.status in yo).map(t=>({path:t.path,label:yo[t.status]}))}function Qe(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(`
11
- `)}async function xo(e,t){let n=e[0];if(n==="--all"){console.log(c("Checking what needs publishing\u2026"));try{let o=Pt((await wo({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(S("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:Qe(o.map(r=>r.path)),display:"/publish --all"}}catch(o){return console.log(x(`Could not compute sync status: ${o.message}`)),{kind:"continue"}}}if(!n){console.log(c("Checking what needs publishing\u2026"));let o;try{o=Pt((await wo({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(c("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(o.length===0)return console.log(S("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(c(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of o)console.log(` ${r.path} ${c(`(${r.label})`)}`);return console.log(c("Run: /publish <article-path>")),{kind:"continue"}}try{n=await t.withPausedInput(()=>Os({message:"Publish which article?",choices:[...o.length>1?[{name:`All ${o.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...o.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(c("Cancelled.")),{kind:"continue"}}if(n==="--all")return{kind:"forward-to-agent",prompt:Qe(o.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Tt(n),display:`/publish ${n}`}}async function St(){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(`
12
- `),display:"/audit"}}N();import{checkbox as Us}from"@inquirer/prompts";import{inventoryRepo as Ws,readProjectConfig as $o,writeProjectConfig as Fs}from"document360-engine";function cn(e,t){let n=$o(e);n&&(n.authoritativeSourceFiles=t,Fs(n,e))}function Rt(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 bo(e,t){let n=t?.cwd??process.cwd();if(!$o(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=Ws(n);if(o.length===0)return console.log(c('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(y("Recommended documentation scope (run /scope in the REPL to choose):"));for(let i of o)console.log(` ${i.recommended?"\u25C9":"\u25CB"} ${i.path} ${c(Rt(i))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Us({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(i=>({name:`${i.path} (${Rt(i)})`,value:i.path,checked:i.recommended})),pageSize:20}))}catch{return console.log(c("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(c("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};cn(n,r),console.log(S(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let i of r)console.log(` ${i}`);return console.log(""),{kind:"continue"}}N();import{confirm as Gs}from"@inquirer/prompts";import{applyPull as So,computeSyncStatus as Ro,planPull as Xs,D360AuthError as Ys}from"document360-engine";N();var vo=[{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:E,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:y,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:z,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:c,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:c,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:z,mark:"\xB7"}];function Bs(e){return e.path?e.path:`${e.title??"(untitled)"} ${z(`[${e.articleId}]`)}`}function jt(e){let t=[];for(let u of vo){let p=e.entries.filter(k=>k.status===u.status);if(p.length!==0){t.push(""),t.push(ne(u.header));for(let k of p)t.push(` ${u.paint(u.mark)} ${u.paint(Bs(k))}${k.detail?z(` (${k.detail})`):""}`)}}let n=e.counts["in-sync"]??0,o=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},i=o===n?S("\u2713 everything in sync"):[n>0?S(`\u2713 ${n} in sync`):null,...vo.map(u=>{let p=e.counts[u.status]??0;return p>0?`${p} ${r[u.status]}`:null})].filter(Boolean).join(z(" \xB7 "));return t.push(i+z(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Hs}from"diff";import Co from"picocolors";var Po=80,qs="\x1B[48;2;74;28;28m",zs="\x1B[48;2;24;66;24m",To="\x1B[49m",un=e=>String(e).padStart(5);function Ae(e,t,n){let o=$=>{let R=$.replace(/\r\n/g,`
13
- `);return R.endsWith(`
14
- `)||R===""?R:R+`
15
- `},r=o(e),i=o(t);if(r===i)return null;let u=Math.max(20,n-10),p=Hs("a","b",r,i,"","",{context:3}),k=0,g=0,v=[];p.hunks.forEach(($,R)=>{R>0&&v.push(Co.gray(" \u2026"));let L=$.oldStart,D=$.newStart;for(let B of $.lines){let F=B[0],Q=B.slice(1).slice(0,u);F==="-"?(g++,v.push(`${qs}${un(L++)} - ${Q}${To}`)):F==="+"?(k++,v.push(`${zs}${un(D++)} + ${Q}${To}`)):(v.push(Co.gray(un(D))+" "+Q),L++,D++)}});let j=v.slice(0,Po);return{added:k,removed:g,lines:j,hidden:Math.max(0,v.length-Po)}}async function jo(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await Vs(t.cwd),{kind:"continue"};if(n==="pull")return await Js(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${n}`)),console.log(c("Usage: /sync drift report (local vs Document360)")),console.log(c(" /sync pull <path> pull portal edits into the local file")),console.log(c(" /sync pull --all pull every remote-ahead article"))}catch(o){o instanceof Ys?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function Vs(e){console.log(c("Checking Document360 for drift\u2026"));let t=await Ro({cwd:e});for(let n of jt(t))console.log(n);console.log("")}async function Js(e,t){let n=t[0];if(!n){console.log(x("Usage: /sync pull <article-path> | --all"));return}let o;if(n==="--all"){if(console.log(c("Checking Document360 for drift\u2026")),o=(await Ro({cwd:e.cwd})).entries.filter(i=>i.status==="remote-ahead"&&i.path).map(i=>i.path),o.length===0){console.log(S("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(c(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${ne(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let i=await Xs({cwd:e.cwd,relPath:r});console.log(""),console.log(`${y("\u25CF")} ${ne(i.title)} ${z(`(${i.path})`)}`);for(let g of i.notes)console.log(E(` \u26A0 ${g}`));i.overwritesLocalChanges&&console.log(E(" \u26A0 This OVERWRITES local edits made since the last sync."));let u=Ae(i.oldContent,i.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!u){console.log(c(" Local file already matches the remote content \u2014 advancing the sync base only.")),So({cwd:e.cwd},i);continue}let p=g=>g===1?"":"s";console.log(z(` \u23BF Added ${u.added} line${p(u.added)}, removed ${u.removed} line${p(u.removed)}`));for(let g of u.lines)console.log(g);if(u.hidden>0&&console.log(c(` \u2026 +${u.hidden} more diff lines`)),!await e.withPausedInput(()=>Gs({message:`Write ${i.path}?`,default:!i.overwritesLocalChanges}))){console.log(c(" Skipped."));continue}So({cwd:e.cwd},i),console.log(S(` \u2713 Pulled ${i.path} (sync base advanced).`))}console.log("")}N();import{search as Ks}from"@inquirer/prompts";import{findByName as Qs,getSession as Zs,listSessions as ei,relativeTime as Io}from"document360-engine";async function Do(e,t){let n=ei(t.cwd).filter(o=>o.uuid!==t.currentUuid());if(n.length===0)return console.log(c("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let o=e.join(" "),r=Qs(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),Ao(n),{kind:"continue"})}if(!process.stdin.isTTY)return Ao(n),console.log(c("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Ks({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async i=>{let u=(i??"").toLowerCase();return n.filter(p=>!u||p.name.toLowerCase().includes(u)||p.firstPrompt.toLowerCase().includes(u)).map(p=>({name:`${p.name} ${Io(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=Zs(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Ao(e){console.log("");for(let t of e.slice(0,15))console.log(` ${y(t.name)} ${c(Io(t.updatedAt))}`),console.log(` ${c(t.firstPrompt.slice(0,80))}`);console.log("")}N();import{renameSession as ti}from"document360-engine";async function Eo(e,t){let n=e.join(" ").trim();if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(ti(o,n)?console.log(S(`\u2713 Session renamed to "${n}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Mo,readProjectConfig as ni,writeProjectConfig as oi}from"document360-engine";N();function dn(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=ni(e);if(!o)return"No .d360-writer.json \u2014 run /init first.";if(o.profiles?.[t])return`Profile "${t}" already exists.`;let r=n??t;return Mo().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},oi(o,e),null):`Unknown environment "${r}". Known: ${Mo().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function _o(e,t){let n=e[0];if(!n)return $t(t.cwd),{kind:"continue"};if(n==="add"){let o=dn(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(S(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${y(`/profile ${e[1]}`)} then ${y("/login")}`),{kind:"continue"})}return bt(t.cwd,n),vt(t.cwd,n),console.log(c(" Restarting agent for the new profile\u2026")),{kind:"clear"}}N();import{select as ri}from"@inquirer/prompts";import{readProjectConfig as si,readUserConfig as Lo,resolveModelSetting as pn,writeUserConfig as No}from"document360-engine";var le=[{value:null,label:"Default",desc:"Claude Code's configured default \u2014 clears your personal override"},{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 At(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=le.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function ii(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 Ze(e,t){let n=()=>{let i=pn(e);return i.source==="project"||i.source==="user"||i.source==="env"?i.model??void 0:void 0};if(t==="default"){let i=Lo();return i.defaultModel?(delete i.defaultModel,No(i),{lines:[`\u2713 Personal model override cleared \u2014 now: ${pn(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:n()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}No({...Lo(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=si(e)?.defaultModel;return r&&o.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:o,changed:!0,effective:n()}}async function Oo(e,t){let n=e[0]?.trim();if(!n){let u=pn(t.cwd);if(!process.stdin.isTTY)return console.log(`${ne("Model:")} ${y(u.model??"Claude Code default")}`),console.log(c(` source: ${ii(u)}`)),console.log(c(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=At(u),k;try{k=await t.withPausedInput(()=>ri({message:`Select model (current: ${u.model??"Claude Code default"})`,default:le[p].value,choices:le.map(($,R)=>({name:`${$.label}${R===p?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(c("Cancelled.")),{kind:"continue"}}let{lines:g,changed:v,effective:j}=Ze(t.cwd,k??"default");for(let $ of g)console.log($.startsWith("\u26A0")?E($):$.startsWith("\u2713")?S($):c($));return v&&await t.setModel(j),{kind:"continue"}}let{lines:o,changed:r,effective:i}=Ze(t.cwd,n);for(let u of o)console.log(u.startsWith("\u26A0")?E(u):u.startsWith("\u2713")?S(u):c(u));return r&&await t.setModel(i),{kind:"continue"}}It();async function Ho(e,t){return await t.withPausedInput(()=>Fe(t.cwd)),{kind:"clear"}}N();import{resolveActiveProfile as ki}from"document360-engine";async function qo(e,t){let n=!1;try{n=ki(t.cwd).production}catch{}return n?(console.log(E("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(c("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}N();var zo=async(e,t)=>{try{await t.withPausedInput(()=>xt({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};N();async function Dt(e){let t=e[0];return t?{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for placeholder id: ${t}`,"","Steps you must follow:","1. Locate the SCREENSHOT HTML comment block with this id across user-docs/**/*.md.","2. Translate the placeholder.steps into Playwright actions using STABLE selectors only (data-testid > aria-label > role+name > visible text). If no stable selector is available for a required action, write `test.skip(...)` plus a TODO comment naming the React component that needs a data-testid.",'3. Write the spec file to <captureDir>/<id>.spec.ts. Use `import { test } from "@playwright/test"` and `import { waitPastLogin, dumpAnnotations, type Placeholder } from "document360-capture/helpers"`.',"4. Use process.env.CAPTURE_START_URL and process.env.CAPTURE_AUTH_BOUNDARY (these are injected at run time by document360-capture; do not hardcode them).","5. Report the path of the generated spec and any TODOs from missing selectors."].join(`
16
- `),display:`/screenshot ${t}`}:(console.log(x("Usage: /screenshot <placeholder-id>")),{kind:"continue"})}var Go={help:rn,"?":rn,clear:ao,exit:sn,quit:sn,init:fo,mcp:Ct,publish:xo,audit:St,scope:bo,sync:jo,resume:Do,rename:Eo,profile:_o,model:Oo,doctor:et,workspace:Ho,"allow-prod":qo,login:zo,screenshot:Dt};function Xo(e){let t=e.trim();if(!t.startsWith("/"))return null;let n=t.slice(1).split(/\s+/),o=(n[0]??"").toLowerCase();return o?{name:o,args:n.slice(1)}:null}N();var wi={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"},Vo=160,gn=200,Jo=40;function K(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function Yo(e){let t=Object.entries(e).filter(([,o])=>o!=null&&o!=="");if(t.length===0)return null;let n=t.slice(0,4).map(([o,r])=>`${o}: ${K(typeof r=="string"?r:JSON.stringify(r),Jo)}`);return t.length>4&&n.push("\u2026"),K(n.join(", "),Vo)}var ye=e=>typeof e=="string"&&e?e:null,fn=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function yi(e){let t=ye(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);if(n.length<2)return null;let o=n[n.length-2],r=o.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?o:r.map(i=>i.charAt(0).toUpperCase()+i.slice(1)).join(" ")}function xi(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=ye(t.title);if(!o)return null;let r=yi(t.local_path);return n("Create article",`"${K(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=ye(t.title),r=fn(t.article_id);return n("Update article",o?`"${K(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",fn(t.article_id)?`id ${fn(t.article_id)}\u2026`:null);case"d360_publish_article":{let o=t.version_number;return n("Publish article LIVE",typeof o=="number"?`v${o}`:null)}case"d360_unpublish_article":return n("Unpublish article",null);case"d360_create_category":return n("Create category",ye(t.name)?`"${K(ye(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=ye(t.file_path);return n("Upload image",o?K(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function Et(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...i]=e.split("__"),u=i.join("__");if(r==="document360"){let g=xi(u,t);if(g)return g}let p=u.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:Yo(t)}}let n=wi[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:K(o,Vo)}:{title:e,sep:"",arg:Yo(t)}}function $i(e){if(e===null||typeof e!="object")return typeof e=="string"?e:null;let t=e;for(let n of["name","title","slug","id"])if(typeof t[n]=="string"&&t[n])return t[n];return null}function bi(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map($i).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>K(r,gn))]}if(t!==null&&typeof t=="object"){let n=Object.entries(t).filter(([,o])=>o!==null&&(typeof o=="string"||typeof o=="number"||typeof o=="boolean")).slice(0,6).map(([o,r])=>`${o}: ${K(String(r),Jo)}`);return n.length>0?[K(n.join(" \xB7 "),gn)]:null}return null}function vi(e){let t=ye(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function Ci(e,t,n){switch(e){case"d360_create_article":{let o=vi(t?.local_path);return[`\u2713 draft created${o?` \xB7 ${o}`:""}`]}case"d360_update_article":return["\u2713 draft updated"];case"d360_fork_article":return["\u2713 forked to a new draft"];case"d360_publish_article":return["\u2713 PUBLISHED LIVE \u2014 visible to readers"];case"d360_unpublish_article":return["\u2713 reverted to draft (removed from readers)"];case"d360_create_category":{let o=ye(t?.name);return[`\u2713 category created${o?` \xB7 "${o}"`:""}`]}case"d360_upload_drive_file":{let o=n.match(/https?:\/\/\S+/);return[`\u2713 uploaded${o?` \xB7 ${K(o[0],120)}`:""}`]}default:return null}}function Mt(e,t=4,n,o){let r=e.replace(/\r\n/g,`
17
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let u=Ci(n.slice(18),o,r);if(u)return{lines:u,hidden:0}}let i=bi(r)??r.split(`
18
- `);return{lines:i.slice(0,t).map(u=>K(u,gn)),hidden:Math.max(0,i.length-t)}}function _t(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function Lt(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let n=JSON.parse(t),r=(Array.isArray(n)?n[0]:n)?.id;return typeof r=="string"&&r?r:null}catch{return null}}function Nt(e){try{let t=JSON.parse(e),o=(Array.isArray(t)?t[0]:t)?.url;return typeof o=="string"&&/^https?:\/\//.test(o)?o:null}catch{return null}}var Li=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Ni(e,t,n,o){if(Li.test(e))try{let r=er(o),i=typeof t.project_id=="string"&&t.project_id||r.project.projectId,u=Lt(t,n),p=Nt(n);e.endsWith("publish_article")&&p&&console.log(y(` \u2B95 Live: ${p}`)),u&&i&&console.log(y(` \u2B95 Preview: ${_t(r.connection.portalUrl,i,u,r.project.languageCode??"en")}`))}catch{}}async function tr(e=process.cwd(),t="auto",n){let o=Ti(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${y("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${y('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Oi(e,n),o.kind==="subscription"&&(o.stored?console.log(c(" Using your Claude subscription (no API key set).")):console.log(c(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,i=hn({cwd:e,profileName:n,allowProdWrites:r}),u={uuid:null,firstPrompt:null,titleFired:!1},p=Pi({input:process.stdin,output:process.stdout}),k=[],g=null,v=!1;p.on("line",R=>{if(g){let L=g;g=null,L(R)}else k.push(R)}),p.on("close",()=>{if(v=!0,g){let R=g;g=null,R(null)}});function j(){return k.length>0?Promise.resolve(k.shift()):v?Promise.resolve(null):(process.stdout.write(y("> ")),new Promise(R=>{g=R}))}let $={cwd:e,restartAgent:()=>{i.close(),i=hn({cwd:e,profileName:n,allowProdWrites:r}),u={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>u.uuid,setModel:async R=>i.setModel(R),withPausedInput:async R=>{p.pause();try{return await R()}finally{p.resume()}}};try{for(;;){let R=await j();if(R===null)break;let L=R.trim();if(L){if(L.startsWith("/")){let D=Xo(L);if(!D)continue;let B=Go[D.name];if(!B){console.log(x(`Unknown command: /${D.name}`)),console.log(c("Type /help for the list."));continue}let F=await B(D.args,$);if(F.kind==="exit")break;if(F.kind==="clear"){$.restartAgent();continue}if(F.kind==="allow-prod"){r=!0,$.restartAgent(),console.log(S("\u2713 Production writes authorized for this session.")),console.log("");continue}if(F.kind==="resume"){i.close(),i=hn({cwd:e,resume:F.uuid,profileName:n,allowProdWrites:r});let Q=Si(F.uuid);u={uuid:F.uuid,firstPrompt:Q?.firstPrompt??null,titleFired:!0},Zo(F.uuid),console.log(S(`\u2713 Resumed "${F.name}"`)),console.log("");continue}F.kind==="forward-to-agent"&&(u.firstPrompt||(u.firstPrompt=F.display??F.prompt),await Qo(i,F.prompt,o,u,e));continue}u.firstPrompt||(u.firstPrompt=L),await Qo(i,L,o,u,e)}}}finally{i.close(),p.close()}}function Oi(e,t){console.log(""),console.log(Ye("document360-writer")),console.log(c(` cwd: ${e}`));let n=Di(e);console.log(c(` model: ${n.model??"Claude Code default"}${n.model?` (${n.source})`:""}`)),console.log(Ui(e,t)),Ei(e)||console.log(E(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(c(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function Ui(e,t){try{let n=er(e,t),o=n.production?E(" \u26A0 PRODUCTION"):"",r=_i(n.name);if(!r)return c(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let i={...Ko(r.idToken)??{},...Ko(r.accessToken)??{}},u=i.email??i.preferred_username??"signed in";return Mi(r)&&!r.refreshToken?E(` Document360: profile "${n.name}"${o} \u2014 session expired (d360-writer login)`):c(` Document360: ${u} \xB7 profile "${n.name}"${o}`)}catch(n){return c(` Document360: ${n.message.split(".")[0]}`)}}function Wi(){console.error(""),console.error(`Sign in with your Claude subscription: run ${y("claude")} once, then retry.`),console.error(` (No Claude Code? ${y("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${y("https://console.anthropic.com/settings/keys")}`)}function Fi(e,t,n){e.uuid=t;let o=new Date().toISOString();Ai({uuid:t,name:ji(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function Bi(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||Ii(o,t).then(r=>{r&&Ri(n,r)}).catch(()=>{})}async function Qo(e,t,n,o,r){let i=new Map;for await(let u of e.send(t))Hi(u,o,r,n,i)}function Hi(e,t,n,o,r){switch(e.type){case"session":t.uuid||Fi(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let i=Et(e.name,e.input);i&&(process.stdout.write(`
9
+ `));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(` ${y(o)} ${c("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let s=Object.keys(n.headers??{}),r=s.length>0?c(` [headers: ${s.join(", ")}]`):"";console.log(` ${y(o)} ${c(`(${n.type})`)} ${n.url}${r}`)}}console.log("")}function Os(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 s=ao(),r;if(o==="stdio")r={type:"stdio",command:n[0],args:n.slice(1)};else{let u={};for(let p=1;p<n.length;p++)if(n[p]==="-H"||n[p]==="--header"){let k=n[++p],g=k?.match(/^([^:=]+)[:=](.+)$/);if(!g){console.log(x(`-H expects key:value (no spaces). Got: ${k??"(nothing)"}`));return}u[g[1].trim()]=g[2].trim()}else{console.log(x(`Unexpected argument: ${n[p]}. After the URL, only -H key:value is allowed.`));return}r={type:o,url:n[0],headers:Object.keys(u).length>0?u:void 0}}s.servers[t]=r,vn(s),console.log(""),console.log(T(`\u2713 Registered "${t}" (${o})`)),console.log(E(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(c(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ws(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let o=ao();if(!o.servers[t]){console.log(x(`No server named "${t}".`));return}delete o.servers[t],vn(o),console.log(T(`\u2713 Removed "${t}".`)),console.log(c(" Run /clear to drop it from the current session immediately."))}_();import{select as Fs}from"@inquirer/prompts";import{computeSyncStatus as Cn}from"document360-engine";function Rt(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(`
10
+ `)}var Pn={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Tt(e){return e.filter(t=>t.path!==null&&t.status in Pn).map(t=>({path:t.path,label:Pn[t.status]}))}function Ze(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(`
11
+ `)}async function Tn(e,t){let o=e[0];if(o==="--all"){console.log(c("Checking what needs publishing\u2026"));try{let n=Tt((await Cn({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(T("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:Ze(n.map(s=>s.path)),display:"/publish --all"}}catch(n){return console.log(x(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(c("Checking what needs publishing\u2026"));let n;try{n=Tt((await Cn({cwd:t?.cwd??process.cwd()})).entries)}catch(s){return console.log(x(`Could not compute sync status: ${s.message}`)),console.log(c("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(T("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(c(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let s of n)console.log(` ${s.path} ${c(`(${s.label})`)}`);return console.log(c("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>Fs({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(s=>({name:s.path,value:s.path,description:s.label}))]}))}catch{return console.log(c("Cancelled.")),{kind:"continue"}}if(o==="--all")return{kind:"forward-to-agent",prompt:Ze(n.map(s=>s.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Rt(o),display:`/publish ${o}`}}async function St(){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(`
12
+ `),display:"/audit"}}_();import{checkbox as Bs}from"@inquirer/prompts";import{inventoryRepo as Hs,readProjectConfig as Rn,writeProjectConfig as qs}from"document360-engine";function co(e,t){let o=Rn(e);o&&(o.authoritativeSourceFiles=t,qs(o,e))}function jt(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 Sn(e,t){let o=t?.cwd??process.cwd();if(!Rn(o))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=Hs(o);if(n.length===0)return console.log(c('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(y("Recommended documentation scope (run /scope in the REPL to choose):"));for(let r of n)console.log(` ${r.recommended?"\u25C9":"\u25CB"} ${r.path} ${c(jt(r))}`);return console.log(""),{kind:"continue"}}let s;try{s=await t.withPausedInput(()=>Bs({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(r=>({name:`${r.path} (${jt(r)})`,value:r.path,checked:r.recommended})),pageSize:20}))}catch{return console.log(c("Cancelled.")),{kind:"continue"}}if(s.length===0)return console.log(c("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};co(o,s),console.log(T(`\u2713 Scoped to ${s.length} folder(s) \u2014 written to .d360-writer.json`));for(let r of s)console.log(` ${r}`);return console.log(""),{kind:"continue"}}_();import{confirm as Xs}from"@inquirer/prompts";import{applyPull as Dn,computeSyncStatus as Mn,planPull as Js,D360AuthError as Ks}from"document360-engine";_();var jn=[{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:E,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:y,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:G,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:c,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:c,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:G,mark:"\xB7"}];function zs(e){return e.path?e.path:`${e.title??"(untitled)"} ${G(`[${e.articleId}]`)}`}function At(e){let t=[];for(let u of jn){let p=e.entries.filter(k=>k.status===u.status);if(p.length!==0){t.push(""),t.push(oe(u.header));for(let k of p)t.push(` ${u.paint(u.mark)} ${u.paint(zs(k))}${k.detail?G(` (${k.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let s={"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"},r=n===o?T("\u2713 everything in sync"):[o>0?T(`\u2713 ${o} in sync`):null,...jn.map(u=>{let p=e.counts[u.status]??0;return p>0?`${p} ${s[u.status]}`:null})].filter(Boolean).join(G(" \xB7 "));return t.push(r+G(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Gs}from"diff";import An from"picocolors";var In=80,Ys="\x1B[48;2;74;28;28m",Vs="\x1B[48;2;24;66;24m",En="\x1B[49m",uo=e=>String(e).padStart(5);function Ae(e,t,o){let n=$=>{let S=$.replace(/\r\n/g,`
13
+ `);return S.endsWith(`
14
+ `)||S===""?S:S+`
15
+ `},s=n(e),r=n(t);if(s===r)return null;let u=Math.max(20,o-10),p=Gs("a","b",s,r,"","",{context:3}),k=0,g=0,v=[];p.hunks.forEach(($,S)=>{S>0&&v.push(An.gray(" \u2026"));let U=$.oldStart,M=$.newStart;for(let B of $.lines){let F=B[0],Q=B.slice(1).slice(0,u);F==="-"?(g++,v.push(`${Ys}${uo(U++)} - ${Q}${En}`)):F==="+"?(k++,v.push(`${Vs}${uo(M++)} + ${Q}${En}`)):(v.push(An.gray(uo(M))+" "+Q),U++,M++)}});let j=v.slice(0,In);return{added:k,removed:g,lines:j,hidden:Math.max(0,v.length-In)}}async function _n(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await Qs(t.cwd),{kind:"continue"};if(o==="pull")return await Zs(t,e.slice(1)),{kind:"continue"};console.log(x(`Unknown subcommand: /sync ${o}`)),console.log(c("Usage: /sync drift report (local vs Document360)")),console.log(c(" /sync pull <path> pull portal edits into the local file")),console.log(c(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof Ks?console.log(x(n.message)):console.log(x(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function Qs(e){console.log(c("Checking Document360 for drift\u2026"));let t=await Mn({cwd:e});for(let o of At(t))console.log(o);console.log("")}async function Zs(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(c("Checking Document360 for drift\u2026")),n=(await Mn({cwd:e.cwd})).entries.filter(r=>r.status==="remote-ahead"&&r.path).map(r=>r.path),n.length===0){console.log(T("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(c(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${oe(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let s of n){let r=await Js({cwd:e.cwd,relPath:s});console.log(""),console.log(`${y("\u25CF")} ${oe(r.title)} ${G(`(${r.path})`)}`);for(let g of r.notes)console.log(E(` \u26A0 ${g}`));r.overwritesLocalChanges&&console.log(E(" \u26A0 This OVERWRITES local edits made since the last sync."));let u=Ae(r.oldContent,r.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!u){console.log(c(" Local file already matches the remote content \u2014 advancing the sync base only.")),Dn({cwd:e.cwd},r);continue}let p=g=>g===1?"":"s";console.log(G(` \u23BF Added ${u.added} line${p(u.added)}, removed ${u.removed} line${p(u.removed)}`));for(let g of u.lines)console.log(g);if(u.hidden>0&&console.log(c(` \u2026 +${u.hidden} more diff lines`)),!await e.withPausedInput(()=>Xs({message:`Write ${r.path}?`,default:!r.overwritesLocalChanges}))){console.log(c(" Skipped."));continue}Dn({cwd:e.cwd},r),console.log(T(` \u2713 Pulled ${r.path} (sync base advanced).`))}console.log("")}_();import{statSync as ui}from"node:fs";import{resolve as di}from"node:path";import{estimateBulkCost as pi,planPartitions as mi,readProjectConfig as fi,resolveModelSetting as gi,runPartitioned as hi,trackedArticlePaths as ki}from"document360-engine";import L from"picocolors";import Fn from"wrap-ansi";import ko from"string-width";_();import ye from"picocolors";import po from"wrap-ansi";import mo from"string-width";var Ln=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Nn=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Un=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function ei(e){let t=e.replace(/\r/g,"").split(`
16
+ `),o=[],n=0;for(;n<t.length;){let s=t[n];if(/^\s*```/.test(s)){let p=[];for(n++;n<t.length&&!/^\s*```/.test(t[n]);)p.push(t[n++]);n++,o.push({kind:"code",lines:p});continue}if(Ln(s)){o.push({kind:"hr"}),n++;continue}if(s.includes("|")&&n+1<t.length&&Nn(t[n+1])){let p=Un(s);n+=2;let k=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)k.push(Un(t[n++]));o.push({kind:"table",header:p,rows:k});continue}let r=s.match(/^(#{1,6})\s+(.*)$/);if(r){o.push({kind:"heading",level:r[1].length,text:r[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(s)){let p=[];for(;n<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[n]);)p.push(t[n++].replace(/^\s*([-*]|\d+\.)\s+/,""));o.push({kind:"list",items:p});continue}if(s.trim()===""){n++;continue}let u=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!Ln(t[n])&&!(t[n].includes("|")&&n+1<t.length&&Nn(t[n+1]));)u.push(t[n++]);o.push({kind:"para",text:u.join(" ")})}return o}function fo(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?ye.bold(t.slice(2,-2)):t.startsWith("`")?y(t.slice(1,-1)):ye.italic(t.slice(1,-1)))}var ti=(e,t)=>e+" ".repeat(Math.max(0,t-mo(e)));function oi(e,t,o){let n=e.length,s=e.map((U,M)=>Math.max(mo(U),...t.map(B=>mo(B[M]??"")))),r=Math.max(24,o),u=3*n+1,p=[...s],k=()=>p.reduce((U,M)=>U+M,0)+u,g=0;for(;k()>r&&g++<1e4;){let U=-1,M=6;for(let B=0;B<n;B++)p[B]>M&&(M=p[B],U=B);if(U===-1)break;p[U]-=1}let v=(U,M,B)=>ye.gray(U+p.map(F=>"\u2500".repeat(F+2)).join(M)+B),j=ye.gray("\u2502"),$=(U,M)=>{let B=p.map((ne,fe)=>{let re=U[fe]??"",se=M?ye.bold(re):fo(re);return po(se,ne,{hard:!0}).split(`
17
+ `)}),F=Math.max(...B.map(ne=>ne.length)),Q=[];for(let ne=0;ne<F;ne++)Q.push(p.map((fe,re)=>`${j} ${ti(B[re][ne]??"",fe)} `).join("")+j);return Q.join(`
18
+ `)},S=[v("\u250C","\u252C","\u2510"),$(e,!0)];return S.push(t.length===0?v("\u2514","\u2534","\u2518"):v("\u251C","\u253C","\u2524")),t.forEach((U,M)=>{S.push($(U,!1)),S.push(M===t.length-1?v("\u2514","\u2534","\u2518"):v("\u251C","\u253C","\u2524"))}),S.join(`
19
+ `)}function ni(e,t){switch(e.kind){case"heading":return ye.bold(e.text);case"hr":return ye.gray("\u2500".repeat(t));case"para":return po(fo(e.text),t);case"list":return e.items.map(o=>{let[n="",...s]=po(fo(o),Math.max(10,t-4)).split(`
20
+ `);return" \u2022 "+n+s.map(r=>`
21
+ `+r).join("")}).join(`
22
+ `);case"code":return e.lines.map(o=>ye.gray(" "+o)).join(`
23
+ `);case"table":return oi(e.header,e.rows,t)}}function go(e,t){let o=Math.max(20,t);return ei(e).map(n=>ni(n,o)).join(`
19
24
 
20
- `),console.log(`${S("\u25CF")} ${ne(i.title)}${i.arg!==null?z(`${i.sep}(${i.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let i=Ae(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!i)break;let u=p=>p===1?"":"s";console.log(z(` \u23BF Added ${i.added} line${u(i.added)}, removed ${i.removed} line${u(i.removed)}`));for(let p of i.lines)console.log(p);i.hidden>0&&console.log(c(` \u2026 +${i.hidden} more diff lines`));break}case"tool_result":{let i=r.get(e.id);if(!i)break;r.delete(e.id);let u=Mt(e.output,4,e.isError?void 0:i.name,i.input),p=e.isError?x:z;u.lines.forEach((k,g)=>console.log(p((g===0?" \u23BF ":" ")+k))),u.hidden>0&&console.log(c(` \u2026 +${u.hidden} lines`)),e.isError||Ni(i.name,i.input,e.output,n);break}case"result":process.stdout.write(`
21
- `),console.log(c(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Zo(t.uuid),t.titleFired||Bi(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&Wi();break}}import{render as Fl}from"ink";import{resolveAuth as Bl}from"document360-engine";import{useCallback as H,useEffect as nt,useMemo as Ft,useRef as G,useState as O}from"react";import{Box as X,Text as b,useApp as rl,useInput as sl,useStdout as il}from"ink";import{existsSync as ll,readFileSync as Pr,readdirSync as al}from"node:fs";import{basename as Tr,isAbsolute as cl,join as qt}from"node:path";import{createSession as Sr,loginPkce as ul,toStoredTokens as dl,saveTokens as pl,getAccessToken as ml,resolveActiveProfile as ae,resolveProjectId as fl,getArticle as gl,decodeJwtClaims as He,isExpired as $e,loadTokens as De,setTitle as hl,slugify as kl,touchSession as Pn,upsertSession as wl,generateTitle as Rr,findByName as yl,listSessions as xl,renameSession as $l,suggestNextAction as bl,readProjectConfig as pe,writeProjectConfig as vl,resolveModelSetting as Ht,loadProfileMap as jr,applyPull as Cl,computeSyncStatus as Tn,planPull as Pl,inventoryRepo as Tl,knownEnvironments as Sl,resolveEnvironment as Rl,planPartitions as jl,trackedArticlePaths as Al,runPartitioned as Il,estimateBulkCost as Dl}from"document360-engine";It();import{statSync as el}from"node:fs";import{resolve as tl}from"node:path";import M from"picocolors";import lr from"wrap-ansi";import bn from"string-width";N();import xe from"picocolors";import kn from"wrap-ansi";import wn from"string-width";var nr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),or=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),rr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function qi(e){let t=e.replace(/\r/g,"").split(`
22
- `),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let p=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)p.push(t[o++]);o++,n.push({kind:"code",lines:p});continue}if(nr(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&or(t[o+1])){let p=rr(r);o+=2;let k=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)k.push(rr(t[o++]));n.push({kind:"table",header:p,rows:k});continue}let i=r.match(/^(#{1,6})\s+(.*)$/);if(i){n.push({kind:"heading",level:i[1].length,text:i[2]}),o++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let p=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)p.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:p});continue}if(r.trim()===""){o++;continue}let u=[];for(;o<t.length&&t[o].trim()!==""&&!/^\s*```/.test(t[o])&&!/^(#{1,6})\s/.test(t[o])&&!/^\s*([-*]|\d+\.)\s+/.test(t[o])&&!nr(t[o])&&!(t[o].includes("|")&&o+1<t.length&&or(t[o+1]));)u.push(t[o++]);n.push({kind:"para",text:u.join(" ")})}return n}function yn(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?xe.bold(t.slice(2,-2)):t.startsWith("`")?y(t.slice(1,-1)):xe.italic(t.slice(1,-1)))}var zi=(e,t)=>e+" ".repeat(Math.max(0,t-wn(e)));function Gi(e,t,n){let o=e.length,r=e.map((L,D)=>Math.max(wn(L),...t.map(B=>wn(B[D]??"")))),i=Math.max(24,n),u=3*o+1,p=[...r],k=()=>p.reduce((L,D)=>L+D,0)+u,g=0;for(;k()>i&&g++<1e4;){let L=-1,D=6;for(let B=0;B<o;B++)p[B]>D&&(D=p[B],L=B);if(L===-1)break;p[L]-=1}let v=(L,D,B)=>xe.gray(L+p.map(F=>"\u2500".repeat(F+2)).join(D)+B),j=xe.gray("\u2502"),$=(L,D)=>{let B=p.map((oe,fe)=>{let re=L[fe]??"",se=D?xe.bold(re):yn(re);return kn(se,oe,{hard:!0}).split(`
23
- `)}),F=Math.max(...B.map(oe=>oe.length)),Q=[];for(let oe=0;oe<F;oe++)Q.push(p.map((fe,re)=>`${j} ${zi(B[re][oe]??"",fe)} `).join("")+j);return Q.join(`
24
- `)},R=[v("\u250C","\u252C","\u2510"),$(e,!0)];return R.push(t.length===0?v("\u2514","\u2534","\u2518"):v("\u251C","\u253C","\u2524")),t.forEach((L,D)=>{R.push($(L,!1)),R.push(D===t.length-1?v("\u2514","\u2534","\u2518"):v("\u251C","\u253C","\u2524"))}),R.join(`
25
- `)}function Xi(e,t){switch(e.kind){case"heading":return xe.bold(e.text);case"hr":return xe.gray("\u2500".repeat(t));case"para":return kn(yn(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=kn(yn(n),Math.max(10,t-4)).split(`
26
- `);return" \u2022 "+o+r.map(i=>`
27
- `+i).join("")}).join(`
28
- `);case"code":return e.lines.map(n=>xe.gray(" "+n)).join(`
29
- `);case"table":return Gi(e.header,e.rows,t)}}function xn(e,t){let n=Math.max(20,t);return qi(e).map(o=>Xi(o,n)).join(`
30
-
31
- `)}N();import Yi from"picocolors";var Vi=[127,86,217],Ji=[22,38,43],sr={T:Vi,E:Ji},tt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],$n=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,Ki=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function ir(){if(!Yi.isColorSupported)return[];let e=tt[0].length,t=[];for(let n=0;n<tt.length;n+=2){let o="";for(let r=0;r<e;r++){let i=sr[tt[n][r]],u=n+1<tt.length?sr[tt[n+1][r]]:void 0;i&&u?o+=`${$n(i)}${Ki(u)}\u2580\x1B[49m\x1B[39m`:i?o+=`${$n(i)}\u2580\x1B[39m`:u?o+=`${$n(u)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function vn(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Ie(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}var Be=(e,t)=>` ${e.padEnd(13)}${t}`;function Qi(e,t){let n=[Ye("\u270E document360-writer")+M.gray(` v${e.version}`),M.gray(" Reads your code, writes your docs."),"",Be("Claude:",`${e.claude}${M.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Be("Document360:",e.configured?e.loggedOut?M.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?M.gray(` (${e.sessionHint})`):""}`:M.yellow("not set up \u2014 run /init")),Be("Profile:",e.configured?`${e.profile}${M.gray(` (${e.apiUrl})`)}${e.prod?M.bold(M.yellow(" \u26A0 PRODUCTION")):""}`:M.gray("\u2014 (run /init)")),Be("Project:",e.project),Be("Mode:",M.gray(e.mode)),Be("cwd:",M.gray(e.cwd))],o=ir();if(o.length===0)return n.join(`
32
- `);let r=2,i=3,u=bn(o[0]);if(!(t>=r+u+i+Math.max(...n.map(v=>bn(v)))))return[...o.map(v=>" "+v),...n].join(`
33
- `);let k=Math.max(0,Math.floor((n.length-o.length)/2)),g=[];for(let v=0;v<Math.max(o.length+k,n.length);v++){let j=o[v-k]??" ".repeat(u);g.push((" ".repeat(r)+j+" ".repeat(i)+(n[v]??"")).trimEnd())}return g.join(`
34
- `)}var Zi={error:M.red,warn:M.yellow,ok:M.green,info:M.gray};function Cn(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return Qi(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",i=Math.max(10,n-4),u=50,p=e.text.split(`
35
- `).flatMap(g=>lr(g,i,{hard:!0}).split(`
36
- `)),k=Math.max(0,p.length-u);return k>0&&(p=[...p.slice(0,u),M.dim(`\u2026 +${k} more lines`)]),`
37
- `+p.map((g,v)=>o+(v===0?y(" \u276F "):" ")+g+" ".repeat(Math.max(0,i-bn(g))+1)+r).join(`
25
+ `)}_();import ri from"picocolors";var si=[127,86,217],ii=[22,38,43],On={T:si,E:ii},et=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],ho=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,li=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function Wn(){if(!ri.isColorSupported)return[];let e=et[0].length,t=[];for(let o=0;o<et.length;o+=2){let n="";for(let s=0;s<e;s++){let r=On[et[o][s]],u=o+1<et.length?On[et[o+1][s]]:void 0;r&&u?n+=`${ho(r)}${li(u)}\u2580\x1B[49m\x1B[39m`:r?n+=`${ho(r)}\u2580\x1B[39m`:u?n+=`${ho(u)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function wo(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function Ie(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}var Be=(e,t)=>` ${e.padEnd(13)}${t}`;function ai(e,t){let o=[Xe("\u270E document360-writer")+L.gray(` v${e.version}`),L.gray(" Reads your code, writes your docs."),"",Be("Claude:",`${e.claude}${L.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Be("Document360:",e.configured?e.loggedOut?L.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?L.gray(` (${e.sessionHint})`):""}`:L.yellow("not set up \u2014 run /init")),Be("Profile:",e.configured?`${e.profile}${L.gray(` (${e.apiUrl})`)}${e.prod?L.bold(L.yellow(" \u26A0 PRODUCTION")):""}`:L.gray("\u2014 (run /init)")),Be("Project:",e.project),Be("Mode:",L.gray(e.mode)),Be("cwd:",L.gray(e.cwd))],n=Wn();if(n.length===0)return o.join(`
26
+ `);let s=2,r=3,u=ko(n[0]);if(!(t>=s+u+r+Math.max(...o.map(v=>ko(v)))))return[...n.map(v=>" "+v),...o].join(`
27
+ `);let k=Math.max(0,Math.floor((o.length-n.length)/2)),g=[];for(let v=0;v<Math.max(n.length+k,o.length);v++){let j=n[v-k]??" ".repeat(u);g.push((" ".repeat(s)+j+" ".repeat(r)+(o[v]??"")).trimEnd())}return g.join(`
28
+ `)}var ci={error:L.red,warn:L.yellow,ok:L.green,info:L.gray};function yo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return ai(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",s="\x1B[49m",r=Math.max(10,o-4),u=50,p=e.text.split(`
29
+ `).flatMap(g=>Fn(g,r,{hard:!0}).split(`
30
+ `)),k=Math.max(0,p.length-u);return k>0&&(p=[...p.slice(0,u),L.dim(`\u2026 +${k} more lines`)]),`
31
+ `+p.map((g,v)=>n+(v===0?y(" \u276F "):" ")+g+" ".repeat(Math.max(0,r-ko(g))+1)+s).join(`
38
32
  `)}case"assistant":return`
39
- `+xn(e.text,n);case"tool":{let o=e.arg!==null?M.gray(`${e.sep}(${e.arg})`):"";return`
40
- `+lr(M.green("\u25CF ")+M.bold(e.title)+o,n)}case"tool-result":{let o=e.isError?M.red:M.gray,r=e.lines.map((i,u)=>o((u===0?" \u23BF ":" ")+i));return e.hidden>0&&r.push(M.dim(` \u2026 +${e.hidden} lines`)),r.join(`
41
- `)}case"diff":{let o=u=>u===1?"":"s",i=[M.gray(` \u23BF Added ${e.added} line${o(e.added)}, removed ${e.removed} line${o(e.removed)}`),...e.lines];return e.hidden>0&&i.push(M.dim(` \u2026 +${e.hidden} more diff lines`)),i.join(`
42
- `)}case"link":return e.lines.map(o=>y(Ve(` \u2B95 ${o}`))).join(`
33
+ `+go(e.text,o);case"tool":{let n=e.arg!==null?L.gray(`${e.sep}(${e.arg})`):"";return`
34
+ `+Fn(L.green("\u25CF ")+L.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?L.red:L.gray,s=e.lines.map((r,u)=>n((u===0?" \u23BF ":" ")+r));return e.hidden>0&&s.push(L.dim(` \u2026 +${e.hidden} lines`)),s.join(`
35
+ `)}case"diff":{let n=u=>u===1?"":"s",r=[L.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&r.push(L.dim(` \u2026 +${e.hidden} more diff lines`)),r.join(`
36
+ `)}case"link":return e.lines.map(n=>y(Je(` \u2B95 ${n}`))).join(`
43
37
  `);case"preview":return`
44
- `+Ye(`\u25A3 Preview \u2014 ${e.name}`)+`
38
+ `+Xe(`\u25A3 Preview \u2014 ${e.name}`)+`
45
39
 
46
- `+xn(e.text,n);case"note":return`
47
- `+Zi[e.tone](Ve(e.text));case"done":return`
48
- `+(e.ok?M.magenta("\u2736 "):M.red("\u2736 "))+M.gray(`Cooked for ${vn(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Ie(e.costUsd)}`:""))}}function ar(e,t){return e.map(n=>Cn(n,t)).join(`
49
- `)}function cr(e,t){return t.map(n=>{let o=0;try{o=el(tl(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function ur(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) \u2014 wrap callouts, FAQs, tabs, accordions, and media embeds per the d360-markdown skill \u2014 then re-publish each as a DRAFT to Document360. Work ONLY on the files in this partition; do not read, touch, or convert any article outside this list:
40
+ `+go(e.text,o);case"note":return`
41
+ `+ci[e.tone](Je(e.text));case"done":return`
42
+ `+(e.ok?L.magenta("\u2736 "):L.red("\u2736 "))+L.gray(`Cooked for ${wo(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Ie(e.costUsd)}`:""))}}function Bn(e,t){return e.map(o=>yo(o,t)).join(`
43
+ `)}var xo=3;function $o(e,t){return t.map(o=>{let n=0;try{n=ui(di(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) \u2014 wrap callouts, FAQs, tabs, accordions, and media embeds per the d360-markdown skill \u2014 then re-publish each as a DRAFT to Document360. Work ONLY on the files in this partition; do not read, touch, or convert any article outside this list:
50
44
  `+e.paths.map(t=>`- ${t}`).join(`
51
- `)}function dr(e,t,n){let o=e.reduce((p,k)=>p+k.paths.length,0),[r,i]=t.usd;return[`Convert ${o} article${o===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${Ie(r)}\u2013${Ie(i)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function pr(e,t){let n=e.filter(i=>i.ok),o=e.filter(i=>!i.ok),r=[`Converted ${n.length}/${e.length} partition${e.length===1?"":"s"} \xB7 ${Ie(t)} total.`];if(o.length>0){r.push(`${o.length} failed \u2014 re-run /convert to retry:`);for(let i of o)r.push(` \u2717 ${i.label}${i.error?` \u2014 ${i.error}`:""}`)}return r}var Ot=[{name:"help",usage:"/help",desc:"Show available commands"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser) without leaving the session"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch connection (picker; s = session only)"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API"},{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Manage MCP servers"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits"},{name:"convert",usage:"/convert [--run]",desc:"Convert all tracked articles to DFM across parallel agents (preview; --run to start)"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (analyses + recommends)"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (incremental)"},{name:"screenshot",usage:"/screenshot <id>",desc:"Emit a capture spec for a placeholder"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)"},{name:"exit",usage:"/exit",desc:"Quit"}];function mr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return Ot.filter(n=>n.name.startsWith(t))}function fr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}var nl=/^(?:\d+[.)]|[-*•])\s+/;function gr(e,t,n=4){let o=new Set(t.map(i=>i.toLowerCase())),r=[];for(let i of e.split(`
52
- `)){let u=i.trim().replace(nl,""),p=u.match(/^`([^`]+)`$/);p&&(u=p[1].trim());let k=u.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!k||!o.has(k[1].toLowerCase()))continue;let g=`/${k[1].toLowerCase()}${k[2]?` ${k[2]}`:""}`;if(r.includes(g)||r.push(g),r.length>=n)break}return r}N();var ol=/\[Pasted text #\d+ \+\d+ lines?\]/g;function hr(e){return e.replace(/\r\n?/g,`
53
- `)}function kr(e){return e.includes(`
54
- `)||e.length>200}function wr(e,t){let n=t.split(`
55
- `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function yr(e,t){return e.replace(ol,n=>t.get(n)??n)}function xr(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Ut(e,t){let n=Math.max(1,t),o=[],r=0;for(let i of e.split(`
56
- `)){if(i.length===0)o.push({start:r,end:r});else for(let u=0;u<i.length;u+=n)o.push({start:r+u,end:r+Math.min(u+n,i.length)});r+=i.length+1}return o.length>0?o:[{start:0,end:0}]}function Wt(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function $r(e,t,n){let o=Wt(e,t),r=o+n;if(r<0||r>=e.length)return t;let i=Math.min(t,e[o].end)-e[o].start;return Math.min(e[r].start+i,e[r].end)}function br(e,t,n){let o=e[Wt(e,t)];return n==="start"?o.start:o.end}function vr(e,t,n){let o=Math.max(1,n),r=e.split(`
57
- `),i=0;for(let u=r.length-1;u>=0;u--){let p=r[u];if(i+=Math.max(1,Math.ceil(p.length/o)),i>=t){let k=i-t;return{text:[k>0?p.slice(k*o):p,...r.slice(u+1)].join(`
58
- `),truncated:u>0||k>0}}}return{text:e,truncated:!1}}function Cr(e){let t=!1,n=0,o=0;for(let r of e.split(`
59
- `)){let i=o+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&i<e.length&&(n=i+1),o=i+1}return n}import{Fragment as Sn,jsx as C,jsxs as _}from"react/jsx-runtime";var El={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function Ml(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",i=Ht(e),u=pe(e),p=(u?.docsDir??"user-docs").replace(/\/+$/,""),k=u?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,g={version:t,claude:r,model:i.model??"Claude Code default model",modelSource:El[i.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:u!==null,mode:k};if(u===null)return g;try{let v=ae(e,o);g.profile=v.name,g.apiUrl=v.connection.apiUrl,g.prod=v.production,g.project=v.project.projectId??"(chosen at login)";let j=De(v.name);if(j){let $={...He(j.idToken)??{},...He(j.accessToken)??{}},R=$.email??$.preferred_username??"signed in";$e(j)?j.refreshToken&&(g.who=R,g.loggedOut=!1,g.sessionHint="session expired \u2014 refreshing\u2026"):(g.who=R,g.loggedOut=!1,g.sessionHint=`session valid until ${new Date(j.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return g}function _l(e,t){try{let n=ae(e,t),o=De(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...He(o.idToken)??{},...He(o.accessToken)??{}},i=r.email??r.preferred_username??"signed in";return $e(o)&&!o.refreshToken?{text:`profile "${n.name}" \u2014 session expired (/login)`,prod:n.production}:{text:`${i} \xB7 profile "${n.name}"`,prod:n.production}}catch(n){return{text:n.message.split(".")[0],prod:!1}}}var Ar=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],Ir=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Ll="Ask me to write or update an article\u2026";function Bt({ch:e,dim:t}){let[n,o]=O(!0);return nt(()=>{let r=setInterval(()=>o(i=>!i),530);return()=>clearInterval(r)},[]),C(b,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var Nl=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Dr(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Ol=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Er(e){try{return al(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Ul(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>Er(qt(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&Er(qt(e,o)))}function Wl({startTime:e,chars:t}){let[n,o]=O(0);nt(()=>{let k=setInterval(()=>o(g=>g+1),120);return()=>clearInterval(k)},[]);let r=Ir[n%Ir.length],i=Ar[Math.floor(n/16)%Ar.length],u=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return _(X,{children:[C(b,{color:W,children:` ${r} ${i}\u2026 `}),C(b,{color:"gray",children:`(${vn(u)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}function Mr({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=rl(),[i,u]=O(n),[p,k]=O(null),[g,v]=O({text:"",pos:0}),j=g.text,$=H(a=>{v(l=>{let s=typeof a=="function"?a(l.text):a;return{text:s,pos:s.length}})},[]),R=H(a=>{v(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),L=H(()=>{v(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),s=xr(l)??l.slice(0,-1);return{text:s+a.text.slice(a.pos),pos:s.length}})},[]),[D,B]=O(!1),[F,Q]=O(!1),[oe,fe]=O(0),[re,se]=O(null),[qe,Y]=O([]),ot=G(0),[Or,An]=O(!1),[Ur,zt]=O(0),rt=G(0),[ze,Ee]=O(0),[Me,st]=O(null),In=G(new Map),Wr=G(0),Gt=G([]),Z=G(null);Z.current===null&&(Z.current=Sr({cwd:e,profileName:i,allowProdWrites:!1}));let be=G({uuid:null,firstPrompt:null,titleFired:!1}),it=G(new Map),Xt=G(!1),Yt=G([]),ge=G([]),[ve,Ce]=O(null),[ce,_e]=O(null),[he,Le]=O(null),[V,Ne]=O(null),[ue,Oe]=O(null),[ee,Pe]=O(null),[J,Ue]=O(null),[Dn,Vt]=O([]),Te=G([]),lt=G(!1),at=G(!1),ct=G(null),[te,Se]=O(null),[ut,En]=O(0),[Mn,Jt]=O(0),[Fr,_n]=O(0),Kt=Ft(()=>{try{return Ht(e).model??"Claude Code default"}catch{return null}},[e,Fr]),{stdout:ie}=il(),[,Br]=O(0),Ln=G(`${ie.columns??80}x${ie.rows??24}`),ke=H(()=>Math.max(20,(ie.columns??80)-1),[ie]),d=H(a=>{Gt.current.push(a),console.log(Cn(a,ke()))},[ke]);nt(()=>{if(d({kind:"banner",info:Ml(e,o,t,i)}),!pe(e)){d({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
45
+ `)}function vo(e,t,o){let n=e.reduce((p,k)=>p+k.paths.length,0),[s,r]=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(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${Ie(s)}\u2013${Ie(r)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function Co(e,t){let o=e.filter(r=>r.ok),n=e.filter(r=>!r.ok),s=[`Converted ${o.length}/${e.length} partition${e.length===1?"":"s"} \xB7 ${Ie(t)} total.`];if(n.length>0){s.push(`${n.length} failed \u2014 re-run /convert to retry:`);for(let r of n)s.push(` \u2717 ${r.label}${r.error?` \u2014 ${r.error}`:""}`)}return s}async function Hn(e,t){if(!fi(t.cwd))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=ki(t.cwd,t.profileName);if(o.length===0)return console.log(x("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let n=mi(o);if(!(e.includes("--run")||e.includes("--yes"))){let r=pi({files:$o(t.cwd,o),op:"convert",model:gi(t.cwd).model});for(let u of vo(n,r,xo))console.log(u);return console.log(c("Run /convert --run to start.")),console.log(""),{kind:"continue"}}console.log(c(`Converting ${o.length} articles across ${n.length} partitions (\u2264${xo} agents at once)\u2026`)),console.log(c(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of hi({cwd:t.cwd,partitions:n,promptFor:bo,concurrency:xo,profileName:t.profileName,allowProdWrites:t.allowProdWrites()}))if(r.type==="partition_status")r.status==="running"?console.log(c(` \u25B8 ${r.label} \u2014 converting\u2026`)):r.status==="done"?console.log(T(` \u2713 ${r.label}`)):console.log(x(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");for(let u of Co(r.results,r.totalCostUsd))console.log(r.ok?T(u):E(u))}}catch(r){console.log(x(`Convert run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}_();import{search as wi}from"@inquirer/prompts";import{findByName as yi,getSession as xi,listSessions as $i,relativeTime as zn}from"document360-engine";async function Gn(e,t){let o=$i(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(c("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),s=yi(t.cwd,n);return s?{kind:"resume",uuid:s.uuid,name:s.name}:(console.log(x(`No session matches "${n}".`)),qn(o),{kind:"continue"})}if(!process.stdin.isTTY)return qn(o),console.log(c("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>wi({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async r=>{let u=(r??"").toLowerCase();return o.filter(p=>!u||p.name.toLowerCase().includes(u)||p.firstPrompt.toLowerCase().includes(u)).map(p=>({name:`${p.name} ${zn(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),s=xi(n);return s?{kind:"resume",uuid:s.uuid,name:s.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function qn(e){console.log("");for(let t of e.slice(0,15))console.log(` ${y(t.name)} ${c(zn(t.updatedAt))}`),console.log(` ${c(t.firstPrompt.slice(0,80))}`);console.log("")}_();import{renameSession as bi}from"document360-engine";async function Yn(e,t){let o=e.join(" ").trim();if(!o)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(bi(n,o)?console.log(T(`\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 Vn,readProjectConfig as vi,writeProjectConfig as Ci}from"document360-engine";_();function Po(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=vi(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let s=o??t;return Vn().includes(s)?(n.profiles={...n.profiles,[t]:{connection:{environment:s},production:!1}},Ci(n,e),null):`Unknown environment "${s}". Known: ${Vn().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Xn(e,t){let o=e[0];if(!o)return bt(t.cwd),{kind:"continue"};if(o==="add"){let n=Po(t.cwd,e[1],e[2]);return n?(console.log(x(n)),{kind:"continue"}):(console.log(T(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${y(`/profile ${e[1]}`)} then ${y("/login")}`),{kind:"continue"})}return vt(t.cwd,o),Ct(t.cwd,o),console.log(c(" Restarting agent for the new profile\u2026")),{kind:"clear"}}_();import{select as Pi}from"@inquirer/prompts";import{readProjectConfig as Ti,readUserConfig as Jn,resolveModelSetting as To,writeUserConfig as Kn}from"document360-engine";var le=[{value:null,label:"Default",desc:"Claude Code's configured default \u2014 clears your personal override"},{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 It(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=le.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function Ri(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 tt(e,t){let o=()=>{let r=To(e);return r.source==="project"||r.source==="user"||r.source==="env"?r.model??void 0:void 0};if(t==="default"){let r=Jn();return r.defaultModel?(delete r.defaultModel,Kn(r),{lines:[`\u2713 Personal model override cleared \u2014 now: ${To(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}}Kn({...Jn(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],s=Ti(e)?.defaultModel;return s&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${s}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function Qn(e,t){let o=e[0]?.trim();if(!o){let u=To(t.cwd);if(!process.stdin.isTTY)return console.log(`${oe("Model:")} ${y(u.model??"Claude Code default")}`),console.log(c(` source: ${Ri(u)}`)),console.log(c(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=It(u),k;try{k=await t.withPausedInput(()=>Pi({message:`Select model (current: ${u.model??"Claude Code default"})`,default:le[p].value,choices:le.map(($,S)=>({name:`${$.label}${S===p?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(c("Cancelled.")),{kind:"continue"}}let{lines:g,changed:v,effective:j}=tt(t.cwd,k??"default");for(let $ of g)console.log($.startsWith("\u26A0")?E($):$.startsWith("\u2713")?T($):c($));return v&&await t.setModel(j),{kind:"continue"}}let{lines:n,changed:s,effective:r}=tt(t.cwd,o);for(let u of n)console.log(u.startsWith("\u26A0")?E(u):u.startsWith("\u2713")?T(u):c(u));return s&&await t.setModel(r),{kind:"continue"}}Et();async function nr(e,t){return await t.withPausedInput(()=>Fe(t.cwd)),{kind:"clear"}}_();import{resolveActiveProfile as Ui}from"document360-engine";async function rr(e,t){let o=!1;try{o=Ui(t.cwd).production}catch{}return o?(console.log(E("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(c("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}_();var sr=async(e,t)=>{try{await t.withPausedInput(()=>$t({}))}catch(o){console.log(x(`Login failed: ${o.message}`))}return{kind:"continue"}};_();async function Dt(e){let t=e[0];return t?{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for placeholder id: ${t}`,"","Steps you must follow:","1. Locate the SCREENSHOT HTML comment block with this id across user-docs/**/*.md.","2. Translate the placeholder.steps into Playwright actions using STABLE selectors only (data-testid > aria-label > role+name > visible text). If no stable selector is available for a required action, write `test.skip(...)` plus a TODO comment naming the React component that needs a data-testid.",'3. Write the spec file to <captureDir>/<id>.spec.ts. Use `import { test } from "@playwright/test"` and `import { waitPastLogin, dumpAnnotations, type Placeholder } from "document360-capture/helpers"`.',"4. Use process.env.CAPTURE_START_URL and process.env.CAPTURE_AUTH_BOUNDARY (these are injected at run time by document360-capture; do not hardcode them).","5. Report the path of the generated spec and any TODOs from missing selectors."].join(`
46
+ `),display:`/screenshot ${t}`}:(console.log(x("Usage: /screenshot <placeholder-id>")),{kind:"continue"})}var ir={help:so,"?":so,clear:gn,exit:io,quit:io,init:xn,mcp:Pt,publish:Tn,audit:St,scope:Sn,sync:_n,convert:Hn,resume:Gn,rename:Yn,profile:Xn,model:Qn,doctor:ot,workspace:nr,"allow-prod":rr,login:sr,screenshot:Dt};function lr(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}_();var Oi={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"},cr=160,jo=200,ur=40;function K(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function ar(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,s])=>`${n}: ${K(typeof s=="string"?s:JSON.stringify(s),ur)}`);return t.length>4&&o.push("\u2026"),K(o.join(", "),cr)}var xe=e=>typeof e=="string"&&e?e:null,So=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function Wi(e){let t=xe(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],s=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return s.length===0?n:s.map(r=>r.charAt(0).toUpperCase()+r.slice(1)).join(" ")}function Fi(e,t){let o=(n,s)=>({title:`Document360: ${n}`,sep:" ",arg:s});switch(e){case"d360_create_article":{let n=xe(t.title);if(!n)return null;let s=Wi(t.local_path);return o("Create article",`"${K(n,60)}"${s?` in ${s}`:""}`)}case"d360_update_article":{let n=xe(t.title),s=So(t.article_id);return o("Update article",n?`"${K(n,60)}"`:s?`id ${s}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",So(t.article_id)?`id ${So(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",xe(t.name)?`"${K(xe(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=xe(t.file_path);return o("Upload image",n?K(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function Mt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,s="",...r]=e.split("__"),u=r.join("__");if(s==="document360"){let g=Fi(u,t);if(g)return g}let p=u.replace(/^d360_/,"").replace(/_/g," ");return{title:`${s==="document360"?"Document360":s.charAt(0).toUpperCase()+s.slice(1)}: ${p}`,sep:" ",arg:ar(t)}}let o=Oi[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:K(n,cr)}:{title:e,sep:"",arg:ar(t)}}function Bi(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 Hi(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(Bi).filter(s=>s!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(s=>K(s,jo))]}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,s])=>`${n}: ${K(String(s),ur)}`);return o.length>0?[K(o.join(" \xB7 "),jo)]:null}return null}function qi(e){let t=xe(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function zi(e,t,o){switch(e){case"d360_create_article":{let n=qi(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=xe(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 ${K(n[0],120)}`:""}`]}default:return null}}function _t(e,t=4,o,n){let s=e.replace(/\r\n/g,`
47
+ `).trimEnd();if(!s)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let u=zi(o.slice(18),n,s);if(u)return{lines:u,hidden:0}}let r=Hi(s)??s.split(`
48
+ `);return{lines:r.slice(0,t).map(u=>K(u,jo)),hidden:Math.max(0,r.length-t)}}function Lt(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function Nt(e,t){if(typeof e.article_id=="string"&&e.article_id)return e.article_id;try{let o=JSON.parse(t),s=(Array.isArray(o)?o[0]:o)?.id;return typeof s=="string"&&s?s:null}catch{return null}}function Ut(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 nl=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function rl(e,t,o,n){if(nl.test(e))try{let s=fr(n),r=typeof t.project_id=="string"&&t.project_id||s.project.projectId,u=Nt(t,o),p=Ut(o);e.endsWith("publish_article")&&p&&console.log(y(` \u2B95 Live: ${p}`)),u&&r&&console.log(y(` \u2B95 Preview: ${Lt(s.connection.portalUrl,r,u,s.project.languageCode??"en")}`))}catch{}}async function gr(e=process.cwd(),t="auto",o){let n=Yi(t);n.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${y("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${y('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),sl(e,o),n.kind==="subscription"&&(n.stored?console.log(c(" Using your Claude subscription (no API key set).")):console.log(c(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let s=!1,r=Ao({cwd:e,profileName:o,allowProdWrites:s}),u={uuid:null,firstPrompt:null,titleFired:!1},p=Gi({input:process.stdin,output:process.stdout}),k=[],g=null,v=!1;p.on("line",S=>{if(g){let U=g;g=null,U(S)}else k.push(S)}),p.on("close",()=>{if(v=!0,g){let S=g;g=null,S(null)}});function j(){return k.length>0?Promise.resolve(k.shift()):v?Promise.resolve(null):(process.stdout.write(y("> ")),new Promise(S=>{g=S}))}let $={cwd:e,profileName:o,allowProdWrites:()=>s,restartAgent:()=>{r.close(),r=Ao({cwd:e,profileName:o,allowProdWrites:s}),u={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>u.uuid,setModel:async S=>r.setModel(S),withPausedInput:async S=>{p.pause();try{return await S()}finally{p.resume()}}};try{for(;;){let S=await j();if(S===null)break;let U=S.trim();if(U){if(U.startsWith("/")){let M=lr(U);if(!M)continue;let B=ir[M.name];if(!B){console.log(x(`Unknown command: /${M.name}`)),console.log(c("Type /help for the list."));continue}let F=await B(M.args,$);if(F.kind==="exit")break;if(F.kind==="clear"){$.restartAgent();continue}if(F.kind==="allow-prod"){s=!0,$.restartAgent(),console.log(T("\u2713 Production writes authorized for this session.")),console.log("");continue}if(F.kind==="resume"){r.close(),r=Ao({cwd:e,resume:F.uuid,profileName:o,allowProdWrites:s});let Q=Vi(F.uuid);u={uuid:F.uuid,firstPrompt:Q?.firstPrompt??null,titleFired:!0},mr(F.uuid),console.log(T(`\u2713 Resumed "${F.name}"`)),console.log("");continue}F.kind==="forward-to-agent"&&(u.firstPrompt||(u.firstPrompt=F.display??F.prompt),await pr(r,F.prompt,n,u,e));continue}u.firstPrompt||(u.firstPrompt=U),await pr(r,U,n,u,e)}}}finally{r.close(),p.close()}}function sl(e,t){console.log(""),console.log(Xe("document360-writer")),console.log(c(` cwd: ${e}`));let o=Zi(e);console.log(c(` model: ${o.model??"Claude Code default"}${o.model?` (${o.source})`:""}`)),console.log(il(e,t)),el(e)||console.log(E(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(c(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function il(e,t){try{let o=fr(e,t),n=o.production?E(" \u26A0 PRODUCTION"):"",s=ol(o.name);if(!s)return c(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let r={...dr(s.idToken)??{},...dr(s.accessToken)??{}},u=r.email??r.preferred_username??"signed in";return tl(s)&&!s.refreshToken?E(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):c(` Document360: ${u} \xB7 profile "${o.name}"${n}`)}catch(o){return c(` Document360: ${o.message.split(".")[0]}`)}}function ll(){console.error(""),console.error(`Sign in with your Claude subscription: run ${y("claude")} once, then retry.`),console.error(` (No Claude Code? ${y("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${y("https://console.anthropic.com/settings/keys")}`)}function al(e,t,o){e.uuid=t;let n=new Date().toISOString();Ki({uuid:t,name:Ji(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function cl(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||Qi(n,t).then(s=>{s&&Xi(o,s)}).catch(()=>{})}async function pr(e,t,o,n,s){let r=new Map;for await(let u of e.send(t))ul(u,n,s,o,r)}function ul(e,t,o,n,s){switch(e.type){case"session":t.uuid||al(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let r=Mt(e.name,e.input);r&&(process.stdout.write(`
49
+
50
+ `),console.log(`${T("\u25CF")} ${oe(r.title)}${r.arg!==null?G(`${r.sep}(${r.arg})`):""}`),s.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let r=Ae(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!r)break;let u=p=>p===1?"":"s";console.log(G(` \u23BF Added ${r.added} line${u(r.added)}, removed ${r.removed} line${u(r.removed)}`));for(let p of r.lines)console.log(p);r.hidden>0&&console.log(c(` \u2026 +${r.hidden} more diff lines`));break}case"tool_result":{let r=s.get(e.id);if(!r)break;s.delete(e.id);let u=_t(e.output,4,e.isError?void 0:r.name,r.input),p=e.isError?x:G;u.lines.forEach((k,g)=>console.log(p((g===0?" \u23BF ":" ")+k))),u.hidden>0&&console.log(c(` \u2026 +${u.hidden} lines`)),e.isError||rl(r.name,r.input,e.output,o);break}case"result":process.stdout.write(`
51
+ `),console.log(c(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(mr(t.uuid),t.titleFired||cl(t,o));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&ll();break}}import{render as Jl}from"ink";import{resolveAuth as Kl}from"document360-engine";import{useCallback as H,useEffect as nt,useMemo as Bt,useRef as z,useState as O}from"react";import{Box as Y,Text as b,useApp as ml,useInput as fl,useStdout as gl}from"ink";import{existsSync as hl,readFileSync as Sr,readdirSync as kl}from"node:fs";import{basename as jr,isAbsolute as wl,join as zt}from"node:path";import{createSession as Ar,loginPkce as yl,toStoredTokens as xl,saveTokens as $l,getAccessToken as bl,resolveActiveProfile as ae,resolveProjectId as vl,getArticle as Cl,decodeJwtClaims as He,isExpired as $e,loadTokens as Ee,setTitle as Pl,slugify as Tl,touchSession as Io,upsertSession as Rl,generateTitle as Ir,findByName as Sl,listSessions as jl,renameSession as Al,suggestNextAction as Il,readProjectConfig as pe,writeProjectConfig as El,resolveModelSetting as qt,loadProfileMap as Er,applyPull as Dl,computeSyncStatus as Eo,planPull as Ml,inventoryRepo as _l,knownEnvironments as Ll,resolveEnvironment as Nl,planPartitions as Ul,trackedArticlePaths as Ol,runPartitioned as Wl,estimateBulkCost as Fl}from"document360-engine";Et();var Ot=[{name:"help",usage:"/help",desc:"Show available commands"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser) without leaving the session"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch connection (picker; s = session only)"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API"},{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Manage MCP servers"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits"},{name:"convert",usage:"/convert [--run]",desc:"Convert all tracked articles to DFM across parallel agents (preview; --run to start)"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (analyses + recommends)"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (incremental)"},{name:"screenshot",usage:"/screenshot <id>",desc:"Emit a capture spec for a placeholder"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)"},{name:"exit",usage:"/exit",desc:"Quit"}];function hr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return Ot.filter(o=>o.name.startsWith(t))}function kr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}var dl=/^(?:\d+[.)]|[-*•])\s+/;function wr(e,t,o=4){let n=new Set(t.map(r=>r.toLowerCase())),s=[];for(let r of e.split(`
52
+ `)){let u=r.trim().replace(dl,""),p=u.match(/^`([^`]+)`$/);p&&(u=p[1].trim());let k=u.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!k||!n.has(k[1].toLowerCase()))continue;let g=`/${k[1].toLowerCase()}${k[2]?` ${k[2]}`:""}`;if(s.includes(g)||s.push(g),s.length>=o)break}return s}_();var pl=/\[Pasted text #\d+ \+\d+ lines?\]/g;function yr(e){return e.replace(/\r\n?/g,`
53
+ `)}function xr(e){return e.includes(`
54
+ `)||e.length>200}function $r(e,t){let o=t.split(`
55
+ `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function br(e,t){return e.replace(pl,o=>t.get(o)??o)}function vr(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Wt(e,t){let o=Math.max(1,t),n=[],s=0;for(let r of e.split(`
56
+ `)){if(r.length===0)n.push({start:s,end:s});else for(let u=0;u<r.length;u+=o)n.push({start:s+u,end:s+Math.min(u+o,r.length)});s+=r.length+1}return n.length>0?n:[{start:0,end:0}]}function Ft(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function Cr(e,t,o){let n=Ft(e,t),s=n+o;if(s<0||s>=e.length)return t;let r=Math.min(t,e[n].end)-e[n].start;return Math.min(e[s].start+r,e[s].end)}function Pr(e,t,o){let n=e[Ft(e,t)];return o==="start"?n.start:n.end}function Tr(e,t,o){let n=Math.max(1,o),s=e.split(`
57
+ `),r=0;for(let u=s.length-1;u>=0;u--){let p=s[u];if(r+=Math.max(1,Math.ceil(p.length/n)),r>=t){let k=r-t;return{text:[k>0?p.slice(k*n):p,...s.slice(u+1)].join(`
58
+ `),truncated:u>0||k>0}}}return{text:e,truncated:!1}}function Rr(e){let t=!1,o=0,n=0;for(let s of e.split(`
59
+ `)){let r=n+s.length;/^\s*```/.test(s)?t=!t:!t&&s.trim()===""&&r<e.length&&(o=r+1),n=r+1}return o}import{Fragment as Do,jsx as C,jsxs as N}from"react/jsx-runtime";var Bl={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function Hl(e,t,o,n){let s=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",r=qt(e),u=pe(e),p=(u?.docsDir??"user-docs").replace(/\/+$/,""),k=u?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,g={version:t,claude:s,model:r.model??"Claude Code default model",modelSource:Bl[r.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:u!==null,mode:k};if(u===null)return g;try{let v=ae(e,n);g.profile=v.name,g.apiUrl=v.connection.apiUrl,g.prod=v.production,g.project=v.project.projectId??"(chosen at login)";let j=Ee(v.name);if(j){let $={...He(j.idToken)??{},...He(j.accessToken)??{}},S=$.email??$.preferred_username??"signed in";$e(j)?j.refreshToken&&(g.who=S,g.loggedOut=!1,g.sessionHint="session expired \u2014 refreshing\u2026"):(g.who=S,g.loggedOut=!1,g.sessionHint=`session valid until ${new Date(j.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return g}function ql(e,t){try{let o=ae(e,t),n=Ee(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let s={...He(n.idToken)??{},...He(n.accessToken)??{}},r=s.email??s.preferred_username??"signed in";return $e(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${r} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var Dr=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],Mr=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],zl="Ask me to write or update an article\u2026";function Ht({ch:e,dim:t}){let[o,n]=O(!0);return nt(()=>{let s=setInterval(()=>n(r=>!r),530);return()=>clearInterval(s)},[]),C(b,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var Gl=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function _r(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Yl=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Lr(e){try{return kl(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Vl(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>Lr(zt(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&Lr(zt(e,n)))}function Xl({startTime:e,chars:t}){let[o,n]=O(0);nt(()=>{let k=setInterval(()=>n(g=>g+1),120);return()=>clearInterval(k)},[]);let s=Mr[o%Mr.length],r=Dr[Math.floor(o/16)%Dr.length],u=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return N(Y,{children:[C(b,{color:W,children:` ${s} ${r}\u2026 `}),C(b,{color:"gray",children:`(${wo(u)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}function Nr({cwd:e,auth:t,profileName:o,version:n}){let{exit:s}=ml(),[r,u]=O(o),[p,k]=O(null),[g,v]=O({text:"",pos:0}),j=g.text,$=H(a=>{v(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),S=H(a=>{v(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),U=H(()=>{v(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=vr(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[M,B]=O(!1),[F,Q]=O(!1),[ne,fe]=O(0),[re,se]=O(null),[qe,V]=O([]),rt=z(0),[Fr,Lo]=O(!1),[Br,Gt]=O(0),st=z(0),[ze,De]=O(0),[Me,it]=O(null),No=z(new Map),Hr=z(0),Yt=z([]),Z=z(null);Z.current===null&&(Z.current=Ar({cwd:e,profileName:r,allowProdWrites:!1}));let be=z({uuid:null,firstPrompt:null,titleFired:!1}),lt=z(new Map),Vt=z(!1),Xt=z([]),ge=z([]),[ve,Ce]=O(null),[ce,_e]=O(null),[he,Le]=O(null),[X,Ne]=O(null),[ue,Ue]=O(null),[ee,Pe]=O(null),[J,Oe]=O(null),[Uo,Jt]=O([]),Te=z([]),at=z(!1),Ge=z(null),ct=z(!1),ut=z(null),[te,Re]=O(null),[dt,Oo]=O(0),[Wo,Kt]=O(0),[qr,Fo]=O(0),Qt=Bt(()=>{try{return qt(e).model??"Claude Code default"}catch{return null}},[e,qr]),{stdout:ie}=gl(),[,zr]=O(0),Bo=z(`${ie.columns??80}x${ie.rows??24}`),ke=H(()=>Math.max(20,(ie.columns??80)-1),[ie]),d=H(a=>{Yt.current.push(a),console.log(yo(a,ke()))},[ke]);nt(()=>{if(d({kind:"banner",info:Hl(e,n,t,r)}),!pe(e)){d({kind:"note",tone:"info",text:`Welcome! This repo isn't set up for d360-writer yet \u2014 three steps and you're writing docs:
60
60
  1. /init \u2014 pick your Document360 environment & scaffold config
61
61
  2. /login signs in to that environment
62
62
  3. /workspace picks where articles go
63
- Press 1 to start.`}),Y(["/init"]);return}try{let a=ae(e,i),l=De(a.name);l&&$e(l)&&l.refreshToken?ml({profile:a.name,connection:a.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),fe(s=>s+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),Y(["/login"])}):(!l||$e(l))&&(d({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),Y(["/login"]))}catch{}try{let a=pe(e),l=ae(e,i),s=De(l.name);a&&s&&!$e(s)&&Ul(e,a.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),Y(["/scope"]))}catch{}},[]),nt(()=>{let a=null,l=null,s=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let h=`${ie.columns??80}x${ie.rows??24}`;h!==Ln.current&&(Ln.current=h,Br(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+ar(Gt.current,ke())+"\x1B[?2026l")},80))},400)};return ie.on("resize",s),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ie.off("resize",s)}},[ie,ke]);let Re=Math.max(20,(ie.columns??80)-1),Nn=Ft(()=>_l(e,i),[e,i,oe]),On=Ft(()=>{if(!pe(e))return"Press 1 to set up this repo, or /help\u2026";try{let a=ae(e,i),l=De(a.name);if(!(!!l&&!($e(l)&&!l.refreshToken)))return`Press 1 to sign in to Document360 (profile "${a.name}")\u2026`;if(!a.project.workspaceId)return"Press 1 to pick a workspace\u2026"}catch{}return Ll},[e,i,oe]),Ge=mr(j),Un=Ge.length>0&&!D,Qt=p!==null?vr(p,8,Re):null,dt=ee?ee.paths.filter(a=>!ee.query||a.toLowerCase().includes(ee.query.toLowerCase())).slice(0,8):[],pt=te?te.sessions.filter(a=>{let l=te.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],de=H((a,l)=>{Z.current?.close(),Z.current=Sr({cwd:e,resume:a,profileName:l??i,allowProdWrites:Xt.current}),a||(be.current={uuid:null,firstPrompt:null,titleFired:!1}),En(0),Jt(0)},[e,i]),Wn=H((a,l,s)=>{if(Nl.test(a))try{let h=ae(e,i),m=typeof l.project_id=="string"&&l.project_id||h.project.projectId,f=Lt(l,s),w=[],A=Nt(s);a.endsWith("publish_article")&&A&&w.push(`Live: ${A}`),f&&m&&w.push(`Preview: ${_t(h.connection.portalUrl,m,f,h.project.languageCode??"en")}`),w.length>0&&d({kind:"link",lines:w})}catch{}},[e,i,d]),je=H(async(a,l)=>{An(!0),se(null),Y([]);let s=++ot.current;d({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let h=be.current;h.firstPrompt||(h.firstPrompt=l?.display??a),rt.current=Date.now(),zt(0),B(!0),it.current.clear();let m="",f="",w=null,A=()=>{w||(w=setTimeout(()=>{w=null,k(f.length>0?f:null)},60))},T=()=>{w&&clearTimeout(w),w=null,k(null)},U=()=>{if(f.trim()){let P=f.trimEnd();d({kind:"assistant",text:P})}f="",T()};try{for await(let P of Z.current.send(a))if(P.type==="session"){if(!h.uuid){h.uuid=P.sessionId;let I=new Date().toISOString();wl({uuid:P.sessionId,name:kl(h.firstPrompt??"session"),renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:I,updatedAt:I})}}else if(P.type==="text"){f+=P.delta,m+=P.delta;let I=Cr(f);if(I>0){let q=f.slice(0,I).trimEnd();q&&d({kind:"assistant",text:q}),f=f.slice(I)}A(),zt(q=>q+P.delta.length)}else if(P.type==="tool"){let I=Et(P.name,P.input);I&&(U(),d({kind:"tool",title:I.title,sep:I.sep,arg:I.arg}),it.current.set(P.id,{name:P.name,input:P.input}))}else if(P.type==="article_diff"){let I=Ae(P.oldContent,P.newContent,ke());I&&(U(),d({kind:"diff",added:I.added,removed:I.removed,lines:I.lines,hidden:I.hidden}))}else if(P.type==="tool_result"){P.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(P.output)&&(at.current=!0);let I=it.current.get(P.id);if(I){it.current.delete(P.id),U();let q=Mt(P.output,4,P.isError?void 0:I.name,I.input);d({kind:"tool-result",lines:q.lines,hidden:q.hidden,isError:P.isError}),P.isError||Wn(I.name,I.input,P.output)}}else if(P.type==="result"){U(),En(q=>q+P.outputTokens),Jt(q=>q+P.costUsd),d({kind:"done",seconds:Math.round((Date.now()-rt.current)/1e3),tokens:P.outputTokens,costUsd:P.costUsd,ok:P.ok});let I=P.ok?gr(m,Ot.map(q=>q.name)):[];if(I.length>0?Y(I):P.ok&&m.trim()&&bl(a,m,e).then(q=>{q&&ot.current===s&&se(q)}).catch(()=>{}),h.uuid&&(Pn(h.uuid),!h.titleFired)){h.titleFired=!0;let q=h.uuid,Xn=h.firstPrompt;Xn&&Rr(Xn,e).then(Yn=>Yn&&hl(q,Yn)).catch(()=>{})}}else P.type==="error"&&(U(),P.kind==="auth"&&(at.current=!0),d({kind:"note",text:`agent error: ${P.message}`,tone:"error"}))}finally{B(!1),T(),at.current&&(at.current=!1,ct.current=l?.display??a,Y(["/login"])),lt.current&&(lt.current=!1,Te.current.length>0&&(d({kind:"note",tone:"info",text:`(${Te.current.length} queued message(s) discarded)`}),Te.current=[],Vt([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,Wn,ke]),Zt=H(a=>{let l=De(a);if(!l||$e(l)&&!l.refreshToken)return null;let s={...He(l.idToken)??{},...He(l.accessToken)??{}};return s.email??s.preferred_username??"signed in"},[]),en=H((a,l)=>{if(l){let s=pe(e);s&&(s.defaultProfile=a,vl(s,e))}u(a),de(void 0,a),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Zt(a)||(d({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),Y(["/login"])),fe(s=>s+1)},[e,d,de,Zt]),Fn=H((a,l,s)=>{kt(e,a,l,s.id),d({kind:"note",tone:"ok",text:`Switched to workspace "${s.name??s.id}" (agent restarted).`}),de(),jr(e,a)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),se("analyze this repo and propose a docs structure"))},[e,d,de]),mt=H(()=>{let a=ge.current[0];if(!a)return;d({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let s of a.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${s}`});a.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Ae(a.oldContent,a.newContent,ke());d(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[d,ke]),Bn=H(a=>{if(ge.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let s=ge.current.shift();try{Cl({cwd:e,profileName:i},s),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${s.path} (sync base advanced).`}),ge.current.length===0&&Y(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(l==="n"||l==="no"){let s=ge.current.shift();d({kind:"note",tone:"info",text:`Skipped ${s.path}.`})}else{let s=ge.current.length;return ge.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${s} article(s) left untouched).`}),!0}return mt(),!0},[e,i,mt,d]),Hn=H(async a=>{let l=a.slice(1).trim().split(/\s+/),s=(l[0]??"").toLowerCase(),h=l.slice(1);switch(s){case"help":d({kind:"note",tone:"info",text:Ot.map(m=>` ${m.usage.padEnd(22)} ${m.desc}`).join(`
64
- `)});return;case"exit":case"quit":Z.current?.close(),r();return;case"clear":de(),Gt.current=[],An(!1),se(null),ot.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let m;try{m=ae(e,i)}catch(f){d({kind:"note",tone:"error",text:f.message});return}d({kind:"note",tone:"info",text:`Profile "${m.name}" \u2192 ${m.connection.name} (${m.connection.apiUrl})${m.production?" \u26A0 PRODUCTION":""}`}),Q(!0);try{let f=await ul(m.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},A=>d({kind:"note",tone:"info",text:A})),w=dl(m.name,f);pl(w),on(w,m.name,A=>d({kind:"note",tone:"info",text:A})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${m.name}" as ${yt(w)}`}),m.project.workspaceId||(d({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),Y(["/workspace"])),ct.current&&(se(ct.current),ct.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(f){d({kind:"note",tone:"error",text:`Login failed: ${f.message}`})}finally{Q(!1),fe(f=>f+1)}return}case"allow-prod":{let m=!1;try{m=ae(e,i).production}catch{}if(!m){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Xt.current=!0,de(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let m=h.join(" ").trim(),f=be.current.uuid;if(!f){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!m){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let w=be.current.firstPrompt??"";Rr(w,e).then(A=>{A?(se(`/rename ${A}`),d({kind:"note",tone:"info",text:`Suggestion: "${A}" \u2014 press tab to accept, or type /rename <your name>.`})):d({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>d({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}$l(f,m),d({kind:"note",tone:"ok",text:`Session renamed to "${m}".`});return}case"profile":{let m=h[0],f=pe(e);if(!m){let w=Object.entries(f?.profiles??{});if(w.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let A=w.map(([P,I])=>({name:P,env:I.connection?.environment??"custom",prod:I.production===!0,who:Zt(P)})),T=i??f?.defaultProfile,U=Math.max(0,A.findIndex(P=>P.name===T));_e({cursor:U,current:U,rows:A});return}if(m==="add"){let w=dn(e,h[1],h[2]);if(w){d({kind:"note",tone:"error",text:w});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),en(h[1],!1);return}if(!f?.profiles?.[m]){d({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}en(m,!0);return}case"doctor":await et(h,{cwd:e});return;case"mcp":await Ct(h);return;case"init":{if(pe(e)){d({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let m=Sl().map(f=>({name:f,apiUrl:Rl(f).apiUrl}));Le({cursor:0,rows:m});return}case"resume":{let m=h.join(" ").trim(),f=xl(e).filter(A=>A.uuid!==be.current.uuid);if(!m){if(!f.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Se({query:"",cursor:0,sessions:f});return}let w=yl(e,m);if(!w){d({kind:"note",tone:"error",text:`No session matches "${m}".`});return}de(w.uuid),be.current={uuid:w.uuid,firstPrompt:w.firstPrompt,titleFired:!0},Pn(w.uuid),d({kind:"note",tone:"ok",text:`Resumed "${w.name}".`});return}case"workspace":{let m=h.join(" ").trim(),f;try{f=await ht(e,i)}catch(A){d({kind:"note",tone:"error",text:`Could not list workspaces: ${A.message}`});return}if(!m){let A=f.workspaces.map(U=>({id:U.id,name:U.name??U.id,type:U.workspace_type}));if(A.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let T=Math.max(0,A.findIndex(U=>U.id===f.current));Ne({cursor:T,current:T,rows:A,profile:f.profile,projectId:f.projectId,environment:f.environment});return}let w=nn(f.workspaces,m);if(!w){d({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}Fn(f.profile,f.projectId,w);return}case"publish":{if(h[0]&&h[0]!=="--all"){await je(Tt(h[0]),{display:`/publish ${h[0]}`,echoDisplay:!0});return}let m=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let f=await Tn({cwd:e,profileName:i}),w=Pt(f.entries);if(w.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let T=f.counts["unknown-base"]??0;T>0&&d({kind:"note",tone:"info",text:`(${T} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(m){await je(Qe(w.map(T=>T.path)),{display:"/publish --all",echoDisplay:!0});return}let A=w.length>1?[{path:"--all",label:`publish all ${w.length} candidates in one run`},...w]:w;Oe({cursor:0,rows:A})}catch(f){d({kind:"note",tone:"error",text:`Could not compute sync status: ${f.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let m=h.join(" ").trim();if(!m){let w=[];try{w=Object.keys(jr(e,ae(e,i).name)?.articles??{})}catch{}if(w.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}Pe({query:"",cursor:0,paths:w});return}let f=cl(m)?m:qt(e,m);if(ll(f)){try{d({kind:"preview",name:Tr(f),text:Dr(Pr(f,"utf8"))})}catch(w){d({kind:"note",tone:"error",text:`Could not read ${f}: ${w.message}`})}return}if(Ol.test(m)){try{let w=ae(e,i),A={profile:w.name,connection:w.connection},T=w.project.projectId??fl(A),U=await gl(A,T,m);d({kind:"preview",name:U.title??m,text:U.content??"*(article has no content)*"})}catch(w){d({kind:"note",tone:"error",text:`Could not fetch article: ${w.message}`})}return}d({kind:"note",tone:"error",text:`"${m}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let m=h[0]?.trim();if(!m){let T=At(Ht(e));Ce({cursor:T,current:T});return}let{lines:f,changed:w,effective:A}=Ze(e,m);for(let T of f)d({kind:"note",tone:T.startsWith("\u26A0")?"warn":T.startsWith("\u2713")?"ok":"info",text:T});w&&(_n(T=>T+1),Z.current?.setModel(A));return}case"convert":{if(!pe(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let m=Al(e,i);if(m.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let f=jl(m),w=3;if(!(h.includes("--run")||h.includes("--yes"))){let T=Dl({files:cr(e,m),op:"convert",model:Ht(e).model});d({kind:"note",tone:"info",text:dr(f,T,w).join(`
65
- `)}),Y(["/convert --run"]);return}rt.current=Date.now(),zt(0),B(!0),d({kind:"note",tone:"info",text:`Converting ${m.length} articles across ${f.length} partitions (\u2264${w} agents at once)\u2026`});try{for await(let T of Il({cwd:e,partitions:f,promptFor:ur,concurrency:w,profileName:i,allowProdWrites:Xt.current}))T.type==="partition_status"?T.status==="running"?d({kind:"note",tone:"info",text:` \u25B8 ${T.label} \u2014 converting\u2026`}):T.status==="done"?d({kind:"note",tone:"ok",text:` \u2713 ${T.label}`}):d({kind:"note",tone:"error",text:` \u2717 ${T.label}`}):T.type==="run_done"&&(Jt(U=>U+T.totalCostUsd),d({kind:"note",tone:T.ok?"ok":"warn",text:pr(T.results,T.totalCostUsd).join(`
66
- `)}))}catch(T){d({kind:"note",tone:"error",text:`Convert run failed: ${T.message}`})}finally{B(!1)}return}case"sync":{let m=(h[0]??"status").toLowerCase();try{if(m==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let f=await Tn({cwd:e,profileName:i});d({kind:"note",tone:"info",text:jt(f).join(`
67
- `)});return}if(m==="pull"){let f=h[1];if(!f){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let w;if(f==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),w=(await Tn({cwd:e,profileName:i})).entries.filter(U=>U.status==="remote-ahead"&&U.path).map(U=>U.path),w.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else w=[f.replace(/\\/g,"/")];let A=[];for(let T of w)A.push(await Pl({cwd:e,profileName:i,relPath:T}));ge.current=A,mt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${m} \u2014 use /sync or /sync pull <path>|--all.`})}catch(f){d({kind:"note",tone:"error",text:`Sync failed: ${f.message}`})}return}case"scope":{if(!pe(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let m=Tl(e);if(m.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ue({cursor:0,rows:m.map(f=>({...f,checked:f.recommended}))});return}case"audit":case"screenshot":{if(s==="screenshot"&&!h[0]){d({kind:"note",tone:"error",text:"Usage: /screenshot <id>"});return}let f=await(s==="audit"?St:Dt)(h,void 0);f.kind==="forward-to-agent"&&f.prompt&&await je(f.prompt,{display:f.display,echoDisplay:!0});return}default:d({kind:"note",tone:"error",text:`Unknown command: /${s} \u2014 type /help.`})}},[e,r,i,mt,d,de,je]),ft=H(a=>{let l=(a??j).trim();if($(""),st(null),Ee(0),l.startsWith("/")){let h=f=>f.trim().split(/\s+/).slice(1).join(" "),m=h(l);Y(f=>f.filter(w=>w.trim()!==l&&!(m&&h(w)===m)))}if(!l||Bn(l))return;Yt.current.push(l);let s=yr(l,In.current);l.startsWith("/")?Hn(s):je(s,{display:l})},[j,Bn,Hn,je]),qn=H(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=hr(a);if(kr(l)){let s=wr(++Wr.current,l);In.current.set(s,l),R(s)}else R(l);return}R(a)},[R]),We=Math.max(10,Re-6),zn=Ft(()=>Ut(g.text,We),[g.text,We]),tn=H(a=>v(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),gt=H(a=>v(l=>({...l,pos:$r(Ut(l.text,We),l.pos,a)})),[We]),Xe=H(a=>v(l=>({...l,pos:br(Ut(l.text,We),l.pos,a)})),[We]),Hr=["\x1B[H","\x1B[1~","\x1BOH"],qr=["\x1B[F","\x1B[4~","\x1BOF"],Gn=H((a,l)=>l.leftArrow?(tn(-1),!0):l.rightArrow?(tn(1),!0):a&&Hr.includes(a)?(Xe("start"),!0):a&&qr.includes(a)?(Xe("end"),!0):l.ctrl&&a==="a"?(Xe("start"),!0):l.ctrl&&a==="e"?(Xe("end"),!0):!1,[tn,Xe]);return sl((a,l)=>{if(l.ctrl&&a==="c"){Z.current?.close(),r();return}if(!F){if(D){if(l.escape){lt.current||(lt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),Z.current?.interrupt());return}if(l.return){let s=j.trim();if(!s)return;Te.current.push(s),Vt([...Te.current]),$("");return}if(Gn(a,l))return;if(l.upArrow){gt(-1);return}if(l.downArrow){gt(1);return}if(l.backspace||l.delete){L();return}a&&!l.ctrl&&!l.meta&&qn(a);return}if(ve){if(l.upArrow){Ce(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Ce(s=>s&&{...s,cursor:Math.min(le.length-1,s.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=le.length){Ce(s=>s&&{...s,cursor:Number(a)-1});return}if(l.return){let s=le[ve.cursor];Ce(null);let{lines:h,changed:m,effective:f}=Ze(e,s.value??"default");for(let w of h)d({kind:"note",tone:w.startsWith("\u26A0")?"warn":w.startsWith("\u2713")?"ok":"info",text:w});m&&(_n(w=>w+1),Z.current?.setModel(f));return}if(a==="s"){let s=le[ve.cursor];Ce(null),Z.current?.setModel(s.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${s.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Ce(null);return}return}if(ce){if(l.upArrow){_e(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){_e(s=>s&&{...s,cursor:Math.min(s.rows.length-1,s.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ce.rows.length){_e(s=>s&&{...s,cursor:Number(a)-1});return}if(l.return||a==="s"){let s=ce.rows[ce.cursor];_e(null),en(s.name,l.return===!0);return}if(l.escape){_e(null);return}return}if(he){if(l.upArrow){Le(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Le(s=>s&&{...s,cursor:Math.min(s.rows.length-1,s.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=he.rows.length){Le(s=>s&&{...s,cursor:Number(a)-1});return}if(l.return){let s=he.rows[he.cursor].name;if(Le(null),!mo(e,s).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${s}").`});let m=!1;try{let f=De(s);m=!!f&&!($e(f)&&!f.refreshToken)}catch{}m?(d({kind:"note",tone:"info",text:`Already signed in to ${s} \u2014 next: pick a workspace.`}),Y(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${s}).`}),Y(["/login"])),fe(f=>f+1);return}if(l.escape){Le(null);return}return}if(V){if(l.upArrow){Ne(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Ne(s=>s&&{...s,cursor:Math.min(s.rows.length-1,s.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=V.rows.length){Ne(s=>s&&{...s,cursor:Number(a)-1});return}if(l.return){let s=V.rows[V.cursor],{profile:h,projectId:m}=V;Ne(null),Fn(h,m,s);return}if(l.escape){Ne(null);return}return}if(J){if(l.upArrow){Ue(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Ue(s=>s&&{...s,cursor:Math.min(s.rows.length-1,s.cursor+1)});return}if(a===" "){Ue(s=>s&&{...s,rows:s.rows.map((h,m)=>m===s.cursor?{...h,checked:!h.checked}:h)});return}if(l.return){let s=J.rows.filter(h=>h.checked).map(h=>h.path);if(Ue(null),s.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}cn(e,s),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${s.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of s)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Ue(null);return}return}if(ue){if(l.upArrow){Oe(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Oe(s=>s&&{...s,cursor:Math.min(s.rows.length-1,s.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ue.rows.length){Oe(s=>s&&{...s,cursor:Number(a)-1});return}if(l.return){let s=ue.rows[ue.cursor],h=ue.rows.filter(m=>m.path!=="--all").map(m=>m.path);Oe(null),je(s.path==="--all"?Qe(h):Tt(s.path),{display:s.path==="--all"?"/publish --all":`/publish ${s.path}`,echoDisplay:!0});return}if(l.escape){Oe(null);return}return}if(ee){if(l.escape){Pe(null);return}if(l.upArrow){Pe(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Pe(s=>s&&{...s,cursor:Math.min(Math.max(0,dt.length-1),s.cursor+1)});return}if(l.return){let s=dt[ee.cursor];if(s){Pe(null);try{d({kind:"preview",name:Tr(s),text:Dr(Pr(qt(e,s),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${s}: ${h.message}`})}}return}if(l.backspace||l.delete){Pe(s=>s&&{...s,query:s.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Pe(s=>s&&{...s,query:s.query+a,cursor:0});return}return}if(te){if(l.escape){Se(null);return}if(l.upArrow){Se(s=>s&&{...s,cursor:Math.max(0,s.cursor-1)});return}if(l.downArrow){Se(s=>s&&{...s,cursor:Math.min(Math.max(0,pt.length-1),s.cursor+1)});return}if(l.return){let s=pt[te.cursor];s&&(Se(null),de(s.uuid),be.current={uuid:s.uuid,firstPrompt:s.firstPrompt,titleFired:!0},Pn(s.uuid),d({kind:"note",tone:"ok",text:`Resumed "${s.name}".`}));return}if(l.backspace||l.delete){Se(s=>s&&{...s,query:s.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Se(s=>s&&{...s,query:s.query+a,cursor:0});return}return}if(l.tab&&!j&&re){$(re),se(null),ot.current++;return}if(!j&&qe.length>0&&a&&/^[1-9]$/.test(a)){let s=qe[Number(a)-1];if(s){$(s);return}}if(!Gn(a,l)){if(Un){if(l.upArrow){Ee(s=>Math.max(0,s-1));return}if(l.downArrow){Ee(s=>Math.min(Ge.length-1,s+1));return}if(l.tab){$("/"+(Ge[ze]?.name??"")+" "),Ee(0);return}if(l.return){let s=Ge[ze];if(s){let h=j.trim().slice(1).split(/\s+/).slice(1).join(" ");if(fr(s.usage)&&!h){$("/"+s.name+" "),Ee(0);return}ft("/"+s.name+(h?" "+h:""));return}}}else{if(l.upArrow){if(j!==""&&Me===null){gt(-1);return}let s=Yt.current;if(!s.length)return;let h=Me===null?s.length-1:Math.max(0,Me-1);st(h),$(s[h]??"");return}if(l.downArrow){if(j!==""&&Me===null){gt(1);return}let s=Yt.current;if(Me===null)return;let h=Me+1;h>=s.length?(st(null),$("")):(st(h),$(s[h]??""));return}}if(l.return){ft();return}if(l.backspace||l.delete){L();return}if(l.escape){$(""),Ee(0),se(null),Y([]);return}a&&!l.ctrl&&!l.meta&&qn(a)}}}),nt(()=>{if(D||F)return;let a=Te.current.shift();a!==void 0&&(Vt([...Te.current]),ft(a))},[D,F,ft]),_(X,{flexDirection:"column",width:Re,children:[Qt!==null&&_(X,{marginTop:1,flexDirection:"column",children:[Qt.truncated&&C(b,{dimColor:!0,children:"\u2026"}),C(b,{children:Qt.text})]}),D&&C(Wl,{startTime:rt.current,chars:Ur}),C(X,{borderStyle:"round",borderColor:Nn.prod?"yellow":W,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:j?zn.map((a,l)=>{let s=g.text.slice(a.start,a.end),h=l===Wt(zn,g.pos),m=Math.min(g.pos,a.end)-a.start;return _(b,{children:[C(b,{color:W,children:l===0?"> ":" "}),h?_(Sn,{children:[s.slice(0,m),C(Bt,{ch:s[m]??" "}),s.slice(m+1)]}):s||" "]},`${l}-${a.start}`)}):_(b,{children:[C(b,{color:W,children:"> "}),re&&!D?_(Sn,{children:[C(Bt,{ch:re[0],dim:!0}),C(b,{color:"gray",children:re.slice(1)}),C(b,{dimColor:!0,children:" (tab)"})]}):Or?C(Bt,{ch:" "}):_(Sn,{children:[C(Bt,{ch:On[0],dim:!0}),C(b,{color:"gray",children:On.slice(1)})]})]})}),Dn.length>0&&C(X,{flexDirection:"column",paddingX:1,children:Dn.map((a,l)=>C(b,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),ve?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Select model"}),C(b,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),le.map((a,l)=>_(b,{color:l===ve.cursor?W:void 0,children:[l===ve.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===ve.current?" \u2714":""}`.padEnd(16),C(b,{color:"gray",children:a.desc})]},a.label)),C(b,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):ce?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Switch connection profile"}),ce.rows.map((a,l)=>_(b,{color:l===ce.cursor?W:void 0,children:[l===ce.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ce.current?" \u2714":""}`.padEnd(20),C(b,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(b,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(b,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):he?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Pick your Document360 environment"}),he.rows.map((a,l)=>_(b,{color:l===he.cursor?W:void 0,children:[l===he.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(b,{color:"gray",children:a.apiUrl})]},a.name)),C(b,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):V?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Switch workspace"}),C(b,{dimColor:!0,children:`environment ${V.environment} \xB7 project ${V.projectId.slice(0,8)}\u2026`}),V.rows.map((a,l)=>_(b,{color:l===V.cursor?W:void 0,children:[l===V.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===V.current?" \u2714":""}`.padEnd(30),C(b,{color:"gray",children:a.type??""})]},a.id)),C(b,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):J?(()=>{let l=Math.min(Math.max(0,J.cursor-Math.floor(7)),Math.max(0,J.rows.length-14)),s=J.rows.slice(l,l+14),h=J.rows.filter(m=>m.checked).length;return _(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:`Which folders back the user docs? (${h} selected of ${J.rows.length})`}),l>0?C(b,{dimColor:!0,children:` \u2191 ${l} more`}):null,s.map((m,f)=>{let w=l+f;return _(b,{color:w===J.cursor?W:void 0,children:[w===J.cursor?"\u276F ":" ",m.checked?"\u25C9 ":"\u25CB ",m.path.padEnd(Math.min(48,Re-34)),C(b,{color:"gray",children:Rt(m)})]},m.path)}),l+14<J.rows.length?C(b,{dimColor:!0,children:` \u2193 ${J.rows.length-l-14} more`}):null,C(b,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ue?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Publish which article?"}),ue.rows.map((a,l)=>_(b,{color:l===ue.cursor?W:void 0,children:[l===ue.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Re-30)),C(b,{color:"gray",children:a.label})]},a.path)),C(b,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):ee?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:`Preview article${ee.query?` \u2014 filter: ${ee.query}`:" (type to filter)"}`}),dt.length===0?C(b,{color:"gray",children:"no articles match"}):dt.map((a,l)=>_(b,{color:l===ee.cursor?W:void 0,children:[l===ee.cursor?"\u276F ":" ",a]},a)),C(b,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):te?_(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:`Resume session${te.query?` \u2014 filter: ${te.query}`:" (type to filter)"}`}),pt.length===0?C(b,{color:"gray",children:"no sessions match"}):pt.map((a,l)=>_(b,{color:l===te.cursor?W:void 0,children:[l===te.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(b,{color:l===te.cursor?W:"gray",children:a.firstPrompt.slice(0,Math.max(10,Re-40))})]},a.uuid)),C(b,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Un?C(X,{flexDirection:"column",children:Ge.map((a,l)=>_(b,{color:l===ze?W:void 0,children:[l===ze?"\u276F ":" ",a.usage.padEnd(22)," ",C(b,{color:l===ze?W:"gray",children:a.name==="model"&&Kt?`${a.desc} (currently ${Kt})`:a.desc})]},a.name))}):!j&&qe.length>0?_(X,{flexDirection:"column",paddingX:1,children:[qe.map((a,l)=>_(b,{children:[C(b,{color:W,children:l+1})," ",a.slice(0,Math.max(20,Re-5))]},a)),C(b,{dimColor:!0,children:`press 1-${qe.length} to fill the command \xB7 esc dismiss`})]}):C(X,{paddingX:1,children:_(b,{color:"gray",children:[Nn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${Kt??"model n/a"}${ut>0?` \xB7 ${ut<1e3?`${ut} tokens`:`${(ut/1e3).toFixed(1)}k tokens`}`:""}${Mn>0?` \xB7 ${Ie(Mn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}N();import{jsx as Hl}from"react/jsx-runtime";async function _r(e=process.cwd(),t="auto",n,o="0.0.0"){let r=Bl(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 ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:i}=Fl(Hl(Mr,{cwd:e,auth:r,profileName:n,version:o}));await i(),process.stdout.write(`
68
- `),process.exit(0)}var Xl=zl(import.meta.url),Lr=Xl("../package.json"),me=new ql;function Rn(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}me.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{Rn(e),await xt({profile:e.profile,manual:e.manual})});me.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Rn(e),await ro({profile:e.profile})});me.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Rn(e),await oo({profile:e.profile})});var jn=me.command("profile").description("Manage connection profiles for the current repo");jn.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>$t(process.cwd()));jn.command("use <name>").description("Set the default profile for this repo").action(e=>bt(process.cwd(),e));jn.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>vt(process.cwd(),e));var Nr=me.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Nr.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Fe(process.cwd(),e.profile));Nr.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await Qn(process.cwd(),e,t.profile)});me.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>io());me.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(()=>(It(),Bo)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});me.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(Lr.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(Gl.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 lo(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 tr(e.cwd,e.auth,e.profile):await _r(e.cwd,e.auth,e.profile,Lr.version)});me.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
63
+ Press 1 to start.`}),V(["/init"]);return}try{let a=ae(e,r),l=Ee(a.name);l&&$e(l)&&l.refreshToken?bl({profile:a.name,connection:a.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),fe(i=>i+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),V(["/login"])}):(!l||$e(l))&&(d({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),V(["/login"]))}catch{}try{let a=pe(e),l=ae(e,r),i=Ee(l.name);a&&i&&!$e(i)&&Vl(e,a.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),V(["/scope"]))}catch{}},[]),nt(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let h=`${ie.columns??80}x${ie.rows??24}`;h!==Bo.current&&(Bo.current=h,zr(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Bn(Yt.current,ke())+"\x1B[?2026l")},80))},400)};return ie.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),ie.off("resize",i)}},[ie,ke]);let Se=Math.max(20,(ie.columns??80)-1),Ho=Bt(()=>ql(e,r),[e,r,ne]),qo=Bt(()=>{if(!pe(e))return"Press 1 to set up this repo, or /help\u2026";try{let a=ae(e,r),l=Ee(a.name);if(!(!!l&&!($e(l)&&!l.refreshToken)))return`Press 1 to sign in to Document360 (profile "${a.name}")\u2026`;if(!a.project.workspaceId)return"Press 1 to pick a workspace\u2026"}catch{}return zl},[e,r,ne]),Ye=hr(j),zo=Ye.length>0&&!M,Zt=p!==null?Tr(p,8,Se):null,pt=ee?ee.paths.filter(a=>!ee.query||a.toLowerCase().includes(ee.query.toLowerCase())).slice(0,8):[],mt=te?te.sessions.filter(a=>{let l=te.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],de=H((a,l)=>{Z.current?.close(),Z.current=Ar({cwd:e,resume:a,profileName:l??r,allowProdWrites:Vt.current}),a||(be.current={uuid:null,firstPrompt:null,titleFired:!1}),Oo(0),Kt(0)},[e,r]),Go=H((a,l,i)=>{if(Gl.test(a))try{let h=ae(e,r),m=typeof l.project_id=="string"&&l.project_id||h.project.projectId,f=Nt(l,i),w=[],A=Ut(i);a.endsWith("publish_article")&&A&&w.push(`Live: ${A}`),f&&m&&w.push(`Preview: ${Lt(h.connection.portalUrl,m,f,h.project.languageCode??"en")}`),w.length>0&&d({kind:"link",lines:w})}catch{}},[e,r,d]),je=H(async(a,l)=>{Lo(!0),se(null),V([]);let i=++rt.current;d({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let h=be.current;h.firstPrompt||(h.firstPrompt=l?.display??a),st.current=Date.now(),Gt(0),B(!0),lt.current.clear();let m="",f="",w=null,A=()=>{w||(w=setTimeout(()=>{w=null,k(f.length>0?f:null)},60))},D=()=>{w&&clearTimeout(w),w=null,k(null)},R=()=>{if(f.trim()){let P=f.trimEnd();d({kind:"assistant",text:P})}f="",D()};try{for await(let P of Z.current.send(a))if(P.type==="session"){if(!h.uuid){h.uuid=P.sessionId;let I=new Date().toISOString();Rl({uuid:P.sessionId,name:Tl(h.firstPrompt??"session"),renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:I,updatedAt:I})}}else if(P.type==="text"){f+=P.delta,m+=P.delta;let I=Rr(f);if(I>0){let q=f.slice(0,I).trimEnd();q&&d({kind:"assistant",text:q}),f=f.slice(I)}A(),Gt(q=>q+P.delta.length)}else if(P.type==="tool"){let I=Mt(P.name,P.input);I&&(R(),d({kind:"tool",title:I.title,sep:I.sep,arg:I.arg}),lt.current.set(P.id,{name:P.name,input:P.input}))}else if(P.type==="article_diff"){let I=Ae(P.oldContent,P.newContent,ke());I&&(R(),d({kind:"diff",added:I.added,removed:I.removed,lines:I.lines,hidden:I.hidden}))}else if(P.type==="tool_result"){P.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(P.output)&&(ct.current=!0);let I=lt.current.get(P.id);if(I){lt.current.delete(P.id),R();let q=_t(P.output,4,P.isError?void 0:I.name,I.input);d({kind:"tool-result",lines:q.lines,hidden:q.hidden,isError:P.isError}),P.isError||Go(I.name,I.input,P.output)}}else if(P.type==="result"){R(),Oo(q=>q+P.outputTokens),Kt(q=>q+P.costUsd),d({kind:"done",seconds:Math.round((Date.now()-st.current)/1e3),tokens:P.outputTokens,costUsd:P.costUsd,ok:P.ok});let I=P.ok?wr(m,Ot.map(q=>q.name)):[];if(I.length>0?V(I):P.ok&&m.trim()&&Il(a,m,e).then(q=>{q&&rt.current===i&&se(q)}).catch(()=>{}),h.uuid&&(Io(h.uuid),!h.titleFired)){h.titleFired=!0;let q=h.uuid,Zo=h.firstPrompt;Zo&&Ir(Zo,e).then(en=>en&&Pl(q,en)).catch(()=>{})}}else P.type==="error"&&(R(),P.kind==="auth"&&(ct.current=!0),d({kind:"note",text:`agent error: ${P.message}`,tone:"error"}))}finally{B(!1),D(),ct.current&&(ct.current=!1,ut.current=l?.display??a,V(["/login"])),at.current&&(at.current=!1,Te.current.length>0&&(d({kind:"note",tone:"info",text:`(${Te.current.length} queued message(s) discarded)`}),Te.current=[],Jt([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,Go,ke]),eo=H(a=>{let l=Ee(a);if(!l||$e(l)&&!l.refreshToken)return null;let i={...He(l.idToken)??{},...He(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),to=H((a,l)=>{if(l){let i=pe(e);i&&(i.defaultProfile=a,El(i,e))}u(a),de(void 0,a),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),eo(a)||(d({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),V(["/login"])),fe(i=>i+1)},[e,d,de,eo]),Yo=H((a,l,i)=>{wt(e,a,l,i.id),d({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),de(),Er(e,a)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),se("analyze this repo and propose a docs structure"))},[e,d,de]),ft=H(()=>{let a=ge.current[0];if(!a)return;d({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=Ae(a.oldContent,a.newContent,ke());d(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[d,ke]),Vo=H(a=>{if(ge.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=ge.current.shift();try{Dl({cwd:e,profileName:r},i),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),ge.current.length===0&&V(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(l==="n"||l==="no"){let i=ge.current.shift();d({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=ge.current.length;return ge.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return ft(),!0},[e,r,ft,d]),Xo=H(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),h=l.slice(1);switch(i){case"help":d({kind:"note",tone:"info",text:Ot.map(m=>` ${m.usage.padEnd(22)} ${m.desc}`).join(`
64
+ `)});return;case"exit":case"quit":Z.current?.close(),s();return;case"clear":de(),Yt.current=[],Lo(!1),se(null),rt.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let m;try{m=ae(e,r)}catch(f){d({kind:"note",tone:"error",text:f.message});return}d({kind:"note",tone:"info",text:`Profile "${m.name}" \u2192 ${m.connection.name} (${m.connection.apiUrl})${m.production?" \u26A0 PRODUCTION":""}`}),Q(!0);try{let f=await yl(m.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},A=>d({kind:"note",tone:"info",text:A})),w=xl(m.name,f);$l(w),ro(w,m.name,A=>d({kind:"note",tone:"info",text:A})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${m.name}" as ${xt(w)}`}),m.project.workspaceId||(d({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),V(["/workspace"])),ut.current&&(se(ut.current),ut.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(f){d({kind:"note",tone:"error",text:`Login failed: ${f.message}`})}finally{Q(!1),fe(f=>f+1)}return}case"allow-prod":{let m=!1;try{m=ae(e,r).production}catch{}if(!m){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Vt.current=!0,de(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let m=h.join(" ").trim(),f=be.current.uuid;if(!f){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!m){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let w=be.current.firstPrompt??"";Ir(w,e).then(A=>{A?(se(`/rename ${A}`),d({kind:"note",tone:"info",text:`Suggestion: "${A}" \u2014 press tab to accept, or type /rename <your name>.`})):d({kind:"note",tone:"info",text:"Usage: /rename <name>"})}).catch(()=>d({kind:"note",tone:"info",text:"Usage: /rename <name>"}));return}Al(f,m),d({kind:"note",tone:"ok",text:`Session renamed to "${m}".`});return}case"profile":{let m=h[0],f=pe(e);if(!m){let w=Object.entries(f?.profiles??{});if(w.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let A=w.map(([P,I])=>({name:P,env:I.connection?.environment??"custom",prod:I.production===!0,who:eo(P)})),D=r??f?.defaultProfile,R=Math.max(0,A.findIndex(P=>P.name===D));_e({cursor:R,current:R,rows:A});return}if(m==="add"){let w=Po(e,h[1],h[2]);if(w){d({kind:"note",tone:"error",text:w});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${h[1]}" created (environment: ${h[2]??h[1]}).`}),to(h[1],!1);return}if(!f?.profiles?.[m]){d({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}to(m,!0);return}case"doctor":await ot(h,{cwd:e});return;case"mcp":await Pt(h);return;case"init":{if(pe(e)){d({kind:"note",tone:"info",text:"This repo is already set up \u2014 edit .d360-writer.json directly (or d360-writer init for the CLI wizard)."});return}let m=Ll().map(f=>({name:f,apiUrl:Nl(f).apiUrl}));Le({cursor:0,rows:m});return}case"resume":{let m=h.join(" ").trim(),f=jl(e).filter(A=>A.uuid!==be.current.uuid);if(!m){if(!f.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Re({query:"",cursor:0,sessions:f});return}let w=Sl(e,m);if(!w){d({kind:"note",tone:"error",text:`No session matches "${m}".`});return}de(w.uuid),be.current={uuid:w.uuid,firstPrompt:w.firstPrompt,titleFired:!0},Io(w.uuid),d({kind:"note",tone:"ok",text:`Resumed "${w.name}".`});return}case"workspace":{let m=h.join(" ").trim(),f;try{f=await kt(e,r)}catch(A){d({kind:"note",tone:"error",text:`Could not list workspaces: ${A.message}`});return}if(!m){let A=f.workspaces.map(R=>({id:R.id,name:R.name??R.id,type:R.workspace_type}));if(A.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let D=Math.max(0,A.findIndex(R=>R.id===f.current));Ne({cursor:D,current:D,rows:A,profile:f.profile,projectId:f.projectId,environment:f.environment});return}let w=no(f.workspaces,m);if(!w){d({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}Yo(f.profile,f.projectId,w);return}case"publish":{if(h[0]&&h[0]!=="--all"){await je(Rt(h[0]),{display:`/publish ${h[0]}`,echoDisplay:!0});return}let m=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let f=await Eo({cwd:e,profileName:r}),w=Tt(f.entries);if(w.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let D=f.counts["unknown-base"]??0;D>0&&d({kind:"note",tone:"info",text:`(${D} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(m){await je(Ze(w.map(D=>D.path)),{display:"/publish --all",echoDisplay:!0});return}let A=w.length>1?[{path:"--all",label:`publish all ${w.length} candidates in one run`},...w]:w;Ue({cursor:0,rows:A})}catch(f){d({kind:"note",tone:"error",text:`Could not compute sync status: ${f.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let m=h.join(" ").trim();if(!m){let w=[];try{w=Object.keys(Er(e,ae(e,r).name)?.articles??{})}catch{}if(w.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}Pe({query:"",cursor:0,paths:w});return}let f=wl(m)?m:zt(e,m);if(hl(f)){try{d({kind:"preview",name:jr(f),text:_r(Sr(f,"utf8"))})}catch(w){d({kind:"note",tone:"error",text:`Could not read ${f}: ${w.message}`})}return}if(Yl.test(m)){try{let w=ae(e,r),A={profile:w.name,connection:w.connection},D=w.project.projectId??vl(A),R=await Cl(A,D,m);d({kind:"preview",name:R.title??m,text:R.content??"*(article has no content)*"})}catch(w){d({kind:"note",tone:"error",text:`Could not fetch article: ${w.message}`})}return}d({kind:"note",tone:"error",text:`"${m}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let m=h[0]?.trim();if(!m){let D=It(qt(e));Ce({cursor:D,current:D});return}let{lines:f,changed:w,effective:A}=tt(e,m);for(let D of f)d({kind:"note",tone:D.startsWith("\u26A0")?"warn":D.startsWith("\u2713")?"ok":"info",text:D});w&&(Fo(D=>D+1),Z.current?.setModel(A));return}case"convert":{if(!pe(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let m=Ol(e,r);if(m.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let f=Ul(m),w=3;if(!(h.includes("--run")||h.includes("--yes"))){let R=Fl({files:$o(e,m),op:"convert",model:qt(e).model});d({kind:"note",tone:"info",text:vo(f,R,w).join(`
65
+ `)}),V(["/convert --run"]);return}st.current=Date.now(),Gt(0),B(!0);let D=new AbortController;Ge.current=D,d({kind:"note",tone:"info",text:`Converting ${m.length} articles across ${f.length} partitions (\u2264${w} agents at once)\u2026 (esc to stop)`});try{for await(let R of Wl({cwd:e,partitions:f,promptFor:bo,concurrency:w,profileName:r,allowProdWrites:Vt.current,signal:D.signal}))if(R.type==="partition_status")R.status==="running"?d({kind:"note",tone:"info",text:` \u25B8 ${R.label} \u2014 converting\u2026`}):R.status==="done"?d({kind:"note",tone:"ok",text:` \u2713 ${R.label}`}):d({kind:"note",tone:"error",text:` \u2717 ${R.label}`});else if(R.type==="run_done"){Kt(I=>I+R.totalCostUsd);let P=R.aborted?"Stopped. ":"";d({kind:"note",tone:R.aborted?"warn":R.ok?"ok":"warn",text:P+Co(R.results,R.totalCostUsd).join(`
66
+ `)})}}catch(R){d({kind:"note",tone:"error",text:`Convert run failed: ${R.message}`})}finally{Ge.current=null,B(!1)}return}case"sync":{let m=(h[0]??"status").toLowerCase();try{if(m==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let f=await Eo({cwd:e,profileName:r});d({kind:"note",tone:"info",text:At(f).join(`
67
+ `)});return}if(m==="pull"){let f=h[1];if(!f){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let w;if(f==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),w=(await Eo({cwd:e,profileName:r})).entries.filter(R=>R.status==="remote-ahead"&&R.path).map(R=>R.path),w.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is remote-ahead \u2014 no pulls needed. (Conflicts are never bulk-pulled; pull them one by one.)"});return}}else w=[f.replace(/\\/g,"/")];let A=[];for(let D of w)A.push(await Ml({cwd:e,profileName:r,relPath:D}));ge.current=A,ft();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${m} \u2014 use /sync or /sync pull <path>|--all.`})}catch(f){d({kind:"note",tone:"error",text:`Sync failed: ${f.message}`})}return}case"scope":{if(!pe(e)){d({kind:"note",tone:"error",text:"No .d360-writer.json here. Run /init first."});return}let m=_l(e);if(m.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Oe({cursor:0,rows:m.map(f=>({...f,checked:f.recommended}))});return}case"audit":case"screenshot":{if(i==="screenshot"&&!h[0]){d({kind:"note",tone:"error",text:"Usage: /screenshot <id>"});return}let f=await(i==="audit"?St:Dt)(h,void 0);f.kind==="forward-to-agent"&&f.prompt&&await je(f.prompt,{display:f.display,echoDisplay:!0});return}default:d({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,s,r,ft,d,de,je]),gt=H(a=>{let l=(a??j).trim();if($(""),it(null),De(0),l.startsWith("/")){let h=f=>f.trim().split(/\s+/).slice(1).join(" "),m=h(l);V(f=>f.filter(w=>w.trim()!==l&&!(m&&h(w)===m)))}if(!l||Vo(l))return;Xt.current.push(l);let i=br(l,No.current);l.startsWith("/")?Xo(i):je(i,{display:l})},[j,Vo,Xo,je]),Jo=H(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=yr(a);if(xr(l)){let i=$r(++Hr.current,l);No.current.set(i,l),S(i)}else S(l);return}S(a)},[S]),We=Math.max(10,Se-6),Ko=Bt(()=>Wt(g.text,We),[g.text,We]),oo=H(a=>v(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),ht=H(a=>v(l=>({...l,pos:Cr(Wt(l.text,We),l.pos,a)})),[We]),Ve=H(a=>v(l=>({...l,pos:Pr(Wt(l.text,We),l.pos,a)})),[We]),Gr=["\x1B[H","\x1B[1~","\x1BOH"],Yr=["\x1B[F","\x1B[4~","\x1BOF"],Qo=H((a,l)=>l.leftArrow?(oo(-1),!0):l.rightArrow?(oo(1),!0):a&&Gr.includes(a)?(Ve("start"),!0):a&&Yr.includes(a)?(Ve("end"),!0):l.ctrl&&a==="a"?(Ve("start"),!0):l.ctrl&&a==="e"?(Ve("end"),!0):!1,[oo,Ve]);return fl((a,l)=>{if(l.ctrl&&a==="c"){Z.current?.close(),s();return}if(!F){if(M){if(l.escape){if(Ge.current){Ge.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),Ge.current.abort());return}at.current||(at.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),Z.current?.interrupt());return}if(l.return){let i=j.trim();if(!i)return;Te.current.push(i),Jt([...Te.current]),$("");return}if(Qo(a,l))return;if(l.upArrow){ht(-1);return}if(l.downArrow){ht(1);return}if(l.backspace||l.delete){U();return}a&&!l.ctrl&&!l.meta&&Jo(a);return}if(ve){if(l.upArrow){Ce(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ce(i=>i&&{...i,cursor:Math.min(le.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=le.length){Ce(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=le[ve.cursor];Ce(null);let{lines:h,changed:m,effective:f}=tt(e,i.value??"default");for(let w of h)d({kind:"note",tone:w.startsWith("\u26A0")?"warn":w.startsWith("\u2713")?"ok":"info",text:w});m&&(Fo(w=>w+1),Z.current?.setModel(f));return}if(a==="s"){let i=le[ve.cursor];Ce(null),Z.current?.setModel(i.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Ce(null);return}return}if(ce){if(l.upArrow){_e(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){_e(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ce.rows.length){_e(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=ce.rows[ce.cursor];_e(null),to(i.name,l.return===!0);return}if(l.escape){_e(null);return}return}if(he){if(l.upArrow){Le(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Le(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=he.rows.length){Le(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=he.rows[he.cursor].name;if(Le(null),!yn(e,i).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let m=!1;try{let f=Ee(i);m=!!f&&!($e(f)&&!f.refreshToken)}catch{}m?(d({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),V(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),V(["/login"])),fe(f=>f+1);return}if(l.escape){Le(null);return}return}if(X){if(l.upArrow){Ne(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ne(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=X.rows.length){Ne(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=X.rows[X.cursor],{profile:h,projectId:m}=X;Ne(null),Yo(h,m,i);return}if(l.escape){Ne(null);return}return}if(J){if(l.upArrow){Oe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Oe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Oe(i=>i&&{...i,rows:i.rows.map((h,m)=>m===i.cursor?{...h,checked:!h.checked}:h)});return}if(l.return){let i=J.rows.filter(h=>h.checked).map(h=>h.path);if(Oe(null),i.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}co(e,i),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of i)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Oe(null);return}return}if(ue){if(l.upArrow){Ue(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ue(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ue.rows.length){Ue(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ue.rows[ue.cursor],h=ue.rows.filter(m=>m.path!=="--all").map(m=>m.path);Ue(null),je(i.path==="--all"?Ze(h):Rt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Ue(null);return}return}if(ee){if(l.escape){Pe(null);return}if(l.upArrow){Pe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Pe(i=>i&&{...i,cursor:Math.min(Math.max(0,pt.length-1),i.cursor+1)});return}if(l.return){let i=pt[ee.cursor];if(i){Pe(null);try{d({kind:"preview",name:jr(i),text:_r(Sr(zt(e,i),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${i}: ${h.message}`})}}return}if(l.backspace||l.delete){Pe(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Pe(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(te){if(l.escape){Re(null);return}if(l.upArrow){Re(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Re(i=>i&&{...i,cursor:Math.min(Math.max(0,mt.length-1),i.cursor+1)});return}if(l.return){let i=mt[te.cursor];i&&(Re(null),de(i.uuid),be.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Io(i.uuid),d({kind:"note",tone:"ok",text:`Resumed "${i.name}".`}));return}if(l.backspace||l.delete){Re(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Re(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!j&&re){$(re),se(null),rt.current++;return}if(!j&&qe.length>0&&a&&/^[1-9]$/.test(a)){let i=qe[Number(a)-1];if(i){$(i);return}}if(!Qo(a,l)){if(zo){if(l.upArrow){De(i=>Math.max(0,i-1));return}if(l.downArrow){De(i=>Math.min(Ye.length-1,i+1));return}if(l.tab){$("/"+(Ye[ze]?.name??"")+" "),De(0);return}if(l.return){let i=Ye[ze];if(i){let h=j.trim().slice(1).split(/\s+/).slice(1).join(" ");if(kr(i.usage)&&!h){$("/"+i.name+" "),De(0);return}gt("/"+i.name+(h?" "+h:""));return}}}else{if(l.upArrow){if(j!==""&&Me===null){ht(-1);return}let i=Xt.current;if(!i.length)return;let h=Me===null?i.length-1:Math.max(0,Me-1);it(h),$(i[h]??"");return}if(l.downArrow){if(j!==""&&Me===null){ht(1);return}let i=Xt.current;if(Me===null)return;let h=Me+1;h>=i.length?(it(null),$("")):(it(h),$(i[h]??""));return}}if(l.return){gt();return}if(l.backspace||l.delete){U();return}if(l.escape){$(""),De(0),se(null),V([]);return}a&&!l.ctrl&&!l.meta&&Jo(a)}}}),nt(()=>{if(M||F)return;let a=Te.current.shift();a!==void 0&&(Jt([...Te.current]),gt(a))},[M,F,gt]),N(Y,{flexDirection:"column",width:Se,children:[Zt!==null&&N(Y,{marginTop:1,flexDirection:"column",children:[Zt.truncated&&C(b,{dimColor:!0,children:"\u2026"}),C(b,{children:Zt.text})]}),M&&C(Xl,{startTime:st.current,chars:Br}),C(Y,{borderStyle:"round",borderColor:Ho.prod?"yellow":W,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:j?Ko.map((a,l)=>{let i=g.text.slice(a.start,a.end),h=l===Ft(Ko,g.pos),m=Math.min(g.pos,a.end)-a.start;return N(b,{children:[C(b,{color:W,children:l===0?"> ":" "}),h?N(Do,{children:[i.slice(0,m),C(Ht,{ch:i[m]??" "}),i.slice(m+1)]}):i||" "]},`${l}-${a.start}`)}):N(b,{children:[C(b,{color:W,children:"> "}),re&&!M?N(Do,{children:[C(Ht,{ch:re[0],dim:!0}),C(b,{color:"gray",children:re.slice(1)}),C(b,{dimColor:!0,children:" (tab)"})]}):Fr?C(Ht,{ch:" "}):N(Do,{children:[C(Ht,{ch:qo[0],dim:!0}),C(b,{color:"gray",children:qo.slice(1)})]})]})}),Uo.length>0&&C(Y,{flexDirection:"column",paddingX:1,children:Uo.map((a,l)=>C(b,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),ve?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Select model"}),C(b,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),le.map((a,l)=>N(b,{color:l===ve.cursor?W:void 0,children:[l===ve.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===ve.current?" \u2714":""}`.padEnd(16),C(b,{color:"gray",children:a.desc})]},a.label)),C(b,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):ce?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Switch connection profile"}),ce.rows.map((a,l)=>N(b,{color:l===ce.cursor?W:void 0,children:[l===ce.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ce.current?" \u2714":""}`.padEnd(20),C(b,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(b,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(b,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):he?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Pick your Document360 environment"}),he.rows.map((a,l)=>N(b,{color:l===he.cursor?W:void 0,children:[l===he.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(b,{color:"gray",children:a.apiUrl})]},a.name)),C(b,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):X?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Switch workspace"}),C(b,{dimColor:!0,children:`environment ${X.environment} \xB7 project ${X.projectId.slice(0,8)}\u2026`}),X.rows.map((a,l)=>N(b,{color:l===X.cursor?W:void 0,children:[l===X.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===X.current?" \u2714":""}`.padEnd(30),C(b,{color:"gray",children:a.type??""})]},a.id)),C(b,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):J?(()=>{let l=Math.min(Math.max(0,J.cursor-Math.floor(7)),Math.max(0,J.rows.length-14)),i=J.rows.slice(l,l+14),h=J.rows.filter(m=>m.checked).length;return N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:`Which folders back the user docs? (${h} selected of ${J.rows.length})`}),l>0?C(b,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((m,f)=>{let w=l+f;return N(b,{color:w===J.cursor?W:void 0,children:[w===J.cursor?"\u276F ":" ",m.checked?"\u25C9 ":"\u25CB ",m.path.padEnd(Math.min(48,Se-34)),C(b,{color:"gray",children:jt(m)})]},m.path)}),l+14<J.rows.length?C(b,{dimColor:!0,children:` \u2193 ${J.rows.length-l-14} more`}):null,C(b,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ue?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Publish which article?"}),ue.rows.map((a,l)=>N(b,{color:l===ue.cursor?W:void 0,children:[l===ue.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Se-30)),C(b,{color:"gray",children:a.label})]},a.path)),C(b,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):ee?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:`Preview article${ee.query?` \u2014 filter: ${ee.query}`:" (type to filter)"}`}),pt.length===0?C(b,{color:"gray",children:"no articles match"}):pt.map((a,l)=>N(b,{color:l===ee.cursor?W:void 0,children:[l===ee.cursor?"\u276F ":" ",a]},a)),C(b,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):te?N(Y,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:`Resume session${te.query?` \u2014 filter: ${te.query}`:" (type to filter)"}`}),mt.length===0?C(b,{color:"gray",children:"no sessions match"}):mt.map((a,l)=>N(b,{color:l===te.cursor?W:void 0,children:[l===te.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(b,{color:l===te.cursor?W:"gray",children:a.firstPrompt.slice(0,Math.max(10,Se-40))})]},a.uuid)),C(b,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):zo?C(Y,{flexDirection:"column",children:Ye.map((a,l)=>N(b,{color:l===ze?W:void 0,children:[l===ze?"\u276F ":" ",a.usage.padEnd(22)," ",C(b,{color:l===ze?W:"gray",children:a.name==="model"&&Qt?`${a.desc} (currently ${Qt})`:a.desc})]},a.name))}):!j&&qe.length>0?N(Y,{flexDirection:"column",paddingX:1,children:[qe.map((a,l)=>N(b,{children:[C(b,{color:W,children:l+1})," ",a.slice(0,Math.max(20,Se-5))]},a)),C(b,{dimColor:!0,children:`press 1-${qe.length} to fill the command \xB7 esc dismiss`})]}):C(Y,{paddingX:1,children:N(b,{color:"gray",children:[Ho.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${Qt??"model n/a"}${dt>0?` \xB7 ${dt<1e3?`${dt} tokens`:`${(dt/1e3).toFixed(1)}k tokens`}`:""}${Wo>0?` \xB7 ${Ie(Wo)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}_();import{jsx as Ql}from"react/jsx-runtime";async function Ur(e=process.cwd(),t="auto",o,n="0.0.0"){let s=Kl(t);s.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${y("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${y("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:r}=Jl(Ql(Nr,{cwd:e,auth:s,profileName:o,version:n}));await r(),process.stdout.write(`
68
+ `),process.exit(0)}var oa=ea(import.meta.url),Or=oa("../package.json"),me=new Zl;function Mo(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}me.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=>{Mo(e),await $t({profile:e.profile,manual:e.manual})});me.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Mo(e),await dn({profile:e.profile})});me.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Mo(e),await un({profile:e.profile})});var _o=me.command("profile").description("Manage connection profiles for the current repo");_o.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>bt(process.cwd()));_o.command("use <name>").description("Set the default profile for this repo").action(e=>vt(process.cwd(),e));_o.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ct(process.cwd(),e));var Wr=me.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Wr.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Fe(process.cwd(),e.profile));Wr.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await rn(process.cwd(),e,t.profile)});me.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>mn());me.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(()=>(Et(),or)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});me.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(Or.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(ta.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 fn(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 gr(e.cwd,e.auth,e.profile):await Ur(e.cwd,e.auth,e.profile,Or.version)});me.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -1,4 +1,6 @@
1
- import type { CostEstimate, Partition, PartitionResult } from 'document360-engine';
1
+ import { type CostEstimate, type Partition, type PartitionResult } from 'document360-engine';
2
+ import type { ReplContext } from '../repl.js';
3
+ import type { SlashCommandResult } from './index.js';
2
4
  /** Stat each repo-relative path for the estimator (missing files report 0 bytes → skipped). */
3
5
  export declare function sizeTargets(cwd: string, paths: string[]): {
4
6
  path: string;
@@ -10,3 +12,10 @@ export declare function buildConvertPrompt(partition: Partition): string;
10
12
  export declare function previewLines(partitions: Partition[], estimate: CostEstimate, concurrency: number): string[];
11
13
  /** Final summary after a run: per-partition outcome + totals. */
12
14
  export declare function summaryLines(results: PartitionResult[], totalCostUsd: number): string[];
15
+ /**
16
+ * Classic-REPL `/convert` — parity with the TUI handler (App.tsx): bare `/convert` previews
17
+ * (plan + cost band, no spend); `/convert --run` fans the tracked articles out across parallel
18
+ * agents and prints compact per-partition status + a summary. Mid-run abort is TUI-only (Esc);
19
+ * in the classic REPL, Ctrl+C exits the process.
20
+ */
21
+ export declare function convertCommand(args: string[], ctx: ReplContext): Promise<SlashCommandResult>;
package/dist/repl.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { type AuthMode } from 'document360-engine';
2
2
  export type ReplContext = {
3
3
  cwd: string;
4
+ /** The --profile override for this session, if any (else the repo default resolves). */
5
+ profileName?: string;
6
+ /** Whether production writes are authorized this session (armed by /allow-prod). */
7
+ allowProdWrites: () => boolean;
4
8
  restartAgent: () => void;
5
9
  /** Switch the live session's model without restarting (conversation continues). */
6
10
  setModel: (model?: string) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.9",
3
+ "version": "0.4.11",
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.6",
37
+ "document360-engine": "^0.2.7",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",