document360-writer 0.4.10 → 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 Gr=Object.defineProperty;var Jn=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var Xr=(e,t)=>{for(var n in t)Gr(e,n,{get:t[n],enumerable:!0})};import we from"picocolors";var W,Yr,Vr,Jr,Kn,Ve,c,x,M,S,y,G,ne,O=Jn(()=>{"use strict";W="#7f56d9",[Yr,Vr,Jr]=[127,86,217],Kn=e=>we.isColorSupported?`\x1B[38;2;${Yr};${Vr};${Jr}m${e}\x1B[39m`:e,Ve=e=>we.bold(Kn(e)),c=e=>we.dim(e),x=e=>we.red(e),M=e=>we.yellow(e),S=e=>we.green(e),y=Kn,G=e=>we.gray(e),ne=e=>we.bold(e)});var Ho={};Xr(Ho,{doctorCommand:()=>tt,renderDoctorChecks:()=>Bo,runDoctorChecks:()=>Fo});import{existsSync as ai}from"node:fs";import{d360GetAll as ci,getAccessToken as ui,isExpired as fn,loadProfileMap as di,loadTokens as pi,packageSkillsDir as Wo,projectConfigPath as mi,readProjectConfig as fi,resolveActiveProfile as gi,resolveAuth as hi,resolveModelSetting as ki}from"document360-engine";async function Fo(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=hi("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=ki(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let i=fi(e);if(!i)return t.push({level:"fail",label:`No ${mi(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=gi(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=pi(k.name);g?fn(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):fn(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=di(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(ai(Wo())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Wo()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!fn(g)||g.refreshToken)){let j={profile:k.name,connection:k.connection};try{await ui(j);let $=await ci(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 Bo(e){let t=[""];for(let r of e){let i=r.level==="ok"?S("\u2713"):r.level==="warn"?M("\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 tt(e,t){console.log(c("Running checks\u2026"));for(let n of Bo(await Fo(t.cwd)))console.log(n);return{kind:"continue"}}var Dt=Jn(()=>{"use strict";O()});import{Command as zl}from"commander";import{createRequire as Gl}from"node:module";import{AUTH_MODES as Xl}from"document360-engine";import{input as is}from"@inquirer/prompts";import{loginPkce as ls,refreshTokens as as,toStoredTokens as to,clearTokens as cs,decodeJwtClaims as eo,isExpired as us,loadTokens as ds,saveTokens as no,resolveActiveProfile as yt,setProfileProject as ps,readProjectConfig as ms}from"document360-engine";O();import{select as Kr}from"@inquirer/prompts";import{resolveActiveProfile as Qr,setProfileProject as Zr,resolveProjectId as es,listWorkspaces as ts}from"document360-engine";async function kt(e,t){let n=Qr(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??es(o);return{workspaces:await ts(o,r),projectId:r,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var Qn=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function on(e,t){let n=t.toLowerCase();return e.find(o=>(o.name??"").toLowerCase()===n)??e.find(o=>(o.name??"").toLowerCase().startsWith(n))??e.find(o=>o.id.startsWith(t))}function wt(e,t,n,o){Zr(e,t,{projectId:n,workspaceId:o})}async function Zn(e,t,n){let o;try{o=await kt(e,n)}catch(i){return console.log(x(`Could not list workspaces: ${i.message}`)),1}let r=on(o.workspaces,t);return r?(wt(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 kt(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"):" "} ${Qn(g)} ${c(g.id)}`);console.log(c("Run: d360-writer workspace use <name>"));return}let p=await Kr({message:"Select the Document360 workspace for this repo:",choices:o.map(g=>({name:`${Qn(g)}${g.id===u?" (current)":""}`,value:g.id}))});wt(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}".`))}O();import ns from"picocolors";function os(e=process.env){return e.FORCE_HYPERLINK==="0"||!ns.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 rs(e,t=e,n){return os(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var ss=/https?:\/\/[^\s\x1b]+/g;function Je(e,t){return e.replace(ss,n=>rs(n,n,t))}function oo(e){return{...eo(e.idToken)??{},...eo(e.accessToken)??{}}}function xt(e){let t=oo(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 $t(e){let t=yt(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 ls(n,{manual:e.manual,promptForRedirect:i=>is({message:i})},i=>console.log(Je(i))),r=to(t.name,o);if(no(r),rn(r,t.name,i=>console.log(c(i))),console.log(""),console.log(S(`\u2713 Logged in to "${t.name}" as ${xt(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{ms(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Fe(process.cwd(),t.name))}catch{}}function rn(e,t,n){let r=oo(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(yt(process.cwd(),t).project.projectId)return;ps(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function ro(e){let t=yt(process.cwd(),e.profile),n=t.connection,o=ds(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":""}: ${xt(o)}`),us(o))if(o.refreshToken)try{let r=to(t.name,await as(n,o.refreshToken));no(r),console.log(S(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(M(`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(M("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 so(e){let t=yt(process.cwd(),e.profile);cs(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.`))}O();import{readProjectConfig as io,writeProjectConfig as fs,resolveActiveProfile as gs,loadTokens as hs,isExpired as ks}from"document360-engine";function bt(e){let t=io(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?M(" \u26A0 PRODUCTION"):"",u=o.connection.environment??"(inline)",p=hs(n),k=p?ks(p)&&!p.refreshToken?M("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 vt(e,t){let n=io(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,fs(n,e);let o=n.profiles[t].production?M(" \u26A0 PRODUCTION"):"";console.log(S(`\u2713 Default profile is now "${t}"${o}`))}function Ct(e,t){try{let n=gs(e,t);console.log(""),console.log(`Profile ${y(n.name)}${n.production?M(" \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}}O();import{existsSync as ws,readdirSync as ys,statSync as xs}from"node:fs";import{join as $s}from"node:path";import{apiLogDir as bs}from"document360-engine";function lo(){let e=bs();if(console.log(""),console.log(`Document360 API logs: ${y(e)}`),!ws(e)){console.log(c(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ys(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=(xs($s(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("")}O();import{createSession as vs,resolveAuth as Cs,findByName as Ps,slugify as Ts,touchSession as Ss,upsertSession as Rs,resolveActiveProfile as js}from"document360-engine";async function ao(e,t,n,o,r,i){let u=Cs(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=js(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}p.production&&(console.error(M(`\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 $=Ps(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=vs({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();Rs({uuid:v,name:Ts(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(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&&Ss(v),process.stdout.write(`
3
- `),process.exit(j)}import{createInterface as Ti}from"node:readline/promises";import{createSession as kn,resolveAuth as Si,getSession as Ri,setTitle as ji,slugify as Ai,touchSession as er,upsertSession as Ii,generateTitle as Di,resolveActiveProfile as tr,resolveModelSetting as Ei,readProjectConfig as Mi,decodeJwtClaims as Qo,isExpired as _i,loadTokens as Li}from"document360-engine";O();async function sn(){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"}}O();import{getSession as As}from"document360-engine";async function co(e,t){let n=t.currentUuid(),o=n?As(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 ln(){return{kind:"exit"}}O();import{input as Ke,confirm as Is}from"@inquirer/prompts";import{basename as uo,join as an}from"node:path";import{existsSync as Qe,readFileSync as Ds,readdirSync as Es}from"node:fs";import{writeProjectConfig as po,readProjectConfig as Ms,projectConfigPath as mo}from"document360-engine";function fo(e,t="berlin"){let n=mo(e);if(Qe(n))return{created:!1,path:n,profileName:""};let o={projectId:ho(e)??uo(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:ko(e)};return po(o,e),{created:!0,path:n,profileName:t}}async function go(){let e=process.cwd(),t=mo(e);if(Qe(t)&&!await Is({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(c("init cancelled.")),{kind:"continue"};let n=ho(e)??uo(e),o=await Ke({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await Ke({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),i=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:o,captureDir:r,outputDir:i,profiles:{[u]:{connection:{environment:p},production:!1}},defaultProfile:u,authoritativeSourceFiles:ko(e)},g=Ms(e);return g?.terminologyGlossary&&(k.terminologyGlossary=g.terminologyGlossary),po(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 ho(e){let t=an(e,"package.json");if(!Qe(t))return null;try{return JSON.parse(Ds(t,"utf8")).name??null}catch{return null}}function ko(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])Qe(an(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=an(e,n);Qe(o)&&!_s(o)&&t.push(n)}return t}function _s(e){try{return Es(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}O();import{readMcpConfig as cn,writeMcpConfig as wo}from"document360-engine";async function Pt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Ls(),{kind:"continue"}):t==="add"?(Ns(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Os(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 Ls(){let e=cn(),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 Ns(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=cn(),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,wo(r),console.log(""),console.log(S(`\u2713 Registered "${t}" (${n})`)),console.log(M(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(c(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Os(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=cn();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],wo(n),console.log(S(`\u2713 Removed "${t}".`)),console.log(c(" Run /clear to drop it from the current session immediately."))}O();import{select as Us}from"@inquirer/prompts";import{computeSyncStatus as yo}from"document360-engine";function St(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 xo={"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 xo).map(t=>({path:t.path,label:xo[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 $o(e,t){let n=e[0];if(n==="--all"){console.log(c("Checking what needs publishing\u2026"));try{let o=Tt((await yo({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:Ze(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=Tt((await yo({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(()=>Us({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:Ze(o.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:St(n),display:`/publish ${n}`}}async function Rt(){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"}}O();import{checkbox as Ws}from"@inquirer/prompts";import{inventoryRepo as Fs,readProjectConfig as bo,writeProjectConfig as Bs}from"document360-engine";function un(e,t){let n=bo(e);n&&(n.authoritativeSourceFiles=t,Bs(n,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 vo(e,t){let n=t?.cwd??process.cwd();if(!bo(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=Fs(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(jt(i))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Ws({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(i=>({name:`${i.path} (${jt(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"};un(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"}}O();import{confirm as Xs}from"@inquirer/prompts";import{applyPull as Ro,computeSyncStatus as jo,planPull as Ys,D360AuthError as Vs}from"document360-engine";O();var Co=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:M,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint: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 Hs(e){return e.path?e.path:`${e.title??"(untitled)"} ${G(`[${e.articleId}]`)}`}function At(e){let t=[];for(let u of Co){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(Hs(k))}${k.detail?G(` (${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,...Co.map(u=>{let p=e.counts[u.status]??0;return p>0?`${p} ${r[u.status]}`:null})].filter(Boolean).join(G(" \xB7 "));return t.push(i+G(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as qs}from"diff";import Po from"picocolors";var To=80,zs="\x1B[48;2;74;28;28m",Gs="\x1B[48;2;24;66;24m",So="\x1B[49m",dn=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=qs("a","b",r,i,"","",{context:3}),k=0,g=0,v=[];p.hunks.forEach(($,R)=>{R>0&&v.push(Po.gray(" \u2026"));let N=$.oldStart,E=$.newStart;for(let B of $.lines){let F=B[0],Q=B.slice(1).slice(0,u);F==="-"?(g++,v.push(`${zs}${dn(N++)} - ${Q}${So}`)):F==="+"?(k++,v.push(`${Gs}${dn(E++)} + ${Q}${So}`)):(v.push(Po.gray(dn(E))+" "+Q),N++,E++)}});let j=v.slice(0,To);return{added:k,removed:g,lines:j,hidden:Math.max(0,v.length-To)}}async function Ao(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await Js(t.cwd),{kind:"continue"};if(n==="pull")return await Ks(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 Vs?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function Js(e){console.log(c("Checking Document360 for drift\u2026"));let t=await jo({cwd:e});for(let n of At(t))console.log(n);console.log("")}async function Ks(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 jo({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 Ys({cwd:e.cwd,relPath:r});console.log(""),console.log(`${y("\u25CF")} ${ne(i.title)} ${G(`(${i.path})`)}`);for(let g of i.notes)console.log(M(` \u26A0 ${g}`));i.overwritesLocalChanges&&console.log(M(" \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.")),Ro({cwd:e.cwd},i);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 ${i.path}?`,default:!i.overwritesLocalChanges}))){console.log(c(" Skipped."));continue}Ro({cwd:e.cwd},i),console.log(S(` \u2713 Pulled ${i.path} (sync base advanced).`))}console.log("")}O();import{search as Qs}from"@inquirer/prompts";import{findByName as Zs,getSession as ei,listSessions as ti,relativeTime as Do}from"document360-engine";async function Eo(e,t){let n=ti(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=Zs(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),Io(n),{kind:"continue"})}if(!process.stdin.isTTY)return Io(n),console.log(c("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Qs({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} ${Do(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=ei(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Io(e){console.log("");for(let t of e.slice(0,15))console.log(` ${y(t.name)} ${c(Do(t.updatedAt))}`),console.log(` ${c(t.firstPrompt.slice(0,80))}`);console.log("")}O();import{renameSession as ni}from"document360-engine";async function Mo(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?(ni(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 _o,readProjectConfig as oi,writeProjectConfig as ri}from"document360-engine";O();function pn(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=oi(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 _o().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},ri(o,e),null):`Unknown environment "${r}". Known: ${_o().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Lo(e,t){let n=e[0];if(!n)return bt(t.cwd),{kind:"continue"};if(n==="add"){let o=pn(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 vt(t.cwd,n),Ct(t.cwd,n),console.log(c(" Restarting agent for the new profile\u2026")),{kind:"clear"}}O();import{select as si}from"@inquirer/prompts";import{readProjectConfig as ii,readUserConfig as No,resolveModelSetting as mn,writeUserConfig as Oo}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(),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 li(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 et(e,t){let n=()=>{let i=mn(e);return i.source==="project"||i.source==="user"||i.source==="env"?i.model??void 0:void 0};if(t==="default"){let i=No();return i.defaultModel?(delete i.defaultModel,Oo(i),{lines:[`\u2713 Personal model override cleared \u2014 now: ${mn(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}}Oo({...No(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=ii(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 Uo(e,t){let n=e[0]?.trim();if(!n){let u=mn(t.cwd);if(!process.stdin.isTTY)return console.log(`${ne("Model:")} ${y(u.model??"Claude Code default")}`),console.log(c(` source: ${li(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(()=>si({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}=et(t.cwd,k??"default");for(let $ of g)console.log($.startsWith("\u26A0")?M($):$.startsWith("\u2713")?S($):c($));return v&&await t.setModel(j),{kind:"continue"}}let{lines:o,changed:r,effective:i}=et(t.cwd,n);for(let u of o)console.log(u.startsWith("\u26A0")?M(u):u.startsWith("\u2713")?S(u):c(u));return r&&await t.setModel(i),{kind:"continue"}}Dt();async function qo(e,t){return await t.withPausedInput(()=>Fe(t.cwd)),{kind:"clear"}}O();import{resolveActiveProfile as wi}from"document360-engine";async function zo(e,t){let n=!1;try{n=wi(t.cwd).production}catch{}return n?(console.log(M("\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"})}O();var Go=async(e,t)=>{try{await t.withPausedInput(()=>$t({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};O();async function Et(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 Xo={help:sn,"?":sn,clear:co,exit:ln,quit:ln,init:go,mcp:Pt,publish:$o,audit:Rt,scope:vo,sync:Ao,resume:Eo,rename:Mo,profile:Lo,model:Uo,doctor:tt,workspace:qo,"allow-prod":zo,login:Go,screenshot:Et};function Yo(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}O();var yi={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"},Jo=160,hn=200,Ko=40;function K(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function Vo(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),Ko)}`);return t.length>4&&n.push("\u2026"),K(n.join(", "),Jo)}var ye=e=>typeof e=="string"&&e?e:null,gn=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function xi(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 $i(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=xi(t.local_path);return n("Create article",`"${K(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=ye(t.title),r=gn(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)",gn(t.article_id)?`id ${gn(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 Mt(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...i]=e.split("__"),u=i.join("__");if(r==="document360"){let g=$i(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:Vo(t)}}let n=yi[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:K(o,Jo)}:{title:e,sep:"",arg:Vo(t)}}function bi(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 vi(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(bi).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>K(r,hn))]}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),Ko)}`);return n.length>0?[K(n.join(" \xB7 "),hn)]:null}return null}function Ci(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 Pi(e,t,n){switch(e){case"d360_create_article":{let o=Ci(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 _t(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=Pi(n.slice(18),o,r);if(u)return{lines:u,hidden:0}}let i=vi(r)??r.split(`
18
- `);return{lines:i.slice(0,t).map(u=>K(u,hn)),hidden:Math.max(0,i.length-t)}}function Lt(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function Nt(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 Ot(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 Ni=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Oi(e,t,n,o){if(Ni.test(e))try{let r=tr(o),i=typeof t.project_id=="string"&&t.project_id||r.project.projectId,u=Nt(t,n),p=Ot(n);e.endsWith("publish_article")&&p&&console.log(y(` \u2B95 Live: ${p}`)),u&&i&&console.log(y(` \u2B95 Preview: ${Lt(r.connection.portalUrl,i,u,r.project.languageCode??"en")}`))}catch{}}async function nr(e=process.cwd(),t="auto",n){let o=Si(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)),Ui(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=kn({cwd:e,profileName:n,allowProdWrites:r}),u={uuid:null,firstPrompt:null,titleFired:!1},p=Ti({input:process.stdin,output:process.stdout}),k=[],g=null,v=!1;p.on("line",R=>{if(g){let N=g;g=null,N(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=kn({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 N=R.trim();if(N){if(N.startsWith("/")){let E=Yo(N);if(!E)continue;let B=Xo[E.name];if(!B){console.log(x(`Unknown command: /${E.name}`)),console.log(c("Type /help for the list."));continue}let F=await B(E.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=kn({cwd:e,resume:F.uuid,profileName:n,allowProdWrites:r});let Q=Ri(F.uuid);u={uuid:F.uuid,firstPrompt:Q?.firstPrompt??null,titleFired:!0},er(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 Zo(i,F.prompt,o,u,e));continue}u.firstPrompt||(u.firstPrompt=N),await Zo(i,N,o,u,e)}}}finally{i.close(),p.close()}}function Ui(e,t){console.log(""),console.log(Ve("document360-writer")),console.log(c(` cwd: ${e}`));let n=Ei(e);console.log(c(` model: ${n.model??"Claude Code default"}${n.model?` (${n.source})`:""}`)),console.log(Wi(e,t)),Mi(e)||console.log(M(" 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 Wi(e,t){try{let n=tr(e,t),o=n.production?M(" \u26A0 PRODUCTION"):"",r=Li(n.name);if(!r)return c(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let i={...Qo(r.idToken)??{},...Qo(r.accessToken)??{}},u=i.email??i.preferred_username??"signed in";return _i(r)&&!r.refreshToken?M(` 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 Fi(){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 Bi(e,t,n){e.uuid=t;let o=new Date().toISOString();Ii({uuid:t,name:Ai(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function Hi(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||Di(o,t).then(r=>{r&&ji(n,r)}).catch(()=>{})}async function Zo(e,t,n,o,r){let i=new Map;for await(let u of e.send(t))qi(u,o,r,n,i)}function qi(e,t,n,o,r){switch(e.type){case"session":t.uuid||Bi(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let i=Mt(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?G(`${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(G(` \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=_t(e.output,4,e.isError?void 0:i.name,i.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||Oi(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&&(er(t.uuid),t.titleFired||Hi(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&Fi();break}}import{render as Bl}from"ink";import{resolveAuth as Hl}from"document360-engine";import{useCallback as H,useEffect as ot,useMemo as Bt,useRef as z,useState as U}from"react";import{Box as X,Text as b,useApp as sl,useInput as il,useStdout as ll}from"ink";import{existsSync as al,readFileSync as Tr,readdirSync as cl}from"node:fs";import{basename as Sr,isAbsolute as ul,join as zt}from"node:path";import{createSession as Rr,loginPkce as dl,toStoredTokens as pl,saveTokens as ml,getAccessToken as fl,resolveActiveProfile as ae,resolveProjectId as gl,getArticle as hl,decodeJwtClaims as He,isExpired as $e,loadTokens as De,setTitle as kl,slugify as wl,touchSession as Tn,upsertSession as yl,generateTitle as jr,findByName as xl,listSessions as $l,renameSession as bl,suggestNextAction as vl,readProjectConfig as pe,writeProjectConfig as Cl,resolveModelSetting as qt,loadProfileMap as Ar,applyPull as Pl,computeSyncStatus as Sn,planPull as Tl,inventoryRepo as Sl,knownEnvironments as Rl,resolveEnvironment as jl,planPartitions as Al,trackedArticlePaths as Il,runPartitioned as Dl,estimateBulkCost as El}from"document360-engine";Dt();import{statSync as tl}from"node:fs";import{resolve as nl}from"node:path";import _ from"picocolors";import ar from"wrap-ansi";import vn from"string-width";O();import xe from"picocolors";import wn from"wrap-ansi";import yn from"string-width";var or=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),rr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),sr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function zi(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(or(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&rr(t[o+1])){let p=sr(r);o+=2;let k=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)k.push(sr(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])&&!or(t[o])&&!(t[o].includes("|")&&o+1<t.length&&rr(t[o+1]));)u.push(t[o++]);n.push({kind:"para",text:u.join(" ")})}return n}function xn(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 Gi=(e,t)=>e+" ".repeat(Math.max(0,t-yn(e)));function Xi(e,t,n){let o=e.length,r=e.map((N,E)=>Math.max(yn(N),...t.map(B=>yn(B[E]??"")))),i=Math.max(24,n),u=3*o+1,p=[...r],k=()=>p.reduce((N,E)=>N+E,0)+u,g=0;for(;k()>i&&g++<1e4;){let N=-1,E=6;for(let B=0;B<o;B++)p[B]>E&&(E=p[B],N=B);if(N===-1)break;p[N]-=1}let v=(N,E,B)=>xe.gray(N+p.map(F=>"\u2500".repeat(F+2)).join(E)+B),j=xe.gray("\u2502"),$=(N,E)=>{let B=p.map((oe,fe)=>{let re=N[fe]??"",se=E?xe.bold(re):xn(re);return wn(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} ${Gi(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((N,E)=>{R.push($(N,!1)),R.push(E===t.length-1?v("\u2514","\u2534","\u2518"):v("\u251C","\u253C","\u2524"))}),R.join(`
25
- `)}function Yi(e,t){switch(e.kind){case"heading":return xe.bold(e.text);case"hr":return xe.gray("\u2500".repeat(t));case"para":return wn(xn(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=wn(xn(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 Xi(e.header,e.rows,t)}}function $n(e,t){let n=Math.max(20,t);return zi(e).map(o=>Yi(o,n)).join(`
30
-
31
- `)}O();import Vi from"picocolors";var Ji=[127,86,217],Ki=[22,38,43],ir={T:Ji,E:Ki},nt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],bn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,Qi=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function lr(){if(!Vi.isColorSupported)return[];let e=nt[0].length,t=[];for(let n=0;n<nt.length;n+=2){let o="";for(let r=0;r<e;r++){let i=ir[nt[n][r]],u=n+1<nt.length?ir[nt[n+1][r]]:void 0;i&&u?o+=`${bn(i)}${Qi(u)}\u2580\x1B[49m\x1B[39m`:i?o+=`${bn(i)}\u2580\x1B[39m`:u?o+=`${bn(u)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function Cn(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 Zi(e,t){let n=[Ve("\u270E document360-writer")+_.gray(` v${e.version}`),_.gray(" Reads your code, writes your docs."),"",Be("Claude:",`${e.claude}${_.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),Be("Document360:",e.configured?e.loggedOut?_.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?_.gray(` (${e.sessionHint})`):""}`:_.yellow("not set up \u2014 run /init")),Be("Profile:",e.configured?`${e.profile}${_.gray(` (${e.apiUrl})`)}${e.prod?_.bold(_.yellow(" \u26A0 PRODUCTION")):""}`:_.gray("\u2014 (run /init)")),Be("Project:",e.project),Be("Mode:",_.gray(e.mode)),Be("cwd:",_.gray(e.cwd))],o=lr();if(o.length===0)return n.join(`
32
- `);let r=2,i=3,u=vn(o[0]);if(!(t>=r+u+i+Math.max(...n.map(v=>vn(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 el={error:_.red,warn:_.yellow,ok:_.green,info:_.gray};function Pn(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return Zi(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=>ar(g,i,{hard:!0}).split(`
36
- `)),k=Math.max(0,p.length-u);return k>0&&(p=[...p.slice(0,u),_.dim(`\u2026 +${k} more lines`)]),`
37
- `+p.map((g,v)=>o+(v===0?y(" \u276F "):" ")+g+" ".repeat(Math.max(0,i-vn(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
- `+$n(e.text,n);case"tool":{let o=e.arg!==null?_.gray(`${e.sep}(${e.arg})`):"";return`
40
- `+ar(_.green("\u25CF ")+_.bold(e.title)+o,n)}case"tool-result":{let o=e.isError?_.red:_.gray,r=e.lines.map((i,u)=>o((u===0?" \u23BF ":" ")+i));return e.hidden>0&&r.push(_.dim(` \u2026 +${e.hidden} lines`)),r.join(`
41
- `)}case"diff":{let o=u=>u===1?"":"s",i=[_.gray(` \u23BF Added ${e.added} line${o(e.added)}, removed ${e.removed} line${o(e.removed)}`),...e.lines];return e.hidden>0&&i.push(_.dim(` \u2026 +${e.hidden} more diff lines`)),i.join(`
42
- `)}case"link":return e.lines.map(o=>y(Je(` \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
- `+Ve(`\u25A3 Preview \u2014 ${e.name}`)+`
38
+ `+Xe(`\u25A3 Preview \u2014 ${e.name}`)+`
45
39
 
46
- `+$n(e.text,n);case"note":return`
47
- `+el[e.tone](Je(e.text));case"done":return`
48
- `+(e.ok?_.magenta("\u2736 "):_.red("\u2736 "))+_.gray(`Cooked for ${Cn(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${Ie(e.costUsd)}`:""))}}function cr(e,t){return e.map(n=>Pn(n,t)).join(`
49
- `)}function ur(e,t){return t.map(n=>{let o=0;try{o=tl(nl(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function dr(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 pr(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 mr(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 Ut=[{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 fr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return Ut.filter(n=>n.name.startsWith(t))}function gr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}var ol=/^(?:\d+[.)]|[-*•])\s+/;function hr(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(ol,""),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}O();var rl=/\[Pasted text #\d+ \+\d+ lines?\]/g;function kr(e){return e.replace(/\r\n?/g,`
53
- `)}function wr(e){return e.includes(`
54
- `)||e.length>200}function yr(e,t){let n=t.split(`
55
- `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function xr(e,t){return e.replace(rl,n=>t.get(n)??n)}function $r(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function Wt(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 Ft(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function br(e,t,n){let o=Ft(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 vr(e,t,n){let o=e[Ft(e,t)];return n==="start"?o.start:o.end}function Cr(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 Pr(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 Rn,jsx as C,jsxs as L}from"react/jsx-runtime";var Ml={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function _l(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",i=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:r,model:i.model??"Claude Code default model",modelSource:Ml[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 Ll(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 Ir=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],Dr=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Nl="Ask me to write or update an article\u2026";function Ht({ch:e,dim:t}){let[n,o]=U(!0);return ot(()=>{let r=setInterval(()=>o(i=>!i),530);return()=>clearInterval(r)},[]),C(b,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var Ol=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Er(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Ul=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Mr(e){try{return cl(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Wl(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>Mr(zt(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&Mr(zt(e,o)))}function Fl({startTime:e,chars:t}){let[n,o]=U(0);ot(()=>{let k=setInterval(()=>o(g=>g+1),120);return()=>clearInterval(k)},[]);let r=Dr[n%Dr.length],i=Ir[Math.floor(n/16)%Ir.length],u=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return L(X,{children:[C(b,{color:W,children:` ${r} ${i}\u2026 `}),C(b,{color:"gray",children:`(${Cn(u)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}function _r({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=sl(),[i,u]=U(n),[p,k]=U(null),[g,v]=U({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}))},[]),N=H(()=>{v(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),s=$r(l)??l.slice(0,-1);return{text:s+a.text.slice(a.pos),pos:s.length}})},[]),[E,B]=U(!1),[F,Q]=U(!1),[oe,fe]=U(0),[re,se]=U(null),[qe,Y]=U([]),rt=z(0),[Ur,In]=U(!1),[Wr,Gt]=U(0),st=z(0),[ze,Ee]=U(0),[Me,it]=U(null),Dn=z(new Map),Fr=z(0),Xt=z([]),Z=z(null);Z.current===null&&(Z.current=Rr({cwd:e,profileName:i,allowProdWrites:!1}));let be=z({uuid:null,firstPrompt:null,titleFired:!1}),lt=z(new Map),Yt=z(!1),Vt=z([]),ge=z([]),[ve,Ce]=U(null),[ce,_e]=U(null),[he,Le]=U(null),[V,Ne]=U(null),[ue,Oe]=U(null),[ee,Pe]=U(null),[J,Ue]=U(null),[En,Jt]=U([]),Te=z([]),at=z(!1),Ge=z(null),ct=z(!1),ut=z(null),[te,Se]=U(null),[dt,Mn]=U(0),[_n,Kt]=U(0),[Br,Ln]=U(0),Qt=Bt(()=>{try{return qt(e).model??"Claude Code default"}catch{return null}},[e,Br]),{stdout:ie}=ll(),[,Hr]=U(0),Nn=z(`${ie.columns??80}x${ie.rows??24}`),ke=H(()=>Math.max(20,(ie.columns??80)-1),[ie]),d=H(a=>{Xt.current.push(a),console.log(Pn(a,ke()))},[ke]);ot(()=>{if(d({kind:"banner",info:_l(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?fl({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)&&Wl(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{}},[]),ot(()=>{let a=null,l=null,s=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let h=`${ie.columns??80}x${ie.rows??24}`;h!==Nn.current&&(Nn.current=h,Hr(m=>m+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+cr(Xt.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),On=Bt(()=>Ll(e,i),[e,i,oe]),Un=Bt(()=>{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 Nl},[e,i,oe]),Xe=fr(j),Wn=Xe.length>0&&!E,Zt=p!==null?Cr(p,8,Re):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=Rr({cwd:e,resume:a,profileName:l??i,allowProdWrites:Yt.current}),a||(be.current={uuid:null,firstPrompt:null,titleFired:!1}),Mn(0),Kt(0)},[e,i]),Fn=H((a,l,s)=>{if(Ol.test(a))try{let h=ae(e,i),m=typeof l.project_id=="string"&&l.project_id||h.project.projectId,f=Nt(l,s),w=[],A=Ot(s);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,i,d]),je=H(async(a,l)=>{In(!0),se(null),Y([]);let s=++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)},T=()=>{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();yl({uuid:P.sessionId,name:wl(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=Pr(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&&(T(),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&&(T(),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),T();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||Fn(I.name,I.input,P.output)}}else if(P.type==="result"){T(),Mn(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?hr(m,Ut.map(q=>q.name)):[];if(I.length>0?Y(I):P.ok&&m.trim()&&vl(a,m,e).then(q=>{q&&rt.current===s&&se(q)}).catch(()=>{}),h.uuid&&(Tn(h.uuid),!h.titleFired)){h.titleFired=!0;let q=h.uuid,Yn=h.firstPrompt;Yn&&jr(Yn,e).then(Vn=>Vn&&kl(q,Vn)).catch(()=>{})}}else P.type==="error"&&(T(),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,Y(["/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,Fn,ke]),en=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"},[]),tn=H((a,l)=>{if(l){let s=pe(e);s&&(s.defaultProfile=a,Cl(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.`}),en(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,en]),Bn=H((a,l,s)=>{wt(e,a,l,s.id),d({kind:"note",tone:"ok",text:`Switched to workspace "${s.name??s.id}" (agent restarted).`}),de(),Ar(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 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]),Hn=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{Pl({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 ft(),!0},[e,i,ft,d]),qn=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:Ut.map(m=>` ${m.usage.padEnd(22)} ${m.desc}`).join(`
64
- `)});return;case"exit":case"quit":Z.current?.close(),r();return;case"clear":de(),Xt.current=[],In(!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,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 dl(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=pl(m.name,f);ml(w),rn(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."}),Y(["/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,i).production}catch{}if(!m){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Yt.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??"";jr(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}bl(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:en(P)})),D=i??f?.defaultProfile,T=Math.max(0,A.findIndex(P=>P.name===D));_e({cursor:T,current:T,rows:A});return}if(m==="add"){let w=pn(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]}).`}),tn(h[1],!1);return}if(!f?.profiles?.[m]){d({kind:"note",tone:"error",text:`Unknown profile "${m}". Create it: /profile add ${m} <environment>`});return}tn(m,!0);return}case"doctor":await tt(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=Rl().map(f=>({name:f,apiUrl:jl(f).apiUrl}));Le({cursor:0,rows:m});return}case"resume":{let m=h.join(" ").trim(),f=$l(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=xl(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},Tn(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,i)}catch(A){d({kind:"note",tone:"error",text:`Could not list workspaces: ${A.message}`});return}if(!m){let A=f.workspaces.map(T=>({id:T.id,name:T.name??T.id,type:T.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(T=>T.id===f.current));Ne({cursor:D,current:D,rows:A,profile:f.profile,projectId:f.projectId,environment:f.environment});return}let w=on(f.workspaces,m);if(!w){d({kind:"note",tone:"error",text:`No workspace matches "${m}".`});return}Bn(f.profile,f.projectId,w);return}case"publish":{if(h[0]&&h[0]!=="--all"){await je(St(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 Sn({cwd:e,profileName:i}),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;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(Ar(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=ul(m)?m:zt(e,m);if(al(f)){try{d({kind:"preview",name:Sr(f),text:Er(Tr(f,"utf8"))})}catch(w){d({kind:"note",tone:"error",text:`Could not read ${f}: ${w.message}`})}return}if(Ul.test(m)){try{let w=ae(e,i),A={profile:w.name,connection:w.connection},D=w.project.projectId??gl(A),T=await hl(A,D,m);d({kind:"preview",name:T.title??m,text:T.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}=et(e,m);for(let D of f)d({kind:"note",tone:D.startsWith("\u26A0")?"warn":D.startsWith("\u2713")?"ok":"info",text:D});w&&(Ln(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=Il(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=Al(m),w=3;if(!(h.includes("--run")||h.includes("--yes"))){let T=El({files:ur(e,m),op:"convert",model:qt(e).model});d({kind:"note",tone:"info",text:pr(f,T,w).join(`
65
- `)}),Y(["/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 T of Dl({cwd:e,partitions:f,promptFor:dr,concurrency:w,profileName:i,allowProdWrites:Yt.current,signal:D.signal}))if(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}`});else if(T.type==="run_done"){Kt(I=>I+T.totalCostUsd);let P=T.aborted?"Stopped. ":"";d({kind:"note",tone:T.aborted?"warn":T.ok?"ok":"warn",text:P+mr(T.results,T.totalCostUsd).join(`
66
- `)})}}catch(T){d({kind:"note",tone:"error",text:`Convert run failed: ${T.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 Sn({cwd:e,profileName:i});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 Sn({cwd:e,profileName:i})).entries.filter(T=>T.status==="remote-ahead"&&T.path).map(T=>T.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 Tl({cwd:e,profileName:i,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=Sl(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"?Rt:Et)(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,ft,d,de,je]),gt=H(a=>{let l=(a??j).trim();if($(""),it(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||Hn(l))return;Vt.current.push(l);let s=xr(l,Dn.current);l.startsWith("/")?qn(s):je(s,{display:l})},[j,Hn,qn,je]),zn=H(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=kr(a);if(wr(l)){let s=yr(++Fr.current,l);Dn.current.set(s,l),R(s)}else R(l);return}R(a)},[R]),We=Math.max(10,Re-6),Gn=Bt(()=>Wt(g.text,We),[g.text,We]),nn=H(a=>v(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),ht=H(a=>v(l=>({...l,pos:br(Wt(l.text,We),l.pos,a)})),[We]),Ye=H(a=>v(l=>({...l,pos:vr(Wt(l.text,We),l.pos,a)})),[We]),qr=["\x1B[H","\x1B[1~","\x1BOH"],zr=["\x1B[F","\x1B[4~","\x1BOF"],Xn=H((a,l)=>l.leftArrow?(nn(-1),!0):l.rightArrow?(nn(1),!0):a&&qr.includes(a)?(Ye("start"),!0):a&&zr.includes(a)?(Ye("end"),!0):l.ctrl&&a==="a"?(Ye("start"),!0):l.ctrl&&a==="e"?(Ye("end"),!0):!1,[nn,Ye]);return il((a,l)=>{if(l.ctrl&&a==="c"){Z.current?.close(),r();return}if(!F){if(E){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 s=j.trim();if(!s)return;Te.current.push(s),Jt([...Te.current]),$("");return}if(Xn(a,l))return;if(l.upArrow){ht(-1);return}if(l.downArrow){ht(1);return}if(l.backspace||l.delete){N();return}a&&!l.ctrl&&!l.meta&&zn(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}=et(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&&(Ln(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),tn(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),!fo(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),Bn(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}un(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"?Ze(h):St(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,pt.length-1),s.cursor+1)});return}if(l.return){let s=pt[ee.cursor];if(s){Pe(null);try{d({kind:"preview",name:Sr(s),text:Er(Tr(zt(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,mt.length-1),s.cursor+1)});return}if(l.return){let s=mt[te.cursor];s&&(Se(null),de(s.uuid),be.current={uuid:s.uuid,firstPrompt:s.firstPrompt,titleFired:!0},Tn(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),rt.current++;return}if(!j&&qe.length>0&&a&&/^[1-9]$/.test(a)){let s=qe[Number(a)-1];if(s){$(s);return}}if(!Xn(a,l)){if(Wn){if(l.upArrow){Ee(s=>Math.max(0,s-1));return}if(l.downArrow){Ee(s=>Math.min(Xe.length-1,s+1));return}if(l.tab){$("/"+(Xe[ze]?.name??"")+" "),Ee(0);return}if(l.return){let s=Xe[ze];if(s){let h=j.trim().slice(1).split(/\s+/).slice(1).join(" ");if(gr(s.usage)&&!h){$("/"+s.name+" "),Ee(0);return}gt("/"+s.name+(h?" "+h:""));return}}}else{if(l.upArrow){if(j!==""&&Me===null){ht(-1);return}let s=Vt.current;if(!s.length)return;let h=Me===null?s.length-1:Math.max(0,Me-1);it(h),$(s[h]??"");return}if(l.downArrow){if(j!==""&&Me===null){ht(1);return}let s=Vt.current;if(Me===null)return;let h=Me+1;h>=s.length?(it(null),$("")):(it(h),$(s[h]??""));return}}if(l.return){gt();return}if(l.backspace||l.delete){N();return}if(l.escape){$(""),Ee(0),se(null),Y([]);return}a&&!l.ctrl&&!l.meta&&zn(a)}}}),ot(()=>{if(E||F)return;let a=Te.current.shift();a!==void 0&&(Jt([...Te.current]),gt(a))},[E,F,gt]),L(X,{flexDirection:"column",width:Re,children:[Zt!==null&&L(X,{marginTop:1,flexDirection:"column",children:[Zt.truncated&&C(b,{dimColor:!0,children:"\u2026"}),C(b,{children:Zt.text})]}),E&&C(Fl,{startTime:st.current,chars:Wr}),C(X,{borderStyle:"round",borderColor:On.prod?"yellow":W,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:j?Gn.map((a,l)=>{let s=g.text.slice(a.start,a.end),h=l===Ft(Gn,g.pos),m=Math.min(g.pos,a.end)-a.start;return L(b,{children:[C(b,{color:W,children:l===0?"> ":" "}),h?L(Rn,{children:[s.slice(0,m),C(Ht,{ch:s[m]??" "}),s.slice(m+1)]}):s||" "]},`${l}-${a.start}`)}):L(b,{children:[C(b,{color:W,children:"> "}),re&&!E?L(Rn,{children:[C(Ht,{ch:re[0],dim:!0}),C(b,{color:"gray",children:re.slice(1)}),C(b,{dimColor:!0,children:" (tab)"})]}):Ur?C(Ht,{ch:" "}):L(Rn,{children:[C(Ht,{ch:Un[0],dim:!0}),C(b,{color:"gray",children:Un.slice(1)})]})]})}),En.length>0&&C(X,{flexDirection:"column",paddingX:1,children:En.map((a,l)=>C(b,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),ve?L(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)=>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?L(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Switch connection profile"}),ce.rows.map((a,l)=>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?L(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Pick your Document360 environment"}),he.rows.map((a,l)=>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?L(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)=>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 L(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 L(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: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?L(X,{flexDirection:"column",paddingX:1,children:[C(b,{color:W,bold:!0,children:"Publish which article?"}),ue.rows.map((a,l)=>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?L(X,{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)=>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?L(X,{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)=>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"})]}):Wn?C(X,{flexDirection:"column",children:Xe.map((a,l)=>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"&&Qt?`${a.desc} (currently ${Qt})`:a.desc})]},a.name))}):!j&&qe.length>0?L(X,{flexDirection:"column",paddingX:1,children:[qe.map((a,l)=>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:L(b,{color:"gray",children:[On.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${Qt??"model n/a"}${dt>0?` \xB7 ${dt<1e3?`${dt} tokens`:`${(dt/1e3).toFixed(1)}k tokens`}`:""}${_n>0?` \xB7 ${Ie(_n)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}O();import{jsx as ql}from"react/jsx-runtime";async function Lr(e=process.cwd(),t="auto",n,o="0.0.0"){let r=Hl(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}=Bl(ql(_r,{cwd:e,auth:r,profileName:n,version:o}));await i(),process.stdout.write(`
68
- `),process.exit(0)}var Yl=Gl(import.meta.url),Nr=Yl("../package.json"),me=new zl;function jn(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=>{jn(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=>{jn(e),await so({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=>{jn(e),await ro({profile:e.profile})});var An=me.command("profile").description("Manage connection profiles for the current repo");An.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>bt(process.cwd()));An.command("use <name>").description("Set the default profile for this repo").action(e=>vt(process.cwd(),e));An.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ct(process.cwd(),e));var Or=me.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");Or.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));Or.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await Zn(process.cwd(),e,t.profile)});me.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>lo());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(()=>(Dt(),Ho)),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(Nr.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(Xl.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 ao(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 nr(e.cwd,e.auth,e.profile):await Lr(e.cwd,e.auth,e.profile,Nr.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.10",
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": {