document360-writer 0.4.6 → 0.4.8

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