document360-writer 0.4.50 → 0.4.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1,68 +1,68 @@
1
1
  #!/usr/bin/env node
2
- var Ri=Object.defineProperty;var sr=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var ji=(e,t)=>{for(var o in t)Ri(e,o,{get:t[o],enumerable:!0})};import Ee from"picocolors";var G,Ai,Ei,Di,ir,ht,f,b,O,I,E,ee,Ce,q=sr(()=>{"use strict";G="#7f56d9",[Ai,Ei,Di]=[127,86,217],ir=e=>Ee.isColorSupported?`\x1B[38;2;${Ai};${Ei};${Di}m${e}\x1B[39m`:e,ht=e=>Ee.bold(ir(e)),f=e=>Ee.dim(e),b=e=>Ee.red(e),O=e=>Ee.yellow(e),I=e=>Ee.green(e),E=ir,ee=e=>Ee.gray(e),Ce=e=>Ee.bold(e)});var fs={};ji(fs,{doctorCommand:()=>Pt,renderDoctorChecks:()=>ps,runDoctorChecks:()=>ds});import{existsSync as bl}from"node:fs";import{d360GetAll as vl,getAccessToken as Cl,isExpired as mn,loadProfileMap as Pl,loadTokens as Sl,packageSkillsDir as us,projectConfigPath as Tl,readProjectConfig as Rl,resolveActiveProfile as jl,resolveAuth as Al,resolveModelSetting as El}from"document360-engine";async function ds(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=Al("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=El(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Rl(e);if(!s)return t.push({level:"fail",label:`No ${Tl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Sources: docs grounded in ${p.length} path(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=jl(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.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 h=Sl(g.name);h?mn(h)&&!h.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):mn(h)?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(h.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${g.name})`}),t.push(g.project.workspaceId?{level:"ok",label:`Workspace: ${g.project.workspaceName??g.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let $=Pl(e,g.name);if($?$.projectId&&g.project.projectId&&$.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${$.projectId}) \u2260 profile projectId (${g.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys($.articles).length} articles, ${Object.keys($.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(bl(us())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${us()}`,fix:"Reinstall document360-writer (broken install)"}),h&&(!mn(h)||h.refreshToken)){let R={profile:g.name,connection:g.connection};try{await Cl(R);let y=await vl(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${y.length} project(s) visible`})}catch(y){t.push({level:"fail",label:`API call failed: ${y.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function ps(e){let t=[""];for(let r of e){let s=r.level==="ok"?I("\u2713"):r.level==="warn"?O("\u26A0"):b("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${E(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?I(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):b(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function Pt(e,t){console.log(f("Running checks\u2026"));for(let o of ps(await ds(t.cwd)))console.log(o);return{kind:"continue"}}var io=sr(()=>{"use strict";q()});import{Command as Ou}from"commander";import{createRequire as Uu}from"node:module";import{AUTH_MODES as Wu}from"document360-engine";import{input as Hi}from"@inquirer/prompts";import{loginPkce as Bi,refreshTokens as qi,toStoredTokens as ur,clearTokens as Gi,decodeJwtClaims as cr,isExpired as zi,loadTokens as Yi,saveTokens as dr,resolveActiveProfile as qt,setProfileProject as Vi,readProjectConfig as Xi}from"document360-engine";q();import{select as Ii}from"@inquirer/prompts";import{resolveActiveProfile as _i,setProfileProject as Ni,resolveProjectId as Mi,listWorkspaces as Li}from"document360-engine";async function wt(e,t){let o=_i(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??Mi(n);return{workspaces:await Li(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var ar=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function No(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function Bt(e,t,o,n,r){Ni(e,t,{projectId:o,workspaceId:n,workspaceName:r})}async function lr(e,t,o){let n;try{n=await wt(e,o)}catch(s){return console.log(b(`Could not list workspaces: ${s.message}`)),1}let r=No(n.workspaces,t);return r?(Bt(e,n.profile,n.projectId,r.id,r.name),console.log(I(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(b(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function lt(e,t){let o;try{o=await wt(e,t)}catch(h){console.log(b(`Could not list workspaces: ${h.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let h of n)console.log(` ${h.id===c?E("\u25CF"):" "} ${ar(h)} ${f(h.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let p=await Ii({message:"Select the Document360 workspace for this repo:",choices:n.map(h=>({name:`${ar(h)}${h.id===c?" (current)":""}`,value:h.id}))}),g=n.find(h=>h.id===p);Bt(e,s,r,p,g?.name),console.log(I(`\u2713 Workspace set to "${g?.name??p}" for profile "${s}".`))}q();import Oi from"picocolors";function Ui(e=process.env){return e.FORCE_HYPERLINK==="0"||!Oi.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 Wi(e,t=e,o){return Ui(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Fi=/https?:\/\/[^\s\x1b]+/g;function kt(e,t){return e.replace(Fi,o=>Wi(o,o,t))}function pr(e){return{...cr(e.idToken)??{},...cr(e.accessToken)??{}}}function Gt(e){let t=pr(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 zt(e){let t=qt(process.cwd(),e.profile),o=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Bi(o,{manual:e.manual,promptForRedirect:s=>Hi({message:s})},s=>console.log(kt(s))),r=ur(t.name,n);if(dr(r),Mo(r,t.name,s=>console.log(f(s))),console.log(""),console.log(I(`\u2713 Logged in to "${t.name}" as ${Gt(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{Xi(process.cwd())?.profiles?.[t.name]&&(console.log(""),await lt(process.cwd(),t.name))}catch{}}function Mo(e,t,o){let r=pr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(qt(process.cwd(),t).project.projectId)return;Vi(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function fr(e){let t=qt(process.cwd(),e.profile),o=t.connection,n=Yi(t.name);if(!n){console.log(b(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${E(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Gt(n)}`),zi(n))if(n.refreshToken)try{let r=ur(t.name,await qi(o,n.refreshToken));dr(r),console.log(I(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(O(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(O("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${n.expiresAt}`))}async function mr(e){let t=qt(process.cwd(),e.profile);Gi(t.name)?console.log(I(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}q();import{readProjectConfig as gr,writeProjectConfig as Ki,resolveActiveProfile as Ji,loadTokens as Qi,isExpired as Zi}from"document360-engine";function Yt(e){let t=gr(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(b("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?E("\u25CF "):" ",s=n.production?O(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",p=Qi(o),g=p?Zi(p)&&!p.refreshToken?O("expired"):f("logged in"):f("not logged in");console.log(`${r}${E(o)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Vt(e,t){let o=gr(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(b(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,Ki(o,e);let n=o.profiles[t].production?O(" \u26A0 PRODUCTION"):"";console.log(I(`\u2713 Default profile is now "${t}"${n}`))}function Xt(e,t){try{let o=Ji(e,t);console.log(""),console.log(`Profile ${E(o.name)}${o.production?O(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${o.connection.apiUrl}`)),console.log(f(` identity: ${o.connection.authorizationUrl}`)),console.log(f(` clientId: ${o.connection.clientId}`)),console.log(f(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(f(` project: ${o.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(b(o.message)),process.exitCode=1}}q();import{existsSync as ea,readdirSync as ta,statSync as oa}from"node:fs";import{join as na}from"node:path";import{apiLogDir as ra}from"document360-engine";function hr(){let e=ra();if(console.log(""),console.log(`Document360 API logs: ${E(e)}`),!ea(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ta(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(oa(na(e,o)).size/1024).toFixed(1);console.log(` ${o} ${f(`${n} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}q();import{createSession as sa,resolveAuth as ia,findByName as aa,slugify as la,touchSession as ca,upsertSession as ua,resolveActiveProfile as da}from"document360-engine";async function wr(e,t,o,n,r,s){let c=ia(o);c.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let p=null;try{p=da(e,r)}catch(y){console.error(b(`Document360 profile error: ${y.message}`)),process.exit(2)}p.production&&(console.error(O(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),s||(console.error(b("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let g;if(n){let y=aa(e,n);y||(console.error(b(`No saved session matches "${n}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),g=y.uuid,console.error(f(`Resuming "${y.name}"`))}let h=sa({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),$=g??null,R=1;for await(let y of h.send(t))switch(y.type){case"session":if(!$){$=y.sessionId;let x=new Date().toISOString();ua({uuid:$,name:la(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:x,updatedAt:x})}break;case"text":process.stdout.write(y.delta);break;case"tool":console.error(ee(` \u2699 ${y.name}`));break;case"result":R=y.ok?0:1,console.error(f(`(${y.inputTokens}\u2192${y.outputTokens} tokens`+(y.costUsd>0?`, $${y.costUsd<.01?y.costUsd.toFixed(4):y.costUsd.toFixed(2)}`:"")+")")),y.ok||console.error(b("agent finished with an error result"));break;case"error":console.error(""),console.error(b(`agent error: ${y.message}`)),process.exit(1)}$&&ca($),process.stdout.write(`
3
- `),process.exit(R)}import{createInterface as kc}from"node:readline/promises";import{createSession as Rn,resolveAuth as yc,getSession as $c,setTitle as xc,slugify as bc,touchSession as Os,upsertSession as vc,generateTitle as Cc,resolveActiveProfile as Us,resolveModelSetting as Pc,readProjectConfig as Sc,decodeJwtClaims as Ms,isExpired as Tc,loadTokens as Rc}from"document360-engine";var yt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"project",usage:"/project [name]",desc:"Choose the Document360 project (picker; lists all you can access)",group:"start"},{name:"devhints",usage:"/devhints",desc:"Set up the dev\u2192docs hint channel (guide + paste block for your repo agent)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],pa=[{key:"start",label:"Start here"},{key:"docs",label:"Write & screenshots"},{key:"publish",label:"Publish & keep in sync"},{key:"setup",label:"Setup & health"},{key:"session",label:"Session"}];function Kt(){let e=Math.max(...yt.map(o=>o.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:o,label:n}of pa){t.push("",n);for(let r of yt.filter(s=>s.group===o))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function kr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return yt.filter(o=>o.name.startsWith(t))}function yr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Lo(){console.log("");for(let e of Kt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}q();import{getSession as fa}from"document360-engine";async function $r(e,t){let o=t.currentUuid(),n=o?fa(o):void 0;return console.log(n?f(`
4
- (conversation reset \u2014 "${n.name}" is still available via /resume)
2
+ var Ii=Object.defineProperty;var dr=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var _i=(e,t)=>{for(var n in t)Ii(e,n,{get:t[n],enumerable:!0})};import Ae from"picocolors";var q,Ni,Mi,Li,pr,ht,f,b,O,A,E,ee,ve,G=dr(()=>{"use strict";q="#7f56d9",[Ni,Mi,Li]=[127,86,217],pr=e=>Ae.isColorSupported?`\x1B[38;2;${Ni};${Mi};${Li}m${e}\x1B[39m`:e,ht=e=>Ae.bold(pr(e)),f=e=>Ae.dim(e),b=e=>Ae.red(e),O=e=>Ae.yellow(e),A=e=>Ae.green(e),E=pr,ee=e=>Ae.gray(e),ve=e=>Ae.bold(e)});var ws={};_i(ws,{doctorCommand:()=>St,renderDoctorChecks:()=>ks,runDoctorChecks:()=>hs});import{existsSync as jl}from"node:fs";import{d360GetAll as Al,getAccessToken as El,isExpired as yo,loadProfileMap as Dl,loadTokens as Il,packageSkillsDir as gs,projectConfigPath as _l,readProjectConfig as Nl,resolveActiveProfile as Ml,resolveAuth as Ll,resolveModelSetting as Ol}from"document360-engine";async function hs(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=Ll("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=Ol(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Nl(e);if(!s)return t.push({level:"fail",label:`No ${_l(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let p=s.authoritativeSourceFiles??[];t.push(p.length>0?{level:"ok",label:`Sources: docs grounded in ${p.length} path(s) (${p.slice(0,3).join(", ")}${p.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let g;try{g=Ml(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.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 k=Il(g.name);k?yo(k)&&!k.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):yo(k)?t.push({level:"warn",label:"Document360: token expired \u2014 will auto-refresh on next call"}):t.push({level:"ok",label:`Document360: logged in (until ${new Date(k.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${g.name})`}),t.push(g.project.workspaceId?{level:"ok",label:`Workspace: ${g.project.workspaceName??g.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let $=Dl(e,g.name);if($?$.projectId&&g.project.projectId&&$.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${$.projectId}) \u2260 profile projectId (${g.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys($.articles).length} articles, ${Object.keys($.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(jl(gs())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${gs()}`,fix:"Reinstall document360-writer (broken install)"}),k&&(!yo(k)||k.refreshToken)){let R={profile:g.name,connection:g.connection};try{await El(R);let w=await Al(R,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${w.length} project(s) visible`})}catch(w){t.push({level:"fail",label:`API call failed: ${w.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function ks(e){let t=[""];for(let r of e){let s=r.level==="ok"?A("\u2713"):r.level==="warn"?O("\u26A0"):b("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${E(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?A(`\u2713 ${o===0?"All checks passed":`Healthy (${o} warning${o===1?"":"s"})`}`):b(`\u2717 ${n} problem${n===1?"":"s"} found`)),t.push(""),t}async function St(e,t){console.log(f("Running checks\u2026"));for(let n of ks(await hs(t.cwd)))console.log(n);return{kind:"continue"}}var cn=dr(()=>{"use strict";G()});import{Command as Gu}from"commander";import{createRequire as zu}from"node:module";import{AUTH_MODES as Yu}from"document360-engine";import{input as Yi}from"@inquirer/prompts";import{loginPkce as Vi,refreshTokens as Xi,toStoredTokens as hr,clearTokens as Ki,decodeJwtClaims as gr,isExpired as Ji,loadTokens as Qi,saveTokens as kr,resolveActiveProfile as Yt,setProfileProject as Zi,readProjectConfig as ea}from"document360-engine";G();import{select as Oi}from"@inquirer/prompts";import{resolveActiveProfile as Ui,setProfileProject as Wi,resolveProjectId as Fi,listWorkspaces as Hi}from"document360-engine";async function kt(e,t){let n=Ui(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??Fi(o);return{workspaces:await Hi(o,r),projectId:r,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var fr=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Ln(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 zt(e,t,n,o,r){Wi(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function mr(e,t,n){let o;try{o=await kt(e,n)}catch(s){return console.log(b(`Could not list workspaces: ${s.message}`)),1}let r=Ln(o.workspaces,t);return r?(zt(e,o.profile,o.projectId,r.id,r.name),console.log(A(`\u2713 Workspace set to "${r.name??r.id}" for profile "${o.profile}".`)),0):(console.log(b(`No workspace matches "${t}". Available: ${o.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function lt(e,t){let n;try{n=await kt(e,t)}catch(k){console.log(b(`Could not list workspaces: ${k.message}`));return}let{workspaces:o,projectId:r,profile:s,current:c}=n;if(o.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let k of o)console.log(` ${k.id===c?E("\u25CF"):" "} ${fr(k)} ${f(k.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let p=await Oi({message:"Select the Document360 workspace for this repo:",choices:o.map(k=>({name:`${fr(k)}${k.id===c?" (current)":""}`,value:k.id}))}),g=o.find(k=>k.id===p);zt(e,s,r,p,g?.name),console.log(A(`\u2713 Workspace set to "${g?.name??p}" for profile "${s}".`))}G();import Bi from"picocolors";function qi(e=process.env){return e.FORCE_HYPERLINK==="0"||!Bi.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 Gi(e,t=e,n){return qi(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var zi=/https?:\/\/[^\s\x1b]+/g;function wt(e,t){return e.replace(zi,n=>Gi(n,n,t))}function wr(e){return{...gr(e.idToken)??{},...gr(e.accessToken)??{}}}function Vt(e){let t=wr(e),n=t.email??t.preferred_username??t.sub??"unknown",o=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${n}${o}`}async function Xt(e){let t=Yt(process.cwd(),e.profile),n=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${n.name} (${n.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let o=await Vi(n,{manual:e.manual,promptForRedirect:s=>Yi({message:s})},s=>console.log(wt(s))),r=hr(t.name,o);if(kr(r),On(r,t.name,s=>console.log(f(s))),console.log(""),console.log(A(`\u2713 Logged in to "${t.name}" as ${Vt(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{ea(process.cwd())?.profiles?.[t.name]&&(console.log(""),await lt(process.cwd(),t.name))}catch{}}function On(e,t,n){let r=wr(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Yt(process.cwd(),t).project.projectId)return;Zi(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function yr(e){let t=Yt(process.cwd(),e.profile),n=t.connection,o=Qi(t.name);if(!o){console.log(b(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${E(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Vt(o)}`),Ji(o))if(o.refreshToken)try{let r=hr(t.name,await Xi(n,o.refreshToken));kr(r),console.log(A(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(O(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(O("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${o.expiresAt}`))}async function $r(e){let t=Yt(process.cwd(),e.profile);Ki(t.name)?console.log(A(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}G();import{readProjectConfig as xr,writeProjectConfig as ta,resolveActiveProfile as na,loadTokens as oa,isExpired as ra}from"document360-engine";function Kt(e){let t=xr(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(b("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?E("\u25CF "):" ",s=o.production?O(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",p=oa(n),g=p?ra(p)&&!p.refreshToken?O("expired"):f("logged in"):f("not logged in");console.log(`${r}${E(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Jt(e,t){let n=xr(e);if(!n?.profiles?.[t]){let r=n?.profiles?Object.keys(n.profiles).join(", "):"(none \u2014 run init)";console.log(b(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}n.defaultProfile=t,ta(n,e);let o=n.profiles[t].production?O(" \u26A0 PRODUCTION"):"";console.log(A(`\u2713 Default profile is now "${t}"${o}`))}function Qt(e,t){try{let n=na(e,t);console.log(""),console.log(`Profile ${E(n.name)}${n.production?O(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${n.connection.apiUrl}`)),console.log(f(` identity: ${n.connection.authorizationUrl}`)),console.log(f(` clientId: ${n.connection.clientId}`)),console.log(f(` scopes: ${n.connection.scopes.join(" ")}`)),console.log(f(` project: ${n.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${n.project.workspaceId?" "+n.project.workspaceId:" (none)"}`)),console.log("")}catch(n){console.log(b(n.message)),process.exitCode=1}}G();import{existsSync as sa,readdirSync as ia,statSync as aa}from"node:fs";import{join as la}from"node:path";import{apiLogDir as ca}from"document360-engine";function br(){let e=ca();if(console.log(""),console.log(`Document360 API logs: ${E(e)}`),!sa(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=ia(e).filter(n=>n.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let n of t.slice(0,14)){let o=(aa(la(e,n)).size/1024).toFixed(1);console.log(` ${n} ${f(`${o} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}G();import{createSession as ua,resolveAuth as da,findByName as pa,slugify as fa,touchSession as ma,upsertSession as ga,resolveActiveProfile as ha}from"document360-engine";async function vr(e,t,n,o,r,s){let c=da(n);c.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let p=null;try{p=ha(e,r)}catch(w){console.error(b(`Document360 profile error: ${w.message}`)),process.exit(2)}p.production&&(console.error(O(`\u26A0 Profile "${p.name}" is PRODUCTION.`)),s||(console.error(b("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let g;if(o){let w=pa(e,o);w||(console.error(b(`No saved session matches "${o}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),g=w.uuid,console.error(f(`Resuming "${w.name}"`))}let k=ua({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),$=g??null,R=1;for await(let w of k.send(t))switch(w.type){case"session":if(!$){$=w.sessionId;let x=new Date().toISOString();ga({uuid:$,name:fa(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:x,updatedAt:x})}break;case"text":process.stdout.write(w.delta);break;case"tool":console.error(ee(` \u2699 ${w.name}`));break;case"result":R=w.ok?0:1,console.error(f(`(${w.inputTokens}\u2192${w.outputTokens} tokens`+(w.costUsd>0?`, $${w.costUsd<.01?w.costUsd.toFixed(4):w.costUsd.toFixed(2)}`:"")+")")),w.ok||console.error(b("agent finished with an error result"));break;case"error":console.error(""),console.error(b(`agent error: ${w.message}`)),process.exit(1)}$&&ma($),process.stdout.write(`
3
+ `),process.exit(R)}import{createInterface as Pc}from"node:readline/promises";import{createSession as Io,resolveAuth as Sc,getSession as Tc,setTitle as Rc,slugify as jc,touchSession as Bs,upsertSession as Ac,generateTitle as Ec,resolveActiveProfile as qs,resolveModelSetting as Dc,readProjectConfig as Ic,decodeJwtClaims as Fs,isExpired as _c,loadTokens as Nc}from"document360-engine";var yt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"project",usage:"/project [name]",desc:"Choose the Document360 project (picker; lists all you can access)",group:"start"},{name:"devhints",usage:"/devhints",desc:"Set up the dev\u2192docs hint channel (guide + paste block for your repo agent)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"write",usage:"/write [--all|--scope <cat>|<path>] [--run]",desc:"Author the planned articles in parallel (bare = preview + cost; --run starts)",group:"docs"},{name:"screenshot",usage:"/screenshot [--list|id|--all|path]",desc:"--list: status; else author capture specs (bulk also refreshes the capture checklist; --no-setup skips)",group:"docs"},{name:"capture-setup",usage:"/capture-setup",desc:"Re-run the data-staging checklist + dev test-id requests (auto-runs after /screenshot --all)",group:"docs"},{name:"preview",usage:"/preview [path|id]",desc:"Render an article (no arg: pick from tracked)",group:"docs"},{name:"publish",usage:"/publish [path|--all]",desc:"Publish to Document360 (no arg: pick; --all: every candidate)",group:"publish"},{name:"audit",usage:"/audit",desc:"Gap analysis: code vs docs vs Document360 (what changed)",group:"publish"},{name:"sync",usage:"/sync [pull <path>|--all]",desc:"Drift report local vs Document360; pull portal edits",group:"publish"},{name:"convert",usage:"/convert [--scope <folder>] [--run]",desc:"Convert tracked articles to DFM (one-off legacy migration)",group:"publish"},{name:"profile",usage:"/profile [name|add <name> [env]]",desc:"Switch/create a Document360 connection (picker; s = session)",group:"setup"},{name:"workspace",usage:"/workspace [name]",desc:"Switch the Document360 workspace (picker)",group:"setup"},{name:"model",usage:"/model [name|default]",desc:"Set the Claude model for d360-writer",group:"setup"},{name:"logout",usage:"/logout",desc:"Sign out + clear project/workspace selection (then /login to re-pick)",group:"setup"},{name:"allow-prod",usage:"/allow-prod",desc:"Authorize writes to a production profile",group:"setup"},{name:"doctor",usage:"/doctor",desc:"Health-check: auth, profile, workspace, map, API",group:"setup"},{name:"reset",usage:"/reset",desc:"[DANGER] Delete all d360-writer files (docs, config, screenshots); types repo name to confirm",group:"setup"},{name:"resume",usage:"/resume [name]",desc:"Resume a session (no arg lists them)",group:"session"},{name:"rename",usage:"/rename [name]",desc:"Name the session (no arg: suggest one)",group:"session"},{name:"clear",usage:"/clear",desc:"Reset the conversation (resumable)",group:"session"},{name:"help",usage:"/help",desc:"Show this help",group:"session"},{name:"exit",usage:"/exit",desc:"Quit",group:"session"}],ka=[{key:"start",label:"Start here"},{key:"docs",label:"Write & screenshots"},{key:"publish",label:"Publish & keep in sync"},{key:"setup",label:"Setup & health"},{key:"session",label:"Session"}];function Zt(){let e=Math.max(...yt.map(n=>n.usage.length))+2,t=["document360-writer \u2014 commands","",'New here? /init \u2192 "write the docs for this repo" \u2192 /publish',""," Screenshots are optional \u2014 add them anytime:"," /screenshot --all \u2192 d360-capture capture \u2192 re-/publish"];for(let{key:n,label:o}of ka){t.push("",o);for(let r of yt.filter(s=>s.group===n))t.push(` ${r.usage.padEnd(e)}${r.desc}`)}return t.push("","Tip: anything not starting with / is sent to the agent."),t}function Cr(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return yt.filter(n=>n.name.startsWith(t))}function Pr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Un(){console.log("");for(let e of Zt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}G();import{getSession as wa}from"document360-engine";async function Sr(e,t){let n=t.currentUuid(),o=n?wa(n):void 0;return console.log(o?f(`
4
+ (conversation reset \u2014 "${o.name}" is still available via /resume)
5
5
  `):f(`
6
6
  (conversation reset \u2014 agent will start fresh on the next prompt)
7
- `)),{kind:"clear"}}async function Oo(){return{kind:"exit"}}q();import{input as $t,confirm as ma}from"@inquirer/prompts";import{basename as xr,join as Jt}from"node:path";import{existsSync as ct,readFileSync as ga,readdirSync as ha,writeFileSync as wa}from"node:fs";import{writeProjectConfig as br,readProjectConfig as ka,projectConfigPath as vr,writerDir as ya}from"document360-engine";function Cr(e,t="berlin"){let o=vr(e);if(ct(o))return{created:!1,path:o,profileName:""};let n={projectId:Tr(e)??xr(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:Rr(e)};return br(n,e),Pr(e),{created:!0,path:o,profileName:t}}function Pr(e){let t=Jt(ya(e),".gitignore");ct(t)||wa(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
- `),"utf8")}async function Sr(){let e=process.cwd(),t=vr(e);if(ct(t)&&!await ma({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let o=Tr(e)??xr(e),n=await $t({message:"Project ID (used to scope sessions, screenshots, etc.):",default:o}),r=await $t({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await $t({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await $t({message:"Default connection profile name:",default:"berlin"}),p=await $t({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:n,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:Rr(e)},h=ka(e);return h?.terminologyGlossary&&(g.terminologyGlossary=h.terminologyGlossary),br(g,e),Pr(e),console.log(""),console.log(I(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${E(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Tr(e){let t=Jt(e,"package.json");if(!ct(t))return null;try{return JSON.parse(ga(t,"utf8")).name??null}catch{return null}}function Rr(e){let t=[];for(let o of["README.md","ARCHITECTURE.md","CLAUDE.md"])ct(Jt(e,o))&&t.push(o);for(let o of["src","api","docs"]){let n=Jt(e,o);ct(n)&&!$a(n)&&t.push(o)}return t}function $a(e){try{return ha(e,{withFileTypes:!0}).filter(o=>o.isDirectory()&&!o.name.startsWith(".")).length>6}catch{return!1}}q();import{readMcpConfig as Uo,writeMcpConfig as jr}from"document360-engine";async function Qt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(xa(),{kind:"continue"}):t==="add"?(ba(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(va(e.slice(1)),{kind:"continue"}):(console.log(b(`Unknown /mcp subcommand: ${t}`)),console.log(f("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function xa(){let e=Uo(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
7
+ `)),{kind:"clear"}}async function Wn(){return{kind:"exit"}}G();import{input as $t,confirm as ya}from"@inquirer/prompts";import{basename as Tr,join as en}from"node:path";import{existsSync as ct,readFileSync as $a,readdirSync as xa,writeFileSync as ba}from"node:fs";import{writeProjectConfig as Rr,readProjectConfig as va,projectConfigPath as jr,writerDir as Ca}from"document360-engine";function Ar(e,t="berlin"){let n=jr(e);if(ct(n))return{created:!1,path:n,profileName:""};let o={projectId:Ir(e)??Tr(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:_r(e)};return Rr(o,e),Er(e),{created:!0,path:n,profileName:t}}function Er(e){let t=en(Ca(e),".gitignore");ct(t)||ba(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
8
+ `),"utf8")}async function Dr(){let e=process.cwd(),t=jr(e);if(ct(t)&&!await ya({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let n=Ir(e)??Tr(e),o=await $t({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await $t({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await $t({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await $t({message:"Default connection profile name:",default:"berlin"}),p=await $t({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:p},production:!1}},defaultProfile:c,authoritativeSourceFiles:_r(e)},k=va(e);return k?.terminologyGlossary&&(g.terminologyGlossary=k.terminologyGlossary),Rr(g,e),Er(e),console.log(""),console.log(A(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${E(`d360-writer login --profile ${c}`)} ${f("(sign in; pick the project)")}`),console.log(' Then ask the agent: "analyze this repo and propose a docs structure"'),console.log(""),{kind:"continue"}}function Ir(e){let t=en(e,"package.json");if(!ct(t))return null;try{return JSON.parse($a(t,"utf8")).name??null}catch{return null}}function _r(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])ct(en(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=en(e,n);ct(o)&&!Pa(o)&&t.push(n)}return t}function Pa(e){try{return xa(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}G();import{readMcpConfig as Fn,writeMcpConfig as Nr}from"document360-engine";async function tn(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Sa(),{kind:"continue"}):t==="add"?(Ta(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Ra(e.slice(1)),{kind:"continue"}):(console.log(b(`Unknown /mcp subcommand: ${t}`)),console.log(f("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Sa(){let e=Fn(),t=Object.keys(e.servers);if(t.length===0){console.log(f(`
9
9
  No MCP servers registered. Add one with /mcp add <name> <type> <ref>
10
- `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let o of t){let n=e.servers[o];if(n.type==="stdio")console.log(` ${E(o)} ${f("(stdio)")} ${n.command} ${(n.args??[]).join(" ")}`);else{let r=Object.keys(n.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${E(o)} ${f(`(${n.type})`)} ${n.url}${s}`)}}console.log("")}function ba(e){let[t,o,...n]=e;if(!t||!o||n.length===0){console.log(b("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(o!=="stdio"&&o!=="http"&&o!=="sse"){console.log(b(`Unknown transport: ${o}. Use stdio, http, or sse.`));return}let r=Uo(),s;if(o==="stdio")s={type:"stdio",command:n[0],args:n.slice(1)};else{let c={};for(let p=1;p<n.length;p++)if(n[p]==="-H"||n[p]==="--header"){let g=n[++p],h=g?.match(/^([^:=]+)[:=](.+)$/);if(!h){console.log(b(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[h[1].trim()]=h[2].trim()}else{console.log(b(`Unexpected argument: ${n[p]}. After the URL, only -H key:value is allowed.`));return}s={type:o,url:n[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,jr(r),console.log(""),console.log(I(`\u2713 Registered "${t}" (${o})`)),console.log(O(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function va(e){let t=e[0];if(!t){console.log(b("Usage: /mcp remove <name>"));return}let o=Uo();if(!o.servers[t]){console.log(b(`No server named "${t}".`));return}delete o.servers[t],jr(o),console.log(I(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}q();import{select as _a}from"@inquirer/prompts";import{computeSyncStatus as Lr,planPartitions as Na,resolveModelForOperation as Ma,runPartitioned as La}from"document360-engine";import z from"picocolors";import Nr from"wrap-ansi";import Go from"string-width";q();import De from"picocolors";import Wo from"wrap-ansi";import Fo from"string-width";var Ar=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Er=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Dr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function Ca(e){let t=e.replace(/\r/g,"").split(`
11
- `),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(Ar(r)){o.push({kind:"hr"}),n++;continue}if(r.includes("|")&&n+1<t.length&&Er(t[n+1])){let p=Dr(r);n+=2;let g=[];for(;n<t.length&&t[n].includes("|")&&t[n].trim()!=="";)g.push(Dr(t[n++]));o.push({kind:"table",header:p,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){o.push({kind:"heading",level:s[1].length,text:s[2]}),n++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let 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 c=[];for(;n<t.length&&t[n].trim()!==""&&!/^\s*```/.test(t[n])&&!/^(#{1,6})\s/.test(t[n])&&!/^\s*([-*]|\d+\.)\s+/.test(t[n])&&!Ar(t[n])&&!(t[n].includes("|")&&n+1<t.length&&Er(t[n+1]));)c.push(t[n++]);o.push({kind:"para",text:c.join(" ")})}return o}function Ho(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?De.bold(t.slice(2,-2)):t.startsWith("`")?E(t.slice(1,-1)):De.italic(t.slice(1,-1)))}var Pa=(e,t)=>e+" ".repeat(Math.max(0,t-Fo(e)));function Sa(e,t,o){let n=e.length,r=e.map((v,_)=>Math.max(Fo(v),...t.map(N=>Fo(N[_]??"")))),s=Math.max(24,o),c=3*n+1,p=[...r],g=()=>p.reduce((v,_)=>v+_,0)+c,h=0;for(;g()>s&&h++<1e4;){let v=-1,_=6;for(let N=0;N<n;N++)p[N]>_&&(_=p[N],v=N);if(v===-1)break;p[v]-=1}let $=(v,_,N)=>De.gray(v+p.map(H=>"\u2500".repeat(H+2)).join(_)+N),R=De.gray("\u2502"),y=(v,_)=>{let N=p.map(($e,te)=>{let we=v[te]??"",ne=_?De.bold(we):Ho(we);return Wo(ne,$e,{hard:!0}).split(`
12
- `)}),H=Math.max(...N.map($e=>$e.length)),Z=[];for(let $e=0;$e<H;$e++)Z.push(p.map((te,we)=>`${R} ${Pa(N[we][$e]??"",te)} `).join("")+R);return Z.join(`
13
- `)},x=[$("\u250C","\u252C","\u2510"),y(e,!0)];return x.push(t.length===0?$("\u2514","\u2534","\u2518"):$("\u251C","\u253C","\u2524")),t.forEach((v,_)=>{x.push(y(v,!1)),x.push(_===t.length-1?$("\u2514","\u2534","\u2518"):$("\u251C","\u253C","\u2524"))}),x.join(`
14
- `)}function Ta(e,t){switch(e.kind){case"heading":return De.bold(e.text);case"hr":return De.gray("\u2500".repeat(t));case"para":return Wo(Ho(e.text),t);case"list":return e.items.map(o=>{let[n="",...r]=Wo(Ho(o),Math.max(10,t-4)).split(`
15
- `);return" \u2022 "+n+r.map(s=>`
10
+ `));return}console.log(""),console.log("Registered MCP servers (~/.document360-writer/mcp.json):");for(let n of t){let o=e.servers[n];if(o.type==="stdio")console.log(` ${E(n)} ${f("(stdio)")} ${o.command} ${(o.args??[]).join(" ")}`);else{let r=Object.keys(o.headers??{}),s=r.length>0?f(` [headers: ${r.join(", ")}]`):"";console.log(` ${E(n)} ${f(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function Ta(e){let[t,n,...o]=e;if(!t||!n||o.length===0){console.log(b("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(n!=="stdio"&&n!=="http"&&n!=="sse"){console.log(b(`Unknown transport: ${n}. Use stdio, http, or sse.`));return}let r=Fn(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let p=1;p<o.length;p++)if(o[p]==="-H"||o[p]==="--header"){let g=o[++p],k=g?.match(/^([^:=]+)[:=](.+)$/);if(!k){console.log(b(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[k[1].trim()]=k[2].trim()}else{console.log(b(`Unexpected argument: ${o[p]}. After the URL, only -H key:value is allowed.`));return}s={type:n,url:o[0],headers:Object.keys(c).length>0?c:void 0}}r.servers[t]=s,Nr(r),console.log(""),console.log(A(`\u2713 Registered "${t}" (${n})`)),console.log(O(" This server loads on your next prompt. The current agent session reads MCP config at startup.")),console.log(f(" Run /clear if you want the next turn to reload immediately.")),console.log("")}function Ra(e){let t=e[0];if(!t){console.log(b("Usage: /mcp remove <name>"));return}let n=Fn();if(!n.servers[t]){console.log(b(`No server named "${t}".`));return}delete n.servers[t],Nr(n),console.log(A(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}G();import{select as Ua}from"@inquirer/prompts";import{computeSyncStatus as Kn,planPartitions as Wa,relatedArticlePaths as Fa,resolveModelForOperation as Ha,runPartitioned as Ba}from"document360-engine";import z from"picocolors";import Fr from"wrap-ansi";import Yn from"string-width";G();import Ee from"picocolors";import Hn from"wrap-ansi";import Bn from"string-width";var Mr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Lr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Or=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function ja(e){let t=e.replace(/\r/g,"").split(`
11
+ `),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(Mr(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Lr(t[o+1])){let p=Or(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(Or(t[o++]));n.push({kind:"table",header:p,rows:g});continue}let s=r.match(/^(#{1,6})\s+(.*)$/);if(s){n.push({kind:"heading",level:s[1].length,text:s[2]}),o++;continue}if(/^\s*([-*]|\d+\.)\s+/.test(r)){let 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 c=[];for(;o<t.length&&t[o].trim()!==""&&!/^\s*```/.test(t[o])&&!/^(#{1,6})\s/.test(t[o])&&!/^\s*([-*]|\d+\.)\s+/.test(t[o])&&!Mr(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Lr(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function qn(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?Ee.bold(t.slice(2,-2)):t.startsWith("`")?E(t.slice(1,-1)):Ee.italic(t.slice(1,-1)))}var Aa=(e,t)=>e+" ".repeat(Math.max(0,t-Bn(e)));function Ea(e,t,n){let o=e.length,r=e.map((v,I)=>Math.max(Bn(v),...t.map(_=>Bn(_[I]??"")))),s=Math.max(24,n),c=3*o+1,p=[...r],g=()=>p.reduce((v,I)=>v+I,0)+c,k=0;for(;g()>s&&k++<1e4;){let v=-1,I=6;for(let _=0;_<o;_++)p[_]>I&&(I=p[_],v=_);if(v===-1)break;p[v]-=1}let $=(v,I,_)=>Ee.gray(v+p.map(H=>"\u2500".repeat(H+2)).join(I)+_),R=Ee.gray("\u2502"),w=(v,I)=>{let _=p.map(($e,te)=>{let ke=v[te]??"",oe=I?Ee.bold(ke):qn(ke);return Hn(oe,$e,{hard:!0}).split(`
12
+ `)}),H=Math.max(..._.map($e=>$e.length)),Z=[];for(let $e=0;$e<H;$e++)Z.push(p.map((te,ke)=>`${R} ${Aa(_[ke][$e]??"",te)} `).join("")+R);return Z.join(`
13
+ `)},x=[$("\u250C","\u252C","\u2510"),w(e,!0)];return x.push(t.length===0?$("\u2514","\u2534","\u2518"):$("\u251C","\u253C","\u2524")),t.forEach((v,I)=>{x.push(w(v,!1)),x.push(I===t.length-1?$("\u2514","\u2534","\u2518"):$("\u251C","\u253C","\u2524"))}),x.join(`
14
+ `)}function Da(e,t){switch(e.kind){case"heading":return Ee.bold(e.text);case"hr":return Ee.gray("\u2500".repeat(t));case"para":return Hn(qn(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=Hn(qn(n),Math.max(10,t-4)).split(`
15
+ `);return" \u2022 "+o+r.map(s=>`
16
16
  `+s).join("")}).join(`
17
- `);case"code":return e.lines.map(o=>De.gray(" "+o)).join(`
18
- `);case"table":return Sa(e.header,e.rows,t)}}function Bo(e,t){let o=Math.max(20,t);return Ca(e).map(n=>Ta(n,o)).join(`
17
+ `);case"code":return e.lines.map(n=>Ee.gray(" "+n)).join(`
18
+ `);case"table":return Ea(e.header,e.rows,t)}}function Gn(e,t){let n=Math.max(20,t);return ja(e).map(o=>Da(o,n)).join(`
19
19
 
20
- `)}q();import Ra from"picocolors";var ja=[127,86,217],Aa=[22,38,43],Ir={T:ja,E:Aa},xt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],qo=([e,t,o])=>`\x1B[38;2;${e};${t};${o}m`,Ea=([e,t,o])=>`\x1B[48;2;${e};${t};${o}m`;function _r(){if(!Ra.isColorSupported)return[];let e=xt[0].length,t=[];for(let o=0;o<xt.length;o+=2){let n="";for(let r=0;r<e;r++){let s=Ir[xt[o][r]],c=o+1<xt.length?Ir[xt[o+1][r]]:void 0;s&&c?n+=`${qo(s)}${Ea(c)}\u2580\x1B[49m\x1B[39m`:s?n+=`${qo(s)}\u2580\x1B[39m`:c?n+=`${qo(c)}\u2584\x1B[39m`:n+=" "}t.push(n)}return t}function bt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function se(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function Ie(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var ut=(e,t)=>` ${e.padEnd(13)}${t}`;function Da(e,t){let o=[ht("\u270E document360-writer")+z.gray(` v${e.version}`),z.gray(" Reads your code, writes your docs."),"",ut("Claude:",`${e.claude}${z.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),ut("Document360:",e.configured?e.loggedOut?z.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?z.gray(` (${e.sessionHint})`):""}`:z.yellow("not set up \u2014 run /init")),ut("Profile:",e.configured?`${e.profile}${z.gray(` (${e.apiUrl})`)}${e.prod?z.bold(z.yellow(" \u26A0 PRODUCTION")):""}`:z.gray("\u2014 (run /init)")),ut("Project:",e.project),ut("Mode:",z.gray(e.mode)),ut("cwd:",z.gray(e.cwd))],n=_r();if(n.length===0)return o.join(`
21
- `);let r=2,s=3,c=Go(n[0]);if(!(t>=r+c+s+Math.max(...o.map($=>Go($)))))return[...n.map($=>" "+$),...o].join(`
22
- `);let g=Math.max(0,Math.floor((o.length-n.length)/2)),h=[];for(let $=0;$<Math.max(n.length+g,o.length);$++){let R=n[$-g]??" ".repeat(c);h.push((" ".repeat(r)+R+" ".repeat(s)+(o[$]??"")).trimEnd())}return h.join(`
23
- `)}var Ia={error:z.red,warn:z.yellow,ok:z.green,info:z.gray};function zo(e,t){let o=Math.max(20,t);switch(e.kind){case"banner":return Da(e.info,o);case"user":{let n="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,o-4),c=50,p=e.text.split(`
24
- `).flatMap(h=>Nr(h,s,{hard:!0}).split(`
20
+ `)}G();import Ia from"picocolors";var _a=[127,86,217],Na=[22,38,43],Ur={T:_a,E:Na},xt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],zn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,Ma=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Wr(){if(!Ia.isColorSupported)return[];let e=xt[0].length,t=[];for(let n=0;n<xt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Ur[xt[n][r]],c=n+1<xt.length?Ur[xt[n+1][r]]:void 0;s&&c?o+=`${zn(s)}${Ma(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${zn(s)}\u2580\x1B[39m`:c?o+=`${zn(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function bt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function se(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function De(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var ut=(e,t)=>` ${e.padEnd(13)}${t}`;function La(e,t){let n=[ht("\u270E document360-writer")+z.gray(` v${e.version}`),z.gray(" Reads your code, writes your docs."),"",ut("Claude:",`${e.claude}${z.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),ut("Document360:",e.configured?e.loggedOut?z.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?z.gray(` (${e.sessionHint})`):""}`:z.yellow("not set up \u2014 run /init")),ut("Profile:",e.configured?`${e.profile}${z.gray(` (${e.apiUrl})`)}${e.prod?z.bold(z.yellow(" \u26A0 PRODUCTION")):""}`:z.gray("\u2014 (run /init)")),ut("Project:",e.project),ut("Mode:",z.gray(e.mode)),ut("cwd:",z.gray(e.cwd))],o=Wr();if(o.length===0)return n.join(`
21
+ `);let r=2,s=3,c=Yn(o[0]);if(!(t>=r+c+s+Math.max(...n.map($=>Yn($)))))return[...o.map($=>" "+$),...n].join(`
22
+ `);let g=Math.max(0,Math.floor((n.length-o.length)/2)),k=[];for(let $=0;$<Math.max(o.length+g,n.length);$++){let R=o[$-g]??" ".repeat(c);k.push((" ".repeat(r)+R+" ".repeat(s)+(n[$]??"")).trimEnd())}return k.join(`
23
+ `)}var Oa={error:z.red,warn:z.yellow,ok:z.green,info:z.gray};function Vn(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return La(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,p=e.text.split(`
24
+ `).flatMap(k=>Fr(k,s,{hard:!0}).split(`
25
25
  `)),g=Math.max(0,p.length-c);return g>0&&(p=[...p.slice(0,c),z.dim(`\u2026 +${g} more lines`)]),`
26
- `+p.map((h,$)=>n+($===0?E(" \u276F "):" ")+h+" ".repeat(Math.max(0,s-Go(h))+1)+r).join(`
26
+ `+p.map((k,$)=>o+($===0?E(" \u276F "):" ")+k+" ".repeat(Math.max(0,s-Yn(k))+1)+r).join(`
27
27
  `)}case"assistant":return`
28
- `+Bo(e.text,o);case"tool":{let n=e.arg!==null?z.gray(`${e.sep}(${e.arg})`):"";return`
29
- `+Nr(z.green("\u25CF ")+z.bold(e.title)+n,o)}case"tool-result":{let n=e.isError?z.red:z.gray,r=e.lines.map((s,c)=>n((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(z.dim(` \u2026 +${e.hidden} lines`)),r.join(`
30
- `)}case"diff":{let n=c=>c===1?"":"s",s=[z.gray(` \u23BF Added ${e.added} line${n(e.added)}, removed ${e.removed} line${n(e.removed)}`),...e.lines];return e.hidden>0&&s.push(z.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
31
- `)}case"link":return e.lines.map(n=>E(kt(` \u2B95 ${n}`))).join(`
28
+ `+Gn(e.text,n);case"tool":{let o=e.arg!==null?z.gray(`${e.sep}(${e.arg})`):"";return`
29
+ `+Fr(z.green("\u25CF ")+z.bold(e.title)+o,n)}case"tool-result":{let o=e.isError?z.red:z.gray,r=e.lines.map((s,c)=>o((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(z.dim(` \u2026 +${e.hidden} lines`)),r.join(`
30
+ `)}case"diff":{let o=c=>c===1?"":"s",s=[z.gray(` \u23BF Added ${e.added} line${o(e.added)}, removed ${e.removed} line${o(e.removed)}`),...e.lines];return e.hidden>0&&s.push(z.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
31
+ `)}case"link":return e.lines.map(o=>E(wt(` \u2B95 ${o}`))).join(`
32
32
  `);case"preview":return`
33
33
  `+ht(`\u25A3 Preview \u2014 ${e.name}`)+`
34
34
 
35
- `+Bo(e.text,o);case"note":return`
36
- `+Ia[e.tone](kt(e.text));case"done":return`
37
- `+(e.ok?z.magenta("\u2736 "):z.red("\u2736 "))+z.gray(`Cooked for ${bt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${se(e.costUsd)}`:""))}}function Mr(e,t){return e.map(o=>zo(o,t)).join(`
38
- `)}var vt=3;function eo(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(`
39
- `)}var Or={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Zt(e){return e.filter(t=>t.path!==null&&t.status in Or).map(t=>({path:t.path,label:Or[t.status]}))}function to(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(`
40
- `)}function Yo(e,t,o){let n=y=>t[y.index]?.paths.length??0,r=e.filter(y=>y.ok),s=e.filter(y=>!y.ok),c=e.reduce((y,x)=>y+n(x),0),p=r.reduce((y,x)=>y+n(x),0),g=o==="api"?`${se(e.reduce((y,x)=>y+x.costUsd,0))} total`:Ie(e.reduce((y,x)=>y+x.outputTokens,0)),h=`${r.length}/${e.length} partition${e.length===1?"":"s"}`,$=y=>`${y} article${y===1?"":"s"}`,R=[];if(s.length===0)R.push(`Published ${$(c)} (${h}) successfully \xB7 ${g}.`),R.push("All are DRAFTS \u2014 review and publish them in the Document360 portal.");else{R.push(`Published ${p}/${$(c)} (${h}) \xB7 ${g}.`),R.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /publish --all to retry just the rest (published articles are skipped):`);for(let y of s)R.push(` \u2717 ${y.label}${y.error?` \u2014 ${y.error}`:""}`)}return R}async function Ur(e,t){let o=Na(t),{model:n}=Ma(e.cwd,"standard");console.log(f(`Publishing ${t.length} article${t.length===1?"":"s"} across ${o.length} partition${o.length===1?"":"s"} (\u2264${vt} agents at once) on ${n}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of La({cwd:e.cwd,partitions:o,promptFor:s=>to(s.paths),concurrency:vt,profileName:e.profileName,allowProdWrites:e.allowProdWrites(),model:n}))if(r.type==="partition_status")r.status==="running"?console.log(f(` \u25B8 ${r.label} \u2014 publishing\u2026`)):r.status==="done"?console.log(I(` \u2713 ${r.label}`)):console.log(b(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");let s=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let c of Yo(r.results,o,s))console.log(r.ok?I(c):O(c))}}catch(r){console.log(b(`Publish run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}async function Wr(e,t){let o=e[0];if(o==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let n=Zt((await Lr({cwd:t?.cwd??process.cwd()})).entries);return n.length===0?(console.log(I("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):t?await Ur(t,n.map(r=>r.path)):{kind:"forward-to-agent",prompt:to(n.map(r=>r.path)),display:"/publish --all"}}catch(n){return console.log(b(`Could not compute sync status: ${n.message}`)),{kind:"continue"}}}if(!o){console.log(f("Checking what needs publishing\u2026"));let n;try{n=Zt((await Lr({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(b(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(n.length===0)return console.log(I("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of n)console.log(` ${r.path} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{o=await t.withPausedInput(()=>_a({message:"Publish which article?",choices:[...n.length>1?[{name:`All ${n.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...n.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(o==="--all")return await Ur(t,n.map(r=>r.path))}return{kind:"forward-to-agent",prompt:eo(o),display:`/publish ${o}`}}async function oo(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","2.5. d360_pending_hints \u2014 the source agent's own notes on what it shipped. Verify each (sources exist, commit is an ancestor of HEAD, source shows the surface) before trusting; fold verified ones into the table, flag the rest.","3. Map changes (diff + hints) to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit and stamp consumed hints.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
41
- `),display:"/audit"}}q();import{checkbox as Oa}from"@inquirer/prompts";import{inventoryRepo as Ua,readProjectConfig as Fr,writeProjectConfig as Wa}from"document360-engine";function Vo(e,t){let o=Fr(e);o&&(o.authoritativeSourceFiles=t,Wa(o,e))}function no(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 Hr(e,t){let o=t?.cwd??process.cwd();if(!Fr(o))return console.log(b("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let n=Ua(o);if(n.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(E("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of n)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(no(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Oa({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:n.map(s=>({name:`${s.path} (${no(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(f("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};Vo(o,r),console.log(I(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}q();import{confirm as Ga}from"@inquirer/prompts";import{applyPull as Yr,computeSyncStatus as Vr,planPull as za,D360AuthError as Ya}from"document360-engine";q();var Br=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:b,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:O,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:E,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:b,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:b,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:ee,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:ee,mark:"\xB7"}];function Fa(e){return e.path?e.path:`${e.title??"(untitled)"} ${ee(`[${e.articleId}]`)}`}function ro(e){let t=[];for(let c of Br){let p=e.entries.filter(g=>g.status===c.status);if(p.length!==0){t.push(""),t.push(Ce(c.header));for(let g of p)t.push(` ${c.paint(c.mark)} ${c.paint(Fa(g))}${g.detail?ee(` (${g.detail})`):""}`)}}let o=e.counts["in-sync"]??0,n=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=n===o?I("\u2713 everything in sync"):[o>0?I(`\u2713 ${o} in sync`):null,...Br.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(ee(" \xB7 "));return t.push(s+ee(` \xB7 ${n} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Ha}from"diff";import qr from"picocolors";var Gr=80,Ba="\x1B[48;2;74;28;28m",qa="\x1B[48;2;24;66;24m",zr="\x1B[49m",Xo=e=>String(e).padStart(5);function qe(e,t,o){let n=y=>{let x=y.replace(/\r\n/g,`
35
+ `+Gn(e.text,n);case"note":return`
36
+ `+Oa[e.tone](wt(e.text));case"done":return`
37
+ `+(e.ok?z.magenta("\u2736 "):z.red("\u2736 "))+z.gray(`Cooked for ${bt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${se(e.costUsd)}`:""))}}function Hr(e,t){return e.map(n=>Vn(n,t)).join(`
38
+ `)}var vt=3;function Jn(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(`
39
+ `)}var Br={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Ct(e){return e.filter(t=>t.path!==null&&t.status in Br).map(t=>({path:t.path,label:Br[t.status]}))}function qa(e){let t=e.replace(/\\/g,"/").split("/").pop()??e,n=t.replace(/^\d+[-_]/,"").replace(/[-_]+/g," ").trim();return n?n.charAt(0).toUpperCase()+n.slice(1):t}function qr(e){let t=[];e.length>1&&t.push({kind:"all",label:`Publish all ${e.length} articles`,paths:e.map(o=>o.path),indent:!1});let n=new Map;for(let o of e){let r=o.path.replace(/\\/g,"/").split("/").slice(0,-1).join("/"),s=n.get(r);s?s.push(o):n.set(r,[o])}for(let o of[...n.keys()].sort()){let r=n.get(o).sort((s,c)=>s.path.localeCompare(c.path));t.push({kind:"category",label:`${qa(o)} (${r.length})`,paths:r.map(s=>s.path),indent:!1});for(let s of r){let c=s.path.replace(/\\/g,"/").split("/").pop()??s.path;t.push({kind:"article",label:c,paths:[s.path],indent:!0,note:s.label})}}return t}function nn(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(`
40
+ `)}function Qn(e,t,n){let o=w=>t[w.index]?.paths.length??0,r=e.filter(w=>w.ok),s=e.filter(w=>!w.ok),c=e.reduce((w,x)=>w+o(x),0),p=r.reduce((w,x)=>w+o(x),0),g=n==="api"?`${se(e.reduce((w,x)=>w+x.costUsd,0))} total`:De(e.reduce((w,x)=>w+x.outputTokens,0)),k=`${r.length}/${e.length} partition${e.length===1?"":"s"}`,$=w=>`${w} article${w===1?"":"s"}`,R=[];if(s.length===0)R.push(`Published ${$(c)} (${k}) successfully \xB7 ${g}.`),R.push("All are DRAFTS \u2014 review and publish them in the Document360 portal.");else{R.push(`Published ${p}/${$(c)} (${k}) \xB7 ${g}.`),R.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /publish --all to retry just the rest (published articles are skipped):`);for(let w of s)R.push(` \u2717 ${w.label}${w.error?` \u2014 ${w.error}`:""}`)}return R}async function Xn(e,t){let n=Wa(t),{model:o}=Ha(e.cwd,"standard");console.log(f(`Publishing ${t.length} article${t.length===1?"":"s"} across ${n.length} partition${n.length===1?"":"s"} (\u2264${vt} agents at once) on ${o}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let r of Ba({cwd:e.cwd,partitions:n,promptFor:s=>nn(s.paths),concurrency:vt,profileName:e.profileName,allowProdWrites:e.allowProdWrites(),model:o}))if(r.type==="partition_status")r.status==="running"?console.log(f(` \u25B8 ${r.label} \u2014 publishing\u2026`)):r.status==="done"?console.log(A(` \u2713 ${r.label}`)):console.log(b(` \u2717 ${r.label}`));else if(r.type==="run_done"){console.log("");let s=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let c of Qn(r.results,n,s))console.log(r.ok?A(c):O(c))}}catch(r){console.log(b(`Publish run failed: ${r.message}`))}return console.log(""),{kind:"continue"}}async function on(e,t,n){let o=Fa(e,n);if(o.length===0)return[];let r=new Set(Ct((await Kn({cwd:e,profileName:t})).entries).map(s=>s.path));return o.filter(s=>r.has(s))}async function Gr(e,t){let n=e[0];if(n==="--related"&&e[1]){if(!t)return{kind:"continue"};let o=await on(t.cwd,t.profileName,e[1].replace(/\\/g,"/"));return o.length===0?(console.log(A("\u2713 All related articles are already published.")),{kind:"continue"}):await Xn(t,o)}if(n==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let o=Ct((await Kn({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):t?await Xn(t,o.map(r=>r.path)):{kind:"forward-to-agent",prompt:nn(o.map(r=>r.path)),display:"/publish --all"}}catch(o){return console.log(b(`Could not compute sync status: ${o.message}`)),{kind:"continue"}}}if(!n){console.log(f("Checking what needs publishing\u2026"));let o;try{o=Ct((await Kn({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(b(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(o.length===0)return console.log(A("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of o)console.log(` ${r.path} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{n=await t.withPausedInput(()=>Ua({message:"Publish which article?",choices:[...o.length>1?[{name:`All ${o.length} candidates`,value:"--all",description:"one agent run over every candidate"}]:[],...o.map(r=>({name:r.path,value:r.path,description:r.label}))]}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(n==="--all")return await Xn(t,o.map(r=>r.path))}return{kind:"forward-to-agent",prompt:Jn(n),display:`/publish ${n}`}}async function rn(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","2.5. d360_pending_hints \u2014 the source agent's own notes on what it shipped. Verify each (sources exist, commit is an ancestor of HEAD, source shows the surface) before trusting; fold verified ones into the table, flag the rest.","3. Map changes (diff + hints) to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit and stamp consumed hints.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
41
+ `),display:"/audit"}}G();import{checkbox as Ga}from"@inquirer/prompts";import{inventoryRepo as za,readProjectConfig as zr,writeProjectConfig as Ya}from"document360-engine";function Zn(e,t){let n=zr(e);n&&(n.authoritativeSourceFiles=t,Ya(n,e))}function sn(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 Yr(e,t){let n=t?.cwd??process.cwd();if(!zr(n))return console.log(b("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=za(n);if(o.length===0)return console.log(f('No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.')),{kind:"continue"};if(!process.stdin.isTTY||!t){console.log(""),console.log(E("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(sn(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Ga({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${sn(s)})`,value:s.path,checked:s.recommended})),pageSize:20}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}if(r.length===0)return console.log(f("Nothing selected \u2014 scope unchanged.")),{kind:"continue"};Zn(n,r),console.log(A(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}G();import{confirm as Qa}from"@inquirer/prompts";import{applyPull as Qr,computeSyncStatus as Zr,planPull as Za,D360AuthError as el}from"document360-engine";G();var Vr=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:b,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:O,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:E,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:b,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:b,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:ee,mark:"X"},{status:"untracked-local",header:"Untracked local articles \u2014 publish to start tracking:",paint:f,mark:"?"},{status:"untracked-remote",header:"Untracked Document360 articles \u2014 no local file maps to them:",paint:f,mark:"?"},{status:"unknown-base",header:"No sync base recorded yet \u2014 the next /publish or /sync pull of each records one:",paint:ee,mark:"\xB7"}];function Va(e){return e.path?e.path:`${e.title??"(untitled)"} ${ee(`[${e.articleId}]`)}`}function an(e){let t=[];for(let c of Vr){let p=e.entries.filter(g=>g.status===c.status);if(p.length!==0){t.push(""),t.push(ve(c.header));for(let g of p)t.push(` ${c.paint(c.mark)} ${c.paint(Va(g))}${g.detail?ee(` (${g.detail})`):""}`)}}let n=e.counts["in-sync"]??0,o=e.entries.length;t.push("");let r={"in-sync":"in sync","local-ahead":"local ahead","remote-ahead":"remote ahead",conflict:"conflicts","untracked-local":"untracked local","untracked-remote":"untracked remote","deleted-local":"deleted locally","deleted-remote":"deleted on D360",orphaned:"orphaned","unknown-base":"no base yet"},s=o===n?A("\u2713 everything in sync"):[n>0?A(`\u2713 ${n} in sync`):null,...Vr.map(c=>{let p=e.counts[c.status]??0;return p>0?`${p} ${r[c.status]}`:null})].filter(Boolean).join(ee(" \xB7 "));return t.push(s+ee(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as Xa}from"diff";import Xr from"picocolors";var Kr=80,Ka="\x1B[48;2;74;28;28m",Ja="\x1B[48;2;24;66;24m",Jr="\x1B[49m",eo=e=>String(e).padStart(5);function qe(e,t,n){let o=w=>{let x=w.replace(/\r\n/g,`
42
42
  `);return x.endsWith(`
43
43
  `)||x===""?x:x+`
44
- `},r=n(e),s=n(t);if(r===s)return null;let c=Math.max(20,o-10),p=Ha("a","b",r,s,"","",{context:3}),g=0,h=0,$=[];p.hunks.forEach((y,x)=>{x>0&&$.push(qr.gray(" \u2026"));let v=y.oldStart,_=y.newStart;for(let N of y.lines){let H=N[0],Z=N.slice(1).slice(0,c);H==="-"?(h++,$.push(`${Ba}${Xo(v++)} - ${Z}${zr}`)):H==="+"?(g++,$.push(`${qa}${Xo(_++)} + ${Z}${zr}`)):($.push(qr.gray(Xo(_))+" "+Z),v++,_++)}});let R=$.slice(0,Gr);return{added:g,removed:h,lines:R,hidden:Math.max(0,$.length-Gr)}}async function Xr(e,t){let o=(e[0]??"status").toLowerCase();try{if(o==="status")return await Va(t.cwd),{kind:"continue"};if(o==="pull")return await Xa(t,e.slice(1)),{kind:"continue"};console.log(b(`Unknown subcommand: /sync ${o}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(n){n instanceof Ya?console.log(b(n.message)):console.log(b(`Sync failed: ${n.message}`))}return{kind:"continue"}}async function Va(e){console.log(f("Checking Document360 for drift\u2026"));let t=await Vr({cwd:e});for(let o of ro(t))console.log(o);console.log("")}async function Xa(e,t){let o=t[0];if(!o){console.log(b("Usage: /sync pull <article-path> | --all"));return}let n;if(o==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),n=(await Vr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),n.length===0){console.log(I("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${Ce(String(n.length))} article(s) are remote-ahead.`)}else n=[o.replace(/\\/g,"/")];for(let r of n){let s=await za({cwd:e.cwd,relPath:r});console.log(""),console.log(`${E("\u25CF")} ${Ce(s.title)} ${ee(`(${s.path})`)}`);for(let h of s.notes)console.log(O(` \u26A0 ${h}`));s.overwritesLocalChanges&&console.log(O(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=qe(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(f(" Local file already matches the remote content \u2014 advancing the sync base only.")),Yr({cwd:e.cwd},s);continue}let p=h=>h===1?"":"s";console.log(ee(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let h of c.lines)console.log(h);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>Ga({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}Yr({cwd:e.cwd},s),console.log(I(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}q();import{statSync as Ka}from"node:fs";import{resolve as Ja}from"node:path";import{estimateBulkCost as Qa,planPartitions as Za,readProjectConfig as el,resolveModelForOperation as tl,runPartitioned as ol,trackedArticlePaths as nl}from"document360-engine";var Ko=3;function Jo(e){let t,o=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?o=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:o}}function Qo(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function Zo(e,t){return t.map(o=>{let n=0;try{n=Ka(Ja(e,o)).size}catch{n=0}return{path:o,bytes:n}})}function en(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
44
+ `},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),p=Xa("a","b",r,s,"","",{context:3}),g=0,k=0,$=[];p.hunks.forEach((w,x)=>{x>0&&$.push(Xr.gray(" \u2026"));let v=w.oldStart,I=w.newStart;for(let _ of w.lines){let H=_[0],Z=_.slice(1).slice(0,c);H==="-"?(k++,$.push(`${Ka}${eo(v++)} - ${Z}${Jr}`)):H==="+"?(g++,$.push(`${Ja}${eo(I++)} + ${Z}${Jr}`)):($.push(Xr.gray(eo(I))+" "+Z),v++,I++)}});let R=$.slice(0,Kr);return{added:g,removed:k,lines:R,hidden:Math.max(0,$.length-Kr)}}async function es(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await tl(t.cwd),{kind:"continue"};if(n==="pull")return await nl(t,e.slice(1)),{kind:"continue"};console.log(b(`Unknown subcommand: /sync ${n}`)),console.log(f("Usage: /sync drift report (local vs Document360)")),console.log(f(" /sync pull <path> pull portal edits into the local file")),console.log(f(" /sync pull --all pull every remote-ahead article"))}catch(o){o instanceof el?console.log(b(o.message)):console.log(b(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function tl(e){console.log(f("Checking Document360 for drift\u2026"));let t=await Zr({cwd:e});for(let n of an(t))console.log(n);console.log("")}async function nl(e,t){let n=t[0];if(!n){console.log(b("Usage: /sync pull <article-path> | --all"));return}let o;if(n==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),o=(await Zr({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(A("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${ve(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await Za({cwd:e.cwd,relPath:r});console.log(""),console.log(`${E("\u25CF")} ${ve(s.title)} ${ee(`(${s.path})`)}`);for(let k of s.notes)console.log(O(` \u26A0 ${k}`));s.overwritesLocalChanges&&console.log(O(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=qe(s.oldContent,s.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!c){console.log(f(" Local file already matches the remote content \u2014 advancing the sync base only.")),Qr({cwd:e.cwd},s);continue}let p=k=>k===1?"":"s";console.log(ee(` \u23BF Added ${c.added} line${p(c.added)}, removed ${c.removed} line${p(c.removed)}`));for(let k of c.lines)console.log(k);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>Qa({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}Qr({cwd:e.cwd},s),console.log(A(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}G();import{statSync as ol}from"node:fs";import{resolve as rl}from"node:path";import{estimateBulkCost as sl,planPartitions as il,readProjectConfig as al,resolveModelForOperation as ll,runPartitioned as cl,trackedArticlePaths as ul}from"document360-engine";var to=3;function no(e){let t,n=!1;for(let r=0;r<e.length;r++){let s=e[r];s==="--run"||s==="--yes"?n=!0:s==="--scope"?t=e[++r]:s?.startsWith("--scope=")&&(t=s.slice(8))}return{scope:t?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0,run:n}}function oo(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function ro(e,t){return t.map(n=>{let o=0;try{o=ol(rl(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function so(e){return`Convert each of these articles to canonical Document360 Flavored Markdown (DFM) and re-publish each as a DRAFT. This is a mechanical syntax conversion, not a rewrite \u2014 be efficient:
45
45
  - Read ONLY the article files listed below. Do NOT read source code, other articles, or fetch the live portal \u2014 you already have everything you need.
46
46
  - Convert in place: wrap callouts, FAQs, tabs, accordions, and media embeds per the d360-markdown skill. Preserve all wording, headings, and structure; change only GFM\u2192DFM syntax.
47
47
  - Publish each as a draft with a single update call. Do NOT re-read, re-verify, or polish after converting, and skip articles already in DFM.
48
48
  - Work only on these files:
49
49
  `+e.paths.map(t=>`- ${t}`).join(`
50
- `)}function tn(e,t,o){let n=e.reduce((p,g)=>p+g.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${se(r)}\u2013${se(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function on(e,t,o){let n=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,_)=>v+n(_),0),p=r.reduce((v,_)=>v+n(_),0),g=e.reduce((v,_)=>v+_.costUsd,0),h=e.reduce((v,_)=>v+_.outputTokens,0),$=o==="api"?`${se(g)} total`:Ie(h),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,y=v=>`${v} article${v===1?"":"s"}`,x=[];if(s.length===0)x.push(`Converted ${y(c)} (${R}) successfully \xB7 ${$}.`);else{x.push(`Converted ${p}/${y(c)} (${R}) \xB7 ${$}.`),x.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let v of s)x.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return x}async function Kr(e,t){if(!el(t.cwd))return console.log(b("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Jo(e),r=nl(t.cwd,t.profileName);if(r.length===0)return console.log(b("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Qo(r,o);if(s.length===0)return console.log(b(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=Za(s),p=`/convert${o?` --scope ${o}`:""} --run`,{model:g,forced:h}=tl(t.cwd,"light");if(!n){let $=Qa({files:Zo(t.cwd,s),op:"convert",model:g});o&&console.log(f(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let R of tn(c,$,Ko))console.log(R);return console.log(f(`Model: ${g}${h?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Ko} agents at once) on ${g}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let $ of ol({cwd:t.cwd,partitions:c,promptFor:en,concurrency:Ko,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if($.type==="partition_status")$.status==="running"?console.log(f(` \u25B8 ${$.label} \u2014 converting\u2026`)):$.status==="done"?console.log(I(` \u2713 ${$.label}`)):console.log(b(` \u2717 ${$.label}`));else if($.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let y of on($.results,c,R))console.log($.ok?I(y):O(y))}}catch($){console.log(b(`Convert run failed: ${$.message}`))}return console.log(""),{kind:"continue"}}q();import{statSync as Jr}from"node:fs";import{resolve as Qr}from"node:path";import{estimateBulkCost as rl,planPartitions as sl,readDocsPlan as il,readProjectConfig as Zr,resolveModelForOperation as al,runPartitioned as ll}from"document360-engine";var nn=5;function rn(e){let t,o,n=!1;for(let s=0;s<e.length;s++){let c=e[s];if(c==="--run"||c==="--yes")n=!0;else{if(c==="--all")continue;c==="--scope"?t=e[++s]:c?.startsWith("--scope=")?t=c.slice(8):c&&!c.startsWith("--")&&(o=c)}}let r=s=>s?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0;return{scope:r(t),path:r(o),run:n}}function cl(e,t){let o=e.replace(/\\/g,"/").replace(/\/+$/,""),n=`${t.replace(/\/+$/,"")}/`;return o.startsWith(n)?o.slice(n.length):o}function ul(e,t){return t?e.filter(o=>{let n=o.replace(/\\/g,"/");return n===t||n.startsWith(`${t}/`)}):e}function dl(e,t,o){return o.filter(n=>{try{return Jr(Qr(e,t,n)).size===0}catch{return!0}})}function sn(e,t,o){let n=new Map(t.map(r=>[r.path,r]));return o.map(r=>{let s=1;for(let c of n.get(r)?.sources??[])try{s+=Jr(Qr(e,c)).size}catch{}return{path:r,bytes:s}})}function an(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
50
+ `)}function io(e,t,n){let o=e.reduce((p,g)=>p+g.paths.length,0),[r,s]=t.usd;return[`Convert ${o} article${o===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(p=>` \u2022 ${p.label} \u2014 ${p.paths.length} article${p.paths.length===1?"":"s"}`),"",`Estimated cost: ${se(r)}\u2013${se(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function ao(e,t,n){let o=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,I)=>v+o(I),0),p=r.reduce((v,I)=>v+o(I),0),g=e.reduce((v,I)=>v+I.costUsd,0),k=e.reduce((v,I)=>v+I.outputTokens,0),$=n==="api"?`${se(g)} total`:De(k),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,w=v=>`${v} article${v===1?"":"s"}`,x=[];if(s.length===0)x.push(`Converted ${w(c)} (${R}) successfully \xB7 ${$}.`);else{x.push(`Converted ${p}/${w(c)} (${R}) \xB7 ${$}.`),x.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let v of s)x.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return x}async function ts(e,t){if(!al(t.cwd))return console.log(b("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=no(e),r=ul(t.cwd,t.profileName);if(r.length===0)return console.log(b("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=oo(r,n);if(s.length===0)return console.log(b(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=il(s),p=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:k}=ll(t.cwd,"light");if(!o){let $=sl({files:ro(t.cwd,s),op:"convert",model:g});n&&console.log(f(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let R of io(c,$,to))console.log(R);return console.log(f(`Model: ${g}${k?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${p} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${to} agents at once) on ${g}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let $ of cl({cwd:t.cwd,partitions:c,promptFor:so,concurrency:to,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if($.type==="partition_status")$.status==="running"?console.log(f(` \u25B8 ${$.label} \u2014 converting\u2026`)):$.status==="done"?console.log(A(` \u2713 ${$.label}`)):console.log(b(` \u2717 ${$.label}`));else if($.type==="run_done"){console.log("");let R=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let w of ao($.results,c,R))console.log($.ok?A(w):O(w))}}catch($){console.log(b(`Convert run failed: ${$.message}`))}return console.log(""),{kind:"continue"}}G();import{statSync as ns}from"node:fs";import{resolve as os}from"node:path";import{estimateBulkCost as dl,planPartitions as pl,readDocsPlan as fl,readProjectConfig as rs,resolveModelForOperation as ml,runPartitioned as gl}from"document360-engine";var lo=5;function co(e){let t,n,o=!1;for(let s=0;s<e.length;s++){let c=e[s];if(c==="--run"||c==="--yes")o=!0;else{if(c==="--all")continue;c==="--scope"?t=e[++s]:c?.startsWith("--scope=")?t=c.slice(8):c&&!c.startsWith("--")&&(n=c)}}let r=s=>s?.replace(/\\/g,"/").replace(/\/+$/,"")||void 0;return{scope:r(t),path:r(n),run:o}}function hl(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function kl(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function wl(e,t,n){return n.filter(o=>{try{return ns(os(e,t,o)).size===0}catch{return!0}})}function uo(e,t,n){let o=new Map(t.map(r=>[r.path,r]));return n.map(r=>{let s=1;for(let c of o.get(r)?.sources??[])try{s+=ns(os(e,c)).size}catch{}return{path:r,bytes:s}})}function po(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
51
51
  - It is relative to ${t}/. Look up its entry (matching "path") in .d360-writer/plan.json for its purpose and the source files it draws from (AGENT-PLAN.md has the same table).
52
52
  - Read ONLY those source files plus any shared context you genuinely need, and ground every statement in them. Never invent UI labels, routes, or behaviour \u2014 if the source does not say it, omit it.
53
53
  - Write the finished article to ${t}/<path>, following the write-article structure and the project terminologyGlossary. Add the \`sources:\` frontmatter listing the files you actually read.
54
54
  - Emit \`<!-- SCREENSHOT -->\` placeholders where a visual helps, but do NOT author capture specs now.
55
55
  - Each article is independent. Do NOT write, edit, or publish any article not in this list, and do NOT publish to Document360 \u2014 these are local drafts.
56
56
  Articles:
57
- `+e.paths.map(o=>`- ${o}`).join(`
58
- `)}function ln(e,t,o){let n=e.reduce((c,p)=>c+p.paths.length,0),[r,s]=t.usd;return[`Write ${n} article${n===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${se(r)}\u2013${se(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function cn(e,t,o){let n=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,_)=>v+n(_),0),p=r.reduce((v,_)=>v+n(_),0),g=e.reduce((v,_)=>v+_.costUsd,0),h=e.reduce((v,_)=>v+_.outputTokens,0),$=o==="api"?`${se(g)} total`:Ie(h),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,y=v=>`${v} article${v===1?"":"s"}`,x=[];if(s.length===0)x.push(`Wrote ${y(c)} (${R}) successfully \xB7 ${$}.`),x.push("Next \u2014 push them to Document360:"),x.push("/publish --all"),x.push("Or add screenshots first with /screenshot --all. Drafts are reviewable in the portal after publishing.");else{x.push(`Wrote ${p}/${y(c)} (${R}) \xB7 ${$}.`),x.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let v of s)x.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return x}function un(e,t,o){let n=(Zr(e)?.docsDir??"user-docs").replace(/\/+$/,""),r=t.map(p=>p.path);if(o.path){let p=cl(o.path,n);return r.includes(p)?{docsDir:n,targets:[p],planCount:r.length}:{docsDir:n,targets:[],planCount:r.length,reason:`"${p}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=ul(r,o.scope);if(o.scope&&s.length===0)return{docsDir:n,targets:[],planCount:r.length,reason:`No planned articles under "${o.scope}".`};let c=dl(e,n,s);if(c.length===0){let p=o.scope?` under "${o.scope}"`:"";return{docsDir:n,targets:[],planCount:r.length,reason:`All planned articles${p} are already written. Use /write <path> to rewrite one.`}}return{docsDir:n,targets:c,planCount:r.length}}async function es(e,t){if(!Zr(t.cwd))return console.log(b("No d360-writer config here. Run /init first.")),{kind:"continue"};let o=il(t.cwd);if(o.length===0)return console.log(b("No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first.")),{kind:"continue"};let{scope:n,path:r,run:s}=rn(e),{docsDir:c,targets:p,reason:g}=un(t.cwd,o,{scope:n,path:r});if(p.length===0)return console.log(g?O(g):O("Nothing to write.")),{kind:"continue"};let h=sl(p),{model:$,forced:R}=al(t.cwd,"standard");if(!s&&!!!r){let x=rl({files:sn(t.cwd,o,p),op:"write",model:$});n&&console.log(f(`Scope: ${n} (${p.length} pending).`));for(let v of ln(h,x,nn))console.log(v);return console.log(f(`Model: ${$}${R?" (forced)":" \u2014 authoring; /model to override"}.`)),console.log(f(`Run /write${n?` --scope ${n}`:" --all"} --run to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Writing ${p.length} article${p.length===1?"":"s"} across ${h.length} partition${h.length===1?"":"s"} (\u2264${nn} agents at once) on ${$}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let x of ll({cwd:t.cwd,partitions:h,promptFor:v=>an(v,c),concurrency:nn,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:$}))if(x.type==="partition_status")x.status==="running"?console.log(f(` \u25B8 ${x.label} \u2014 writing\u2026`)):x.status==="done"?console.log(I(` \u2713 ${x.label}`)):console.log(b(` \u2717 ${x.label}`));else if(x.type==="run_done"){console.log("");let v=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let _ of cn(x.results,h,v))console.log(x.ok?I(_):O(_))}}catch(x){console.log(b(`Write run failed: ${x.message}`))}return console.log(""),{kind:"continue"}}q();import{search as pl}from"@inquirer/prompts";import{findByName as fl,getSession as ml,listSessions as gl,relativeTime as os}from"document360-engine";async function ns(e,t){let o=gl(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(f("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=fl(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(b(`No session matches "${n}".`)),ts(o),{kind:"continue"})}if(!process.stdin.isTTY)return ts(o),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>pl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${os(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=ml(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function ts(e){console.log("");for(let t of e.slice(0,15))console.log(` ${E(t.name)} ${f(os(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}q();import{renameSession as hl}from"document360-engine";function dn(e){let t=e.trim();return t.length>=2&&(t[0]==='"'&&t.endsWith('"')||t[0]==="'"&&t.endsWith("'"))?t.slice(1,-1).trim():t}async function rs(e,t){let o=dn(e.join(" "));if(!o)return console.log(b("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(hl(n,o)?console.log(I(`\u2713 Session renamed to "${o}"`)):console.log(b("Could not find the current session record.")),{kind:"continue"}):(console.log(b("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as ss,readProjectConfig as wl,writeProjectConfig as kl}from"document360-engine";q();function pn(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=wl(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 ss().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},kl(n,e),null):`Unknown environment "${r}". Known: ${ss().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function is(e,t){let o=e[0];if(!o)return Yt(t.cwd),{kind:"continue"};if(o==="add"){let n=pn(t.cwd,e[1],e[2]);return n?(console.log(b(n)),{kind:"continue"}):(console.log(I(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${E(`/profile ${e[1]}`)} then ${E("/login")}`),{kind:"continue"})}return Vt(t.cwd,o),Xt(t.cwd,o),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}q();import{select as yl}from"@inquirer/prompts";import{readProjectConfig as $l,readUserConfig as as,resolveModelSetting as fn,writeUserConfig as ls}from"document360-engine";var ye=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function so(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=ye.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function xl(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 Ct(e,t){let o=()=>{let s=fn(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=as();return s.defaultModel?(delete s.defaultModel,ls(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${fn(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}}ls({...as(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=$l(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 cs(e,t){let o=e[0]?.trim();if(!o){let c=fn(t.cwd);if(!process.stdin.isTTY)return console.log(`${Ce("Model:")} ${E(c.model??"Claude Code default")}`),console.log(f(` source: ${xl(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=so(c),g;try{g=await t.withPausedInput(()=>yl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ye[p].value,choices:ye.map((y,x)=>({name:`${y.label}${x===p?" \u2714":""}`,value:y.value,description:y.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:h,changed:$,effective:R}=Ct(t.cwd,g??"default");for(let y of h)console.log(y.startsWith("\u26A0")?O(y):y.startsWith("\u2713")?I(y):f(y));return $&&await t.setModel(R),{kind:"continue"}}let{lines:n,changed:r,effective:s}=Ct(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?O(c):c.startsWith("\u2713")?I(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}io();async function ms(e,t){return await t.withPausedInput(()=>lt(t.cwd)),{kind:"clear"}}q();import{select as Dl}from"@inquirer/prompts";import{resolveActiveProfile as Il,setProfileProject as _l,listProjects as Nl}from"document360-engine";async function gn(e,t){let o=Il(e,t),n={profile:o.name,connection:o.connection};return{projects:await Nl(n),profile:o.name,environment:o.connection.name,current:o.project.projectId}}var gs=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function hs(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 hn(e,t,o){_l(e,t,{projectId:o,workspaceId:void 0,workspaceName:void 0})}async function ws(e,t){let o;try{o=await gn(e,t)}catch(g){console.log(b(`Could not list projects: ${g.message}`));return}let{projects:n,profile:r,current:s}=o;if(n.length===0){console.log(f("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===s?E("\u25CF"):" "} ${gs(g)} ${f(g.id)}`);console.log(f("Run: d360-writer project use <name>"));return}let c=await Dl({message:"Select the Document360 project for this repo:",choices:n.map(g=>({name:`${gs(g)}${g.id===s?" (current)":""}`,value:g.id}))});hn(e,r,c);let p=n.find(g=>g.id===c);console.log(I(`\u2713 Project set to "${p?.name??c}" for profile "${r}". Next: pick a workspace.`))}async function ks(e,t){return await t.withPausedInput(()=>ws(t.cwd,t.profileName)),{kind:"clear"}}q();import{resolveActiveProfile as Ml}from"document360-engine";async function ys(e,t){let o=!1;try{o=Ml(t.cwd).production}catch{}return o?(console.log(O("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}q();var $s=async(e,t)=>{try{await t.withPausedInput(()=>zt({}))}catch(o){console.log(b(`Login failed: ${o.message}`))}return{kind:"continue"}};q();import{resolveActiveProfile as Ll,clearTokens as Ol,clearProfileProject as Ul}from"document360-engine";async function xs(e,t){let o;try{o=Ll(t.cwd,t.profileName).name}catch(r){return console.log(b(r.message)),{kind:"continue"}}let n=Ol(o);return Ul(t.cwd,o),console.log(n?I(`\u2713 Signed out of "${o}" and cleared its project/workspace selection.`):f(`Profile "${o}" was not signed in. Cleared any project/workspace selection.`)),console.log(f("Run /login to sign in and pick a project.")),{kind:"continue"}}import{existsSync as bs}from"node:fs";import{isAbsolute as Wl,join as Fl,resolve as Hl}from"node:path";import{readProjectConfig as Bl,screenshotPlaceholderIds as ql}from"document360-engine";var vs=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function wn(e){let t=!e.includes("--no-setup"),o=e.filter(r=>r!=="--no-setup");if(o[0]==="--list")return{mode:"list",scope:o[1]?vs(o[1]):void 0};let n=o[0];return!n||n==="--all"?{mode:"all",setup:t}:/[\\/]/.test(n)||n.endsWith(".md")?{mode:"scope",scope:vs(n),setup:t}:{mode:"single",id:n}}function kn(e,t){let o=Bl(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,p)=>Wl(c)?Fl(c,p):Hl(e,c,p);return ql(e,{scope:t}).map(({id:c,file:p})=>{let g=bs(s(r,`${c}.png`))?"captured":bs(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:g}})}var Gl={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},zl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function yn(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${Gl[c.state]} ${c.id.padEnd(34)} ${zl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Cs(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
57
+ `+e.paths.map(n=>`- ${n}`).join(`
58
+ `)}function fo(e,t,n){let o=e.reduce((c,p)=>c+p.paths.length,0),[r,s]=t.usd;return[`Write ${o} article${o===1?"":"s"} across ${e.length} partition${e.length===1?"":"s"} (\u2264${n} agents at once):`,...e.map(c=>` \u2022 ${c.label} \u2014 ${c.paths.length} article${c.paths.length===1?"":"s"}`),"",`Estimated cost: ${se(r)}\u2013${se(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function mo(e,t,n){let o=v=>t[v.index]?.paths.length??0,r=e.filter(v=>v.ok),s=e.filter(v=>!v.ok),c=e.reduce((v,I)=>v+o(I),0),p=r.reduce((v,I)=>v+o(I),0),g=e.reduce((v,I)=>v+I.costUsd,0),k=e.reduce((v,I)=>v+I.outputTokens,0),$=n==="api"?`${se(g)} total`:De(k),R=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,w=v=>`${v} article${v===1?"":"s"}`,x=[];if(s.length===0)x.push(`Wrote ${w(c)} (${R}) successfully \xB7 ${$}.`),x.push("Next \u2014 push them to Document360:"),x.push("/publish --all"),x.push("Or add screenshots first with /screenshot --all. Drafts are reviewable in the portal after publishing.");else{x.push(`Wrote ${p}/${w(c)} (${R}) \xB7 ${$}.`),x.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let v of s)x.push(` \u2717 ${v.label}${v.error?` \u2014 ${v.error}`:""}`)}return x}function go(e,t,n){let o=(rs(e)?.docsDir??"user-docs").replace(/\/+$/,""),r=t.map(p=>p.path);if(n.path){let p=hl(n.path,o);return r.includes(p)?{docsDir:o,targets:[p],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${p}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=kl(r,n.scope);if(n.scope&&s.length===0)return{docsDir:o,targets:[],planCount:r.length,reason:`No planned articles under "${n.scope}".`};let c=wl(e,o,s);if(c.length===0){let p=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${p} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function ss(e,t){if(!rs(t.cwd))return console.log(b("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=fl(t.cwd);if(n.length===0)return console.log(b("No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first.")),{kind:"continue"};let{scope:o,path:r,run:s}=co(e),{docsDir:c,targets:p,reason:g}=go(t.cwd,n,{scope:o,path:r});if(p.length===0)return console.log(g?O(g):O("Nothing to write.")),{kind:"continue"};let k=pl(p),{model:$,forced:R}=ml(t.cwd,"standard");if(!s&&!!!r){let x=dl({files:uo(t.cwd,n,p),op:"write",model:$});o&&console.log(f(`Scope: ${o} (${p.length} pending).`));for(let v of fo(k,x,lo))console.log(v);return console.log(f(`Model: ${$}${R?" (forced)":" \u2014 authoring; /model to override"}.`)),console.log(f(`Run /write${o?` --scope ${o}`:" --all"} --run to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Writing ${p.length} article${p.length===1?"":"s"} across ${k.length} partition${k.length===1?"":"s"} (\u2264${lo} agents at once) on ${$}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let x of gl({cwd:t.cwd,partitions:k,promptFor:v=>po(v,c),concurrency:lo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:$}))if(x.type==="partition_status")x.status==="running"?console.log(f(` \u25B8 ${x.label} \u2014 writing\u2026`)):x.status==="done"?console.log(A(` \u2713 ${x.label}`)):console.log(b(` \u2717 ${x.label}`));else if(x.type==="run_done"){console.log("");let v=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let I of mo(x.results,k,v))console.log(x.ok?A(I):O(I))}}catch(x){console.log(b(`Write run failed: ${x.message}`))}return console.log(""),{kind:"continue"}}G();import{search as yl}from"@inquirer/prompts";import{findByName as $l,getSession as xl,listSessions as bl,relativeTime as as}from"document360-engine";async function ls(e,t){let n=bl(t.cwd).filter(o=>o.uuid!==t.currentUuid());if(n.length===0)return console.log(f("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let o=e.join(" "),r=$l(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(b(`No session matches "${o}".`)),is(n),{kind:"continue"})}if(!process.stdin.isTTY)return is(n),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>yl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(p=>!c||p.name.toLowerCase().includes(c)||p.firstPrompt.toLowerCase().includes(c)).map(p=>({name:`${p.name} ${as(p.updatedAt)}`,value:p.uuid,description:p.firstPrompt.slice(0,100)}))}})),r=xl(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function is(e){console.log("");for(let t of e.slice(0,15))console.log(` ${E(t.name)} ${f(as(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}G();import{renameSession as vl}from"document360-engine";function ho(e){let t=e.trim();return t.length>=2&&(t[0]==='"'&&t.endsWith('"')||t[0]==="'"&&t.endsWith("'"))?t.slice(1,-1).trim():t}async function cs(e,t){let n=ho(e.join(" "));if(!n)return console.log(b("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(vl(o,n)?console.log(A(`\u2713 Session renamed to "${n}"`)):console.log(b("Could not find the current session record.")),{kind:"continue"}):(console.log(b("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as us,readProjectConfig as Cl,writeProjectConfig as Pl}from"document360-engine";G();function ko(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=Cl(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 us().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},Pl(o,e),null):`Unknown environment "${r}". Known: ${us().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function ds(e,t){let n=e[0];if(!n)return Kt(t.cwd),{kind:"continue"};if(n==="add"){let o=ko(t.cwd,e[1],e[2]);return o?(console.log(b(o)),{kind:"continue"}):(console.log(A(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${E(`/profile ${e[1]}`)} then ${E("/login")}`),{kind:"continue"})}return Jt(t.cwd,n),Qt(t.cwd,n),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}G();import{select as Sl}from"@inquirer/prompts";import{readProjectConfig as Tl,readUserConfig as ps,resolveModelSetting as wo,writeUserConfig as fs}from"document360-engine";var ye=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function ln(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=ye.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function Rl(e){switch(e.source){case"project":return".d360-writer.json defaultModel (team setting)";case"user":return"~/.document360-writer/config.json (your /model setting)";case"env":return"ANTHROPIC_MODEL environment variable";case"claude-settings":return"Claude Code's own settings (~/.claude/settings.json)";case"claude-default":return"Claude Code default (no override configured)"}}function Pt(e,t){let n=()=>{let s=wo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=ps();return s.defaultModel?(delete s.defaultModel,fs(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${wo(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}}fs({...ps(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=Tl(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 ms(e,t){let n=e[0]?.trim();if(!n){let c=wo(t.cwd);if(!process.stdin.isTTY)return console.log(`${ve("Model:")} ${E(c.model??"Claude Code default")}`),console.log(f(` source: ${Rl(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let p=ln(c),g;try{g=await t.withPausedInput(()=>Sl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ye[p].value,choices:ye.map((w,x)=>({name:`${w.label}${x===p?" \u2714":""}`,value:w.value,description:w.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:k,changed:$,effective:R}=Pt(t.cwd,g??"default");for(let w of k)console.log(w.startsWith("\u26A0")?O(w):w.startsWith("\u2713")?A(w):f(w));return $&&await t.setModel(R),{kind:"continue"}}let{lines:o,changed:r,effective:s}=Pt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?O(c):c.startsWith("\u2713")?A(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}cn();async function ys(e,t){return await t.withPausedInput(()=>lt(t.cwd)),{kind:"clear"}}G();import{select as Ul}from"@inquirer/prompts";import{resolveActiveProfile as Wl,setProfileProject as Fl,listProjects as Hl}from"document360-engine";async function $o(e,t){let n=Wl(e,t),o={profile:n.name,connection:n.connection};return{projects:await Hl(o),profile:n.name,environment:n.connection.name,current:n.project.projectId}}var $s=e=>`${e.name??e.id}${e.sub_domain?` \xB7 ${e.sub_domain}`:""}`;function xs(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 xo(e,t,n){Fl(e,t,{projectId:n,workspaceId:void 0,workspaceName:void 0})}async function bs(e,t){let n;try{n=await $o(e,t)}catch(g){console.log(b(`Could not list projects: ${g.message}`));return}let{projects:o,profile:r,current:s}=n;if(o.length===0){console.log(f("No projects found for this identity."));return}if(!process.stdin.isTTY){console.log("");for(let g of o)console.log(` ${g.id===s?E("\u25CF"):" "} ${$s(g)} ${f(g.id)}`);console.log(f("Run: d360-writer project use <name>"));return}let c=await Ul({message:"Select the Document360 project for this repo:",choices:o.map(g=>({name:`${$s(g)}${g.id===s?" (current)":""}`,value:g.id}))});xo(e,r,c);let p=o.find(g=>g.id===c);console.log(A(`\u2713 Project set to "${p?.name??c}" for profile "${r}". Next: pick a workspace.`))}async function vs(e,t){return await t.withPausedInput(()=>bs(t.cwd,t.profileName)),{kind:"clear"}}G();import{resolveActiveProfile as Bl}from"document360-engine";async function Cs(e,t){let n=!1;try{n=Bl(t.cwd).production}catch{}return n?(console.log(O("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}G();var Ps=async(e,t)=>{try{await t.withPausedInput(()=>Xt({}))}catch(n){console.log(b(`Login failed: ${n.message}`))}return{kind:"continue"}};G();import{resolveActiveProfile as ql,clearTokens as Gl,clearProfileProject as zl}from"document360-engine";async function Ss(e,t){let n;try{n=ql(t.cwd,t.profileName).name}catch(r){return console.log(b(r.message)),{kind:"continue"}}let o=Gl(n);return zl(t.cwd,n),console.log(o?A(`\u2713 Signed out of "${n}" and cleared its project/workspace selection.`):f(`Profile "${n}" was not signed in. Cleared any project/workspace selection.`)),console.log(f("Run /login to sign in and pick a project.")),{kind:"continue"}}import{existsSync as Ts}from"node:fs";import{isAbsolute as Yl,join as Vl,resolve as Xl}from"node:path";import{readProjectConfig as Kl,screenshotPlaceholderIds as Jl}from"document360-engine";var Rs=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function bo(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?Rs(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:Rs(o),setup:t}:{mode:"single",id:o}}function vo(e,t){let n=Kl(e),o=n?.captureDir??"user-docs/_capture",r=n?.outputDir??"user-docs/_screenshots",s=(c,p)=>Yl(c)?Vl(c,p):Xl(e,c,p);return Jl(e,{scope:t}).map(({id:c,file:p})=>{let g=Ts(s(r,`${c}.png`))?"captured":Ts(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:p,state:g}})}var Ql={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Zl={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Co(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let n=[...new Set(e.map(s=>s.file))].sort(),o=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${n.length} article${n.length===1?"":"s"} \xB7 ${o} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of n){r.push(s);for(let c of e.filter(p=>p.file===s))r.push(` ${Ql[c.state]} ${c.id.padEnd(34)} ${Zl[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function js(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
59
59
  `),"","For each: locate its <!-- SCREENSHOT --> block in user-docs/**/*.md; read the product source for","EXACT routes + stable data-testid selectors (never guess); enter the prepared context via","captureScope(); guard data prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts","(skip-with-TODO if a stable selector is missing). Do NOT run other skills. Report the specs you","wrote and any TODO data-testids."].join(`
60
- `)}async function ao(e,t){let o=wn(e);if(o.mode==="list"){let c=t?.cwd??process.cwd();for(let p of yn(kn(c,o.scope),o.scope))console.log(p);return{kind:"continue"}}if(o.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${o.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${o.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
61
- `),display:`/screenshot ${o.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${o.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${o.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${o.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
62
- `),s=o.mode==="scope"?`/screenshot ${o.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as Yl,readFileSync as Vl}from"node:fs";import{isAbsolute as Xl,join as Kl,resolve as Jl}from"node:path";import{readProjectConfig as Ql}from"document360-engine";async function St(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
63
- `),display:"/capture-setup"}}var Ps="-".repeat(64);function $n(e){let t="user-docs/_capture";try{let $=Ql(e);$?.captureDir&&(t=$.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let o=Xl(t)?Kl(t,"CAPTURE-TESTID-REQUESTS.json"):Jl(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!Yl(o))return null;let n;try{let $=JSON.parse(Vl(o,"utf8").replace(/^/,""));n=Array.isArray($?.requests)?$.requests:[]}catch{return null}if(n.length===0)return null;let r=new Set(n.map($=>$.file).filter(Boolean)).size,s=n.length,c=`${s} data-testid attribute${s===1?"":"s"}`,p=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,h=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${p}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",Ps,`Read ${g} and ${h}. They list ${c} that d360-writer THINKS this repo needs for screenshot automation. Treat them as a hypothesis to VERIFY against the source \u2014 not a verified patch list.`,"","For EACH request in the JSON:",'1. Locate the element in THIS repo. Trace it from the route/page, not the feature name. If "anchorVerified" is false, find it by the "element"/"reason" description; otherwise confirm the "anchor" snippet still exists in "file".',"2. If the element already has a data-testid, skip it and note the existing one.","3. Confirm the attribute will reach the DOM: the rendering component must forward unknown props (a {...rest} spread or explicit pass-through). If it is a shared wrapper (e.g. Modal, ContextMenu) that destructures a fixed prop list, a data-testid is silently dropped \u2014 thread it through the component, or report it as needing a code change instead of forcing it.",'4. Only then add data-testid="<testid>" to the verified element. Add nothing else; do not rename, restructure, reformat, or change behaviour.',"","When done, report: which testids you added (file + element), which were already present, which requests were wrong (wrong file/anchor/page) with the correction, and which need threading through a shared component. Never force a request that does not match the source \u2014 flag it instead.",Ps]}import{writeFileSync as Zl}from"node:fs";import{devHintsGuidePath as ec,hintsDir as tc,ensureDir as oc}from"document360-engine";var Ss="-".repeat(64),nc=1;function xn(){return`# Dev \u2192 Docs hints \u2014 protocol guide
60
+ `)}async function un(e,t){let n=bo(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let p of Co(vo(c,n.scope),n.scope))console.log(p);return{kind:"continue"}}if(n.mode==="single")return{kind:"forward-to-agent",prompt:[`Run the emit-screenshot-spec skill for the SCREENSHOT placeholder with id \`${n.id}\`.`,"Locate its <!-- SCREENSHOT --> block in user-docs/**/*.md, then follow that skill exactly: read the","product source for EXACT routes + stable data-testid selectors (never guess); enter the prepared","context via captureScope(); guard data prerequisites with test.skip(reason); write",`<captureDir>/${n.id}.spec.ts. Report the spec path and any TODO data-testids or data to stage.`].join(`
61
+ `),display:`/screenshot ${n.id}`};let r=[`Run the emit-screenshot-spec skill to (re)generate the document360-capture spec for ${n.mode==="scope"?`every <!-- SCREENSHOT --> placeholder in articles under ${n.scope}`:"every <!-- SCREENSHOT --> placeholder across user-docs/**/*.md"}.`,"Follow that skill exactly for each: read the SCREENSHOT block; read the product source for EXACT routes","and stable data-testid selectors (never guess); enter the prepared context via captureScope(); guard data",`prerequisites with test.skip(reason); write <captureDir>/<id>.spec.ts.${n.setup?" When done, run capture-setup-checklist to refresh CAPTURE-SETUP.md.":""} Report specs written, TODO data-testids, and data to stage.`].join(`
62
+ `),s=n.mode==="scope"?`/screenshot ${n.scope}`:`/screenshot${e[0]?` ${e[0]}`:""}`;return{kind:"forward-to-agent",prompt:r,display:s}}import{existsSync as ec,readFileSync as tc}from"node:fs";import{isAbsolute as nc,join as oc,resolve as rc}from"node:path";import{readProjectConfig as sc}from"document360-engine";async function Tt(){return{kind:"forward-to-agent",prompt:["Run the capture-setup-checklist skill.","","Scan every <!-- SCREENSHOT --> block across user-docs/**/*.md, read each block\u2019s prerequisites,","anchor them to the capture scope keys in .d360-capture.json, then synthesize a deduped, grouped","\u201Cstage this data\u201D checklist and write it to <captureDir>/CAPTURE-SETUP.md. Separate transient/manual","states, flag any spec whose prerequisites are vague, and report the path when done."].join(`
63
+ `),display:"/capture-setup"}}var As="-".repeat(64);function Po(e){let t="user-docs/_capture";try{let $=sc(e);$?.captureDir&&(t=$.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=nc(t)?oc(t,"CAPTURE-TESTID-REQUESTS.json"):rc(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!ec(n))return null;let o;try{let $=JSON.parse(tc(n,"utf8").replace(/^/,""));o=Array.isArray($?.requests)?$.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map($=>$.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,p=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,k=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${p}.`,"\u26A0 Do NOT run this prompt here. d360-writer documents code; it does not change it \u2014"," these edits belong in your code repo. Open a SEPARATE terminal in:",` ${e}`," start Claude Code (or your coding agent), and paste everything between the lines."," Tip: run it in plan mode first \u2014 these are a hypothesis to verify, not a verified patch.","",As,`Read ${g} and ${k}. They list ${c} that d360-writer THINKS this repo needs for screenshot automation. Treat them as a hypothesis to VERIFY against the source \u2014 not a verified patch list.`,"","For EACH request in the JSON:",'1. Locate the element in THIS repo. Trace it from the route/page, not the feature name. If "anchorVerified" is false, find it by the "element"/"reason" description; otherwise confirm the "anchor" snippet still exists in "file".',"2. If the element already has a data-testid, skip it and note the existing one.","3. Confirm the attribute will reach the DOM: the rendering component must forward unknown props (a {...rest} spread or explicit pass-through). If it is a shared wrapper (e.g. Modal, ContextMenu) that destructures a fixed prop list, a data-testid is silently dropped \u2014 thread it through the component, or report it as needing a code change instead of forcing it.",'4. Only then add data-testid="<testid>" to the verified element. Add nothing else; do not rename, restructure, reformat, or change behaviour.',"","When done, report: which testids you added (file + element), which were already present, which requests were wrong (wrong file/anchor/page) with the correction, and which need threading through a shared component. Never force a request that does not match the source \u2014 flag it instead.",As]}import{writeFileSync as ic}from"node:fs";import{devHintsGuidePath as ac,hintsDir as lc,ensureDir as cc}from"document360-engine";var Es="-".repeat(64),uc=1;function So(){return`# Dev \u2192 Docs hints \u2014 protocol guide
64
64
 
65
- <!-- Guide version: ${nc} \xB7 maintained by document360-writer (/devhints). -->
65
+ <!-- Guide version: ${uc} \xB7 maintained by document360-writer (/devhints). -->
66
66
 
67
67
  This repo's user documentation is written by **document360-writer**, a separate documentation agent.
68
68
  It can read your code, but it can't read your *intent* \u2014 why a change matters to an end user, the
@@ -119,40 +119,40 @@ Prose: what you built and the gotchas a code diff would miss \u2014 e.g. "the bu
119
119
  - Keep the body about user-facing intent, not implementation. Don't propose docs structure.
120
120
 
121
121
  That's it \u2014 drop the file and keep building. The docs agent picks it up on its next analysis run.
122
- `}async function Ts(e,t){oc(tc(t.cwd)),Zl(ec(t.cwd),xn());for(let o of bn())console.log(o);return{kind:"continue"}}function bn(){return["\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md and created the .d360-writer/hints/ inbox.","","\u{1F4CB} One-time setup \u2014 connect this repo's coding agent to the docs agent."," Add the block below to your SOURCE repo's agent memory (CLAUDE.md / AGENTS.md), then commit"," .d360-writer/DEV-HINTS-GUIDE.md so teammates' agents get the same instruction:","",Ss,"## Documentation hints (for the docs agent)","","When you ship a user-facing change (new/changed/removed screen, button, flow, setting, API field,","CLI flag, or permission), record a doc hint so the documentation team's agent can update the help","center. How: read `.d360-writer/DEV-HINTS-GUIDE.md` and follow it \u2014 create one markdown file under","`.d360-writer/hints/`. Do this as part of finishing the feature. Skip it only for changes with no","user-visible effect (refactors, tests, build, deps).",Ss,"","Then keep building as usual. On its next analysis run (/audit), the docs agent reads open hints,","verifies each against your code, writes/updates the docs, and marks the hint consumed."]}q();import{existsSync as rc,rmSync as sc}from"node:fs";import{basename as Rs,resolve as js}from"node:path";import{execFileSync as ic}from"node:child_process";import{input as ac}from"@inquirer/prompts";import{readProjectConfig as lc}from"document360-engine";var cc=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function vn(e){let t=null;try{t=lc(e)}catch{t=null}let o=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),n=o(t?.docsDir)??"user-docs",r=new Set([n]);for(let s of[o(t?.captureDir),o(t?.outputDir)])s&&s!==n&&!s.startsWith(`${n}/`)&&r.add(s);return[...r,...cc].filter(s=>rc(js(e,s)))}function Cn(e,t){let o=[],n=[];for(let r of t)try{sc(js(e,r),{recursive:!0,force:!0}),o.push(r)}catch(s){n.push({path:r,error:s.message})}return{removed:o,failed:n}}function uc(e){try{return ic("git",["status","--porcelain","--",".d360-writer/hints"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
123
- `).filter(o=>o.trim()&&/\.md\s*$/.test(o)).length}catch{return 0}}function Pn(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let o=uc(e);return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(n=>` \u2022 ${n}`),"",...o>0?[`\u26A0 ${o} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Rs(e)}`]}async function As(e,t){let o=vn(t.cwd);for(let p of Pn(t.cwd,o))console.log(o.length===0?f(p):p);if(o.length===0)return{kind:"continue"};let n=Rs(t.cwd);if((await t.withPausedInput(()=>ac({message:`Type "${n}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==n)return console.log(O("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=Cn(t.cwd,o);console.log(I(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let p of c)console.log(b(` \u2717 ${p.path}: ${p.error}`));return console.log(f("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var Es={help:Lo,"?":Lo,clear:$r,exit:Oo,quit:Oo,init:Sr,mcp:Qt,publish:Wr,audit:oo,scope:Hr,sync:Xr,convert:Kr,write:es,resume:ns,rename:rs,profile:is,model:cs,doctor:Pt,workspace:ms,project:ks,"allow-prod":ys,login:$s,logout:xs,screenshot:ao,"capture-setup":St,devhints:Ts,reset:As};function Ds(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}q();var dc={Bash:"command",PowerShell:"command",Read:"file_path",Write:"file_path",Edit:"file_path",NotebookEdit:"notebook_path",Glob:"pattern",Grep:"pattern",WebFetch:"url",WebSearch:"query",Agent:"description",Task:"description",Skill:"skill"},_s=160,Tn=200,Ns=40;function ue(e,t){let o=e.replace(/\s+/g," ").trim();return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function Is(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}: ${ue(typeof r=="string"?r:JSON.stringify(r),Ns)}`);return t.length>4&&o.push("\u2026"),ue(o.join(", "),_s)}var _e=e=>typeof e=="string"&&e?e:null,Sn=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function pc(e){let t=_e(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);if(o.length<2)return null;let n=o[o.length-2],r=n.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?n:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function fc(e,t){let o=(n,r)=>({title:`Document360: ${n}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let n=_e(t.title);if(!n)return null;let r=pc(t.local_path);return o("Create article",`"${ue(n,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let n=_e(t.title),r=Sn(t.article_id);return o("Update article",n?`"${ue(n,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return o("Fork article (new draft)",Sn(t.article_id)?`id ${Sn(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",_e(t.name)?`"${ue(_e(t.name),60)}"`:null);case"d360_upload_drive_file":{let n=_e(t.file_path);return o("Upload image",n?ue(n.replace(/\\/g,"/").split("/").pop()??n,60):null)}case"d360_sync_status":return o("Check sync status",null);default:return null}}function lo(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let h=fc(c,t);if(h)return h}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:Is(t)}}let o=dc[e],n=o?t[o]:void 0;return typeof n=="string"&&n?{title:e,sep:"",arg:ue(n,_s)}:{title:e,sep:"",arg:Is(t)}}function mc(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 gc(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let o=t.map(mc).filter(r=>r!==null),n=`${t.length} item${t.length===1?"":"s"}`;return o.length===0?[n]:[n,...o.map(r=>ue(r,Tn))]}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}: ${ue(String(r),Ns)}`);return o.length>0?[ue(o.join(" \xB7 "),Tn)]:null}return null}function hc(e){let t=_e(e)?.replace(/\\/g,"/");if(!t)return null;let o=t.split("/").filter(Boolean);return o.length>1?o.slice(1).join("/"):t}function wc(e,t,o){switch(e){case"d360_create_article":{let n=hc(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=_e(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 ${ue(n[0],120)}`:""}`]}default:return null}}function co(e,t=4,o,n){let r=e.replace(/\r\n/g,`
124
- `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(o?.startsWith("mcp__document360__")){let c=wc(o.slice(18),n,r);if(c)return{lines:c,hidden:0}}let s=gc(r)??r.split(`
125
- `);return{lines:s.slice(0,t).map(c=>ue(c,Tn)),hidden:Math.max(0,s.length-t)}}function uo(e,t,o,n="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${n}/${o}`}function po(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 fo(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 jc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Ac(e,t,o,n){if(jc.test(e))try{let r=Us(n),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=po(t,o),p=fo(o);e.endsWith("publish_article")&&p&&console.log(E(` \u2B95 Live: ${p}`)),c&&s&&console.log(E(` \u2B95 Preview: ${uo(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Ws(e=process.cwd(),t="auto",o){let n=yc(t);n.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${E("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${E('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Ec(e,o),n.kind==="subscription"&&(n.stored?console.log(f(" Using your Claude subscription (no API key set).")):console.log(f(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=Rn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=kc({input:process.stdin,output:process.stdout}),g=[],h=null,$=!1;p.on("line",x=>{if(h){let v=h;h=null,v(x)}else g.push(x)}),p.on("close",()=>{if($=!0,h){let x=h;h=null,x(null)}});function R(){return g.length>0?Promise.resolve(g.shift()):$?Promise.resolve(null):(process.stdout.write(E("> ")),new Promise(x=>{h=x}))}let y={cwd:e,profileName:o,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=Rn({cwd:e,profileName:o,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async x=>s.setModel(x),withPausedInput:async x=>{p.pause();try{return await x()}finally{p.resume()}}};try{for(;;){let x=await R();if(x===null)break;let v=x.trim();if(v){if(v.startsWith("/")){let _=Ds(v);if(!_)continue;let N=Es[_.name];if(!N){console.log(b(`Unknown command: /${_.name}`)),console.log(f("Type /help for the list."));continue}let H=await N(_.args,y);if(H.kind==="exit")break;if(H.kind==="clear"){y.restartAgent();continue}if(H.kind==="allow-prod"){r=!0,y.restartAgent(),console.log(I("\u2713 Production writes authorized for this session.")),console.log("");continue}if(H.kind==="resume"){s.close(),s=Rn({cwd:e,resume:H.uuid,profileName:o,allowProdWrites:r});let Z=$c(H.uuid);c={uuid:H.uuid,firstPrompt:Z?.firstPrompt??null,titleFired:!0},Os(H.uuid),console.log(I(`\u2713 Resumed "${H.name}"`)),console.log("");continue}H.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=H.display??H.prompt),await Ls(s,H.prompt,n,c,e));continue}c.firstPrompt||(c.firstPrompt=v),await Ls(s,v,n,c,e)}}}finally{s.close(),p.close()}}function Ec(e,t){console.log(""),console.log(ht("document360-writer")),console.log(f(` cwd: ${e}`));let o=Pc(e);console.log(f(` model: ${o.model??"auto (engine right-sizes per task)"}${o.model?` (${o.source})`:""}`)),console.log(Dc(e,t)),Sc(e)||console.log(O(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function Dc(e,t){try{let o=Us(e,t),n=o.production?O(" \u26A0 PRODUCTION"):"",r=Rc(o.name);if(!r)return f(` Document360: profile "${o.name}"${n} \u2014 not logged in (d360-writer login)`);let s={...Ms(r.idToken)??{},...Ms(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return Tc(r)&&!r.refreshToken?O(` Document360: profile "${o.name}"${n} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${o.name}"${n}`)}catch(o){return f(` Document360: ${o.message.split(".")[0]}`)}}function Ic(){console.error(""),console.error(`Sign in with your Claude subscription: run ${E("claude")} once, then retry.`),console.error(` (No Claude Code? ${E("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${E("https://console.anthropic.com/settings/keys")}`)}function _c(e,t,o){e.uuid=t;let n=new Date().toISOString();vc({uuid:t,name:bc(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:o,firstPrompt:e.firstPrompt??"",createdAt:n,updatedAt:n})}function Nc(e,t){e.titleFired=!0;let o=e.uuid,n=e.firstPrompt;!o||!n||Cc(n,t).then(r=>{r&&xc(o,r)}).catch(()=>{})}async function Ls(e,t,o,n,r){let s=new Map;for await(let c of e.send(t))Mc(c,n,r,o,s)}function Mc(e,t,o,n,r){switch(e.type){case"session":t.uuid||_c(t,e.sessionId,o);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=lo(e.name,e.input);s&&(process.stdout.write(`
122
+ `}async function Ds(e,t){cc(lc(t.cwd)),ic(ac(t.cwd),So());for(let n of To())console.log(n);return{kind:"continue"}}function To(){return["\u2713 Wrote .d360-writer/DEV-HINTS-GUIDE.md and created the .d360-writer/hints/ inbox.","","\u{1F4CB} One-time setup \u2014 connect this repo's coding agent to the docs agent."," Add the block below to your SOURCE repo's agent memory (CLAUDE.md / AGENTS.md), then commit"," .d360-writer/DEV-HINTS-GUIDE.md so teammates' agents get the same instruction:","",Es,"## Documentation hints (for the docs agent)","","When you ship a user-facing change (new/changed/removed screen, button, flow, setting, API field,","CLI flag, or permission), record a doc hint so the documentation team's agent can update the help","center. How: read `.d360-writer/DEV-HINTS-GUIDE.md` and follow it \u2014 create one markdown file under","`.d360-writer/hints/`. Do this as part of finishing the feature. Skip it only for changes with no","user-visible effect (refactors, tests, build, deps).",Es,"","Then keep building as usual. On its next analysis run (/audit), the docs agent reads open hints,","verifies each against your code, writes/updates the docs, and marks the hint consumed."]}G();import{existsSync as dc,rmSync as pc}from"node:fs";import{basename as Is,resolve as _s}from"node:path";import{execFileSync as fc}from"node:child_process";import{input as mc}from"@inquirer/prompts";import{readProjectConfig as gc}from"document360-engine";var hc=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function Ro(e){let t=null;try{t=gc(e)}catch{t=null}let n=s=>s?.replace(/\\/g,"/").replace(/\/+$/,""),o=n(t?.docsDir)??"user-docs",r=new Set([o]);for(let s of[n(t?.captureDir),n(t?.outputDir)])s&&s!==o&&!s.startsWith(`${o}/`)&&r.add(s);return[...r,...hc].filter(s=>dc(_s(e,s)))}function jo(e,t){let n=[],o=[];for(let r of t)try{pc(_s(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}return{removed:n,failed:o}}function kc(e){try{return fc("git",["status","--porcelain","--",".d360-writer/hints"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).split(`
123
+ `).filter(n=>n.trim()&&/\.md\s*$/.test(n)).length}catch{return 0}}function Ao(e,t){if(t.length===0)return["Nothing to reset \u2014 no d360-writer files found in this repo."];let n=kc(e);return["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(o=>` \u2022 ${o}`),"",...n>0?[`\u26A0 ${n} doc hint(s) under .d360-writer/hints/ are not committed \u2014 commit them first or they're gone.`]:[],"Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${Is(e)}`]}async function Ns(e,t){let n=Ro(t.cwd);for(let p of Ao(t.cwd,n))console.log(n.length===0?f(p):p);if(n.length===0)return{kind:"continue"};let o=Is(t.cwd);if((await t.withPausedInput(()=>mc({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(O("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=jo(t.cwd,n);console.log(A(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let p of c)console.log(b(` \u2717 ${p.path}: ${p.error}`));return console.log(f("Set up d360-writer again with /init (then /login, /workspace).")),{kind:"continue"}}var Ms={help:Un,"?":Un,clear:Sr,exit:Wn,quit:Wn,init:Dr,mcp:tn,publish:Gr,audit:rn,scope:Yr,sync:es,convert:ts,write:ss,resume:ls,rename:cs,profile:ds,model:ms,doctor:St,workspace:ys,project:vs,"allow-prod":Cs,login:Ps,logout:Ss,screenshot:un,"capture-setup":Tt,devhints:Ds,reset:Ns};function Ls(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}G();var wc={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"},Us=160,Do=200,Ws=40;function ue(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function Os(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}: ${ue(typeof r=="string"?r:JSON.stringify(r),Ws)}`);return t.length>4&&n.push("\u2026"),ue(n.join(", "),Us)}var Ie=e=>typeof e=="string"&&e?e:null,Eo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function yc(e){let t=Ie(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);if(n.length<2)return null;let o=n[n.length-2],r=o.replace(/^\d+[-_.]/,"").split(/[-_]/).filter(Boolean);return r.length===0?o:r.map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}function $c(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ie(t.title);if(!o)return null;let r=yc(t.local_path);return n("Create article",`"${ue(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ie(t.title),r=Eo(t.article_id);return n("Update article",o?`"${ue(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",Eo(t.article_id)?`id ${Eo(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",Ie(t.name)?`"${ue(Ie(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ie(t.file_path);return n("Upload image",o?ue(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function dn(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let k=$c(c,t);if(k)return k}let p=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${p}`,sep:" ",arg:Os(t)}}let n=wc[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:ue(o,Us)}:{title:e,sep:"",arg:Os(t)}}function xc(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 bc(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(xc).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>ue(r,Do))]}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}: ${ue(String(r),Ws)}`);return n.length>0?[ue(n.join(" \xB7 "),Do)]:null}return null}function vc(e){let t=Ie(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function Cc(e,t,n){switch(e){case"d360_create_article":{let o=vc(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=Ie(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 ${ue(o[0],120)}`:""}`]}default:return null}}function pn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
124
+ `).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=Cc(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=bc(r)??r.split(`
125
+ `);return{lines:s.slice(0,t).map(c=>ue(c,Do)),hidden:Math.max(0,s.length-t)}}function fn(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function mn(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 gn(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 Mc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Lc(e,t,n,o){if(Mc.test(e))try{let r=qs(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=mn(t,n),p=gn(n);e.endsWith("publish_article")&&p&&console.log(E(` \u2B95 Live: ${p}`)),c&&s&&console.log(E(` \u2B95 Preview: ${fn(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function Gs(e=process.cwd(),t="auto",n){let o=Sc(t);o.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${E("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${E('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Oc(e,n),o.kind==="subscription"&&(o.stored?console.log(f(" Using your Claude subscription (no API key set).")):console.log(f(" No API key or stored Claude Code login found \u2014 trying your Claude session anyway.")),console.log(""));let r=!1,s=Io({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},p=Pc({input:process.stdin,output:process.stdout}),g=[],k=null,$=!1;p.on("line",x=>{if(k){let v=k;k=null,v(x)}else g.push(x)}),p.on("close",()=>{if($=!0,k){let x=k;k=null,x(null)}});function R(){return g.length>0?Promise.resolve(g.shift()):$?Promise.resolve(null):(process.stdout.write(E("> ")),new Promise(x=>{k=x}))}let w={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=Io({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async x=>s.setModel(x),withPausedInput:async x=>{p.pause();try{return await x()}finally{p.resume()}}};try{for(;;){let x=await R();if(x===null)break;let v=x.trim();if(v){if(v.startsWith("/")){let I=Ls(v);if(!I)continue;let _=Ms[I.name];if(!_){console.log(b(`Unknown command: /${I.name}`)),console.log(f("Type /help for the list."));continue}let H=await _(I.args,w);if(H.kind==="exit")break;if(H.kind==="clear"){w.restartAgent();continue}if(H.kind==="allow-prod"){r=!0,w.restartAgent(),console.log(A("\u2713 Production writes authorized for this session.")),console.log("");continue}if(H.kind==="resume"){s.close(),s=Io({cwd:e,resume:H.uuid,profileName:n,allowProdWrites:r});let Z=Tc(H.uuid);c={uuid:H.uuid,firstPrompt:Z?.firstPrompt??null,titleFired:!0},Bs(H.uuid),console.log(A(`\u2713 Resumed "${H.name}"`)),console.log("");continue}H.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=H.display??H.prompt),await Hs(s,H.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=v),await Hs(s,v,o,c,e)}}}finally{s.close(),p.close()}}function Oc(e,t){console.log(""),console.log(ht("document360-writer")),console.log(f(` cwd: ${e}`));let n=Dc(e);console.log(f(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(Uc(e,t)),Ic(e)||console.log(O(" First run: /init \u2192 /login \u2192 /workspace, then ask for a docs analysis.")),console.log(f(" Type a prompt, or /help for slash commands. /exit to quit.")),console.log("")}function Uc(e,t){try{let n=qs(e,t),o=n.production?O(" \u26A0 PRODUCTION"):"",r=Nc(n.name);if(!r)return f(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...Fs(r.idToken)??{},...Fs(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return _c(r)&&!r.refreshToken?O(` Document360: profile "${n.name}"${o} \u2014 session expired (d360-writer login)`):f(` Document360: ${c} \xB7 profile "${n.name}"${o}`)}catch(n){return f(` Document360: ${n.message.split(".")[0]}`)}}function Wc(){console.error(""),console.error(`Sign in with your Claude subscription: run ${E("claude")} once, then retry.`),console.error(` (No Claude Code? ${E("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${E("https://console.anthropic.com/settings/keys")}`)}function Fc(e,t,n){e.uuid=t;let o=new Date().toISOString();Ac({uuid:t,name:jc(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function Hc(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||Ec(o,t).then(r=>{r&&Rc(n,r)}).catch(()=>{})}async function Hs(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))Bc(c,o,r,n,s)}function Bc(e,t,n,o,r){switch(e.type){case"session":t.uuid||Fc(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=dn(e.name,e.input);s&&(process.stdout.write(`
126
126
 
127
- `),console.log(`${I("\u25CF")} ${Ce(s.title)}${s.arg!==null?ee(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=qe(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(ee(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(f(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=co(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?b:ee;c.lines.forEach((g,h)=>console.log(p((h===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||Ac(s.name,s.input,e.output,o);break}case"result":process.stdout.write(`
128
- `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Os(t.uuid),t.titleFired||Nc(t,o));break;case"error":console.error(""),console.error(b(`agent error: ${e.message}`)),n.kind==="subscription"&&e.kind==="auth"&&Ic();break}}import{render as Nu}from"ink";import{resolveAuth as Mu}from"document360-engine";import{useCallback as V,useEffect as Me,useMemo as In,useRef as Q,useState as B}from"react";import{Box as J,Text as C,useApp as zc,useInput as Yc,useStdout as Vc}from"ink";import{existsSync as ri,readFileSync as si,readdirSync as mi,writeFileSync as Xc}from"node:fs";import{basename as jt,isAbsolute as Kc,join as At}from"node:path";import{createSession as ii,loginPkce as Jc,toStoredTokens as Qc,saveTokens as Zc,getAccessToken as eu,resolveActiveProfile as de,resolveProjectId as tu,getArticle as ou,decodeJwtClaims as dt,isExpired as Ne,loadTokens as Ge,clearTokens as nu,clearProfileProject as ru,setTitle as su,slugify as iu,touchSession as _n,upsertSession as au,generateTitle as ai,findByName as lu,listSessions as cu,renameSession as uu,suggestNextAction as du,readProjectConfig as ie,writeProjectConfig as pu,resolveModelSetting as On,loadProfileMap as li,applyPull as fu,computeSyncStatus as Nn,planPull as mu,inventoryRepo as gu,knownEnvironments as hu,resolveEnvironment as wu,planPartitions as Mn,partitionEvenly as ku,screenshotPlaceholderIds as yu,trackedArticlePaths as $u,runPartitioned as yo,estimateBulkCost as ci,resolveModelForOperation as $o,readDocsPlan as xu,ensureDir as bu,hintsDir as vu,devHintsGuidePath as Cu}from"document360-engine";import{existsSync as Lc,mkdirSync as Oc,readFileSync as Uc,writeFileSync as Wc}from"node:fs";import{join as Fs}from"node:path";import{writerDir as Fc}from"document360-engine";function Hs(e){return Fs(Fc(e),".sessions")}function Bs(e,t){return Fs(Hs(e),`${t}.json`)}function qs(e,t,o){try{Oc(Hs(e),{recursive:!0});let n=o.filter(r=>r.kind!=="banner");Wc(Bs(e,t),JSON.stringify({v:1,items:n}),"utf8")}catch{}}function jn(e,t){try{let o=Bs(e,t);if(!Lc(o))return[];let n=JSON.parse(Uc(o,"utf8"));return Array.isArray(n.items)?n.items:[]}catch{return[]}}var Hc=e=>`\x1B]0;${e}\x07`,Bc=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function Gs(e){process.stdout.isTTY&&process.stdout.write(e)}function Tt(e){Gs(Hc(e))}function mo(e){Gs(Bc(e))}var go=["\xB7 "," \xB7"],An="\u{1F7E3}";io();var qc=/^(?:\d+[.)]|[-*•])\s+/;function zs(e,t,o=4){let n=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
129
- `)){let c=s.trim().replace(qc,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!n.has(g[1].toLowerCase()))continue;let h=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(h)||r.push(h),r.length>=o)break}return r}q();function Rt(e){return e.map(t=>({label:t.label,items:t.paths.length,status:"pending"}))}function Pe(e,t,o){return e.map(n=>n.label===t?{...n,status:o}:n)}function En(e){return e.replace(/^[\w.-]+\//,"")}var ho=16,Ys={pending:"\u25CB",active:"\u25A0",done:"\u2714",failed:"\u2717"};function Dn(e){return e.trim().split(/\s+/).slice(1).join(" ")}function Vs(e,t){let o=Dn(t);return e.some(r=>r.trim()===t.trim()||!!o&&Dn(r)===o)?e.filter(r=>r.trim()!==t.trim()&&!(o&&Dn(r)===o)):[]}var Gc=/\[Pasted text #\d+ \+\d+ lines?\]/g;function Xs(e){return e.replace(/\r\n?/g,`
130
- `)}function Ks(e){return e.includes(`
131
- `)||e.length>200}function Js(e,t){let o=t.split(`
132
- `).length;return`[Pasted text #${e} +${o} line${o===1?"":"s"}]`}function Qs(e,t){return e.replace(Gc,o=>t.get(o)??o)}function Zs(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function wo(e,t){let o=Math.max(1,t),n=[],r=0;for(let s of e.split(`
133
- `)){if(s.length===0)n.push({start:r,end:r});else for(let c=0;c<s.length;c+=o)n.push({start:r+c,end:r+Math.min(c+o,s.length)});r+=s.length+1}return n.length>0?n:[{start:0,end:0}]}function ko(e,t){let o=0;for(let n=0;n<e.length&&e[n].start<=t;n++)o=n;return o}function ei(e,t,o){let n=ko(e,t),r=n+o;if(r<0||r>=e.length)return t;let s=Math.min(t,e[n].end)-e[n].start;return Math.min(e[r].start+s,e[r].end)}function ti(e,t,o){let n=e[ko(e,t)];return o==="start"?n.start:n.end}function oi(e,t,o){let n=Math.max(1,o),r=e.split(`
134
- `),s=0;for(let c=r.length-1;c>=0;c--){let p=r[c];if(s+=Math.max(1,Math.ceil(p.length/n)),s>=t){let g=s-t;return{text:[g>0?p.slice(g*n):p,...r.slice(c+1)].join(`
135
- `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function ni(e){let t=!1,o=0,n=0;for(let r of e.split(`
136
- `)){let s=n+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(o=s+1),n=s+1}return o}import{Fragment as Ln,jsx as S,jsxs as F}from"react/jsx-runtime";var Pu={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},ui=` 1. /init \u2014 pick your Document360 environment & scaffold config
127
+ `),console.log(`${A("\u25CF")} ${ve(s.title)}${s.arg!==null?ee(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=qe(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=p=>p===1?"":"s";console.log(ee(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let p of s.lines)console.log(p);s.hidden>0&&console.log(f(` \u2026 +${s.hidden} more diff lines`));break}case"tool_result":{let s=r.get(e.id);if(!s)break;r.delete(e.id);let c=pn(e.output,4,e.isError?void 0:s.name,s.input),p=e.isError?b:ee;c.lines.forEach((g,k)=>console.log(p((k===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||Lc(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
128
+ `),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(Bs(t.uuid),t.titleFired||Hc(t,n));break;case"error":console.error(""),console.error(b(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&Wc();break}}import{render as Hu}from"ink";import{resolveAuth as Bu}from"document360-engine";import{useCallback as V,useEffect as Ne,useMemo as Oo,useRef as Q,useState as B}from"react";import{Box as J,Text as C,useApp as Zc,useInput as eu,useStdout as tu}from"ink";import{existsSync as ci,readFileSync as ui,readdirSync as yi,writeFileSync as nu}from"node:fs";import{basename as At,isAbsolute as ou,join as Et}from"node:path";import{createSession as di,loginPkce as ru,toStoredTokens as su,saveTokens as iu,getAccessToken as au,resolveActiveProfile as de,resolveProjectId as lu,getArticle as cu,decodeJwtClaims as dt,isExpired as _e,loadTokens as Ge,clearTokens as uu,clearProfileProject as du,setTitle as pu,slugify as fu,touchSession as Uo,upsertSession as mu,generateTitle as pi,findByName as gu,listSessions as hu,renameSession as ku,suggestNextAction as wu,readProjectConfig as ie,writeProjectConfig as yu,resolveModelSetting as Bo,loadProfileMap as fi,applyPull as $u,computeSyncStatus as Wo,planPull as xu,inventoryRepo as bu,knownEnvironments as vu,resolveEnvironment as Cu,planPartitions as Fo,partitionEvenly as Pu,screenshotPlaceholderIds as Su,trackedArticlePaths as Tu,runPartitioned as xn,estimateBulkCost as mi,resolveModelForOperation as bn,readDocsPlan as Ru,ensureDir as ju,hintsDir as Au,devHintsGuidePath as Eu}from"document360-engine";import{existsSync as qc,mkdirSync as Gc,readFileSync as zc,writeFileSync as Yc}from"node:fs";import{join as zs}from"node:path";import{writerDir as Vc}from"document360-engine";function Ys(e){return zs(Vc(e),".sessions")}function Vs(e,t){return zs(Ys(e),`${t}.json`)}function Xs(e,t,n){try{Gc(Ys(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");Yc(Vs(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function _o(e,t){try{let n=Vs(e,t);if(!qc(n))return[];let o=JSON.parse(zc(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var Xc=e=>`\x1B]0;${e}\x07`,Kc=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function Ks(e){process.stdout.isTTY&&process.stdout.write(e)}function Rt(e){Ks(Xc(e))}function hn(e){Ks(Kc(e))}var kn=["\xB7 "," \xB7"],No="\u{1F7E3}";cn();var Jc=/^(?:\d+[.)]|[-*•])\s+/;function Js(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
129
+ `)){let c=s.trim().replace(Jc,""),p=c.match(/^`([^`]+)`$/);p&&(c=p[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let k=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(k)||r.push(k),r.length>=n)break}return r}G();function jt(e){return e.map(t=>({label:t.label,items:t.paths.length,status:"pending"}))}function Ce(e,t,n){return e.map(o=>o.label===t?{...o,status:n}:o)}function Mo(e){return e.replace(/^[\w.-]+\//,"")}var wn=16,Qs={pending:"\u25CB",active:"\u25A0",done:"\u2714",failed:"\u2717"};function Lo(e){return e.trim().split(/\s+/).slice(1).join(" ")}function Zs(e,t){let n=Lo(t);return e.some(r=>r.trim()===t.trim()||!!n&&Lo(r)===n)?e.filter(r=>r.trim()!==t.trim()&&!(n&&Lo(r)===n)):[]}var Qc=/\[Pasted text #\d+ \+\d+ lines?\]/g;function ei(e){return e.replace(/\r\n?/g,`
130
+ `)}function ti(e){return e.includes(`
131
+ `)||e.length>200}function ni(e,t){let n=t.split(`
132
+ `).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function oi(e,t){return e.replace(Qc,n=>t.get(n)??n)}function ri(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function yn(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
133
+ `)){if(s.length===0)o.push({start:r,end:r});else for(let c=0;c<s.length;c+=n)o.push({start:r+c,end:r+Math.min(c+n,s.length)});r+=s.length+1}return o.length>0?o:[{start:0,end:0}]}function $n(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function si(e,t,n){let o=$n(e,t),r=o+n;if(r<0||r>=e.length)return t;let s=Math.min(t,e[o].end)-e[o].start;return Math.min(e[r].start+s,e[r].end)}function ii(e,t,n){let o=e[$n(e,t)];return n==="start"?o.start:o.end}function ai(e,t,n){let o=Math.max(1,n),r=e.split(`
134
+ `),s=0;for(let c=r.length-1;c>=0;c--){let p=r[c];if(s+=Math.max(1,Math.ceil(p.length/o)),s>=t){let g=s-t;return{text:[g>0?p.slice(g*o):p,...r.slice(c+1)].join(`
135
+ `),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function li(e){let t=!1,n=0,o=0;for(let r of e.split(`
136
+ `)){let s=o+r.length;/^\s*```/.test(r)?t=!t:!t&&r.trim()===""&&s<e.length&&(n=s+1),o=s+1}return n}import{Fragment as Ho,jsx as S,jsxs as F}from"react/jsx-runtime";var Du={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""},gi=` 1. /init \u2014 pick your Document360 environment & scaffold config
137
137
  2. /login signs in to that environment
138
138
  3. /project then /workspace \u2014 pick the project and where articles go
139
- Press 1 to start.`;function Su(e,t,o,n){let r=o.kind==="api"?"API key":o.kind==="subscription"?"subscription":"not configured",s=On(e),c=ie(e),p=(c?.docsDir??"user-docs").replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,h={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:Pu[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:g};if(c===null)return h;try{let $=de(e,n);h.profile=$.name,h.apiUrl=$.connection.apiUrl,h.prod=$.production,h.project=$.project.projectId??"(chosen at login)";let R=Ge($.name);if(R){let y={...dt(R.idToken)??{},...dt(R.accessToken)??{}},x=y.email??y.preferred_username??"signed in";Ne(R)?R.refreshToken&&(h.who=x,h.loggedOut=!1,h.sessionHint="session expired \u2014 refreshing\u2026"):(h.who=x,h.loggedOut=!1,h.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return h}function Tu(e,t){try{let o=de(e,t),n=Ge(o.name);if(!n)return{text:`profile "${o.name}" \u2014 not logged in (/login)`,prod:o.production};let r={...dt(n.idToken)??{},...dt(n.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return Ne(n)&&!n.refreshToken?{text:`profile "${o.name}" \u2014 session expired (/login)`,prod:o.production}:{text:`${s} \xB7 profile "${o.name}"`,prod:o.production}}catch(o){return{text:o.message.split(".")[0],prod:!1}}}var di=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],bo=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Ru="Ask me to write or update an article\u2026";function xo({ch:e,dim:t}){let[o,n]=B(!0);return Me(()=>{let r=setInterval(()=>n(s=>!s),530);return()=>clearInterval(r)},[]),S(C,{inverse:o,color:t&&!o?"gray":void 0,children:e})}var ju=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function pi(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Au=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function fi(e){try{return mi(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Eu(e,t){let o=t??[];return o.length===0?["src","api","services","packages","modules"].some(n=>fi(At(e,n))):o.some(n=>!n.includes("/")&&!n.endsWith(".md")&&fi(At(e,n)))}function Du({startTime:e,chars:t}){let[o,n]=B(0);Me(()=>{let g=setInterval(()=>n(h=>h+1),120);return()=>clearInterval(g)},[]);let r=bo[o%bo.length],s=di[Math.floor(o/16)%di.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return F(J,{children:[S(C,{color:G,children:` ${r} ${s}\u2026 `}),S(C,{color:"gray",children:`(${bt(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var Iu=12e4;function _u({p:e}){let[t,o]=B(0);Me(()=>{let N=setInterval(()=>o(H=>H+1),150);return()=>clearInterval(N)},[]);let n=bo[t%bo.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),p=Date.now()-e.lastAt>=Iu,g=e.verb??"Converting",h=e.unit??"article",$=e.rows.reduce((N,H)=>N+H.items,0),R=g.toLowerCase(),y=e.rows,x=0;if(y.length>ho){let N=y.findIndex(Z=>Z.status==="active"||Z.status==="pending"),H=Math.max(0,Math.min(N-2,y.length-ho));x=y.slice(0,H).filter(Z=>Z.status==="done").length,y=y.slice(H,H+ho)}let v=Math.min(40,Math.max(...y.map(N=>En(N.label).length),0)),_=N=>N==="failed"?"red":N==="active"?G:void 0;return F(J,{flexDirection:"column",children:[F(J,{children:[S(C,{color:G,children:` ${n} ${g} ${$} ${h}${$===1?"":"s"} `}),S(C,{color:"gray",children:`\xB7 ${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${bt(r)} \xB7 esc to stop`})]}),x>0&&S(C,{dimColor:!0,children:` \u2714 +${x} done`}),y.map(N=>F(C,{color:_(N.status),dimColor:N.status==="done"||N.status==="pending",children:[` ${Ys[N.status]} ${En(N.label).padEnd(v)}`,N.status!=="pending"?` (${N.items} ${h}${N.items===1?"":"s"})`:"",N.status==="active"?` \u2190 ${R}\u2026`:""]},N.label)),p&&S(C,{color:"yellow",children:` \u26A0 no activity for ${bt(s)} \u2014 press esc to stop if it's wedged`})]})}function gi({cwd:e,auth:t,profileName:o,version:n}){let{exit:r}=zc(),[s,c]=B(o),[p,g]=B(null),[h,$]=B({text:"",pos:0}),R=h.text,y=V(l=>{$(i=>{let a=typeof l=="function"?l(i.text):l;return{text:a,pos:a.length}})},[]),x=V(l=>{$(i=>({text:i.text.slice(0,i.pos)+l+i.text.slice(i.pos),pos:i.pos+l.length}))},[]),v=V(()=>{$(l=>{if(l.pos===0)return l;let i=l.text.slice(0,l.pos),a=Zs(i)??i.slice(0,-1);return{text:a+l.text.slice(l.pos),pos:a.length}})},[]),[_,N]=B(!1),[H,Z]=B(!1),[$e,te]=B(0),[we,ne]=B(null),[pt,X]=B([]),ze=Q(0),[yi,Et]=B(!1),[$i,Ye]=B(0),Le=Q(0),[ft,Ve]=B(0),[Xe,Dt]=B(null),Fn=Q(new Map),xi=Q(0),Ke=Q([]),pe=Q(null);pe.current===null&&(pe.current=ii({cwd:e,profileName:s,allowProdWrites:!1}));let xe=Q({uuid:null,firstPrompt:null,titleFired:!1}),It=Q(new Map),Je=Q(!1),vo=Q([]),Te=Q([]),Co=Q(null),[Hn,Qe]=B(null),[Oe,Ue]=B(null),[be,Ze]=B(null),[Re,et]=B(null),[ae,tt]=B(null),[fe,ot]=B(null),[ve,nt]=B(null),[me,We]=B(null),[le,rt]=B(null),[Bn,Po]=B([]),Fe=Q([]),_t=Q(!1),ge=Q(null),[qn,K]=B(null),Nt=Q(!1),Mt=Q(null),[he,He]=B(null),[Gn,st]=B(0),[zn,it]=B(0),[bi,Yn]=B(0),So=In(()=>{try{return On(e).model??"auto"}catch{return null}},[e,bi]),{stdout:ke}=Vc(),[,vi]=B(0),Vn=Q(`${ke.columns??80}x${ke.rows??24}`),je=V(()=>Math.max(20,(ke.columns??80)-1),[ke]),d=V(l=>{Ke.current.push(l),console.log(zo(l,je()))},[je]);Me(()=>{let l=`d360-writer \xB7 ${Hn??jt(e)}`;if(!_){mo(0),Tt(`${An} ${l}`);return}mo(3);let i=0;Tt(`${go[0]} ${l}`);let a=setInterval(()=>{i=(i+1)%go.length,Tt(`${go[i]} ${l}`)},400);return()=>clearInterval(a)},[_,e,Hn]),Me(()=>()=>mo(0),[]),Me(()=>{if(d({kind:"banner",info:Su(e,n,t,s)}),!ie(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:
140
- ${ui}`}),X(["/init"]);return}try{let l=de(e,s),i=Ge(l.name);i&&Ne(i)&&i.refreshToken?eu({profile:l.name,connection:l.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),te(a=>a+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),X(["/login"])}):(!i||Ne(i))&&(d({kind:"note",tone:"warn",text:`Profile "${l.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"]))}catch{}try{let l=ie(e),i=de(e,s),a=Ge(i.name);l&&a&&!Ne(a)&&Eu(e,l.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."}),X(["/scope"]))}catch{}},[]),Me(()=>{let l=null,i=null,a=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let w=`${ke.columns??80}x${ke.rows??24}`;w!==Vn.current&&(Vn.current=w,vi(u=>u+1),i&&clearTimeout(i),i=setTimeout(()=>{i=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Mr(Ke.current,je())+"\x1B[?2026l")},80))},400)};return ke.on("resize",a),()=>{l&&clearTimeout(l),i&&clearTimeout(i),ke.off("resize",a)}},[ke,je]);let Be=Math.max(20,(ke.columns??80)-1),Xn=In(()=>Tu(e,s),[e,s,$e]),To=(()=>{let l=ie(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};let i=At(e,(l.docsDir??"user-docs").replace(/\/+$/,"")),a=(()=>{try{return ri(i)&&mi(i).length>0}catch{return!1}})();try{let w=de(e,s),u=Ge(w.name);if(!(!!u&&!(Ne(u)&&!u.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${w.name}")\u2026`,isSetup:!0};if(!w.project.workspaceId&&!a)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return a?{text:Ru,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}})(),mt=kr(R),Kn=mt.length>0&&!_,Ro=p!==null?oi(p,8,Be):null,Lt=me?me.paths.filter(l=>!me.query||l.toLowerCase().includes(me.query.toLowerCase())).slice(0,8):[],Ot=he?he.sessions.filter(l=>{let i=he.query.toLowerCase();return!i||l.name.toLowerCase().includes(i)||l.firstPrompt.toLowerCase().includes(i)}).slice(0,8):[],ce=V((l,i)=>{pe.current?.close(),pe.current=ii({cwd:e,resume:l,profileName:i??s,allowProdWrites:Je.current}),l||(xe.current={uuid:null,firstPrompt:null,titleFired:!1},Qe(null)),st(0),it(0)},[e,s]),Jn=V((l,i,a)=>{if(ju.test(l))try{let w=de(e,s),u=typeof i.project_id=="string"&&i.project_id||w.project.projectId,m=po(i,a),k=[],T=fo(a);l.endsWith("publish_article")&&T&&k.push(`Live: ${T}`),m&&u&&k.push(`Preview: ${uo(w.connection.portalUrl,u,m,w.project.languageCode??"en")}`),k.length>0&&d({kind:"link",lines:k})}catch{}},[e,s,d]),Ae=V(async(l,i)=>{Et(!0),ne(null),X([]);let a=++ze.current;d({kind:"user",text:i?.echoDisplay&&i.display?i.display:l});let w=xe.current;w.firstPrompt||(w.firstPrompt=i?.display??l),Le.current=Date.now(),Ye(0),N(!0),It.current.clear();let u="",m="",k=null,T=()=>{k||(k=setTimeout(()=>{k=null,g(m.length>0?m:null)},60))},A=()=>{k&&clearTimeout(k),k=null,g(null)},M=()=>{if(m.trim()){let D=m.trimEnd();d({kind:"assistant",text:D})}m="",A()};try{for await(let D of pe.current.send(l))if(D.type==="session"){if(!w.uuid){w.uuid=D.sessionId;let L=new Date().toISOString(),P=iu(w.firstPrompt??"session");au({uuid:D.sessionId,name:P,renamed:!1,titled:!1,cwd:e,firstPrompt:w.firstPrompt??"",createdAt:L,updatedAt:L}),Qe(P)}}else if(D.type==="text"){m+=D.delta,u+=D.delta;let L=ni(m);if(L>0){let P=m.slice(0,L).trimEnd();P&&d({kind:"assistant",text:P}),m=m.slice(L)}T(),Ye(P=>P+D.delta.length)}else if(D.type==="tool"){let L=lo(D.name,D.input);L&&(M(),d({kind:"tool",title:L.title,sep:L.sep,arg:L.arg}),It.current.set(D.id,{name:D.name,input:D.input}))}else if(D.type==="article_diff"){let L=qe(D.oldContent,D.newContent,je());L&&(M(),d({kind:"diff",added:L.added,removed:L.removed,lines:L.lines,hidden:L.hidden}))}else if(D.type==="tool_result"){D.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(D.output)&&(Nt.current=!0);let L=It.current.get(D.id);if(L){It.current.delete(D.id),M();let P=co(D.output,4,D.isError?void 0:L.name,L.input);d({kind:"tool-result",lines:P.lines,hidden:P.hidden,isError:D.isError}),D.isError||Jn(L.name,L.input,D.output)}}else if(D.type==="result"){M(),st(P=>P+D.outputTokens),it(P=>P+D.costUsd),d({kind:"done",seconds:Math.round((Date.now()-Le.current)/1e3),tokens:D.outputTokens,costUsd:D.costUsd,ok:D.ok});let L=D.ok?zs(u,yt.map(P=>P.name)):[];if(L.length>0?X(L):D.ok&&u.trim()&&du(l,u,e).then(P=>{P&&ze.current===a&&ne(P)}).catch(()=>{}),w.uuid&&(_n(w.uuid),!w.titleFired)){w.titleFired=!0;let P=w.uuid,j=w.firstPrompt;j&&ai(j,e).then(W=>{W&&(su(P,W),Qe(W))}).catch(()=>{})}}else D.type==="error"&&(M(),D.kind==="auth"&&(Nt.current=!0),d({kind:"note",text:`agent error: ${D.message}`,tone:"error"}))}finally{N(!1),A(),te(D=>D+1),xe.current.uuid&&qs(e,xe.current.uuid,Ke.current),Nt.current&&(Nt.current=!1,Mt.current=i?.display??l,X(["/login"])),_t.current&&(_t.current=!1,Fe.current.length>0&&(d({kind:"note",tone:"info",text:`(${Fe.current.length} queued message(s) discarded)`}),Fe.current=[],Po([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,Jn,je]),jo=V(async l=>{let i=Mn(l),a=$o(e,"standard");Le.current=Date.now(),Ye(0),N(!0);let w=new AbortController;ge.current=w,K({verb:"Publishing",total:i.length,done:0,active:[],rows:Rt(i),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Publishing ${l.length} article${l.length===1?"":"s"} across ${i.length} partition${i.length===1?"":"s"} (\u2264${vt} agents at once) on ${a.model}\u2026 (esc to stop)`});try{for await(let u of yo({cwd:e,partitions:i,promptFor:m=>to(m.paths),concurrency:vt,profileName:s,allowProdWrites:Je.current,model:a.model,signal:w.signal}))if(u.type==="partition_status")u.status==="running"?K(m=>m&&{...m,active:[...m.active,u.label],rows:Pe(m.rows,u.label,"active"),lastAt:Date.now()}):K(m=>m&&{...m,active:m.active.filter(k=>k!==u.label),done:m.done+1,rows:Pe(m.rows,u.label,u.status==="done"?"done":"failed"),lastAt:Date.now()});else if(u.type==="partition_event")K(m=>{if(!m)return m;let k={...m,lastAt:Date.now()};return u.event.type==="tool"?k.tools=m.tools+1:u.event.type==="text"&&(k.chars=m.chars+u.event.delta.length),k});else if(u.type==="run_done"){it(T=>T+u.totalCostUsd),st(T=>T+u.results.reduce((A,M)=>A+M.outputTokens,0));let m=u.aborted?"Stopped. ":"",k=t.kind==="api"?"api":"subscription";d({kind:"note",tone:u.aborted?"warn":u.ok?"ok":"warn",text:m+Yo(u.results,i,k).join(`
141
- `)})}}catch(u){d({kind:"note",tone:"error",text:`Publish run failed: ${u.message}`})}finally{ge.current=null,K(null),N(!1),te(u=>u+1)}},[e,s,d,t]),Ao=V(l=>{let i=Ge(l);if(!i||Ne(i)&&!i.refreshToken)return null;let a={...dt(i.idToken)??{},...dt(i.accessToken)??{}};return a.email??a.preferred_username??"signed in"},[]),Eo=V((l,i)=>{if(i){let a=ie(e);a&&(a.defaultProfile=l,pu(a,e))}c(l),ce(void 0,l),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${i?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Ao(l)||(d({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"])),te(a=>a+1)},[e,d,ce,Ao]),Ut=V((l,i,a)=>{Bt(e,l,i,a.id,a.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${a.name??a.id}" (agent restarted).`}),ce(),li(e,l)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),ne("analyze this repo and propose a docs structure"))},[e,d,ce]),Do=V(async l=>{let i;try{i=await wt(e,l)}catch{d({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),X(["/workspace"]);return}if(i.workspaces.length===0){d({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let a=i.workspaces.filter(u=>(u.workspace_type??"").toLowerCase()!=="apidocumentation"),w=a.length>0?a:i.workspaces;if(w.length===1){let u=w[0],m=i.workspaces.length-w.length;d({kind:"note",tone:"ok",text:`Selected the "${u.name??u.id}" workspace${m>0?" (skipped the API-documentation workspace)":""}.`}),Ut(i.profile,i.projectId,u);return}d({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${w.length} available).`}),X(["/workspace"])},[e,d,Ut]),Qn=V((l,i)=>{hn(e,l,i.id),d({kind:"note",tone:"ok",text:`Project set to "${i.name??i.id}" (agent restarted).`}),ce(),Do(l)},[e,d,ce,Do]),Wt=V(()=>{let l=Te.current[0];if(!l)return;d({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let a of l.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${a}`});l.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let i=qe(l.oldContent,l.newContent,je());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 ${l.path}? (y/n \u2014 anything else cancels)`})},[d,je]),Zn=V(l=>{if(Te.current.length===0)return!1;let i=l.trim().toLowerCase();if(i==="y"||i==="yes"){let a=Te.current.shift();try{fu({cwd:e,profileName:s},a),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${a.path} (sync base advanced).`}),Te.current.length===0&&X(w=>w.length>0?w:["/sync"])}catch(w){d({kind:"note",tone:"error",text:`Pull failed: ${w.message}`})}}else if(i==="n"||i==="no"){let a=Te.current.shift();d({kind:"note",tone:"info",text:`Skipped ${a.path}.`})}else{let a=Te.current.length;return Te.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${a} article(s) left untouched).`}),!0}return Wt(),!0},[e,s,Wt,d]),er=V(l=>{let i=Co.current;if(!i)return!1;if(Co.current=null,ne(null),ze.current++,l.trim()!==i.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:a,failed:w}=Cn(e,i.targets);d({kind:"note",tone:w.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${a.length} item${a.length===1?"":"s"}. The repo is back to its original state.`});for(let u of w)d({kind:"note",tone:"error",text:` \u2717 ${u.path}: ${u.error}`});return d({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
142
- ${ui}`}),X(["/init"]),Et(!1),!0},[e,d]),tr=V(async l=>{let i=l.slice(1).trim().split(/\s+/),a=(i[0]??"").toLowerCase(),w=i.slice(1);switch(Et(!0),a){case"help":d({kind:"note",tone:"info",text:Kt().join(`
143
- `)});return;case"exit":case"quit":pe.current?.close(),r();return;case"clear":ce(),Ke.current=[],Et(!1),ne(null),ze.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let u;try{u=de(e,s)}catch(m){d({kind:"note",tone:"error",text:m.message});return}d({kind:"note",tone:"info",text:`Profile "${u.name}" \u2192 ${u.connection.name} (${u.connection.apiUrl})${u.production?" \u26A0 PRODUCTION":""}`}),Z(!0);try{let m=await Jc(u.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},A=>d({kind:"note",tone:"info",text:A})),k=Qc(u.name,m);Zc(k),Mo(k,u.name,A=>d({kind:"note",tone:"info",text:A})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${u.name}" as ${Gt(k)}`});let T=(()=>{try{return de(e,s)}catch{return u}})();T.project.workspaceId||(T.project.projectId?await Do(T.name):(d({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),X(["/project"]))),Mt.current&&(ne(Mt.current),Mt.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(m){d({kind:"note",tone:"error",text:`Login failed: ${m.message}`})}finally{Z(!1),te(m=>m+1)}return}case"allow-prod":{let u=!1;try{u=de(e,s).production}catch{}if(!u){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Je.current=!0,ce(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let u=dn(w.join(" ")),m=xe.current.uuid;if(!m){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!u){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let k=xe.current.firstPrompt??"";ai(k,e).then(T=>{T?(ne(`/rename ${T}`),d({kind:"note",tone:"info",text:`Suggestion: "${T}" \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}uu(m,u),Qe(u),Tt(`${An} d360-writer \xB7 ${u}`),d({kind:"note",tone:"ok",text:`Session renamed to "${u}".`});return}case"profile":{let u=w[0],m=ie(e);if(!u){let k=Object.entries(m?.profiles??{});if(k.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let T=k.map(([D,L])=>({name:D,env:L.connection?.environment??"custom",prod:L.production===!0,who:Ao(D)})),A=s??m?.defaultProfile,M=Math.max(0,T.findIndex(D=>D.name===A));Ze({cursor:M,current:M,rows:T});return}if(u==="add"){let k=pn(e,w[1],w[2]);if(k){d({kind:"note",tone:"error",text:k});return}d({kind:"note",tone:"ok",text:`\u2713 Profile "${w[1]}" created (environment: ${w[2]??w[1]}).`}),Eo(w[1],!1);return}if(!m?.profiles?.[u]){d({kind:"note",tone:"error",text:`Unknown profile "${u}". Create it: /profile add ${u} <environment>`});return}Eo(u,!0);return}case"doctor":await Pt(w,{cwd:e});return;case"mcp":await Qt(w);return;case"init":{if(ie(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 u=hu().map(m=>({name:m,apiUrl:wu(m).apiUrl}));et({cursor:0,rows:u});return}case"resume":{let u=w.join(" ").trim(),m=cu(e).filter(A=>A.uuid!==xe.current.uuid);if(!u){if(!m.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}He({query:"",cursor:0,sessions:m});return}let k=lu(e,u);if(!k){d({kind:"note",tone:"error",text:`No session matches "${u}".`});return}ce(k.uuid),xe.current={uuid:k.uuid,firstPrompt:k.firstPrompt,titleFired:!0},Qe(k.name),_n(k.uuid),Ke.current=[];let T=jn(e,k.uuid);for(let A of T)d(A);d({kind:"note",tone:"ok",text:T.length?`Resumed "${k.name}" \u2014 restored ${T.length} message(s); continue where you left off.`:`Resumed "${k.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let u=w.join(" ").trim(),m;try{m=await wt(e,s)}catch(T){d({kind:"note",tone:"error",text:`Could not list workspaces: ${T.message}`});return}if(!u){let T=m.workspaces.map(M=>({id:M.id,name:M.name??M.id,type:M.workspace_type}));if(T.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let A=Math.max(0,T.findIndex(M=>M.id===m.current));tt({cursor:A,current:A,rows:T,profile:m.profile,projectId:m.projectId,environment:m.environment});return}let k=No(m.workspaces,u);if(!k){d({kind:"note",tone:"error",text:`No workspace matches "${u}".`});return}Ut(m.profile,m.projectId,k);return}case"project":{let u=w.join(" ").trim(),m;try{m=await gn(e,s)}catch(T){d({kind:"note",tone:"error",text:`Could not list projects: ${T.message} (signed in? try /login)`});return}if(!u){let T=m.projects.map(M=>({id:M.id,name:M.name??M.id,sub:M.sub_domain}));if(T.length===0){d({kind:"note",tone:"info",text:"No projects found for this identity."});return}let A=T.findIndex(M=>M.id===m.current);ot({cursor:Math.max(0,A),current:A,rows:T,profile:m.profile,environment:m.environment});return}let k=hs(m.projects,u);if(!k){d({kind:"note",tone:"error",text:`No project matches "${u}". Available: ${m.projects.map(T=>T.name??T.id).join(", ")}`});return}Qn(m.profile,k);return}case"logout":{let u;try{u=de(e,s).name}catch(k){d({kind:"note",tone:"error",text:k.message});return}let m=nu(u);ru(e,u),ce(),te(k=>k+1),d({kind:"note",tone:m?"ok":"info",text:m?`\u2713 Signed out of "${u}" and cleared its project/workspace selection.`:`Profile "${u}" was not signed in. Cleared any project/workspace selection.`}),d({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),X(["/login"]);return}case"publish":{if(w[0]&&w[0]!=="--all"){await Ae(eo(w[0]),{display:`/publish ${w[0]}`,echoDisplay:!0});return}let u=w[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let m=await Nn({cwd:e,profileName:s}),k=Zt(m.entries);if(k.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let A=m.counts["unknown-base"]??0;A>0&&d({kind:"note",tone:"info",text:`(${A} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(u){await jo(k.map(A=>A.path));return}let T=k.length>1?[{path:"--all",label:`publish all ${k.length} candidates in one run`},...k]:k;nt({cursor:0,rows:T})}catch(m){d({kind:"note",tone:"error",text:`Could not compute sync status: ${m.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let u=w.join(" ").trim();if(!u){let k=[];try{k=Object.keys(li(e,de(e,s).name)?.articles??{})}catch{}if(k.length===0){d({kind:"note",tone:"info",text:"No tracked articles to pick from yet. Usage: /preview <path-to.md | article-id>"});return}We({query:"",cursor:0,paths:k});return}let m=Kc(u)?u:At(e,u);if(ri(m)){try{d({kind:"preview",name:jt(m),text:pi(si(m,"utf8"))})}catch(k){d({kind:"note",tone:"error",text:`Could not read ${m}: ${k.message}`})}return}if(Au.test(u)){try{let k=de(e,s),T={profile:k.name,connection:k.connection},A=k.project.projectId??tu(T),M=await ou(T,A,u);d({kind:"preview",name:M.title??u,text:M.content??"*(article has no content)*"})}catch(k){d({kind:"note",tone:"error",text:`Could not fetch article: ${k.message}`})}return}d({kind:"note",tone:"error",text:`"${u}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let u=w[0]?.trim();if(!u){let A=so(On(e));Ue({cursor:A,current:A});return}let{lines:m,changed:k,effective:T}=Ct(e,u);for(let A of m)d({kind:"note",tone:A.startsWith("\u26A0")?"warn":A.startsWith("\u2713")?"ok":"info",text:A});k&&(Yn(A=>A+1),pe.current?.setModel(T));return}case"convert":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:u,run:m}=Jo(w),k=$u(e,s);if(k.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let T=Qo(k,u);if(T.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${u}". (${k.length} are tracked overall.)`});return}let A=Mn(T),M=3,D=`/convert${u?` --scope ${u}`:""} --run`,L=$o(e,"light");if(!m){let j=ci({files:Zo(e,T),op:"convert",model:L.model}),W=u?`Scope: ${u} (${T.length} of ${k.length} tracked)
144
- `:"",oe=`
145
- Model: ${L.model}${L.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:W+tn(A,j,M).join(`
146
- `)+oe}),X([D]);return}Le.current=Date.now(),Ye(0),N(!0);let P=new AbortController;ge.current=P,K({verb:"Converting",total:A.length,done:0,active:[],rows:Rt(A),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${T.length} articles across ${A.length} partitions (\u2264${M} agents at once) on ${L.model}\u2026 (esc to stop)`});try{for await(let j of yo({cwd:e,partitions:A,promptFor:en,concurrency:M,profileName:s,allowProdWrites:Je.current,model:L.model,signal:P.signal}))if(j.type==="partition_status")j.status==="running"?K(W=>W&&{...W,active:[...W.active,j.label],rows:Pe(W.rows,j.label,"active"),lastAt:Date.now()}):K(W=>W&&{...W,active:W.active.filter(oe=>oe!==j.label),done:W.done+1,rows:Pe(W.rows,j.label,j.status==="done"?"done":"failed"),lastAt:Date.now()});else if(j.type==="partition_event")K(W=>{if(!W)return W;let oe={...W,lastAt:Date.now()};return j.event.type==="tool"?oe.tools=W.tools+1:j.event.type==="text"&&(oe.chars=W.chars+j.event.delta.length),oe});else if(j.type==="run_done"){it(U=>U+j.totalCostUsd),st(U=>U+j.results.reduce((Y,re)=>Y+re.outputTokens,0));let W=j.aborted?"Stopped. ":"",oe=t.kind==="api"?"api":"subscription";d({kind:"note",tone:j.aborted?"warn":j.ok?"ok":"warn",text:W+on(j.results,A,oe).join(`
147
- `)})}}catch(j){d({kind:"note",tone:"error",text:`Convert run failed: ${j.message}`})}finally{ge.current=null,K(null),N(!1),te(j=>j+1)}return}case"write":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=xu(e);if(u.length===0){d({kind:"note",tone:"error",text:"No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first \u2014 try: analyze this repo and propose a docs structure."});return}let{scope:m,path:k,run:T}=rn(w),{docsDir:A,targets:M,reason:D}=un(e,u,{scope:m,path:k});if(M.length===0){d({kind:"note",tone:"warn",text:D??"Nothing to write."});return}let L=Mn(M),P=5,j=$o(e,"standard");if(!T&&!!!k){let U=ci({files:sn(e,u,M),op:"write",model:j.model}),Y=m?`Scope: ${m} (${M.length} pending)
139
+ Press 1 to start.`;function Iu(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",s=Bo(e),c=ie(e),p=(c?.docsDir??"user-docs").replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${p}/ + config`,k={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:Du[s.source],who:null,sessionHint:null,profile:"\u2014",apiUrl:"\u2014",project:"\u2014",cwd:e,prod:!1,loggedOut:!0,configured:c!==null,mode:g};if(c===null)return k;try{let $=de(e,o);k.profile=$.name,k.apiUrl=$.connection.apiUrl,k.prod=$.production,k.project=$.project.projectId??"(chosen at login)";let R=Ge($.name);if(R){let w={...dt(R.idToken)??{},...dt(R.accessToken)??{}},x=w.email??w.preferred_username??"signed in";_e(R)?R.refreshToken&&(k.who=x,k.loggedOut=!1,k.sessionHint="session expired \u2014 refreshing\u2026"):(k.who=x,k.loggedOut=!1,k.sessionHint=`session valid until ${new Date(R.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return k}function _u(e,t){try{let n=de(e,t),o=Ge(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...dt(o.idToken)??{},...dt(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return _e(o)&&!o.refreshToken?{text:`profile "${n.name}" \u2014 session expired (/login)`,prod:n.production}:{text:`${s} \xB7 profile "${n.name}"`,prod:n.production}}catch(n){return{text:n.message.split(".")[0],prod:!1}}}var hi=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],Cn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Nu="Ask me to write or update an article\u2026";function vn({ch:e,dim:t}){let[n,o]=B(!0);return Ne(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),S(C,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var Mu=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function ki(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Lu=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function wi(e){try{return yi(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function Ou(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>wi(Et(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&wi(Et(e,o)))}function Uu({startTime:e,chars:t}){let[n,o]=B(0);Ne(()=>{let g=setInterval(()=>o(k=>k+1),120);return()=>clearInterval(g)},[]);let r=Cn[n%Cn.length],s=hi[Math.floor(n/16)%hi.length],c=Math.floor((Date.now()-e)/1e3),p=Math.round(t/4);return F(J,{children:[S(C,{color:q,children:` ${r} ${s}\u2026 `}),S(C,{color:"gray",children:`(${bt(c)} \xB7 ~${p} tokens \xB7 esc to interrupt)`})]})}var Wu=12e4;function Fu({p:e}){let[t,n]=B(0);Ne(()=>{let _=setInterval(()=>n(H=>H+1),150);return()=>clearInterval(_)},[]);let o=Cn[t%Cn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),p=Date.now()-e.lastAt>=Wu,g=e.verb??"Converting",k=e.unit??"article",$=e.rows.reduce((_,H)=>_+H.items,0),R=g.toLowerCase(),w=e.rows,x=0;if(w.length>wn){let _=w.findIndex(Z=>Z.status==="active"||Z.status==="pending"),H=Math.max(0,Math.min(_-2,w.length-wn));x=w.slice(0,H).filter(Z=>Z.status==="done").length,w=w.slice(H,H+wn)}let v=Math.min(40,Math.max(...w.map(_=>Mo(_.label).length),0)),I=_=>_==="failed"?"red":_==="active"?q:void 0;return F(J,{flexDirection:"column",children:[F(J,{children:[S(C,{color:q,children:` ${o} ${g} ${$} ${k}${$===1?"":"s"} `}),S(C,{color:"gray",children:`\xB7 ${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${bt(r)} \xB7 esc to stop`})]}),x>0&&S(C,{dimColor:!0,children:` \u2714 +${x} done`}),w.map(_=>F(C,{color:I(_.status),dimColor:_.status==="done"||_.status==="pending",children:[` ${Qs[_.status]} ${Mo(_.label).padEnd(v)}`,_.status!=="pending"?` (${_.items} ${k}${_.items===1?"":"s"})`:"",_.status==="active"?` \u2190 ${R}\u2026`:""]},_.label)),p&&S(C,{color:"yellow",children:` \u26A0 no activity for ${bt(s)} \u2014 press esc to stop if it's wedged`})]})}function $i({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=Zc(),[s,c]=B(n),[p,g]=B(null),[k,$]=B({text:"",pos:0}),R=k.text,w=V(l=>{$(a=>{let i=typeof l=="function"?l(a.text):l;return{text:i,pos:i.length}})},[]),x=V(l=>{$(a=>({text:a.text.slice(0,a.pos)+l+a.text.slice(a.pos),pos:a.pos+l.length}))},[]),v=V(()=>{$(l=>{if(l.pos===0)return l;let a=l.text.slice(0,l.pos),i=ri(a)??a.slice(0,-1);return{text:i+l.text.slice(l.pos),pos:i.length}})},[]),[I,_]=B(!1),[H,Z]=B(!1),[$e,te]=B(0),[ke,oe]=B(null),[pt,X]=B([]),ze=Q(0),[Ci,Dt]=B(!1),[Pi,Ye]=B(0),Me=Q(0),[ft,Ve]=B(0),[Xe,It]=B(null),zo=Q(new Map),Si=Q(0),Ke=Q([]),pe=Q(null);pe.current===null&&(pe.current=di({cwd:e,profileName:s,allowProdWrites:!1}));let xe=Q({uuid:null,firstPrompt:null,titleFired:!1}),_t=Q(new Map),Je=Q(!1),Pn=Q([]),Se=Q([]),Sn=Q(null),[Yo,Qe]=B(null),[Le,Oe]=B(null),[be,Ze]=B(null),[Te,et]=B(null),[ae,tt]=B(null),[fe,nt]=B(null),[Ue,ot]=B(null),[Vo,Nt]=B(null),[me,We]=B(null),[le,rt]=B(null),[Xo,Tn]=B([]),Fe=Q([]),Mt=Q(!1),ge=Q(null),[Ko,K]=B(null),Lt=Q(!1),Ot=Q(null),[he,He]=B(null),[Jo,st]=B(0),[Qo,it]=B(0),[Ti,Zo]=B(0),Rn=Oo(()=>{try{return Bo(e).model??"auto"}catch{return null}},[e,Ti]),{stdout:we}=tu(),[,Ri]=B(0),er=Q(`${we.columns??80}x${we.rows??24}`),Re=V(()=>Math.max(20,(we.columns??80)-1),[we]),d=V(l=>{Ke.current.push(l),console.log(Vn(l,Re()))},[Re]);Ne(()=>{let l=`d360-writer \xB7 ${Yo??At(e)}`;if(!I){hn(0),Rt(`${No} ${l}`);return}hn(3);let a=0;Rt(`${kn[0]} ${l}`);let i=setInterval(()=>{a=(a+1)%kn.length,Rt(`${kn[a]} ${l}`)},400);return()=>clearInterval(i)},[I,e,Yo]),Ne(()=>()=>hn(0),[]),Ne(()=>{if(d({kind:"banner",info:Iu(e,o,t,s)}),!ie(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:
140
+ ${gi}`}),X(["/init"]);return}try{let l=de(e,s),a=Ge(l.name);a&&_e(a)&&a.refreshToken?au({profile:l.name,connection:l.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),te(i=>i+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),X(["/login"])}):(!a||_e(a))&&(d({kind:"note",tone:"warn",text:`Profile "${l.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"]))}catch{}try{let l=ie(e),a=de(e,s),i=Ge(a.name);l&&i&&!_e(i)&&Ou(e,l.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."}),X(["/scope"]))}catch{}},[]),Ne(()=>{let l=null,a=null,i=()=>{l&&clearTimeout(l),l=setTimeout(()=>{l=null;let h=`${we.columns??80}x${we.rows??24}`;h!==er.current&&(er.current=h,Ri(u=>u+1),a&&clearTimeout(a),a=setTimeout(()=>{a=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+Hr(Ke.current,Re())+"\x1B[?2026l")},80))},400)};return we.on("resize",i),()=>{l&&clearTimeout(l),a&&clearTimeout(a),we.off("resize",i)}},[we,Re]);let Be=Math.max(20,(we.columns??80)-1),tr=Oo(()=>_u(e,s),[e,s,$e]),jn=(()=>{let l=ie(e);if(!l)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};let a=Et(e,(l.docsDir??"user-docs").replace(/\/+$/,"")),i=(()=>{try{return ci(a)&&yi(a).length>0}catch{return!1}})();try{let h=de(e,s),u=Ge(h.name);if(!(!!u&&!(_e(u)&&!u.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId&&!i)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}return i?{text:Nu,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}})(),mt=Cr(R),nr=mt.length>0&&!I,An=p!==null?ai(p,8,Be):null,Ut=me?me.paths.filter(l=>!me.query||l.toLowerCase().includes(me.query.toLowerCase())).slice(0,8):[],Wt=he?he.sessions.filter(l=>{let a=he.query.toLowerCase();return!a||l.name.toLowerCase().includes(a)||l.firstPrompt.toLowerCase().includes(a)}).slice(0,8):[],ce=V((l,a)=>{pe.current?.close(),pe.current=di({cwd:e,resume:l,profileName:a??s,allowProdWrites:Je.current}),l||(xe.current={uuid:null,firstPrompt:null,titleFired:!1},Qe(null)),st(0),it(0)},[e,s]),or=V((l,a,i)=>{if(Mu.test(l))try{let h=de(e,s),u=typeof a.project_id=="string"&&a.project_id||h.project.projectId,m=mn(a,i),y=[],T=gn(i);l.endsWith("publish_article")&&T&&y.push(`Live: ${T}`),m&&u&&y.push(`Preview: ${fn(h.connection.portalUrl,u,m,h.project.languageCode??"en")}`),y.length>0&&d({kind:"link",lines:y})}catch{}},[e,s,d]),je=V(async(l,a)=>{Dt(!0),oe(null),X([]),Nt(null);let i=++ze.current;d({kind:"user",text:a?.echoDisplay&&a.display?a.display:l});let h=xe.current;h.firstPrompt||(h.firstPrompt=a?.display??l),Me.current=Date.now(),Ye(0),_(!0),_t.current.clear();let u="",m="",y=null,T=()=>{y||(y=setTimeout(()=>{y=null,g(m.length>0?m:null)},60))},N=()=>{y&&clearTimeout(y),y=null,g(null)},M=()=>{if(m.trim()){let D=m.trimEnd();d({kind:"assistant",text:D})}m="",N()};try{for await(let D of pe.current.send(l))if(D.type==="session"){if(!h.uuid){h.uuid=D.sessionId;let L=new Date().toISOString(),P=fu(h.firstPrompt??"session");mu({uuid:D.sessionId,name:P,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:L,updatedAt:L}),Qe(P)}}else if(D.type==="text"){m+=D.delta,u+=D.delta;let L=li(m);if(L>0){let P=m.slice(0,L).trimEnd();P&&d({kind:"assistant",text:P}),m=m.slice(L)}T(),Ye(P=>P+D.delta.length)}else if(D.type==="tool"){let L=dn(D.name,D.input);L&&(M(),d({kind:"tool",title:L.title,sep:L.sep,arg:L.arg}),_t.current.set(D.id,{name:D.name,input:D.input}))}else if(D.type==="article_diff"){let L=qe(D.oldContent,D.newContent,Re());L&&(M(),d({kind:"diff",added:L.added,removed:L.removed,lines:L.lines,hidden:L.hidden}))}else if(D.type==="tool_result"){D.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(D.output)&&(Lt.current=!0);let L=_t.current.get(D.id);if(L){_t.current.delete(D.id),M();let P=pn(D.output,4,D.isError?void 0:L.name,L.input);d({kind:"tool-result",lines:P.lines,hidden:P.hidden,isError:D.isError}),D.isError||or(L.name,L.input,D.output)}}else if(D.type==="result"){M(),st(P=>P+D.outputTokens),it(P=>P+D.costUsd),d({kind:"done",seconds:Math.round((Date.now()-Me.current)/1e3),tokens:D.outputTokens,costUsd:D.costUsd,ok:D.ok});let L=D.ok?Js(u,yt.map(P=>P.name)):[];if(L.length>0?X(L):D.ok&&u.trim()&&wu(l,u,e).then(P=>{P&&ze.current===i&&oe(P)}).catch(()=>{}),h.uuid&&(Uo(h.uuid),!h.titleFired)){h.titleFired=!0;let P=h.uuid,j=h.firstPrompt;j&&pi(j,e).then(W=>{W&&(pu(P,W),Qe(W))}).catch(()=>{})}}else D.type==="error"&&(M(),D.kind==="auth"&&(Lt.current=!0),d({kind:"note",text:`agent error: ${D.message}`,tone:"error"}))}finally{_(!1),N(),te(D=>D+1),xe.current.uuid&&Xs(e,xe.current.uuid,Ke.current),Lt.current&&(Lt.current=!1,Ot.current=a?.display??l,X(["/login"])),Mt.current&&(Mt.current=!1,Fe.current.length>0&&(d({kind:"note",tone:"info",text:`(${Fe.current.length} queued message(s) discarded)`}),Fe.current=[],Tn([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,or,Re]),Ft=V(async l=>{let a=Fo(l),i=bn(e,"standard");Me.current=Date.now(),Ye(0),_(!0);let h=new AbortController;ge.current=h,K({verb:"Publishing",total:a.length,done:0,active:[],rows:jt(a),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Publishing ${l.length} article${l.length===1?"":"s"} across ${a.length} partition${a.length===1?"":"s"} (\u2264${vt} agents at once) on ${i.model}\u2026 (esc to stop)`});try{for await(let u of xn({cwd:e,partitions:a,promptFor:m=>nn(m.paths),concurrency:vt,profileName:s,allowProdWrites:Je.current,model:i.model,signal:h.signal}))if(u.type==="partition_status")u.status==="running"?K(m=>m&&{...m,active:[...m.active,u.label],rows:Ce(m.rows,u.label,"active"),lastAt:Date.now()}):K(m=>m&&{...m,active:m.active.filter(y=>y!==u.label),done:m.done+1,rows:Ce(m.rows,u.label,u.status==="done"?"done":"failed"),lastAt:Date.now()});else if(u.type==="partition_event")K(m=>{if(!m)return m;let y={...m,lastAt:Date.now()};return u.event.type==="tool"?y.tools=m.tools+1:u.event.type==="text"&&(y.chars=m.chars+u.event.delta.length),y});else if(u.type==="run_done"){it(T=>T+u.totalCostUsd),st(T=>T+u.results.reduce((N,M)=>N+M.outputTokens,0));let m=u.aborted?"Stopped. ":"",y=t.kind==="api"?"api":"subscription";d({kind:"note",tone:u.aborted?"warn":u.ok?"ok":"warn",text:m+Qn(u.results,a,y).join(`
141
+ `)})}}catch(u){d({kind:"note",tone:"error",text:`Publish run failed: ${u.message}`})}finally{ge.current=null,K(null),_(!1),te(u=>u+1)}},[e,s,d,t]),En=V(async l=>{await je(Jn(l),{display:`/publish ${l}`,echoDisplay:!0});try{let a=await on(e,s,l.replace(/\\/g,"/"));a.length>0&&(Nt(`Related articles not yet published (${a.length}):`),X([`/publish --related ${l}`,...a.map(i=>`/publish ${i}`)]))}catch{}},[e,s,je]),Dn=V(l=>{let a=Ge(l);if(!a||_e(a)&&!a.refreshToken)return null;let i={...dt(a.idToken)??{},...dt(a.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),In=V((l,a)=>{if(a){let i=ie(e);i&&(i.defaultProfile=l,yu(i,e))}c(l),ce(void 0,l),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${l}"${a?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),Dn(l)||(d({kind:"note",tone:"warn",text:`Profile "${l}" is not signed in \u2014 do you want to log in now? (press 1)`}),X(["/login"])),te(i=>i+1)},[e,d,ce,Dn]),Ht=V((l,a,i)=>{zt(e,l,a,i.id,i.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),ce(),fi(e,l)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),oe("analyze this repo and propose a docs structure"))},[e,d,ce]),_n=V(async l=>{let a;try{a=await kt(e,l)}catch{d({kind:"note",tone:"info",text:"Next: pick a workspace \u2014 run /workspace."}),X(["/workspace"]);return}if(a.workspaces.length===0){d({kind:"note",tone:"warn",text:"This project has no workspaces yet. Create one in Document360, then run /workspace."});return}let i=a.workspaces.filter(u=>(u.workspace_type??"").toLowerCase()!=="apidocumentation"),h=i.length>0?i:a.workspaces;if(h.length===1){let u=h[0],m=a.workspaces.length-h.length;d({kind:"note",tone:"ok",text:`Selected the "${u.name??u.id}" workspace${m>0?" (skipped the API-documentation workspace)":""}.`}),Ht(a.profile,a.projectId,u);return}d({kind:"note",tone:"info",text:`Next: pick the workspace your articles publish to (${h.length} available).`}),X(["/workspace"])},[e,d,Ht]),rr=V((l,a)=>{xo(e,l,a.id),d({kind:"note",tone:"ok",text:`Project set to "${a.name??a.id}" (agent restarted).`}),ce(),_n(l)},[e,d,ce,_n]),Bt=V(()=>{let l=Se.current[0];if(!l)return;d({kind:"note",tone:"info",text:`\u25CF ${l.title} (${l.path})`});for(let i of l.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${i}`});l.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let a=qe(l.oldContent,l.newContent,Re());d(a?{kind:"diff",added:a.added,removed:a.removed,lines:a.lines,hidden:a.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${l.path}? (y/n \u2014 anything else cancels)`})},[d,Re]),sr=V(l=>{if(Se.current.length===0)return!1;let a=l.trim().toLowerCase();if(a==="y"||a==="yes"){let i=Se.current.shift();try{$u({cwd:e,profileName:s},i),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),Se.current.length===0&&X(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(a==="n"||a==="no"){let i=Se.current.shift();d({kind:"note",tone:"info",text:`Skipped ${i.path}.`})}else{let i=Se.current.length;return Se.current=[],d({kind:"note",tone:"info",text:`Pull cancelled (${i} article(s) left untouched).`}),!0}return Bt(),!0},[e,s,Bt,d]),ir=V(l=>{let a=Sn.current;if(!a)return!1;if(Sn.current=null,oe(null),ze.current++,l.trim()!==a.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:h}=jo(e,a.targets);d({kind:"note",tone:h.length?"warn":"ok",text:`\u2713 Reset complete \u2014 removed ${i.length} item${i.length===1?"":"s"}. The repo is back to its original state.`});for(let u of h)d({kind:"note",tone:"error",text:` \u2717 ${u.path}: ${u.error}`});return d({kind:"note",tone:"info",text:`Clean slate \u2014 set up d360-writer again whenever you're ready:
142
+ ${gi}`}),X(["/init"]),Dt(!1),!0},[e,d]),ar=V(async l=>{let a=l.slice(1).trim().split(/\s+/),i=(a[0]??"").toLowerCase(),h=a.slice(1);switch(Dt(!0),i){case"help":d({kind:"note",tone:"info",text:Zt().join(`
143
+ `)});return;case"exit":case"quit":pe.current?.close(),r();return;case"clear":ce(),Ke.current=[],Dt(!1),oe(null),ze.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let u;try{u=de(e,s)}catch(m){d({kind:"note",tone:"error",text:m.message});return}d({kind:"note",tone:"info",text:`Profile "${u.name}" \u2192 ${u.connection.name} (${u.connection.apiUrl})${u.production?" \u26A0 PRODUCTION":""}`}),Z(!0);try{let m=await ru(u.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},N=>d({kind:"note",tone:"info",text:N})),y=su(u.name,m);iu(y),On(y,u.name,N=>d({kind:"note",tone:"info",text:N})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${u.name}" as ${Vt(y)}`});let T=(()=>{try{return de(e,s)}catch{return u}})();T.project.workspaceId||(T.project.projectId?await _n(T.name):(d({kind:"note",tone:"info",text:"Next: pick the Document360 project."}),X(["/project"]))),Ot.current&&(oe(Ot.current),Ot.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(m){d({kind:"note",tone:"error",text:`Login failed: ${m.message}`})}finally{Z(!1),te(m=>m+1)}return}case"allow-prod":{let u=!1;try{u=de(e,s).production}catch{}if(!u){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}Je.current=!0,ce(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let u=ho(h.join(" ")),m=xe.current.uuid;if(!m){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!u){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=xe.current.firstPrompt??"";pi(y,e).then(T=>{T?(oe(`/rename ${T}`),d({kind:"note",tone:"info",text:`Suggestion: "${T}" \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}ku(m,u),Qe(u),Rt(`${No} d360-writer \xB7 ${u}`),d({kind:"note",tone:"ok",text:`Session renamed to "${u}".`});return}case"profile":{let u=h[0],m=ie(e);if(!u){let y=Object.entries(m?.profiles??{});if(y.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let T=y.map(([D,L])=>({name:D,env:L.connection?.environment??"custom",prod:L.production===!0,who:Dn(D)})),N=s??m?.defaultProfile,M=Math.max(0,T.findIndex(D=>D.name===N));Ze({cursor:M,current:M,rows:T});return}if(u==="add"){let y=ko(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]}).`}),In(h[1],!1);return}if(!m?.profiles?.[u]){d({kind:"note",tone:"error",text:`Unknown profile "${u}". Create it: /profile add ${u} <environment>`});return}In(u,!0);return}case"doctor":await St(h,{cwd:e});return;case"mcp":await tn(h);return;case"init":{if(ie(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 u=vu().map(m=>({name:m,apiUrl:Cu(m).apiUrl}));et({cursor:0,rows:u});return}case"resume":{let u=h.join(" ").trim(),m=hu(e).filter(N=>N.uuid!==xe.current.uuid);if(!u){if(!m.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}He({query:"",cursor:0,sessions:m});return}let y=gu(e,u);if(!y){d({kind:"note",tone:"error",text:`No session matches "${u}".`});return}ce(y.uuid),xe.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Qe(y.name),Uo(y.uuid),Ke.current=[];let T=_o(e,y.uuid);for(let N of T)d(N);d({kind:"note",tone:"ok",text:T.length?`Resumed "${y.name}" \u2014 restored ${T.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let u=h.join(" ").trim(),m;try{m=await kt(e,s)}catch(T){d({kind:"note",tone:"error",text:`Could not list workspaces: ${T.message}`});return}if(!u){let T=m.workspaces.map(M=>({id:M.id,name:M.name??M.id,type:M.workspace_type}));if(T.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let N=Math.max(0,T.findIndex(M=>M.id===m.current));tt({cursor:N,current:N,rows:T,profile:m.profile,projectId:m.projectId,environment:m.environment});return}let y=Ln(m.workspaces,u);if(!y){d({kind:"note",tone:"error",text:`No workspace matches "${u}".`});return}Ht(m.profile,m.projectId,y);return}case"project":{let u=h.join(" ").trim(),m;try{m=await $o(e,s)}catch(T){d({kind:"note",tone:"error",text:`Could not list projects: ${T.message} (signed in? try /login)`});return}if(!u){let T=m.projects.map(M=>({id:M.id,name:M.name??M.id,sub:M.sub_domain}));if(T.length===0){d({kind:"note",tone:"info",text:"No projects found for this identity."});return}let N=T.findIndex(M=>M.id===m.current);nt({cursor:Math.max(0,N),current:N,rows:T,profile:m.profile,environment:m.environment});return}let y=xs(m.projects,u);if(!y){d({kind:"note",tone:"error",text:`No project matches "${u}". Available: ${m.projects.map(T=>T.name??T.id).join(", ")}`});return}rr(m.profile,y);return}case"logout":{let u;try{u=de(e,s).name}catch(y){d({kind:"note",tone:"error",text:y.message});return}let m=uu(u);du(e,u),ce(),te(y=>y+1),d({kind:"note",tone:m?"ok":"info",text:m?`\u2713 Signed out of "${u}" and cleared its project/workspace selection.`:`Profile "${u}" was not signed in. Cleared any project/workspace selection.`}),d({kind:"note",tone:"info",text:"Run /login to sign in and pick a project."}),X(["/login"]);return}case"publish":{if(h[0]==="--related"&&h[1]){let m=await on(e,s,h[1].replace(/\\/g,"/"));if(m.length===0){d({kind:"note",tone:"ok",text:"\u2713 All related articles are already published."});return}await Ft(m);return}if(h[0]&&h[0]!=="--all"){await En(h[0]);return}let u=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let m=await Wo({cwd:e,profileName:s}),y=Ct(m.entries);if(y.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let T=m.counts["unknown-base"]??0;T>0&&d({kind:"note",tone:"info",text:`(${T} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(u){await Ft(y.map(T=>T.path));return}ot({cursor:0,rows:qr(y)})}catch(m){d({kind:"note",tone:"error",text:`Could not compute sync status: ${m.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let u=h.join(" ").trim();if(!u){let y=[];try{y=Object.keys(fi(e,de(e,s).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}We({query:"",cursor:0,paths:y});return}let m=ou(u)?u:Et(e,u);if(ci(m)){try{d({kind:"preview",name:At(m),text:ki(ui(m,"utf8"))})}catch(y){d({kind:"note",tone:"error",text:`Could not read ${m}: ${y.message}`})}return}if(Lu.test(u)){try{let y=de(e,s),T={profile:y.name,connection:y.connection},N=y.project.projectId??lu(T),M=await cu(T,N,u);d({kind:"preview",name:M.title??u,text:M.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:`"${u}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let u=h[0]?.trim();if(!u){let N=ln(Bo(e));Oe({cursor:N,current:N});return}let{lines:m,changed:y,effective:T}=Pt(e,u);for(let N of m)d({kind:"note",tone:N.startsWith("\u26A0")?"warn":N.startsWith("\u2713")?"ok":"info",text:N});y&&(Zo(N=>N+1),pe.current?.setModel(T));return}case"convert":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:u,run:m}=no(h),y=Tu(e,s);if(y.length===0){d({kind:"note",tone:"error",text:"No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert."});return}let T=oo(y,u);if(T.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${u}". (${y.length} are tracked overall.)`});return}let N=Fo(T),M=3,D=`/convert${u?` --scope ${u}`:""} --run`,L=bn(e,"light");if(!m){let j=mi({files:ro(e,T),op:"convert",model:L.model}),W=u?`Scope: ${u} (${T.length} of ${y.length} tracked)
144
+ `:"",ne=`
145
+ Model: ${L.model}${L.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:W+io(N,j,M).join(`
146
+ `)+ne}),X([D]);return}Me.current=Date.now(),Ye(0),_(!0);let P=new AbortController;ge.current=P,K({verb:"Converting",total:N.length,done:0,active:[],rows:jt(N),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${T.length} articles across ${N.length} partitions (\u2264${M} agents at once) on ${L.model}\u2026 (esc to stop)`});try{for await(let j of xn({cwd:e,partitions:N,promptFor:so,concurrency:M,profileName:s,allowProdWrites:Je.current,model:L.model,signal:P.signal}))if(j.type==="partition_status")j.status==="running"?K(W=>W&&{...W,active:[...W.active,j.label],rows:Ce(W.rows,j.label,"active"),lastAt:Date.now()}):K(W=>W&&{...W,active:W.active.filter(ne=>ne!==j.label),done:W.done+1,rows:Ce(W.rows,j.label,j.status==="done"?"done":"failed"),lastAt:Date.now()});else if(j.type==="partition_event")K(W=>{if(!W)return W;let ne={...W,lastAt:Date.now()};return j.event.type==="tool"?ne.tools=W.tools+1:j.event.type==="text"&&(ne.chars=W.chars+j.event.delta.length),ne});else if(j.type==="run_done"){it(U=>U+j.totalCostUsd),st(U=>U+j.results.reduce((Y,re)=>Y+re.outputTokens,0));let W=j.aborted?"Stopped. ":"",ne=t.kind==="api"?"api":"subscription";d({kind:"note",tone:j.aborted?"warn":j.ok?"ok":"warn",text:W+ao(j.results,N,ne).join(`
147
+ `)})}}catch(j){d({kind:"note",tone:"error",text:`Convert run failed: ${j.message}`})}finally{ge.current=null,K(null),_(!1),te(j=>j+1)}return}case"write":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=Ru(e);if(u.length===0){d({kind:"note",tone:"error",text:"No docs plan found (.d360-writer/plan.json). Analyze the repo and propose a structure first \u2014 try: analyze this repo and propose a docs structure."});return}let{scope:m,path:y,run:T}=co(h),{docsDir:N,targets:M,reason:D}=go(e,u,{scope:m,path:y});if(M.length===0){d({kind:"note",tone:"warn",text:D??"Nothing to write."});return}let L=Fo(M),P=5,j=bn(e,"standard");if(!T&&!!!y){let U=mi({files:uo(e,u,M),op:"write",model:j.model}),Y=m?`Scope: ${m} (${M.length} pending)
148
148
  `:"",re=`
149
- Model: ${j.model}${j.forced?" (forced)":" \u2014 authoring; /model to override"}`;d({kind:"note",tone:"info",text:Y+ln(L,U,P).join(`
150
- `)+re}),X([`/write${m?` --scope ${m}`:" --all"} --run`]);return}Le.current=Date.now(),Ye(0),N(!0);let oe=new AbortController;ge.current=oe,K({verb:"Writing",total:L.length,done:0,active:[],rows:Rt(L),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${M.length} article${M.length===1?"":"s"} across ${L.length} partition${L.length===1?"":"s"} (\u2264${P} agents at once) on ${j.model}${j.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let U of yo({cwd:e,partitions:L,promptFor:Y=>an(Y,A),concurrency:P,profileName:s,allowProdWrites:Je.current,model:j.model,signal:oe.signal}))if(U.type==="partition_status")U.status==="running"?K(Y=>Y&&{...Y,active:[...Y.active,U.label],rows:Pe(Y.rows,U.label,"active"),lastAt:Date.now()}):K(Y=>Y&&{...Y,active:Y.active.filter(re=>re!==U.label),done:Y.done+1,rows:Pe(Y.rows,U.label,U.status==="done"?"done":"failed"),lastAt:Date.now()});else if(U.type==="partition_event")K(Y=>{if(!Y)return Y;let re={...Y,lastAt:Date.now()};return U.event.type==="tool"?re.tools=Y.tools+1:U.event.type==="text"&&(re.chars=Y.chars+U.event.delta.length),re});else if(U.type==="run_done"){it(_o=>_o+U.totalCostUsd),st(_o=>_o+U.results.reduce((Si,Ti)=>Si+Ti.outputTokens,0));let Y=U.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";d({kind:"note",tone:U.aborted?"warn":U.ok?"ok":"warn",text:Y+cn(U.results,L,re).join(`
151
- `)}),!U.aborted&&U.ok&&X(["/publish --all","/screenshot --all"])}}catch(U){d({kind:"note",tone:"error",text:`Write run failed: ${U.message}`})}finally{ge.current=null,K(null),N(!1),te(U=>U+1)}return}case"sync":{let u=(w[0]??"status").toLowerCase();try{if(u==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let m=await Nn({cwd:e,profileName:s});d({kind:"note",tone:"info",text:ro(m).join(`
152
- `)});return}if(u==="pull"){let m=w[1];if(!m){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let k;if(m==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),k=(await Nn({cwd:e,profileName:s})).entries.filter(M=>M.status==="remote-ahead"&&M.path).map(M=>M.path),k.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 k=[m.replace(/\\/g,"/")];let T=[];for(let A of k)T.push(await mu({cwd:e,profileName:s,relPath:A}));Te.current=T,Wt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${u} \u2014 use /sync or /sync pull <path>|--all.`})}catch(m){d({kind:"note",tone:"error",text:`Sync failed: ${m.message}`})}return}case"scope":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=gu(e);if(u.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}rt({cursor:0,rows:u.map(m=>({...m,checked:m.recommended}))});return}case"audit":case"capture-setup":{let m=await(a==="audit"?oo:St)(w,void 0);if(m.kind==="forward-to-agent"&&m.prompt&&await Ae(m.prompt,{display:m.display,echoDisplay:!0}),a==="capture-setup"){let k=$n(e);k&&d({kind:"note",tone:"info",text:k.join(`
153
- `)})}return}case"devhints":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}bu(vu(e)),Xc(Cu(e),xn()),d({kind:"note",tone:"info",text:bn().join(`
154
- `)});return}case"screenshot":{let u=wn(w);if(u.mode==="list"){if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}d({kind:"note",tone:"info",text:yn(kn(e,u.scope),u.scope).join(`
155
- `)});return}if(u.mode==="single"){let P=await ao(w);P.kind==="forward-to-agent"&&P.prompt&&await Ae(P.prompt,{display:P.display,echoDisplay:!0});return}if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let m=u.mode==="scope"?u.scope:void 0,k=yu(e,{scope:m}).map(P=>P.id);if(k.length===0){d({kind:"note",tone:"info",text:m?`No screenshot placeholders under ${m}.`:"No screenshot placeholders found in the docs."});return}let T=6,A=ku(k,3),M=$o(e,"standard");Le.current=Date.now(),Ye(0),N(!0);let D=new AbortController;ge.current=D,K({verb:"Authoring",unit:"spec",total:A.length,done:0,active:[],rows:Rt(A),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Authoring ${k.length} screenshot spec${k.length===1?"":"s"} across ${A.length} partition${A.length===1?"":"s"} (\u2264${T} agents at once) on ${M.model}${M.forced?" (forced)":""}\u2026 (esc to stop)`});let L=!1;try{for await(let P of yo({cwd:e,partitions:A,promptFor:Cs,concurrency:T,profileName:s,allowProdWrites:Je.current,model:M.model,signal:D.signal}))if(P.type==="partition_status")P.status==="running"?K(j=>j&&{...j,active:[...j.active,P.label],rows:Pe(j.rows,P.label,"active"),lastAt:Date.now()}):K(j=>j&&{...j,active:j.active.filter(W=>W!==P.label),done:j.done+1,rows:Pe(j.rows,P.label,P.status==="done"?"done":"failed"),lastAt:Date.now()});else if(P.type==="partition_event")K(j=>{if(!j)return j;let W={...j,lastAt:Date.now()};return P.event.type==="tool"?W.tools=j.tools+1:P.event.type==="text"&&(W.chars=j.chars+P.event.delta.length),W});else if(P.type==="run_done"){it(U=>U+P.totalCostUsd),st(U=>U+P.results.reduce((Y,re)=>Y+re.outputTokens,0));let j=P.results.filter(U=>U.ok).length,W=P.aborted?"Stopped. ":"";L=!P.aborted&&u.setup;let oe=P.aborted?"":L?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:P.aborted?"warn":P.ok?"ok":"warn",text:`${W}Authored specs \u2014 ${j}/${P.results.length} batches ok.${oe}`})}}catch(P){d({kind:"note",tone:"error",text:`Spec authoring failed: ${P.message}`})}finally{ge.current=null,K(null),N(!1),te(P=>P+1)}if(L){let P=await St();P.kind==="forward-to-agent"&&P.prompt&&await Ae(P.prompt,{display:P.display,echoDisplay:!0});let j=$n(e);j&&d({kind:"note",tone:"info",text:j.join(`
156
- `)})}return}case"reset":{let u=vn(e);if(u.length===0){d({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}Co.current={repoName:jt(e),targets:u},d({kind:"note",tone:"warn",text:Pn(e,u).join(`
157
- `)}),ne(jt(e));return}default:d({kind:"note",tone:"error",text:`Unknown command: /${a} \u2014 type /help.`})}},[e,r,s,Wt,d,ce,Ae,jo]),Ft=V(l=>{let i=(l??R).trim();if(y(""),Dt(null),Ve(0),i.startsWith("/")&&(ne(null),ze.current++,X(w=>Vs(w,i))),!i||er(i)||Zn(i))return;vo.current.push(i);let a=Qs(i,Fn.current);i.startsWith("/")?tr(a):Ae(a,{display:i})},[R,er,Zn,tr,Ae]),or=V(l=>{if(l.length>1){if(l.includes("\x1B"))return;let i=Xs(l);if(Ks(i)){let a=Js(++xi.current,i);Fn.current.set(a,i),x(a)}else x(i);return}x(l)},[x]),at=Math.max(10,Be-6),nr=In(()=>wo(h.text,at),[h.text,at]),Io=V(l=>$(i=>({...i,pos:Math.max(0,Math.min(i.text.length,i.pos+l))})),[]),Ht=V(l=>$(i=>({...i,pos:ei(wo(i.text,at),i.pos,l)})),[at]),gt=V(l=>$(i=>({...i,pos:ti(wo(i.text,at),i.pos,l)})),[at]),Ci=["\x1B[H","\x1B[1~","\x1BOH"],Pi=["\x1B[F","\x1B[4~","\x1BOF"],rr=V((l,i)=>i.leftArrow?(Io(-1),!0):i.rightArrow?(Io(1),!0):l&&Ci.includes(l)?(gt("start"),!0):l&&Pi.includes(l)?(gt("end"),!0):i.ctrl&&l==="a"?(gt("start"),!0):i.ctrl&&l==="e"?(gt("end"),!0):!1,[Io,gt]);return Yc((l,i)=>{if(i.ctrl&&l==="c"){pe.current?.close(),r();return}if(!H){if(_){if(i.escape){if(ge.current){ge.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),ge.current.abort());return}_t.current||(_t.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),pe.current?.interrupt());return}if(i.return){let a=R.trim();if(!a)return;Fe.current.push(a),Po([...Fe.current]),y("");return}if(rr(l,i))return;if(i.upArrow){Ht(-1);return}if(i.downArrow){Ht(1);return}if(i.backspace||i.delete){v();return}l&&!i.ctrl&&!i.meta&&or(l);return}if(Oe){if(i.upArrow){Ue(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Ue(a=>a&&{...a,cursor:Math.min(ye.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ye.length){Ue(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ye[Oe.cursor];Ue(null);let{lines:w,changed:u,effective:m}=Ct(e,a.value??"default");for(let k of w)d({kind:"note",tone:k.startsWith("\u26A0")?"warn":k.startsWith("\u2713")?"ok":"info",text:k});u&&(Yn(k=>k+1),pe.current?.setModel(m));return}if(l==="s"){let a=ye[Oe.cursor];Ue(null),pe.current?.setModel(a.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${a.label} for this session only (your saved default is unchanged).`});return}if(i.escape){Ue(null);return}return}if(be){if(i.upArrow){Ze(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){Ze(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=be.rows.length){Ze(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return||l==="s"){let a=be.rows[be.cursor];Ze(null),Eo(a.name,i.return===!0);return}if(i.escape){Ze(null);return}return}if(Re){if(i.upArrow){et(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){et(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Re.rows.length){et(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=Re.rows[Re.cursor].name;if(et(null),!Cr(e,a).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 "${a}").`});let u=!1;try{let m=Ge(a);u=!!m&&!(Ne(m)&&!m.refreshToken)}catch{}u?(d({kind:"note",tone:"info",text:`Already signed in to ${a} \u2014 next: pick a workspace.`}),X(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${a}).`}),X(["/login"])),te(m=>m+1);return}if(i.escape){et(null);return}return}if(ae){if(i.upArrow){tt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){tt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ae.rows.length){tt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ae.rows[ae.cursor],{profile:w,projectId:u}=ae;tt(null),Ut(w,u,a);return}if(i.escape){tt(null);return}return}if(fe){if(i.upArrow){ot(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){ot(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=fe.rows.length){ot(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=fe.rows[fe.cursor],{profile:w}=fe;ot(null),Qn(w,a);return}if(i.escape){ot(null);return}return}if(le){if(i.upArrow){rt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){rt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l===" "){rt(a=>a&&{...a,rows:a.rows.map((w,u)=>u===a.cursor?{...w,checked:!w.checked}:w)});return}if(i.return){let a=le.rows.filter(w=>w.checked).map(w=>w.path);if(rt(null),a.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}Vo(e,a),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${a.length} folder(s) \u2014 written to .d360-writer.json`});for(let w of a)d({kind:"note",tone:"info",text:` ${w}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(i.escape){rt(null);return}return}if(ve){if(i.upArrow){nt(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){nt(a=>a&&{...a,cursor:Math.min(a.rows.length-1,a.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ve.rows.length){nt(a=>a&&{...a,cursor:Number(l)-1});return}if(i.return){let a=ve.rows[ve.cursor],w=ve.rows.filter(u=>u.path!=="--all").map(u=>u.path);nt(null),a.path==="--all"?jo(w):Ae(eo(a.path),{display:`/publish ${a.path}`,echoDisplay:!0});return}if(i.escape){nt(null);return}return}if(me){if(i.escape){We(null);return}if(i.upArrow){We(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){We(a=>a&&{...a,cursor:Math.min(Math.max(0,Lt.length-1),a.cursor+1)});return}if(i.return){let a=Lt[me.cursor];if(a){We(null);try{d({kind:"preview",name:jt(a),text:pi(si(At(e,a),"utf8"))})}catch(w){d({kind:"note",tone:"error",text:`Could not read ${a}: ${w.message}`})}}return}if(i.backspace||i.delete){We(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){We(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(he){if(i.escape){He(null);return}if(i.upArrow){He(a=>a&&{...a,cursor:Math.max(0,a.cursor-1)});return}if(i.downArrow){He(a=>a&&{...a,cursor:Math.min(Math.max(0,Ot.length-1),a.cursor+1)});return}if(i.return){let a=Ot[he.cursor];if(a){He(null),ce(a.uuid),xe.current={uuid:a.uuid,firstPrompt:a.firstPrompt,titleFired:!0},Qe(a.name),_n(a.uuid),Ke.current=[];let w=jn(e,a.uuid);for(let u of w)d(u);d({kind:"note",tone:"ok",text:w.length?`Resumed "${a.name}" \u2014 restored ${w.length} message(s); continue where you left off.`:`Resumed "${a.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(i.backspace||i.delete){He(a=>a&&{...a,query:a.query.slice(0,-1),cursor:0});return}if(l&&!i.ctrl&&!i.meta&&l.length===1){He(a=>a&&{...a,query:a.query+l,cursor:0});return}return}if(i.tab&&!R&&we){y(we),ne(null),ze.current++;return}if(!R&&pt.length>0&&l&&/^[1-9]$/.test(l)){let a=pt[Number(l)-1];if(a){y(a);return}}if(!rr(l,i)){if(Kn){if(i.upArrow){Ve(a=>Math.max(0,a-1));return}if(i.downArrow){Ve(a=>Math.min(mt.length-1,a+1));return}if(i.tab){y("/"+(mt[ft]?.name??"")+" "),Ve(0);return}if(i.return){let a=mt[ft];if(a){let w=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(yr(a.usage)&&!w){y("/"+a.name+" "),Ve(0);return}Ft("/"+a.name+(w?" "+w:""));return}}}else{if(i.upArrow){if(R!==""&&Xe===null){Ht(-1);return}let a=vo.current;if(!a.length)return;let w=Xe===null?a.length-1:Math.max(0,Xe-1);Dt(w),y(a[w]??"");return}if(i.downArrow){if(R!==""&&Xe===null){Ht(1);return}let a=vo.current;if(Xe===null)return;let w=Xe+1;w>=a.length?(Dt(null),y("")):(Dt(w),y(a[w]??""));return}}if(i.return){Ft();return}if(i.backspace||i.delete){v();return}if(i.escape){y(""),Ve(0),ne(null),X([]);return}l&&!i.ctrl&&!i.meta&&or(l)}}}),Me(()=>{if(_||H)return;let l=Fe.current.shift();l!==void 0&&(Po([...Fe.current]),Ft(l))},[_,H,Ft]),F(J,{flexDirection:"column",width:Be,children:[Ro!==null&&F(J,{marginTop:1,flexDirection:"column",children:[Ro.truncated&&S(C,{dimColor:!0,children:"\u2026"}),S(C,{children:Ro.text})]}),_&&(qn?S(_u,{p:qn}):S(Du,{startTime:Le.current,chars:$i})),S(J,{borderStyle:"round",borderColor:Xn.prod?"yellow":G,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?nr.map((l,i)=>{let a=h.text.slice(l.start,l.end),w=i===ko(nr,h.pos),u=Math.min(h.pos,l.end)-l.start;return F(C,{children:[S(C,{color:G,children:i===0?"> ":" "}),w?F(Ln,{children:[a.slice(0,u),S(xo,{ch:a[u]??" "}),a.slice(u+1)]}):a||" "]},`${i}-${l.start}`)}):F(C,{children:[S(C,{color:G,children:"> "}),we&&!_?F(Ln,{children:[S(xo,{ch:we[0],dim:!0}),S(C,{color:"gray",children:we.slice(1)}),S(C,{dimColor:!0,children:" (tab)"})]}):To.isSetup||!yi?F(Ln,{children:[S(xo,{ch:To.text[0],dim:!0}),S(C,{color:"gray",children:To.text.slice(1)})]}):S(xo,{ch:" "})]})}),Bn.length>0&&S(J,{flexDirection:"column",paddingX:1,children:Bn.map((l,i)=>S(C,{color:"gray",children:`\u29D7 queued: ${l}`},`${i}-${l.slice(0,24)}`))}),Oe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Select model"}),S(C,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ye.map((l,i)=>F(C,{color:i===Oe.cursor?G:void 0,children:[i===Oe.cursor?"\u276F ":" ",`${i+1}. ${l.label}${i===Oe.current?" \u2714":""}`.padEnd(16),S(C,{color:"gray",children:l.desc})]},l.label)),S(C,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):be?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Switch connection profile"}),be.rows.map((l,i)=>F(C,{color:i===be.cursor?G:void 0,children:[i===be.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===be.current?" \u2714":""}`.padEnd(20),S(C,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?S(C,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),S(C,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Re?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Pick your Document360 environment"}),Re.rows.map((l,i)=>F(C,{color:i===Re.cursor?G:void 0,children:[i===Re.cursor?"\u276F ":" ",`${i+1}. ${l.name}`.padEnd(16),S(C,{color:"gray",children:l.apiUrl})]},l.name)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ae?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Switch workspace"}),S(C,{dimColor:!0,children:`environment ${ae.environment} \xB7 project ${ae.projectId.slice(0,8)}\u2026`}),ae.rows.map((l,i)=>F(C,{color:i===ae.cursor?G:void 0,children:[i===ae.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===ae.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.type??""})]},l.id)),S(C,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):fe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Choose project"}),S(C,{dimColor:!0,children:`environment ${fe.environment}`}),fe.rows.map((l,i)=>F(C,{color:i===fe.cursor?G:void 0,children:[i===fe.cursor?"\u276F ":" ",`${i+1}. ${l.name}${i===fe.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.sub??""})]},l.id)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):le?(()=>{let i=Math.min(Math.max(0,le.cursor-Math.floor(7)),Math.max(0,le.rows.length-14)),a=le.rows.slice(i,i+14),w=le.rows.filter(u=>u.checked).length;return F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Which folders back the user docs? (${w} selected of ${le.rows.length})`}),i>0?S(C,{dimColor:!0,children:` \u2191 ${i} more`}):null,a.map((u,m)=>{let k=i+m;return F(C,{color:k===le.cursor?G:void 0,children:[k===le.cursor?"\u276F ":" ",u.checked?"\u25C9 ":"\u25CB ",u.path.padEnd(Math.min(48,Be-34)),S(C,{color:"gray",children:no(u)})]},u.path)}),i+14<le.rows.length?S(C,{dimColor:!0,children:` \u2193 ${le.rows.length-i-14} more`}):null,S(C,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ve?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:"Publish which article?"}),ve.rows.map((l,i)=>F(C,{color:i===ve.cursor?G:void 0,children:[i===ve.cursor?"\u276F ":" ",`${i+1}. ${l.path}`.padEnd(Math.min(56,Be-30)),S(C,{color:"gray",children:l.label})]},l.path)),S(C,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):me?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Preview article${me.query?` \u2014 filter: ${me.query}`:" (type to filter)"}`}),Lt.length===0?S(C,{color:"gray",children:"no articles match"}):Lt.map((l,i)=>F(C,{color:i===me.cursor?G:void 0,children:[i===me.cursor?"\u276F ":" ",l]},l)),S(C,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):he?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:G,bold:!0,children:`Resume session${he.query?` \u2014 filter: ${he.query}`:" (type to filter)"}`}),Ot.length===0?S(C,{color:"gray",children:"no sessions match"}):Ot.map((l,i)=>F(C,{color:i===he.cursor?G:void 0,children:[i===he.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),S(C,{color:i===he.cursor?G:"gray",children:l.firstPrompt.slice(0,Math.max(10,Be-40))})]},l.uuid)),S(C,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Kn?S(J,{flexDirection:"column",children:mt.map((l,i)=>F(C,{color:i===ft?G:void 0,children:[i===ft?"\u276F ":" ",l.usage.padEnd(22)," ",S(C,{color:i===ft?G:"gray",children:l.name==="model"&&So?`${l.desc} (currently ${So})`:l.desc})]},l.name))}):!R&&pt.length>0?F(J,{flexDirection:"column",paddingX:1,children:[pt.map((l,i)=>F(C,{children:[S(C,{color:G,children:i+1})," ",l.slice(0,Math.max(20,Be-5))]},l)),S(C,{dimColor:!0,children:`press 1-${pt.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:F(C,{color:"gray",children:[Xn.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${So??"model n/a"}${t.kind==="api"?zn>0?` \xB7 ${se(zn)}`:"":Gn>0?` \xB7 ${Ie(Gn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}q();import{jsx as Lu}from"react/jsx-runtime";async function hi(e=process.cwd(),t="auto",o,n="0.0.0"){let r=Mu(t);r.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=Nu(Lu(gi,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
158
- `),process.exit(0)}var Fu=Uu(import.meta.url),wi=Fu("../package.json"),Se=new Ou;function Un(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Se.command("login").description("Sign in to Document360 (browser OAuth; project chosen during login)").option("--profile <name>","Connection profile (defaults to the repo's defaultProfile)").option("--env <name>",!1).option("--manual","No local listener \u2014 paste the redirect URL instead (SSH/locked-down setups)").action(async e=>{Un(e),await zt({profile:e.profile,manual:e.manual})});Se.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Un(e),await mr({profile:e.profile})});Se.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{Un(e),await fr({profile:e.profile})});var Wn=Se.command("profile").description("Manage connection profiles for the current repo");Wn.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Yt(process.cwd()));Wn.command("use <name>").description("Set the default profile for this repo").action(e=>Vt(process.cwd(),e));Wn.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Xt(process.cwd(),e));var ki=Se.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");ki.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>lt(process.cwd(),e.profile));ki.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await lr(process.cwd(),e,t.profile)});Se.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>hr());Se.command("doctor").description("Health-check: node, Claude auth, Document360 login, profile/workspace, category map, API reachability").action(async()=>{let{renderDoctorChecks:e,runDoctorChecks:t}=await Promise.resolve().then(()=>(io(),fs)),o=await t(process.cwd());for(let n of e(o))console.log(n);process.exitCode=o.some(n=>n.level==="fail")?1:0});Se.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(wi.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(Wu.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 wr(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 Ws(e.cwd,e.auth,e.profile):await hi(e.cwd,e.auth,e.profile,wi.version)});Se.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
149
+ Model: ${j.model}${j.forced?" (forced)":" \u2014 authoring; /model to override"}`;d({kind:"note",tone:"info",text:Y+fo(L,U,P).join(`
150
+ `)+re}),X([`/write${m?` --scope ${m}`:" --all"} --run`]);return}Me.current=Date.now(),Ye(0),_(!0);let ne=new AbortController;ge.current=ne,K({verb:"Writing",total:L.length,done:0,active:[],rows:jt(L),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${M.length} article${M.length===1?"":"s"} across ${L.length} partition${L.length===1?"":"s"} (\u2264${P} agents at once) on ${j.model}${j.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let U of xn({cwd:e,partitions:L,promptFor:Y=>po(Y,N),concurrency:P,profileName:s,allowProdWrites:Je.current,model:j.model,signal:ne.signal}))if(U.type==="partition_status")U.status==="running"?K(Y=>Y&&{...Y,active:[...Y.active,U.label],rows:Ce(Y.rows,U.label,"active"),lastAt:Date.now()}):K(Y=>Y&&{...Y,active:Y.active.filter(re=>re!==U.label),done:Y.done+1,rows:Ce(Y.rows,U.label,U.status==="done"?"done":"failed"),lastAt:Date.now()});else if(U.type==="partition_event")K(Y=>{if(!Y)return Y;let re={...Y,lastAt:Date.now()};return U.event.type==="tool"?re.tools=Y.tools+1:U.event.type==="text"&&(re.chars=Y.chars+U.event.delta.length),re});else if(U.type==="run_done"){it(Mn=>Mn+U.totalCostUsd),st(Mn=>Mn+U.results.reduce((Ei,Di)=>Ei+Di.outputTokens,0));let Y=U.aborted?"Stopped. ":"",re=t.kind==="api"?"api":"subscription";d({kind:"note",tone:U.aborted?"warn":U.ok?"ok":"warn",text:Y+mo(U.results,L,re).join(`
151
+ `)}),!U.aborted&&U.ok&&X(["/publish --all","/screenshot --all"])}}catch(U){d({kind:"note",tone:"error",text:`Write run failed: ${U.message}`})}finally{ge.current=null,K(null),_(!1),te(U=>U+1)}return}case"sync":{let u=(h[0]??"status").toLowerCase();try{if(u==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let m=await Wo({cwd:e,profileName:s});d({kind:"note",tone:"info",text:an(m).join(`
152
+ `)});return}if(u==="pull"){let m=h[1];if(!m){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(m==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await Wo({cwd:e,profileName:s})).entries.filter(M=>M.status==="remote-ahead"&&M.path).map(M=>M.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=[m.replace(/\\/g,"/")];let T=[];for(let N of y)T.push(await xu({cwd:e,profileName:s,relPath:N}));Se.current=T,Bt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${u} \u2014 use /sync or /sync pull <path>|--all.`})}catch(m){d({kind:"note",tone:"error",text:`Sync failed: ${m.message}`})}return}case"scope":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let u=bu(e);if(u.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}rt({cursor:0,rows:u.map(m=>({...m,checked:m.recommended}))});return}case"audit":case"capture-setup":{let m=await(i==="audit"?rn:Tt)(h,void 0);if(m.kind==="forward-to-agent"&&m.prompt&&await je(m.prompt,{display:m.display,echoDisplay:!0}),i==="capture-setup"){let y=Po(e);y&&d({kind:"note",tone:"info",text:y.join(`
153
+ `)})}return}case"devhints":{if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}ju(Au(e)),nu(Eu(e),So()),d({kind:"note",tone:"info",text:To().join(`
154
+ `)});return}case"screenshot":{let u=bo(h);if(u.mode==="list"){if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}d({kind:"note",tone:"info",text:Co(vo(e,u.scope),u.scope).join(`
155
+ `)});return}if(u.mode==="single"){let P=await un(h);P.kind==="forward-to-agent"&&P.prompt&&await je(P.prompt,{display:P.display,echoDisplay:!0});return}if(!ie(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let m=u.mode==="scope"?u.scope:void 0,y=Su(e,{scope:m}).map(P=>P.id);if(y.length===0){d({kind:"note",tone:"info",text:m?`No screenshot placeholders under ${m}.`:"No screenshot placeholders found in the docs."});return}let T=6,N=Pu(y,3),M=bn(e,"standard");Me.current=Date.now(),Ye(0),_(!0);let D=new AbortController;ge.current=D,K({verb:"Authoring",unit:"spec",total:N.length,done:0,active:[],rows:jt(N),tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Authoring ${y.length} screenshot spec${y.length===1?"":"s"} across ${N.length} partition${N.length===1?"":"s"} (\u2264${T} agents at once) on ${M.model}${M.forced?" (forced)":""}\u2026 (esc to stop)`});let L=!1;try{for await(let P of xn({cwd:e,partitions:N,promptFor:js,concurrency:T,profileName:s,allowProdWrites:Je.current,model:M.model,signal:D.signal}))if(P.type==="partition_status")P.status==="running"?K(j=>j&&{...j,active:[...j.active,P.label],rows:Ce(j.rows,P.label,"active"),lastAt:Date.now()}):K(j=>j&&{...j,active:j.active.filter(W=>W!==P.label),done:j.done+1,rows:Ce(j.rows,P.label,P.status==="done"?"done":"failed"),lastAt:Date.now()});else if(P.type==="partition_event")K(j=>{if(!j)return j;let W={...j,lastAt:Date.now()};return P.event.type==="tool"?W.tools=j.tools+1:P.event.type==="text"&&(W.chars=j.chars+P.event.delta.length),W});else if(P.type==="run_done"){it(U=>U+P.totalCostUsd),st(U=>U+P.results.reduce((Y,re)=>Y+re.outputTokens,0));let j=P.results.filter(U=>U.ok).length,W=P.aborted?"Stopped. ":"";L=!P.aborted&&u.setup;let ne=P.aborted?"":L?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:P.aborted?"warn":P.ok?"ok":"warn",text:`${W}Authored specs \u2014 ${j}/${P.results.length} batches ok.${ne}`})}}catch(P){d({kind:"note",tone:"error",text:`Spec authoring failed: ${P.message}`})}finally{ge.current=null,K(null),_(!1),te(P=>P+1)}if(L){let P=await Tt();P.kind==="forward-to-agent"&&P.prompt&&await je(P.prompt,{display:P.display,echoDisplay:!0});let j=Po(e);j&&d({kind:"note",tone:"info",text:j.join(`
156
+ `)})}return}case"reset":{let u=Ro(e);if(u.length===0){d({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}Sn.current={repoName:At(e),targets:u},d({kind:"note",tone:"warn",text:Ao(e,u).join(`
157
+ `)}),oe(At(e));return}default:d({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Bt,d,ce,je,Ft,En]),qt=V(l=>{let a=(l??R).trim();if(w(""),It(null),Ve(0),a.startsWith("/")&&(oe(null),ze.current++,Nt(null),X(h=>Zs(h,a))),!a||ir(a)||sr(a))return;Pn.current.push(a);let i=oi(a,zo.current);a.startsWith("/")?ar(i):je(i,{display:a})},[R,ir,sr,ar,je]),lr=V(l=>{if(l.length>1){if(l.includes("\x1B"))return;let a=ei(l);if(ti(a)){let i=ni(++Si.current,a);zo.current.set(i,a),x(i)}else x(a);return}x(l)},[x]),at=Math.max(10,Be-6),cr=Oo(()=>yn(k.text,at),[k.text,at]),Nn=V(l=>$(a=>({...a,pos:Math.max(0,Math.min(a.text.length,a.pos+l))})),[]),Gt=V(l=>$(a=>({...a,pos:si(yn(a.text,at),a.pos,l)})),[at]),gt=V(l=>$(a=>({...a,pos:ii(yn(a.text,at),a.pos,l)})),[at]),ji=["\x1B[H","\x1B[1~","\x1BOH"],Ai=["\x1B[F","\x1B[4~","\x1BOF"],ur=V((l,a)=>a.leftArrow?(Nn(-1),!0):a.rightArrow?(Nn(1),!0):l&&ji.includes(l)?(gt("start"),!0):l&&Ai.includes(l)?(gt("end"),!0):a.ctrl&&l==="a"?(gt("start"),!0):a.ctrl&&l==="e"?(gt("end"),!0):!1,[Nn,gt]);return eu((l,a)=>{if(a.ctrl&&l==="c"){pe.current?.close(),r();return}if(!H){if(I){if(a.escape){if(ge.current){ge.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),ge.current.abort());return}Mt.current||(Mt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),pe.current?.interrupt());return}if(a.return){let i=R.trim();if(!i)return;Fe.current.push(i),Tn([...Fe.current]),w("");return}if(ur(l,a))return;if(a.upArrow){Gt(-1);return}if(a.downArrow){Gt(1);return}if(a.backspace||a.delete){v();return}l&&!a.ctrl&&!a.meta&&lr(l);return}if(Le){if(a.upArrow){Oe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Oe(i=>i&&{...i,cursor:Math.min(ye.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ye.length){Oe(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=ye[Le.cursor];Oe(null);let{lines:h,changed:u,effective:m}=Pt(e,i.value??"default");for(let y of h)d({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});u&&(Zo(y=>y+1),pe.current?.setModel(m));return}if(l==="s"){let i=ye[Le.cursor];Oe(null),pe.current?.setModel(i.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(a.escape){Oe(null);return}return}if(be){if(a.upArrow){Ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){Ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=be.rows.length){Ze(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return||l==="s"){let i=be.rows[be.cursor];Ze(null),In(i.name,a.return===!0);return}if(a.escape){Ze(null);return}return}if(Te){if(a.upArrow){et(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){et(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Te.rows.length){et(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=Te.rows[Te.cursor].name;if(et(null),!Ar(e,i).created){d({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}d({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let u=!1;try{let m=Ge(i);u=!!m&&!(_e(m)&&!m.refreshToken)}catch{}u?(d({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),X(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),X(["/login"])),te(m=>m+1);return}if(a.escape){et(null);return}return}if(ae){if(a.upArrow){tt(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){tt(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=ae.rows.length){tt(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=ae.rows[ae.cursor],{profile:h,projectId:u}=ae;tt(null),Ht(h,u,i);return}if(a.escape){tt(null);return}return}if(fe){if(a.upArrow){nt(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){nt(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=fe.rows.length){nt(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=fe.rows[fe.cursor],{profile:h}=fe;nt(null),rr(h,i);return}if(a.escape){nt(null);return}return}if(le){if(a.upArrow){rt(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){rt(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l===" "){rt(i=>i&&{...i,rows:i.rows.map((h,u)=>u===i.cursor?{...h,checked:!h.checked}:h)});return}if(a.return){let i=le.rows.filter(h=>h.checked).map(h=>h.path);if(rt(null),i.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}Zn(e,i),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of i)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(a.escape){rt(null);return}return}if(Ue){if(a.upArrow){ot(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){ot(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(l&&/^[1-9]$/.test(l)&&Number(l)<=Ue.rows.length){ot(i=>i&&{...i,cursor:Number(l)-1});return}if(a.return){let i=Ue.rows[Ue.cursor];ot(null),i.kind==="article"?En(i.paths[0]):Ft(i.paths);return}if(a.escape){ot(null);return}return}if(me){if(a.escape){We(null);return}if(a.upArrow){We(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){We(i=>i&&{...i,cursor:Math.min(Math.max(0,Ut.length-1),i.cursor+1)});return}if(a.return){let i=Ut[me.cursor];if(i){We(null);try{d({kind:"preview",name:At(i),text:ki(ui(Et(e,i),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${i}: ${h.message}`})}}return}if(a.backspace||a.delete){We(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(l&&!a.ctrl&&!a.meta&&l.length===1){We(i=>i&&{...i,query:i.query+l,cursor:0});return}return}if(he){if(a.escape){He(null);return}if(a.upArrow){He(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(a.downArrow){He(i=>i&&{...i,cursor:Math.min(Math.max(0,Wt.length-1),i.cursor+1)});return}if(a.return){let i=Wt[he.cursor];if(i){He(null),ce(i.uuid),xe.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Qe(i.name),Uo(i.uuid),Ke.current=[];let h=_o(e,i.uuid);for(let u of h)d(u);d({kind:"note",tone:"ok",text:h.length?`Resumed "${i.name}" \u2014 restored ${h.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(a.backspace||a.delete){He(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(l&&!a.ctrl&&!a.meta&&l.length===1){He(i=>i&&{...i,query:i.query+l,cursor:0});return}return}if(a.tab&&!R&&ke){w(ke),oe(null),ze.current++;return}if(!R&&pt.length>0&&l&&/^[1-9]$/.test(l)){let i=pt[Number(l)-1];if(i){w(i);return}}if(!ur(l,a)){if(nr){if(a.upArrow){Ve(i=>Math.max(0,i-1));return}if(a.downArrow){Ve(i=>Math.min(mt.length-1,i+1));return}if(a.tab){w("/"+(mt[ft]?.name??"")+" "),Ve(0);return}if(a.return){let i=mt[ft];if(i){let h=R.trim().slice(1).split(/\s+/).slice(1).join(" ");if(Pr(i.usage)&&!h){w("/"+i.name+" "),Ve(0);return}qt("/"+i.name+(h?" "+h:""));return}}}else{if(a.upArrow){if(R!==""&&Xe===null){Gt(-1);return}let i=Pn.current;if(!i.length)return;let h=Xe===null?i.length-1:Math.max(0,Xe-1);It(h),w(i[h]??"");return}if(a.downArrow){if(R!==""&&Xe===null){Gt(1);return}let i=Pn.current;if(Xe===null)return;let h=Xe+1;h>=i.length?(It(null),w("")):(It(h),w(i[h]??""));return}}if(a.return){qt();return}if(a.backspace||a.delete){v();return}if(a.escape){w(""),Ve(0),oe(null),X([]),Nt(null);return}l&&!a.ctrl&&!a.meta&&lr(l)}}}),Ne(()=>{if(I||H)return;let l=Fe.current.shift();l!==void 0&&(Tn([...Fe.current]),qt(l))},[I,H,qt]),F(J,{flexDirection:"column",width:Be,children:[An!==null&&F(J,{marginTop:1,flexDirection:"column",children:[An.truncated&&S(C,{dimColor:!0,children:"\u2026"}),S(C,{children:An.text})]}),I&&(Ko?S(Fu,{p:Ko}):S(Uu,{startTime:Me.current,chars:Pi})),S(J,{borderStyle:"round",borderColor:tr.prod?"yellow":q,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:R?cr.map((l,a)=>{let i=k.text.slice(l.start,l.end),h=a===$n(cr,k.pos),u=Math.min(k.pos,l.end)-l.start;return F(C,{children:[S(C,{color:q,children:a===0?"> ":" "}),h?F(Ho,{children:[i.slice(0,u),S(vn,{ch:i[u]??" "}),i.slice(u+1)]}):i||" "]},`${a}-${l.start}`)}):F(C,{children:[S(C,{color:q,children:"> "}),ke&&!I?F(Ho,{children:[S(vn,{ch:ke[0],dim:!0}),S(C,{color:"gray",children:ke.slice(1)}),S(C,{dimColor:!0,children:" (tab)"})]}):jn.isSetup||!Ci?F(Ho,{children:[S(vn,{ch:jn.text[0],dim:!0}),S(C,{color:"gray",children:jn.text.slice(1)})]}):S(vn,{ch:" "})]})}),Xo.length>0&&S(J,{flexDirection:"column",paddingX:1,children:Xo.map((l,a)=>S(C,{color:"gray",children:`\u29D7 queued: ${l}`},`${a}-${l.slice(0,24)}`))}),Le?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:"Select model"}),S(C,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ye.map((l,a)=>F(C,{color:a===Le.cursor?q:void 0,children:[a===Le.cursor?"\u276F ":" ",`${a+1}. ${l.label}${a===Le.current?" \u2714":""}`.padEnd(16),S(C,{color:"gray",children:l.desc})]},l.label)),S(C,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):be?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:"Switch connection profile"}),be.rows.map((l,a)=>F(C,{color:a===be.cursor?q:void 0,children:[a===be.cursor?"\u276F ":" ",`${a+1}. ${l.name}${a===be.current?" \u2714":""}`.padEnd(20),S(C,{color:"gray",children:`${l.env} \xB7 ${l.who??"not signed in"}`}),l.prod?S(C,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},l.name)),S(C,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):Te?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:"Pick your Document360 environment"}),Te.rows.map((l,a)=>F(C,{color:a===Te.cursor?q:void 0,children:[a===Te.cursor?"\u276F ":" ",`${a+1}. ${l.name}`.padEnd(16),S(C,{color:"gray",children:l.apiUrl})]},l.name)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ae?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:"Switch workspace"}),S(C,{dimColor:!0,children:`environment ${ae.environment} \xB7 project ${ae.projectId.slice(0,8)}\u2026`}),ae.rows.map((l,a)=>F(C,{color:a===ae.cursor?q:void 0,children:[a===ae.cursor?"\u276F ":" ",`${a+1}. ${l.name}${a===ae.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.type??""})]},l.id)),S(C,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):fe?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:"Choose project"}),S(C,{dimColor:!0,children:`environment ${fe.environment}`}),fe.rows.map((l,a)=>F(C,{color:a===fe.cursor?q:void 0,children:[a===fe.cursor?"\u276F ":" ",`${a+1}. ${l.name}${a===fe.current?" \u2714":""}`.padEnd(30),S(C,{color:"gray",children:l.sub??""})]},l.id)),S(C,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):le?(()=>{let a=Math.min(Math.max(0,le.cursor-Math.floor(7)),Math.max(0,le.rows.length-14)),i=le.rows.slice(a,a+14),h=le.rows.filter(u=>u.checked).length;return F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:`Which folders back the user docs? (${h} selected of ${le.rows.length})`}),a>0?S(C,{dimColor:!0,children:` \u2191 ${a} more`}):null,i.map((u,m)=>{let y=a+m;return F(C,{color:y===le.cursor?q:void 0,children:[y===le.cursor?"\u276F ":" ",u.checked?"\u25C9 ":"\u25CB ",u.path.padEnd(Math.min(48,Be-34)),S(C,{color:"gray",children:sn(u)})]},u.path)}),a+14<le.rows.length?S(C,{dimColor:!0,children:` \u2193 ${le.rows.length-a-14} more`}):null,S(C,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():Ue?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:"Publish to Document360 \u2014 pick a category (whole) or an article:"}),Ue.rows.map((l,a)=>{let i=a===Ue.cursor,h=l.indent?" ":" ",u=`${a+1}.`.padEnd(3);return F(C,{color:i?q:void 0,bold:l.kind==="category"&&!i,children:[i?"\u276F ":" ",`${h}${u} ${l.label}`.padEnd(Math.min(56,Be-24)),l.note?S(C,{color:"gray",children:l.note}):null]},`${l.kind}:${l.paths.join(",")}`)}),S(C,{dimColor:!0,children:"\u2191\u2193 move \xB7 1-9 jump \xB7 enter publish (draft) \xB7 esc cancel"})]}):me?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:`Preview article${me.query?` \u2014 filter: ${me.query}`:" (type to filter)"}`}),Ut.length===0?S(C,{color:"gray",children:"no articles match"}):Ut.map((l,a)=>F(C,{color:a===me.cursor?q:void 0,children:[a===me.cursor?"\u276F ":" ",l]},l)),S(C,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):he?F(J,{flexDirection:"column",paddingX:1,children:[S(C,{color:q,bold:!0,children:`Resume session${he.query?` \u2014 filter: ${he.query}`:" (type to filter)"}`}),Wt.length===0?S(C,{color:"gray",children:"no sessions match"}):Wt.map((l,a)=>F(C,{color:a===he.cursor?q:void 0,children:[a===he.cursor?"\u276F ":" ",l.name.slice(0,28).padEnd(30),S(C,{color:a===he.cursor?q:"gray",children:l.firstPrompt.slice(0,Math.max(10,Be-40))})]},l.uuid)),S(C,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):nr?S(J,{flexDirection:"column",children:mt.map((l,a)=>F(C,{color:a===ft?q:void 0,children:[a===ft?"\u276F ":" ",l.usage.padEnd(22)," ",S(C,{color:a===ft?q:"gray",children:l.name==="model"&&Rn?`${l.desc} (currently ${Rn})`:l.desc})]},l.name))}):!R&&pt.length>0?F(J,{flexDirection:"column",paddingX:1,children:[Vo?S(C,{color:q,children:Vo}):null,pt.map((l,a)=>F(C,{children:[S(C,{color:q,children:a+1})," ",l.slice(0,Math.max(20,Be-5))]},l)),S(C,{dimColor:!0,children:`press 1-${pt.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:F(C,{color:"gray",children:[tr.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${Rn??"model n/a"}${t.kind==="api"?Qo>0?` \xB7 ${se(Qo)}`:"":Jo>0?` \xB7 ${De(Jo)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}G();import{jsx as qu}from"react/jsx-runtime";async function xi(e=process.cwd(),t="auto",n,o="0.0.0"){let r=Bu(t);r.kind==="none"&&(console.error(""),console.error(b("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${E("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${E("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=Hu(qu($i,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
158
+ `),process.exit(0)}var Vu=zu(import.meta.url),bi=Vu("../package.json"),Pe=new Gu;function qo(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=>{qo(e),await Xt({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=>{qo(e),await $r({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=>{qo(e),await yr({profile:e.profile})});var Go=Pe.command("profile").description("Manage connection profiles for the current repo");Go.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Kt(process.cwd()));Go.command("use <name>").description("Set the default profile for this repo").action(e=>Jt(process.cwd(),e));Go.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Qt(process.cwd(),e));var vi=Pe.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");vi.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>lt(process.cwd(),e.profile));vi.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await mr(process.cwd(),e,t.profile)});Pe.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>br());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(()=>(cn(),ws)),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(bi.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(Yu.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 vr(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 Gs(e.cwd,e.auth,e.profile):await xi(e.cwd,e.auth,e.profile,bi.version)});Pe.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
@@ -10,6 +10,25 @@ export declare function publishCandidates(entries: SyncEntryState[]): {
10
10
  path: string;
11
11
  label: string;
12
12
  }[];
13
+ /** A row in the grouped /publish picker: "all", a category (publishes all its candidates), or one
14
+ article. `paths` is what gets published when the row is chosen (one for an article, many otherwise). */
15
+ export type PublishRow = {
16
+ kind: 'all' | 'category' | 'article';
17
+ label: string;
18
+ paths: string[];
19
+ /** Article rows render indented under their category; gray status note (new/modified) sits beside. */
20
+ indent: boolean;
21
+ note?: string;
22
+ };
23
+ /**
24
+ * Build the grouped publish picker: a "publish all" row, then each category (sorted) as a selectable
25
+ * parent that publishes ALL its candidate articles, with its articles listed beneath as indented,
26
+ * individually-selectable rows. Both categories and articles are pickable (SNAG-0135).
27
+ */
28
+ export declare function buildPublishRows(candidates: {
29
+ path: string;
30
+ label: string;
31
+ }[]): PublishRow[];
13
32
  /** The publish prompt for ONE partition (a category's worth of articles). The bulk `/publish --all`
14
33
  fans these out across parallel agents (engine runPartitioned) so no single agent context carries
15
34
  the whole repo — the SNAG-0128 crash (a 38-article single turn OOM'd the SDK subprocess). */
@@ -24,4 +43,8 @@ export declare function publishSummaryLines(results: PartitionResult[], partitio
24
43
  * completes, so a re-run skips what's already in sync.
25
44
  */
26
45
  export declare function runBulkPublishClassic(ctx: ReplContext, paths: string[]): Promise<SlashCommandResult>;
46
+ /** Repo-relative paths of `articlePath`'s related articles that aren't published yet — its
47
+ `## Related articles` targets intersected with the current publish candidates. Deterministic, so
48
+ the post-publish offer lists ALL of them (the agent's prose only listed some — SNAG-0136). */
49
+ export declare function unpublishedRelated(cwd: string, profileName: string | undefined, articlePath: string): Promise<string[]>;
27
50
  export declare function publishCommand(args: string[], ctx?: ReplContext): Promise<SlashCommandResult>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-writer",
3
- "version": "0.4.50",
3
+ "version": "0.4.51",
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.27",
37
+ "document360-engine": "^0.2.28",
38
38
  "ink": "^5.2.1",
39
39
  "picocolors": "^1.1.1",
40
40
  "react": "^18.3.1",