document360-writer 0.4.34 → 0.4.35
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 +76 -64
- package/dist/commands/write.d.ts +51 -0
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -1,83 +1,95 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var Os=Object.defineProperty;var En=(e,t,o)=>()=>{if(o)throw o[0];try{return e&&(t=e(e=0)),t}catch(n){throw o=[n],n}};var Us=(e,t)=>{for(var o in t)Os(e,o,{get:t[o],enumerable:!0})};import Se from"picocolors";var B,Ws,Fs,Hs,Dn,st,f,x,N,D,S,V,we,H=En(()=>{"use strict";B="#7f56d9",[Ws,Fs,Hs]=[127,86,217],Dn=e=>Se.isColorSupported?`\x1B[38;2;${Ws};${Fs};${Hs}m${e}\x1B[39m`:e,st=e=>Se.bold(Dn(e)),f=e=>Se.dim(e),x=e=>Se.red(e),N=e=>Se.yellow(e),D=e=>Se.green(e),S=Dn,V=e=>Se.gray(e),we=e=>Se.bold(e)});var _r={};Us(_r,{doctorCommand:()=>ft,renderDoctorChecks:()=>Ir,runDoctorChecks:()=>Dr});import{existsSync as $l}from"node:fs";import{d360GetAll as bl,getAccessToken as vl,isExpired as Xo,loadProfileMap as Cl,loadTokens as Sl,packageSkillsDir as Er,projectConfigPath as Tl,readProjectConfig as Pl,resolveActiveProfile as Rl,resolveAuth as Al,resolveModelSetting as jl}from"document360-engine";async function Dr(e){let t=[],o=Number(process.versions.node.split(".")[0]);t.push(o>=20?{level:"ok",label:`Node ${process.versions.node}`}:{level:"fail",label:`Node ${process.versions.node} \u2014 20+ required`,fix:"Install Node 20 or later (nodejs.org)"});let n=Al("auto");t.push(n.kind==="none"?{level:"fail",label:"Claude auth: not configured",fix:"Set ANTHROPIC_API_KEY, or sign in to Claude Code once (subscription reuse)"}:{level:"ok",label:`Claude auth: ${n.kind==="api"?"API key":"subscription"}`});let r=jl(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Pl(e);if(!s)return t.push({level:"fail",label:`No ${Tl(e)}`,fix:"Run: /init (or d360-writer init)"}),t;t.push({level:"ok",label:`Project config: ${s.projectId}`});let c=(s.docsDir??"user-docs").replace(/\/+$/,"");t.push(s.mode==="engineer"?{level:"warn",label:"Mode: engineer \u2014 agent may modify any source file (dogfooding)",fix:'Remove "mode" from .d360-writer/config.json for the writer-mode boundary'}:{level:"ok",label:`Mode: writer \u2014 edits limited to ${c}/ and .d360-writer/ (docs, capture specs, config); product source is read-only`});let d=s.authoritativeSourceFiles??[];t.push(d.length>0?{level:"ok",label:`Sources: docs grounded in ${d.length} path(s) (${d.slice(0,3).join(", ")}${d.length>3?", \u2026":""})`}:{level:"warn",label:"Sources: not set \u2014 docs are grounded in the whole repo",fix:"Run: /scope to choose which files/folders the docs are written from"});let h;try{h=Rl(e),t.push({level:"ok",label:`Profile: ${h.name} (${h.connection.name})${h.production?" \u26A0 PRODUCTION":""}`})}catch(A){return t.push({level:"fail",label:`Profile config: ${A.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let g=Sl(h.name);g?Xo(g)&&!g.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):Xo(g)?t.push({level:"warn",label:"Document360: token expired \u2014 will auto-refresh on next call"}):t.push({level:"ok",label:`Document360: logged in (until ${new Date(g.expiresAt).toLocaleString()})`}):t.push({level:"fail",label:"Document360: not logged in",fix:`Run: /login (or d360-writer login --profile ${h.name})`}),t.push(h.project.workspaceId?{level:"ok",label:`Workspace: ${h.project.workspaceName??h.project.workspaceId}`}:{level:"warn",label:"No workspace selected",fix:"Run: /workspace"});let w=Cl(e,h.name);if(w?w.projectId&&h.project.projectId&&w.projectId!==h.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.projectId}) \u2260 profile projectId (${h.project.projectId})`,fix:"The map section belongs to a different project \u2014 fix .d360-writer/config.json or the map before publishing"}):t.push({level:"ok",label:`Category map: ${Object.keys(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${h.name}" (created on first /publish)`}),t.push($l(Er())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${Er()}`,fix:"Reinstall document360-writer (broken install)"}),g&&(!Xo(g)||g.refreshToken)){let A={profile:h.name,connection:h.connection};try{await vl(A);let $=await bl(A,"/v3/projects");t.push({level:"ok",label:`API reachable (${h.connection.apiUrl}) \u2014 ${$.length} project(s) visible`})}catch($){t.push({level:"fail",label:`API call failed: ${$.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Ir(e){let t=[""];for(let r of e){let s=r.level==="ok"?D("\u2713"):r.level==="warn"?N("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${S(r.fix)}`)}let o=e.filter(r=>r.level==="fail").length,n=e.filter(r=>r.level==="warn").length;return t.push(""),t.push(o===0?D(`\u2713 ${n===0?"All checks passed":`Healthy (${n} warning${n===1?"":"s"})`}`):x(`\u2717 ${o} problem${o===1?"":"s"} found`)),t.push(""),t}async function ft(e,t){console.log(f("Running checks\u2026"));for(let o of Ir(await Dr(t.cwd)))console.log(o);return{kind:"continue"}}var Jt=En(()=>{"use strict";H()});import{Command as yc}from"commander";import{createRequire as wc}from"node:module";import{AUTH_MODES as xc}from"document360-engine";import{input as Qs}from"@inquirer/prompts";import{loginPkce as Zs,refreshTokens as ei,toStoredTokens as Nn,clearTokens as ti,decodeJwtClaims as Mn,isExpired as oi,loadTokens as ni,saveTokens as Ln,resolveActiveProfile as _t,setProfileProject as ri,readProjectConfig as si}from"document360-engine";H();import{select as Bs}from"@inquirer/prompts";import{resolveActiveProfile as qs,setProfileProject as zs,resolveProjectId as Gs,listWorkspaces as Ys}from"document360-engine";async function Dt(e,t){let o=qs(e,t),n={profile:o.name,connection:o.connection},r=o.project.projectId??Gs(n);return{workspaces:await Ys(n,r),projectId:r,profile:o.name,environment:o.connection.name,current:o.project.workspaceId}}var In=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Co(e,t){let o=t.toLowerCase();return e.find(n=>(n.name??"").toLowerCase()===o)??e.find(n=>(n.name??"").toLowerCase().startsWith(o))??e.find(n=>n.id.startsWith(t))}function It(e,t,o,n,r){zs(e,t,{projectId:o,workspaceId:n,workspaceName:r})}async function _n(e,t,o){let n;try{n=await Dt(e,o)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Co(n.workspaces,t);return r?(It(e,n.profile,n.projectId,r.id,r.name),console.log(D(`\u2713 Workspace set to "${r.name??r.id}" for profile "${n.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${n.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Je(e,t){let o;try{o=await Dt(e,t)}catch(g){console.log(x(`Could not list workspaces: ${g.message}`));return}let{workspaces:n,projectId:r,profile:s,current:c}=o;if(n.length===0){console.log(f("No workspaces found in this project."));return}if(!process.stdin.isTTY){console.log("");for(let g of n)console.log(` ${g.id===c?S("\u25CF"):" "} ${In(g)} ${f(g.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let d=await Bs({message:"Select the Document360 workspace for this repo:",choices:n.map(g=>({name:`${In(g)}${g.id===c?" (current)":""}`,value:g.id}))}),h=n.find(g=>g.id===d);It(e,s,r,d,h?.name),console.log(D(`\u2713 Workspace set to "${h?.name??d}" for profile "${s}".`))}H();import Xs from"picocolors";function Vs(e=process.env){return e.FORCE_HYPERLINK==="0"||!Xs.isColorSupported?!1:e.FORCE_HYPERLINK?!0:!!(e.WT_SESSION||e.TERM_PROGRAM==="vscode"||e.TERM_PROGRAM==="iTerm.app"||e.TERM_PROGRAM==="WezTerm"||e.TERM_PROGRAM==="ghostty"||e.VTE_VERSION||e.KONSOLE_VERSION)}function Js(e,t=e,o){return Vs(o)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var Ks=/https?:\/\/[^\s\x1b]+/g;function it(e,t){return e.replace(Ks,o=>Js(o,o,t))}function On(e){return{...Mn(e.idToken)??{},...Mn(e.accessToken)??{}}}function Mt(e){let t=On(e),o=t.email??t.preferred_username??t.sub??"unknown",n=t.doc360_project_id?` \xB7 project ${t.doc360_project_id}`:"";return`${o}${n}`}async function Nt(e){let t=_t(process.cwd(),e.profile),o=t.connection;console.log(f(`Profile "${t.name}" \u2192 ${o.name} (${o.apiUrl})${t.production?" \u26A0 PRODUCTION":""}`));let n=await Zs(o,{manual:e.manual,promptForRedirect:s=>Qs({message:s})},s=>console.log(it(s))),r=Nn(t.name,n);if(Ln(r),So(r,t.name,s=>console.log(f(s))),console.log(""),console.log(D(`\u2713 Logged in to "${t.name}" as ${Mt(r)}`)),console.log(f(` access token expires: ${r.expiresAt}`)),console.log(f(` refresh token: ${r.refreshToken?"yes":"NO \u2014 session ends at expiry"}`)),process.stdin.isTTY)try{si(process.cwd())?.profiles?.[t.name]&&(console.log(""),await Je(process.cwd(),t.name))}catch{}}function So(e,t,o){let r=On(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(_t(process.cwd(),t).project.projectId)return;ri(process.cwd(),t,{projectId:r}),o(` Project ${r} written to profile "${t}".`)}catch{}}async function Un(e){let t=_t(process.cwd(),e.profile),o=t.connection,n=ni(t.name);if(!n){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${S(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Mt(n)}`),oi(n))if(n.refreshToken)try{let r=Nn(t.name,await ei(o,n.refreshToken));Ln(r),console.log(D(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(N(`Session expired and refresh failed (${r.message.slice(0,120)})`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1}else console.log(N("Session expired (no refresh token).")),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;else console.log(f(` expires: ${n.expiresAt}`))}async function Wn(e){let t=_t(process.cwd(),e.profile);ti(t.name)?console.log(D(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as Fn,writeProjectConfig as ii,resolveActiveProfile as li,loadTokens as ai,isExpired as ci}from"document360-engine";function Lt(e){let t=Fn(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[o,n]of Object.entries(t.profiles)){let r=o===t.defaultProfile?S("\u25CF "):" ",s=n.production?N(" \u26A0 PRODUCTION"):"",c=n.connection.environment??"(inline)",d=ai(o),h=d?ci(d)&&!d.refreshToken?N("expired"):f("logged in"):f("not logged in");console.log(`${r}${S(o)} \u2192 ${c}${s} [${h}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Ot(e,t){let o=Fn(e);if(!o?.profiles?.[t]){let r=o?.profiles?Object.keys(o.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}o.defaultProfile=t,ii(o,e);let n=o.profiles[t].production?N(" \u26A0 PRODUCTION"):"";console.log(D(`\u2713 Default profile is now "${t}"${n}`))}function Ut(e,t){try{let o=li(e,t);console.log(""),console.log(`Profile ${S(o.name)}${o.production?N(" \u26A0 PRODUCTION"):""}`),console.log(f(` api: ${o.connection.apiUrl}`)),console.log(f(` identity: ${o.connection.authorizationUrl}`)),console.log(f(` clientId: ${o.connection.clientId}`)),console.log(f(` scopes: ${o.connection.scopes.join(" ")}`)),console.log(f(` project: ${o.project.projectId??"(set at login)"}`)),console.log(f(` workspace:${o.project.workspaceId?" "+o.project.workspaceId:" (none)"}`)),console.log("")}catch(o){console.log(x(o.message)),process.exitCode=1}}H();import{existsSync as ui,readdirSync as di,statSync as pi}from"node:fs";import{join as fi}from"node:path";import{apiLogDir as mi}from"document360-engine";function Hn(){let e=mi();if(console.log(""),console.log(`Document360 API logs: ${S(e)}`),!ui(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=di(e).filter(o=>o.endsWith(".jsonl")).sort().reverse();t.length===0&&console.log(f(" No logs yet \u2014 they appear after the first Document360 API call."));for(let o of t.slice(0,14)){let n=(pi(fi(e,o)).size/1024).toFixed(1);console.log(` ${o} ${f(`${n} KB`)}`)}console.log(""),console.log(f("Failed calls include request/response bodies (tokens redacted, 4 KB cap).")),console.log(f("Set D360_LOG_BODIES=1 to also log bodies for successful calls.")),console.log("")}H();import{createSession as gi,resolveAuth as hi,findByName as ki,slugify as yi,touchSession as wi,upsertSession as xi,resolveActiveProfile as $i}from"document360-engine";async function Bn(e,t,o,n,r,s){let c=hi(o);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let d=null;try{d=$i(e,r)}catch($){console.error(x(`Document360 profile error: ${$.message}`)),process.exit(2)}d.production&&(console.error(N(`\u26A0 Profile "${d.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let h;if(n){let $=ki(e,n);$||(console.error(x(`No saved session matches "${n}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),h=$.uuid,console.error(f(`Resuming "${$.name}"`))}let g=gi({cwd:e,resume:h,profileName:r,allowProdWrites:s===!0}),w=h??null,A=1;for await(let $ of g.send(t))switch($.type){case"session":if(!w){w=$.sessionId;let j=new Date().toISOString();xi({uuid:w,name:yi(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:j,updatedAt:j})}break;case"text":process.stdout.write($.delta);break;case"tool":console.error(V(` \u2699 ${$.name}`));break;case"result":A=$.ok?0:1,console.error(f(`(${$.inputTokens}\u2192${$.outputTokens} tokens`+($.costUsd>0?`, $${$.costUsd<.01?$.costUsd.toFixed(4):$.costUsd.toFixed(2)}`:"")+")")),$.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${$.message}`)),process.exit(1)}w&&wi(w),process.stdout.write(`
|
|
3
|
-
`),process.exit(
|
|
4
|
-
(conversation reset \u2014 "${
|
|
2
|
+
var ei=Object.defineProperty;var Fo=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(o){throw n=[o],o}};var ti=(e,t)=>{for(var n in t)ei(e,n,{get:t[n],enumerable:!0})};import Ae from"picocolors";var z,ni,oi,ri,Ho,pt,f,x,L,D,R,Q,ve,H=Fo(()=>{"use strict";z="#7f56d9",[ni,oi,ri]=[127,86,217],Ho=e=>Ae.isColorSupported?`\x1B[38;2;${ni};${oi};${ri}m${e}\x1B[39m`:e,pt=e=>Ae.bold(Ho(e)),f=e=>Ae.dim(e),x=e=>Ae.red(e),L=e=>Ae.yellow(e),D=e=>Ae.green(e),R=Ho,Q=e=>Ae.gray(e),ve=e=>Ae.bold(e)});var Xr={};ti(Xr,{doctorCommand:()=>$t,renderDoctorChecks:()=>Yr,runDoctorChecks:()=>Gr});import{existsSync as zl}from"node:fs";import{d360GetAll as Gl,getAccessToken as Yl,isExpired as no,loadProfileMap as Xl,loadTokens as Vl,packageSkillsDir as zr,projectConfigPath as Jl,readProjectConfig as Kl,resolveActiveProfile as Ql,resolveAuth as Zl,resolveModelSetting as ea}from"document360-engine";async function Gr(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=Zl("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=ea(e);t.push({level:"ok",label:`Model: ${r.model??"Claude Code default"} (${r.source})`});let s=Kl(e);if(!s)return t.push({level:"fail",label:`No ${Jl(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 u=s.authoritativeSourceFiles??[];t.push(u.length>0?{level:"ok",label:`Sources: docs grounded in ${u.length} path(s) (${u.slice(0,3).join(", ")}${u.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=Ql(e),t.push({level:"ok",label:`Profile: ${g.name} (${g.connection.name})${g.production?" \u26A0 PRODUCTION":""}`})}catch(j){return t.push({level:"fail",label:`Profile config: ${j.message.split(".")[0]}`,fix:"Run: /init to scaffold the profiles map"}),t}let m=Vl(g.name);m?no(m)&&!m.refreshToken?t.push({level:"fail",label:"Document360: session expired (no refresh token)",fix:"/login"}):no(m)?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(m.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 w=Xl(e,g.name);if(w?w.projectId&&g.project.projectId&&w.projectId!==g.project.projectId?t.push({level:"fail",label:`Category map projectId (${w.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(w.articles).length} articles, ${Object.keys(w.categories).length} categories`}):t.push({level:"ok",label:`Category map: none yet for "${g.name}" (created on first /publish)`}),t.push(zl(zr())?{level:"ok",label:"Skills bundle present"}:{level:"fail",label:`Skills folder missing at ${zr()}`,fix:"Reinstall document360-writer (broken install)"}),m&&(!no(m)||m.refreshToken)){let j={profile:g.name,connection:g.connection};try{await Yl(j);let b=await Gl(j,"/v3/projects");t.push({level:"ok",label:`API reachable (${g.connection.apiUrl}) \u2014 ${b.length} project(s) visible`})}catch(b){t.push({level:"fail",label:`API call failed: ${b.message.slice(0,120)}`,fix:"/login if auth-related; otherwise check the apiUrl/network"})}}return t}function Yr(e){let t=[""];for(let r of e){let s=r.level==="ok"?D("\u2713"):r.level==="warn"?L("\u26A0"):x("\u2717");t.push(` ${s} ${r.label}${r.detail?f(` \u2014 ${r.detail}`):""}`),r.fix&&t.push(` ${f("fix:")} ${R(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?D(`\u2713 ${o===0?"All checks passed":`Healthy (${o} warning${o===1?"":"s"})`}`):x(`\u2717 ${n} problem${n===1?"":"s"} found`)),t.push(""),t}async function $t(e,t){console.log(f("Running checks\u2026"));for(let n of Yr(await Gr(t.cwd)))console.log(n);return{kind:"continue"}}var Qt=Fo(()=>{"use strict";H()});import{Command as Fc}from"commander";import{createRequire as Hc}from"node:module";import{AUTH_MODES as Bc}from"document360-engine";import{input as mi}from"@inquirer/prompts";import{loginPkce as gi,refreshTokens as hi,toStoredTokens as Go,clearTokens as ki,decodeJwtClaims as zo,isExpired as yi,loadTokens as wi,saveTokens as Yo,resolveActiveProfile as Lt,setProfileProject as $i,readProjectConfig as xi}from"document360-engine";H();import{select as si}from"@inquirer/prompts";import{resolveActiveProfile as ii,setProfileProject as li,resolveProjectId as ai,listWorkspaces as ci}from"document360-engine";async function Mt(e,t){let n=ii(e,t),o={profile:n.name,connection:n.connection},r=n.project.projectId??ai(o);return{workspaces:await ci(o,r),projectId:r,profile:n.name,environment:n.connection.name,current:n.project.workspaceId}}var Bo=e=>`${e.name??e.id}${e.workspace_type?` \xB7 ${e.workspace_type}`:""}`;function Pn(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 Nt(e,t,n,o,r){li(e,t,{projectId:n,workspaceId:o,workspaceName:r})}async function qo(e,t,n){let o;try{o=await Mt(e,n)}catch(s){return console.log(x(`Could not list workspaces: ${s.message}`)),1}let r=Pn(o.workspaces,t);return r?(Nt(e,o.profile,o.projectId,r.id,r.name),console.log(D(`\u2713 Workspace set to "${r.name??r.id}" for profile "${o.profile}".`)),0):(console.log(x(`No workspace matches "${t}". Available: ${o.workspaces.map(s=>s.name??s.id).join(", ")}`)),1)}async function Ze(e,t){let n;try{n=await Mt(e,t)}catch(m){console.log(x(`Could not list workspaces: ${m.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 m of o)console.log(` ${m.id===c?R("\u25CF"):" "} ${Bo(m)} ${f(m.id)}`);console.log(f("Run: d360-writer workspace use <name>"));return}let u=await si({message:"Select the Document360 workspace for this repo:",choices:o.map(m=>({name:`${Bo(m)}${m.id===c?" (current)":""}`,value:m.id}))}),g=o.find(m=>m.id===u);Nt(e,s,r,u,g?.name),console.log(D(`\u2713 Workspace set to "${g?.name??u}" for profile "${s}".`))}H();import ui from"picocolors";function di(e=process.env){return e.FORCE_HYPERLINK==="0"||!ui.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 pi(e,t=e,n){return di(n)?`\x1B]8;;${e}\x07${t}\x1B]8;;\x07`:t}var fi=/https?:\/\/[^\s\x1b]+/g;function ft(e,t){return e.replace(fi,n=>pi(n,n,t))}function Xo(e){return{...zo(e.idToken)??{},...zo(e.accessToken)??{}}}function Ot(e){let t=Xo(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 Ut(e){let t=Lt(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 gi(n,{manual:e.manual,promptForRedirect:s=>mi({message:s})},s=>console.log(ft(s))),r=Go(t.name,o);if(Yo(r),Sn(r,t.name,s=>console.log(f(s))),console.log(""),console.log(D(`\u2713 Logged in to "${t.name}" as ${Ot(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 Ze(process.cwd(),t.name))}catch{}}function Sn(e,t,n){let r=Xo(e).doc360_project_id;if(!(typeof r!="string"||!r))try{if(Lt(process.cwd(),t).project.projectId)return;$i(process.cwd(),t,{projectId:r}),n(` Project ${r} written to profile "${t}".`)}catch{}}async function Vo(e){let t=Lt(process.cwd(),e.profile),n=t.connection,o=wi(t.name);if(!o){console.log(x(`Not logged in to Document360 (profile "${t.name}").`)),console.log(f(`Run: d360-writer login --profile ${t.name}`)),process.exitCode=1;return}if(console.log(`Profile ${R(t.name)}${t.production?" \u26A0 PRODUCTION":""}: ${Ot(o)}`),yi(o))if(o.refreshToken)try{let r=Go(t.name,await hi(n,o.refreshToken));Yo(r),console.log(D(`\u2713 Session refreshed \u2014 expires ${r.expiresAt}`))}catch(r){console.log(L(`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(L("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 Jo(e){let t=Lt(process.cwd(),e.profile);ki(t.name)?console.log(D(`\u2713 Logged out of Document360 (profile "${t.name}").`)):console.log(f(`No Document360 session for profile "${t.name}" \u2014 nothing to do.`))}H();import{readProjectConfig as Ko,writeProjectConfig as bi,resolveActiveProfile as vi,loadTokens as Ci,isExpired as Pi}from"document360-engine";function Wt(e){let t=Ko(e);if(!t?.profiles||Object.keys(t.profiles).length===0){console.log(x("No profiles in .d360-writer.json. Run: d360-writer init")),process.exitCode=1;return}console.log("");for(let[n,o]of Object.entries(t.profiles)){let r=n===t.defaultProfile?R("\u25CF "):" ",s=o.production?L(" \u26A0 PRODUCTION"):"",c=o.connection.environment??"(inline)",u=Ci(n),g=u?Pi(u)&&!u.refreshToken?L("expired"):f("logged in"):f("not logged in");console.log(`${r}${R(n)} \u2192 ${c}${s} [${g}]`)}console.log(""),console.log(f("\u25CF = default. Switch with: d360-writer profile use <name>")),console.log("")}function Ft(e,t){let n=Ko(e);if(!n?.profiles?.[t]){let r=n?.profiles?Object.keys(n.profiles).join(", "):"(none \u2014 run init)";console.log(x(`Unknown profile "${t}". Available: ${r}`)),process.exitCode=1;return}n.defaultProfile=t,bi(n,e);let o=n.profiles[t].production?L(" \u26A0 PRODUCTION"):"";console.log(D(`\u2713 Default profile is now "${t}"${o}`))}function Ht(e,t){try{let n=vi(e,t);console.log(""),console.log(`Profile ${R(n.name)}${n.production?L(" \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(x(n.message)),process.exitCode=1}}H();import{existsSync as Si,readdirSync as Ti,statSync as Ri}from"node:fs";import{join as Ai}from"node:path";import{apiLogDir as ji}from"document360-engine";function Qo(){let e=ji();if(console.log(""),console.log(`Document360 API logs: ${R(e)}`),!Si(e)){console.log(f(" No logs yet \u2014 they appear after the first Document360 API call.")),console.log("");return}let t=Ti(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=(Ri(Ai(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("")}H();import{createSession as Ei,resolveAuth as Di,findByName as Ii,slugify as _i,touchSession as Mi,upsertSession as Ni,resolveActiveProfile as Li}from"document360-engine";async function Zo(e,t,n,o,r,s){let c=Di(n);c.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2)),c.kind==="subscription"&&console.error(f("Using your Claude subscription (no API key set)."));let u=null;try{u=Li(e,r)}catch(b){console.error(x(`Document360 profile error: ${b.message}`)),process.exit(2)}u.production&&(console.error(L(`\u26A0 Profile "${u.name}" is PRODUCTION.`)),s||(console.error(x("Refusing to run against a production profile without --yes.")),process.exit(2)),console.error(f(" --yes given \u2014 proceeding against production.")));let g;if(o){let b=Ii(e,o);b||(console.error(x(`No saved session matches "${o}" in this repo.`)),console.error(f("List sessions inside the REPL with /resume.")),process.exit(2)),g=b.uuid,console.error(f(`Resuming "${b.name}"`))}let m=Ei({cwd:e,resume:g,profileName:r,allowProdWrites:s===!0}),w=g??null,j=1;for await(let b of m.send(t))switch(b.type){case"session":if(!w){w=b.sessionId;let v=new Date().toISOString();Ni({uuid:w,name:_i(t),renamed:!1,titled:!1,cwd:e,firstPrompt:t,createdAt:v,updatedAt:v})}break;case"text":process.stdout.write(b.delta);break;case"tool":console.error(Q(` \u2699 ${b.name}`));break;case"result":j=b.ok?0:1,console.error(f(`(${b.inputTokens}\u2192${b.outputTokens} tokens`+(b.costUsd>0?`, $${b.costUsd<.01?b.costUsd.toFixed(4):b.costUsd.toFixed(2)}`:"")+")")),b.ok||console.error(x("agent finished with an error result"));break;case"error":console.error(""),console.error(x(`agent error: ${b.message}`)),process.exit(1)}w&&Mi(w),process.stdout.write(`
|
|
3
|
+
`),process.exit(j)}import{createInterface as Ta}from"node:readline/promises";import{createSession as fo,resolveAuth as Ra,getSession as Aa,setTitle as ja,slugify as Ea,touchSession as ps,upsertSession as Da,generateTitle as Ia,resolveActiveProfile as fs,resolveModelSetting as _a,readProjectConfig as Ma,decodeJwtClaims as us,isExpired as Na,loadTokens as La}from"document360-engine";var mt=[{name:"init",usage:"/init",desc:"Pick an environment & scaffold .d360-writer.json",group:"start"},{name:"login",usage:"/login",desc:"Sign in to Document360 (browser)",group:"start"},{name:"scope",usage:"/scope",desc:"Choose which repo folders back the docs (monorepos)",group:"start"},{name:"mcp",usage:"/mcp [list|add|remove]",desc:"Connect MCP servers (Notion, Linear, \u2026)",group:"start"},{name:"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:"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"}],Oi=[{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 Bt(){let e=Math.max(...mt.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 Oi){t.push("",o);for(let r of mt.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 er(e){if(!e.startsWith("/"))return[];let t=e.slice(1).toLowerCase().split(/\s/)[0]??"";return mt.filter(n=>n.name.startsWith(t))}function tr(e){return/<[^>]+>/.test(e.replace(/\[[^\]]*\]/g,""))}async function Tn(){console.log("");for(let e of Bt())console.log(e);return console.log(""),console.log("Reporting a problem? Run `d360-writer logs` for the API log files."),console.log(""),{kind:"continue"}}H();import{getSession as Ui}from"document360-engine";async function nr(e,t){let n=t.currentUuid(),o=n?Ui(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
|
|
8
|
-
`),"utf8")}async function
|
|
7
|
+
`)),{kind:"clear"}}async function Rn(){return{kind:"exit"}}H();import{input as gt,confirm as Wi}from"@inquirer/prompts";import{basename as or,join as qt}from"node:path";import{existsSync as et,readFileSync as Fi,readdirSync as Hi,writeFileSync as Bi}from"node:fs";import{writeProjectConfig as rr,readProjectConfig as qi,projectConfigPath as sr,writerDir as zi}from"document360-engine";function ir(e,t="berlin"){let n=sr(e);if(et(n))return{created:!1,path:n,profileName:""};let o={projectId:cr(e)??or(e),captureDir:"user-docs/_capture",outputDir:"user-docs/_screenshots",profiles:{[t]:{connection:{environment:t},production:!1}},defaultProfile:t,authoritativeSourceFiles:ur(e)};return rr(o,e),lr(e),{created:!0,path:n,profileName:t}}function lr(e){let t=qt(zi(e),".gitignore");et(t)||Bi(t,["# Local/transient \u2014 the config files here ARE meant to be committed.",".sessions/","cache/",""].join(`
|
|
8
|
+
`),"utf8")}async function ar(){let e=process.cwd(),t=sr(e);if(et(t)&&!await Wi({message:`${t} already exists. Overwrite?`,default:!1}))return console.log(f("init cancelled.")),{kind:"continue"};let n=cr(e)??or(e),o=await gt({message:"Project ID (used to scope sessions, screenshots, etc.):",default:n}),r=await gt({message:"Capture directory (where document360-capture .spec.ts files live):",default:"user-docs/_capture"}),s=await gt({message:"Screenshot output directory:",default:"user-docs/_screenshots"}),c=await gt({message:"Default connection profile name:",default:"berlin"}),u=await gt({message:"Document360 environment for this profile (baked preset):",default:"berlin"}),g={projectId:o,captureDir:r,outputDir:s,profiles:{[c]:{connection:{environment:u},production:!1}},defaultProfile:c,authoritativeSourceFiles:ur(e)},m=qi(e);return m?.terminologyGlossary&&(g.terminologyGlossary=m.terminologyGlossary),rr(g,e),lr(e),console.log(""),console.log(D(`\u2713 Wrote ${t}`)),console.log(""),console.log("Next:"),console.log(` ${R(`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 cr(e){let t=qt(e,"package.json");if(!et(t))return null;try{return JSON.parse(Fi(t,"utf8")).name??null}catch{return null}}function ur(e){let t=[];for(let n of["README.md","ARCHITECTURE.md","CLAUDE.md"])et(qt(e,n))&&t.push(n);for(let n of["src","api","docs"]){let o=qt(e,n);et(o)&&!Gi(o)&&t.push(n)}return t}function Gi(e){try{return Hi(e,{withFileTypes:!0}).filter(n=>n.isDirectory()&&!n.name.startsWith(".")).length>6}catch{return!1}}H();import{readMcpConfig as An,writeMcpConfig as dr}from"document360-engine";async function zt(e){let t=(e[0]??"").toLowerCase();return t==="list"||!t?(Yi(),{kind:"continue"}):t==="add"?(Xi(e.slice(1)),{kind:"continue"}):t==="remove"||t==="rm"?(Vi(e.slice(1)),{kind:"continue"}):(console.log(x(`Unknown /mcp subcommand: ${t}`)),console.log(f("Try: /mcp add <name> <stdio|http|sse> <command-or-url>, /mcp list, /mcp remove <name>")),{kind:"continue"})}function Yi(){let e=An(),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
|
|
11
|
-
`)}var
|
|
12
|
-
`)}async function
|
|
13
|
-
`),display:"/audit"}}H();import{checkbox as
|
|
14
|
-
`);return
|
|
15
|
-
`)||
|
|
16
|
-
`},r=
|
|
17
|
-
`),
|
|
18
|
-
`)}),
|
|
19
|
-
`)},
|
|
20
|
-
`)}function
|
|
21
|
-
`);return" \u2022 "+
|
|
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(` ${R(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(` ${R(n)} ${f(`(${o.type})`)} ${o.url}${s}`)}}console.log("")}function Xi(e){let[t,n,...o]=e;if(!t||!n||o.length===0){console.log(x("Usage: /mcp add <name> <stdio|http|sse> <command-or-url> [args...] [-H key:value ...]"));return}if(n!=="stdio"&&n!=="http"&&n!=="sse"){console.log(x(`Unknown transport: ${n}. Use stdio, http, or sse.`));return}let r=An(),s;if(n==="stdio")s={type:"stdio",command:o[0],args:o.slice(1)};else{let c={};for(let u=1;u<o.length;u++)if(o[u]==="-H"||o[u]==="--header"){let g=o[++u],m=g?.match(/^([^:=]+)[:=](.+)$/);if(!m){console.log(x(`-H expects key:value (no spaces). Got: ${g??"(nothing)"}`));return}c[m[1].trim()]=m[2].trim()}else{console.log(x(`Unexpected argument: ${o[u]}. 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,dr(r),console.log(""),console.log(D(`\u2713 Registered "${t}" (${n})`)),console.log(L(" 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 Vi(e){let t=e[0];if(!t){console.log(x("Usage: /mcp remove <name>"));return}let n=An();if(!n.servers[t]){console.log(x(`No server named "${t}".`));return}delete n.servers[t],dr(n),console.log(D(`\u2713 Removed "${t}".`)),console.log(f(" Run /clear to drop it from the current session immediately."))}H();import{select as Ji}from"@inquirer/prompts";import{computeSyncStatus as pr}from"document360-engine";function Yt(e){return[`Run the publish-to-d360 skill for the article at: ${e}`,"","Steps you must follow:","1. Read the article markdown \u2014 use the repo-relative path exactly as given above (it resolves against the working directory; do not reconstruct an absolute path).","2. Compute the D360 publish form (strip the YAML frontmatter and the H1 title, strip every <!-- SCREENSHOT ... --> block while keeping the visible [Screenshot: ...] line, normalize cross-article links).","3. Look up the article ID in <repo>/d360-category-map.json. If present, call the document360 MCP update-article tool. If absent, call create-article with local_path set to the repo-relative .md path (the tool records the new ID + sync base in the category map itself \u2014 do not edit the articles map by hand).","4. Report what changed and remind me to publish the draft manually in the Document360 portal."].join(`
|
|
11
|
+
`)}var fr={"local-ahead":"modified locally","untracked-local":"new (never published)",conflict:"\u26A0 conflict \u2014 publishing overwrites the portal edit"};function Gt(e){return e.filter(t=>t.path!==null&&t.status in fr).map(t=>({path:t.path,label:fr[t.status]}))}function ht(e){return[`Run the publish-to-d360 skill for ALL of these ${e.length} articles, one by one:`,"",...e.map(t=>`- ${t}`),"","Use the repo-relative paths exactly as listed (they resolve against the working directory; do not reconstruct absolute paths). Apply the normal per-article publish flow (frontmatter/H1/screenshot-comment strips, link normalization; update when mapped, create with local_path when not). If one article fails, note it and continue. Finish with a summary table (article | created/updated | failures) and remind me drafts need review in the portal."].join(`
|
|
12
|
+
`)}async function mr(e,t){let n=e[0];if(n==="--all"){console.log(f("Checking what needs publishing\u2026"));try{let o=Gt((await pr({cwd:t?.cwd??process.cwd()})).entries);return o.length===0?(console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),{kind:"continue"}):{kind:"forward-to-agent",prompt:ht(o.map(r=>r.path)),display:"/publish --all"}}catch(o){return console.log(x(`Could not compute sync status: ${o.message}`)),{kind:"continue"}}}if(!n){console.log(f("Checking what needs publishing\u2026"));let o;try{o=Gt((await pr({cwd:t?.cwd??process.cwd()})).entries)}catch(r){return console.log(x(`Could not compute sync status: ${r.message}`)),console.log(f("Publish a specific article: /publish <article-path>")),{kind:"continue"}}if(o.length===0)return console.log(D("\u2713 Nothing is ahead of Document360 \u2014 no publish candidates.")),console.log(f(" (Articles without a sync base are unverified \u2014 publish those by path if needed.)")),{kind:"continue"};if(!process.stdin.isTTY||!t){for(let r of o)console.log(` ${r.path} ${f(`(${r.label})`)}`);return console.log(f("Run: /publish <article-path>")),{kind:"continue"}}try{n=await t.withPausedInput(()=>Ji({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{kind:"forward-to-agent",prompt:ht(o.map(r=>r.path)),display:"/publish --all"}}return{kind:"forward-to-agent",prompt:Yt(n),display:`/publish ${n}`}}async function Xt(){return{kind:"forward-to-agent",prompt:["Run the gap-analysis skill against this repo.","","Follow its stages in order:","1. d360_sync_status first \u2014 stop and report if any article is in conflict or remote-ahead (the local copy cannot be trusted until resolved).","2. git diff since the lastAnalyzedCommit marker in d360-category-map.json (or last 30 days if no marker), filtered to the authoritativeSourceFiles paths.","3. Map changed files to articles via their `sources:` frontmatter; read only implicated articles and changed source files. If articles are missing `sources:`, this is the one-time bootstrap pass \u2014 backfill them.","4. Output the proposal table: | Article (path or proposed title) | Action (create | update | retire | adopt) | Reason | Evidence | Scope |, then advance lastAnalyzedCommit.","","Do not start writing or updating any article yet \u2014 the proposal table is the deliverable."].join(`
|
|
13
|
+
`),display:"/audit"}}H();import{checkbox as Ki}from"@inquirer/prompts";import{inventoryRepo as Qi,readProjectConfig as gr,writeProjectConfig as Zi}from"document360-engine";function jn(e,t){let n=gr(e);n&&(n.authoritativeSourceFiles=t,Zi(n,e))}function Vt(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 n=t?.cwd??process.cwd();if(!gr(n))return console.log(x("No .d360-writer.json here. Run /init first.")),{kind:"continue"};let o=Qi(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(R("Recommended documentation scope (run /scope in the REPL to choose):"));for(let s of o)console.log(` ${s.recommended?"\u25C9":"\u25CB"} ${s.path} ${f(Vt(s))}`);return console.log(""),{kind:"continue"}}let r;try{r=await t.withPausedInput(()=>Ki({message:"Which folders back the user docs? (space toggles, enter confirms)",choices:o.map(s=>({name:`${s.path} (${Vt(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"};jn(n,r),console.log(D(`\u2713 Scoped to ${r.length} folder(s) \u2014 written to .d360-writer.json`));for(let s of r)console.log(` ${s}`);return console.log(""),{kind:"continue"}}H();import{confirm as rl}from"@inquirer/prompts";import{applyPull as xr,computeSyncStatus as br,planPull as sl,D360AuthError as il}from"document360-engine";H();var kr=[{status:"conflict",header:"Conflicts \u2014 both sides changed; pick a direction (/sync pull <path> or /publish <path>):",paint:x,mark:"!"},{status:"local-ahead",header:"Local ahead \u2014 push with /publish <path>:",paint:L,mark:"M"},{status:"remote-ahead",header:"Remote ahead \u2014 pull with /sync pull <path>:",paint:R,mark:"M"},{status:"deleted-local",header:"Deleted locally \u2014 still on Document360 (pull to restore, or remove from the map):",paint:x,mark:"D"},{status:"deleted-remote",header:"Deleted on Document360 \u2014 still local (publish to recreate, or delete the file):",paint:x,mark:"D"},{status:"orphaned",header:"Orphaned map entries \u2014 gone on both sides (remove from d360-category-map.json):",paint:Q,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:Q,mark:"\xB7"}];function el(e){return e.path?e.path:`${e.title??"(untitled)"} ${Q(`[${e.articleId}]`)}`}function Jt(e){let t=[];for(let c of kr){let u=e.entries.filter(g=>g.status===c.status);if(u.length!==0){t.push(""),t.push(ve(c.header));for(let g of u)t.push(` ${c.paint(c.mark)} ${c.paint(el(g))}${g.detail?Q(` (${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?D("\u2713 everything in sync"):[n>0?D(`\u2713 ${n} in sync`):null,...kr.map(c=>{let u=e.counts[c.status]??0;return u>0?`${u} ${r[c.status]}`:null})].filter(Boolean).join(Q(" \xB7 "));return t.push(s+Q(` \xB7 ${o} tracked+seen \xB7 profile "${e.profile}" \xB7 docs root ${e.docsRoot}/`)),t}import{structuredPatch as tl}from"diff";import yr from"picocolors";var wr=80,nl="\x1B[48;2;74;28;28m",ol="\x1B[48;2;24;66;24m",$r="\x1B[49m",En=e=>String(e).padStart(5);function We(e,t,n){let o=b=>{let v=b.replace(/\r\n/g,`
|
|
14
|
+
`);return v.endsWith(`
|
|
15
|
+
`)||v===""?v:v+`
|
|
16
|
+
`},r=o(e),s=o(t);if(r===s)return null;let c=Math.max(20,n-10),u=tl("a","b",r,s,"","",{context:3}),g=0,m=0,w=[];u.hunks.forEach((b,v)=>{v>0&&w.push(yr.gray(" \u2026"));let $=b.oldStart,E=b.newStart;for(let q of b.lines){let G=q[0],ie=q.slice(1).slice(0,c);G==="-"?(m++,w.push(`${nl}${En($++)} - ${ie}${$r}`)):G==="+"?(g++,w.push(`${ol}${En(E++)} + ${ie}${$r}`)):(w.push(yr.gray(En(E))+" "+ie),$++,E++)}});let j=w.slice(0,wr);return{added:g,removed:m,lines:j,hidden:Math.max(0,w.length-wr)}}async function vr(e,t){let n=(e[0]??"status").toLowerCase();try{if(n==="status")return await ll(t.cwd),{kind:"continue"};if(n==="pull")return await al(t,e.slice(1)),{kind:"continue"};console.log(x(`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 il?console.log(x(o.message)):console.log(x(`Sync failed: ${o.message}`))}return{kind:"continue"}}async function ll(e){console.log(f("Checking Document360 for drift\u2026"));let t=await br({cwd:e});for(let n of Jt(t))console.log(n);console.log("")}async function al(e,t){let n=t[0];if(!n){console.log(x("Usage: /sync pull <article-path> | --all"));return}let o;if(n==="--all"){if(console.log(f("Checking Document360 for drift\u2026")),o=(await br({cwd:e.cwd})).entries.filter(s=>s.status==="remote-ahead"&&s.path).map(s=>s.path),o.length===0){console.log(D("\u2713 Nothing is remote-ahead \u2014 no pulls needed.")),console.log(f(" (conflicts are never bulk-pulled; pull them one by one: /sync pull <path>)"));return}console.log(`${ve(String(o.length))} article(s) are remote-ahead.`)}else o=[n.replace(/\\/g,"/")];for(let r of o){let s=await sl({cwd:e.cwd,relPath:r});console.log(""),console.log(`${R("\u25CF")} ${ve(s.title)} ${Q(`(${s.path})`)}`);for(let m of s.notes)console.log(L(` \u26A0 ${m}`));s.overwritesLocalChanges&&console.log(L(" \u26A0 This OVERWRITES local edits made since the last sync."));let c=We(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.")),xr({cwd:e.cwd},s);continue}let u=m=>m===1?"":"s";console.log(Q(` \u23BF Added ${c.added} line${u(c.added)}, removed ${c.removed} line${u(c.removed)}`));for(let m of c.lines)console.log(m);if(c.hidden>0&&console.log(f(` \u2026 +${c.hidden} more diff lines`)),!await e.withPausedInput(()=>rl({message:`Write ${s.path}?`,default:!s.overwritesLocalChanges}))){console.log(f(" Skipped."));continue}xr({cwd:e.cwd},s),console.log(D(` \u2713 Pulled ${s.path} (sync base advanced).`))}console.log("")}H();import{statSync as wl}from"node:fs";import{resolve as $l}from"node:path";import{estimateBulkCost as xl,planPartitions as bl,readProjectConfig as vl,resolveModelForOperation as Cl,runPartitioned as Pl,trackedArticlePaths as Sl}from"document360-engine";import B from"picocolors";import Ar from"wrap-ansi";import Ln from"string-width";H();import je from"picocolors";import Dn from"wrap-ansi";import In from"string-width";var Cr=e=>/^\s*(-{3,}|\*{3,}|_{3,})\s*$/.test(e),Pr=e=>/^\s*\|?[\s:|-]*-[\s:|-]*\|?\s*$/.test(e)&&e.includes("-"),Sr=e=>e.replace(/^\s*\|/,"").replace(/\|\s*$/,"").split("|").map(t=>t.trim());function cl(e){let t=e.replace(/\r/g,"").split(`
|
|
17
|
+
`),n=[],o=0;for(;o<t.length;){let r=t[o];if(/^\s*```/.test(r)){let u=[];for(o++;o<t.length&&!/^\s*```/.test(t[o]);)u.push(t[o++]);o++,n.push({kind:"code",lines:u});continue}if(Cr(r)){n.push({kind:"hr"}),o++;continue}if(r.includes("|")&&o+1<t.length&&Pr(t[o+1])){let u=Sr(r);o+=2;let g=[];for(;o<t.length&&t[o].includes("|")&&t[o].trim()!=="";)g.push(Sr(t[o++]));n.push({kind:"table",header:u,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 u=[];for(;o<t.length&&/^\s*([-*]|\d+\.)\s+/.test(t[o]);)u.push(t[o++].replace(/^\s*([-*]|\d+\.)\s+/,""));n.push({kind:"list",items:u});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])&&!Cr(t[o])&&!(t[o].includes("|")&&o+1<t.length&&Pr(t[o+1]));)c.push(t[o++]);n.push({kind:"para",text:c.join(" ")})}return n}function _n(e){return e.replace(/(\*\*[^*]+\*\*|`[^`]+`|\*[^*]+\*)/g,t=>t.startsWith("**")?je.bold(t.slice(2,-2)):t.startsWith("`")?R(t.slice(1,-1)):je.italic(t.slice(1,-1)))}var ul=(e,t)=>e+" ".repeat(Math.max(0,t-In(e)));function dl(e,t,n){let o=e.length,r=e.map(($,E)=>Math.max(In($),...t.map(q=>In(q[E]??"")))),s=Math.max(24,n),c=3*o+1,u=[...r],g=()=>u.reduce(($,E)=>$+E,0)+c,m=0;for(;g()>s&&m++<1e4;){let $=-1,E=6;for(let q=0;q<o;q++)u[q]>E&&(E=u[q],$=q);if($===-1)break;u[$]-=1}let w=($,E,q)=>je.gray($+u.map(G=>"\u2500".repeat(G+2)).join(E)+q),j=je.gray("\u2502"),b=($,E)=>{let q=u.map((de,Pe)=>{let pe=$[Pe]??"",fe=E?je.bold(pe):_n(pe);return Dn(fe,de,{hard:!0}).split(`
|
|
18
|
+
`)}),G=Math.max(...q.map(de=>de.length)),ie=[];for(let de=0;de<G;de++)ie.push(u.map((Pe,pe)=>`${j} ${ul(q[pe][de]??"",Pe)} `).join("")+j);return ie.join(`
|
|
19
|
+
`)},v=[w("\u250C","\u252C","\u2510"),b(e,!0)];return v.push(t.length===0?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524")),t.forEach(($,E)=>{v.push(b($,!1)),v.push(E===t.length-1?w("\u2514","\u2534","\u2518"):w("\u251C","\u253C","\u2524"))}),v.join(`
|
|
20
|
+
`)}function pl(e,t){switch(e.kind){case"heading":return je.bold(e.text);case"hr":return je.gray("\u2500".repeat(t));case"para":return Dn(_n(e.text),t);case"list":return e.items.map(n=>{let[o="",...r]=Dn(_n(n),Math.max(10,t-4)).split(`
|
|
21
|
+
`);return" \u2022 "+o+r.map(s=>`
|
|
22
22
|
`+s).join("")}).join(`
|
|
23
|
-
`);case"code":return e.lines.map(
|
|
24
|
-
`);case"table":return
|
|
23
|
+
`);case"code":return e.lines.map(n=>je.gray(" "+n)).join(`
|
|
24
|
+
`);case"table":return dl(e.header,e.rows,t)}}function Mn(e,t){let n=Math.max(20,t);return cl(e).map(o=>pl(o,n)).join(`
|
|
25
25
|
|
|
26
|
-
`)}H();import
|
|
27
|
-
`);let r=2,s=3,c=
|
|
28
|
-
`);let
|
|
29
|
-
`)}var
|
|
30
|
-
`).flatMap(
|
|
31
|
-
`)),
|
|
32
|
-
`+
|
|
26
|
+
`)}H();import fl from"picocolors";var ml=[127,86,217],gl=[22,38,43],Tr={T:ml,E:gl},kt=["..TTTTTT....","..TTTTTTT...","..TTTTTTTT..","..TTETTETT..","..TTETTETT..","..TTTTTTTT..","..TTTTTTT...","..TTTTTT....","...T..T....."],Nn=([e,t,n])=>`\x1B[38;2;${e};${t};${n}m`,hl=([e,t,n])=>`\x1B[48;2;${e};${t};${n}m`;function Rr(){if(!fl.isColorSupported)return[];let e=kt[0].length,t=[];for(let n=0;n<kt.length;n+=2){let o="";for(let r=0;r<e;r++){let s=Tr[kt[n][r]],c=n+1<kt.length?Tr[kt[n+1][r]]:void 0;s&&c?o+=`${Nn(s)}${hl(c)}\u2580\x1B[49m\x1B[39m`:s?o+=`${Nn(s)}\u2580\x1B[39m`:c?o+=`${Nn(c)}\u2584\x1B[39m`:o+=" "}t.push(o)}return t}function yt(e){return e<60?`${e}s`:`${Math.floor(e/60)}m ${e%60}s`}function ue(e){return e<=0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function nt(e){return e<1e3?`${e} tokens`:e<1e6?`${(e/1e3).toFixed(1)}k tokens`:`${(e/1e6).toFixed(2)}M tokens`}var tt=(e,t)=>` ${e.padEnd(13)}${t}`;function kl(e,t){let n=[pt("\u270E document360-writer")+B.gray(` v${e.version}`),B.gray(" Reads your code, writes your docs."),"",tt("Claude:",`${e.claude}${B.gray(` \xB7 ${e.model}${e.modelSource?` (${e.modelSource})`:""}`)}`),tt("Document360:",e.configured?e.loggedOut?B.yellow("not logged in \u2014 run /login"):`${e.who??""}${e.sessionHint?B.gray(` (${e.sessionHint})`):""}`:B.yellow("not set up \u2014 run /init")),tt("Profile:",e.configured?`${e.profile}${B.gray(` (${e.apiUrl})`)}${e.prod?B.bold(B.yellow(" \u26A0 PRODUCTION")):""}`:B.gray("\u2014 (run /init)")),tt("Project:",e.project),tt("Mode:",B.gray(e.mode)),tt("cwd:",B.gray(e.cwd))],o=Rr();if(o.length===0)return n.join(`
|
|
27
|
+
`);let r=2,s=3,c=Ln(o[0]);if(!(t>=r+c+s+Math.max(...n.map(w=>Ln(w)))))return[...o.map(w=>" "+w),...n].join(`
|
|
28
|
+
`);let g=Math.max(0,Math.floor((n.length-o.length)/2)),m=[];for(let w=0;w<Math.max(o.length+g,n.length);w++){let j=o[w-g]??" ".repeat(c);m.push((" ".repeat(r)+j+" ".repeat(s)+(n[w]??"")).trimEnd())}return m.join(`
|
|
29
|
+
`)}var yl={error:B.red,warn:B.yellow,ok:B.green,info:B.gray};function On(e,t){let n=Math.max(20,t);switch(e.kind){case"banner":return kl(e.info,n);case"user":{let o="\x1B[48;2;42;42;46m",r="\x1B[49m",s=Math.max(10,n-4),c=50,u=e.text.split(`
|
|
30
|
+
`).flatMap(m=>Ar(m,s,{hard:!0}).split(`
|
|
31
|
+
`)),g=Math.max(0,u.length-c);return g>0&&(u=[...u.slice(0,c),B.dim(`\u2026 +${g} more lines`)]),`
|
|
32
|
+
`+u.map((m,w)=>o+(w===0?R(" \u276F "):" ")+m+" ".repeat(Math.max(0,s-Ln(m))+1)+r).join(`
|
|
33
33
|
`)}case"assistant":return`
|
|
34
|
-
`+
|
|
35
|
-
`+
|
|
36
|
-
`)}case"diff":{let
|
|
37
|
-
`)}case"link":return e.lines.map(
|
|
34
|
+
`+Mn(e.text,n);case"tool":{let o=e.arg!==null?B.gray(`${e.sep}(${e.arg})`):"";return`
|
|
35
|
+
`+Ar(B.green("\u25CF ")+B.bold(e.title)+o,n)}case"tool-result":{let o=e.isError?B.red:B.gray,r=e.lines.map((s,c)=>o((c===0?" \u23BF ":" ")+s));return e.hidden>0&&r.push(B.dim(` \u2026 +${e.hidden} lines`)),r.join(`
|
|
36
|
+
`)}case"diff":{let o=c=>c===1?"":"s",s=[B.gray(` \u23BF Added ${e.added} line${o(e.added)}, removed ${e.removed} line${o(e.removed)}`),...e.lines];return e.hidden>0&&s.push(B.dim(` \u2026 +${e.hidden} more diff lines`)),s.join(`
|
|
37
|
+
`)}case"link":return e.lines.map(o=>R(ft(` \u2B95 ${o}`))).join(`
|
|
38
38
|
`);case"preview":return`
|
|
39
|
-
`+
|
|
39
|
+
`+pt(`\u25A3 Preview \u2014 ${e.name}`)+`
|
|
40
40
|
|
|
41
|
-
`+
|
|
42
|
-
`+
|
|
43
|
-
`+(e.ok?
|
|
44
|
-
`)}var
|
|
41
|
+
`+Mn(e.text,n);case"note":return`
|
|
42
|
+
`+yl[e.tone](ft(e.text));case"done":return`
|
|
43
|
+
`+(e.ok?B.magenta("\u2736 "):B.red("\u2736 "))+B.gray(`Cooked for ${yt(e.seconds)} \xB7 ${e.tokens} tokens`+(e.costUsd>0?` \xB7 ${ue(e.costUsd)}`:""))}}function jr(e,t){return e.map(n=>On(n,t)).join(`
|
|
44
|
+
`)}var Un=3;function Wn(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 Fn(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function Hn(e,t){return t.map(n=>{let o=0;try{o=wl($l(e,n)).size}catch{o=0}return{path:n,bytes:o}})}function Bn(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 Bo(e,t,o){let n=e.reduce((d,h)=>d+h.paths.length,0),[r,s]=t.usd;return[`Convert ${n} article${n===1?"":"s"} to DFM across ${e.length} partition${e.length===1?"":"s"} (\u2264${o} agents at once):`,...e.map(d=>` \u2022 ${d.label} \u2014 ${d.paths.length} article${d.paths.length===1?"":"s"}`),"",`Estimated cost: ${Oe(r)}\u2013${Oe(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function qo(e,t,o){let n=P=>t[P.index]?.paths.length??0,r=e.filter(P=>P.ok),s=e.filter(P=>!P.ok),c=e.reduce((P,M)=>P+n(M),0),d=r.reduce((P,M)=>P+n(M),0),h=e.reduce((P,M)=>P+M.costUsd,0),g=e.reduce((P,M)=>P+M.outputTokens,0),w=o==="api"?`${Oe(h)} total`:Xt(g),A=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,$=P=>`${P} article${P===1?"":"s"}`,j=[];if(s.length===0)j.push(`Converted ${$(c)} (${A}) successfully \xB7 ${w}.`);else{j.push(`Converted ${d}/${$(c)} (${A}) \xB7 ${w}.`),j.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let P of s)j.push(` \u2717 ${P.label}${P.error?` \u2014 ${P.error}`:""}`)}return j}async function $r(e,t){if(!ll(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:o,run:n}=Uo(e),r=ul(t.cwd,t.profileName);if(r.length===0)return console.log(x("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Wo(r,o);if(s.length===0)return console.log(x(`No tracked articles under "${o}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=il(s),d=`/convert${o?` --scope ${o}`:""} --run`,{model:h,forced:g}=al(t.cwd,"light");if(!n){let w=sl({files:Fo(t.cwd,s),op:"convert",model:h});o&&console.log(f(`Scope: ${o} (${s.length} of ${r.length} tracked articles).`));for(let A of Bo(c,w,Oo))console.log(A);return console.log(f(`Model: ${h}${g?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${d} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Oo} agents at once) on ${h}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let w of cl({cwd:t.cwd,partitions:c,promptFor:Ho,concurrency:Oo,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:h}))if(w.type==="partition_status")w.status==="running"?console.log(f(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(D(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let A=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let $ of qo(w.results,c,A))console.log(w.ok?D($):N($))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}H();import{search as dl}from"@inquirer/prompts";import{findByName as pl,getSession as fl,listSessions as ml,relativeTime as vr}from"document360-engine";async function Cr(e,t){let o=ml(t.cwd).filter(n=>n.uuid!==t.currentUuid());if(o.length===0)return console.log(f("No saved sessions for this repo yet \u2014 sessions auto-save as you work.")),{kind:"continue"};if(e.length>0){let n=e.join(" "),r=pl(t.cwd,n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${n}".`)),br(o),{kind:"continue"})}if(!process.stdin.isTTY)return br(o),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let n=await t.withPausedInput(()=>dl({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return o.filter(d=>!c||d.name.toLowerCase().includes(c)||d.firstPrompt.toLowerCase().includes(c)).map(d=>({name:`${d.name} ${vr(d.updatedAt)}`,value:d.uuid,description:d.firstPrompt.slice(0,100)}))}})),r=fl(n);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function br(e){console.log("");for(let t of e.slice(0,15))console.log(` ${S(t.name)} ${f(vr(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as gl}from"document360-engine";function zo(e){let t=e.trim();return t.length>=2&&(t[0]==='"'&&t.endsWith('"')||t[0]==="'"&&t.endsWith("'"))?t.slice(1,-1).trim():t}async function Sr(e,t){let o=zo(e.join(" "));if(!o)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let n=t.currentUuid();return n?(gl(n,o)?console.log(D(`\u2713 Session renamed to "${o}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Tr,readProjectConfig as hl,writeProjectConfig as kl}from"document360-engine";H();function Go(e,t,o){if(!t)return"Usage: /profile add <name> [environment]";let n=hl(e);if(!n)return"No .d360-writer.json \u2014 run /init first.";if(n.profiles?.[t])return`Profile "${t}" already exists.`;let r=o??t;return Tr().includes(r)?(n.profiles={...n.profiles,[t]:{connection:{environment:r},production:!1}},kl(n,e),null):`Unknown environment "${r}". Known: ${Tr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Pr(e,t){let o=e[0];if(!o)return Lt(t.cwd),{kind:"continue"};if(o==="add"){let n=Go(t.cwd,e[1],e[2]);return n?(console.log(x(n)),{kind:"continue"}):(console.log(D(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${S(`/profile ${e[1]}`)} then ${S("/login")}`),{kind:"continue"})}return Ot(t.cwd,o),Ut(t.cwd,o),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as yl}from"@inquirer/prompts";import{readProjectConfig as wl,readUserConfig as Rr,resolveModelSetting as Yo,writeUserConfig as Ar}from"document360-engine";var de=[{value:null,label:"Auto",desc:"Engine right-sizes per task \u2014 Sonnet for routine work, Opus for analysis (recommended)"},{value:"claude-fable-5",label:"Fable",desc:"Fable 5 \xB7 most capable, for the hardest and longest-running tasks"},{value:"opus",label:"Opus",desc:"Opus 4.8 \xB7 best for everyday complex tasks"},{value:"sonnet",label:"Sonnet",desc:"Sonnet 4.6 \xB7 efficient for routine tasks"},{value:"haiku",label:"Haiku",desc:"Haiku 4.5 \xB7 fastest for quick answers"}];function Vt(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),o=de.findIndex(n=>n.value!==null&&(n.value===t||n.label.toLowerCase()===t||t.includes(n.label.toLowerCase())));return o>=0?o:0}function 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 pt(e,t){let o=()=>{let s=Yo(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Rr();return s.defaultModel?(delete s.defaultModel,Ar(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${Yo(e).model??"Claude Code default"} (applies from your next message)`],changed:!0,effective:o()}):{lines:["No personal model override set \u2014 nothing to clear."],changed:!1,effective:void 0}}Ar({...Rr(),defaultModel:t});let n=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=wl(e)?.defaultModel;return r&&n.push(`\u26A0 .d360-writer.json sets defaultModel "${r}" \u2014 the team setting overrides yours until it is removed.`),{lines:n,changed:!0,effective:o()}}async function jr(e,t){let o=e[0]?.trim();if(!o){let c=Yo(t.cwd);if(!process.stdin.isTTY)return console.log(`${we("Model:")} ${S(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 d=Vt(c),h;try{h=await t.withPausedInput(()=>yl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:de[d].value,choices:de.map(($,j)=>({name:`${$.label}${j===d?" \u2714":""}`,value:$.value,description:$.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:g,changed:w,effective:A}=pt(t.cwd,h??"default");for(let $ of g)console.log($.startsWith("\u26A0")?N($):$.startsWith("\u2713")?D($):f($));return w&&await t.setModel(A),{kind:"continue"}}let{lines:n,changed:r,effective:s}=pt(t.cwd,o);for(let c of n)console.log(c.startsWith("\u26A0")?N(c):c.startsWith("\u2713")?D(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Jt();async function Mr(e,t){return await t.withPausedInput(()=>Je(t.cwd)),{kind:"clear"}}H();import{resolveActiveProfile as El}from"document360-engine";async function Nr(e,t){let o=!1;try{o=El(t.cwd).production}catch{}return o?(console.log(N("\u26A0 Authorizing writes to the PRODUCTION profile for this session.")),{kind:"allow-prod"}):(console.log(f("Current profile is not a production profile \u2014 writes are already allowed.")),{kind:"continue"})}H();var Lr=async(e,t)=>{try{await t.withPausedInput(()=>Nt({}))}catch(o){console.log(x(`Login failed: ${o.message}`))}return{kind:"continue"}};import{existsSync as Or}from"node:fs";import{isAbsolute as Dl,join as Il,resolve as _l}from"node:path";import{readProjectConfig as Ml,screenshotPlaceholderIds as Nl}from"document360-engine";var Ur=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function Vo(e){let t=!e.includes("--no-setup"),o=e.filter(r=>r!=="--no-setup");if(o[0]==="--list")return{mode:"list",scope:o[1]?Ur(o[1]):void 0};let n=o[0];return!n||n==="--all"?{mode:"all",setup:t}:/[\\/]/.test(n)||n.endsWith(".md")?{mode:"scope",scope:Ur(n),setup:t}:{mode:"single",id:n}}function Jo(e,t){let o=Ml(e),n=o?.captureDir??"user-docs/_capture",r=o?.outputDir??"user-docs/_screenshots",s=(c,d)=>Dl(c)?Il(c,d):_l(e,c,d);return Nl(e,{scope:t}).map(({id:c,file:d})=>{let h=Or(s(r,`${c}.png`))?"captured":Or(s(n,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:d,state:h}})}var Ll={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},Ol={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function Ko(e,t){if(e.length===0)return[t?`No screenshot placeholders under ${t}.`:"No screenshot placeholders found in the docs."];let o=[...new Set(e.map(s=>s.file))].sort(),n=e.filter(s=>s.state==="captured").length,r=[`Screenshots: ${e.length} placeholder${e.length===1?"":"s"} across ${o.length} article${o.length===1?"":"s"} \xB7 ${n} captured${t?` \xB7 scope ${t}`:""}`,""];for(let s of o){r.push(s);for(let c of e.filter(d=>d.file===s))r.push(` ${Ll[c.state]} ${c.id.padEnd(34)} ${Ol[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function Wr(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
|
|
50
|
+
`)}function qn(e,t,n){let o=e.reduce((u,g)=>u+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(u=>` \u2022 ${u.label} \u2014 ${u.paths.length} article${u.paths.length===1?"":"s"}`),"",`Estimated cost: ${ue(r)}\u2013${ue(s)}. ${t.note}`,"","Each article is rewritten and re-published as a DRAFT. Run /convert --run to start."]}function zn(e,t,n){let o=$=>t[$.index]?.paths.length??0,r=e.filter($=>$.ok),s=e.filter($=>!$.ok),c=e.reduce(($,E)=>$+o(E),0),u=r.reduce(($,E)=>$+o(E),0),g=e.reduce(($,E)=>$+E.costUsd,0),m=e.reduce(($,E)=>$+E.outputTokens,0),w=n==="api"?`${ue(g)} total`:nt(m),j=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=$=>`${$} article${$===1?"":"s"}`,v=[];if(s.length===0)v.push(`Converted ${b(c)} (${j}) successfully \xB7 ${w}.`);else{v.push(`Converted ${u}/${b(c)} (${j}) \xB7 ${w}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /convert to retry:`);for(let $ of s)v.push(` \u2717 ${$.label}${$.error?` \u2014 ${$.error}`:""}`)}return v}async function Er(e,t){if(!vl(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let{scope:n,run:o}=Wn(e),r=Sl(t.cwd,t.profileName);if(r.length===0)return console.log(x("No tracked articles in d360-category-map.json. Publish some first (/publish), then /convert.")),{kind:"continue"};let s=Fn(r,n);if(s.length===0)return console.log(x(`No tracked articles under "${n}". (${r.length} are tracked overall.)`)),{kind:"continue"};let c=bl(s),u=`/convert${n?` --scope ${n}`:""} --run`,{model:g,forced:m}=Cl(t.cwd,"light");if(!o){let w=xl({files:Hn(t.cwd,s),op:"convert",model:g});n&&console.log(f(`Scope: ${n} (${s.length} of ${r.length} tracked articles).`));for(let j of qn(c,w,Un))console.log(j);return console.log(f(`Model: ${g}${m?" (forced)":" \u2014 mechanical work; /model to override"}.`)),console.log(f(`Run ${u} to start.`)),console.log(""),{kind:"continue"}}console.log(f(`Converting ${s.length} articles across ${c.length} partitions (\u2264${Un} 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 w of Pl({cwd:t.cwd,partitions:c,promptFor:Bn,concurrency:Un,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:g}))if(w.type==="partition_status")w.status==="running"?console.log(f(` \u25B8 ${w.label} \u2014 converting\u2026`)):w.status==="done"?console.log(D(` \u2713 ${w.label}`)):console.log(x(` \u2717 ${w.label}`));else if(w.type==="run_done"){console.log("");let j=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let b of zn(w.results,c,j))console.log(w.ok?D(b):L(b))}}catch(w){console.log(x(`Convert run failed: ${w.message}`))}return console.log(""),{kind:"continue"}}H();import{statSync as Dr}from"node:fs";import{resolve as Ir}from"node:path";import{estimateBulkCost as Tl,planPartitions as Rl,readDocsPlan as Al,readProjectConfig as _r,resolveModelForOperation as jl,runPartitioned as El}from"document360-engine";var Gn=5;function Yn(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 Dl(e,t){let n=e.replace(/\\/g,"/").replace(/\/+$/,""),o=`${t.replace(/\/+$/,"")}/`;return n.startsWith(o)?n.slice(o.length):n}function Il(e,t){return t?e.filter(n=>{let o=n.replace(/\\/g,"/");return o===t||o.startsWith(`${t}/`)}):e}function _l(e,t,n){return n.filter(o=>{try{return Dr(Ir(e,t,o)).size===0}catch{return!0}})}function Xn(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+=Dr(Ir(e,c)).size}catch{}return{path:r,bytes:s}})}function Vn(e,t){return`Run the write-article skill to author EACH documentation article listed below. For each path:
|
|
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
|
+
- 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
|
+
- 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
|
+
- Emit \`<!-- SCREENSHOT -->\` placeholders where a visual helps, but do NOT author capture specs now.
|
|
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
|
+
Articles:
|
|
57
|
+
`+e.paths.map(n=>`- ${n}`).join(`
|
|
58
|
+
`)}function Jn(e,t,n){let o=e.reduce((c,u)=>c+u.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: ${ue(r)}\u2013${ue(s)}. ${t.note}`,"","Articles are written as local drafts in the docs tree (nothing is published). Run /write --run to start."]}function Kn(e,t,n){let o=$=>t[$.index]?.paths.length??0,r=e.filter($=>$.ok),s=e.filter($=>!$.ok),c=e.reduce(($,E)=>$+o(E),0),u=r.reduce(($,E)=>$+o(E),0),g=e.reduce(($,E)=>$+E.costUsd,0),m=e.reduce(($,E)=>$+E.outputTokens,0),w=n==="api"?`${ue(g)} total`:nt(m),j=`${r.length}/${e.length} partition${e.length===1?"":"s"} completed`,b=$=>`${$} article${$===1?"":"s"}`,v=[];if(s.length===0)v.push(`Wrote ${b(c)} (${j}) successfully \xB7 ${w}.`),v.push("Review them in the docs tree, then /publish when ready.");else{v.push(`Wrote ${u}/${b(c)} (${j}) \xB7 ${w}.`),v.push(`${s.length} partition${s.length===1?"":"s"} failed \u2014 re-run /write to retry the rest:`);for(let $ of s)v.push(` \u2717 ${$.label}${$.error?` \u2014 ${$.error}`:""}`)}return v}function Qn(e,t,n){let o=(_r(e)?.docsDir??"user-docs").replace(/\/+$/,""),r=t.map(u=>u.path);if(n.path){let u=Dl(n.path,o);return r.includes(u)?{docsDir:o,targets:[u],planCount:r.length}:{docsDir:o,targets:[],planCount:r.length,reason:`"${u}" isn't in the docs plan (.d360-writer/plan.json).`}}let s=Il(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=_l(e,o,s);if(c.length===0){let u=n.scope?` under "${n.scope}"`:"";return{docsDir:o,targets:[],planCount:r.length,reason:`All planned articles${u} are already written. Use /write <path> to rewrite one.`}}return{docsDir:o,targets:c,planCount:r.length}}async function Mr(e,t){if(!_r(t.cwd))return console.log(x("No d360-writer config here. Run /init first.")),{kind:"continue"};let n=Al(t.cwd);if(n.length===0)return console.log(x("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}=Yn(e),{docsDir:c,targets:u,reason:g}=Qn(t.cwd,n,{scope:o,path:r});if(u.length===0)return console.log(g?L(g):L("Nothing to write.")),{kind:"continue"};let m=Rl(u),{model:w,forced:j}=jl(t.cwd,"standard");if(!s&&!!!r){let v=Tl({files:Xn(t.cwd,n,u),op:"write",model:w});o&&console.log(f(`Scope: ${o} (${u.length} pending).`));for(let $ of Jn(m,v,Gn))console.log($);return console.log(f(`Model: ${w}${j?" (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 ${u.length} article${u.length===1?"":"s"} across ${m.length} partition${m.length===1?"":"s"} (\u2264${Gn} agents at once) on ${w}\u2026`)),console.log(f(" (mid-run abort is TUI-only \u2014 Ctrl+C exits the REPL.)"));try{for await(let v of El({cwd:t.cwd,partitions:m,promptFor:$=>Vn($,c),concurrency:Gn,profileName:t.profileName,allowProdWrites:t.allowProdWrites(),model:w}))if(v.type==="partition_status")v.status==="running"?console.log(f(` \u25B8 ${v.label} \u2014 writing\u2026`)):v.status==="done"?console.log(D(` \u2713 ${v.label}`)):console.log(x(` \u2717 ${v.label}`));else if(v.type==="run_done"){console.log("");let $=process.env.ANTHROPIC_API_KEY?"api":"subscription";for(let E of Kn(v.results,m,$))console.log(v.ok?D(E):L(E))}}catch(v){console.log(x(`Write run failed: ${v.message}`))}return console.log(""),{kind:"continue"}}H();import{search as Ml}from"@inquirer/prompts";import{findByName as Nl,getSession as Ll,listSessions as Ol,relativeTime as Lr}from"document360-engine";async function Or(e,t){let n=Ol(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=Nl(t.cwd,o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:(console.log(x(`No session matches "${o}".`)),Nr(n),{kind:"continue"})}if(!process.stdin.isTTY)return Nr(n),console.log(f("Run: /resume <name>")),{kind:"continue"};try{let o=await t.withPausedInput(()=>Ml({message:"Resume session (type to filter, \u2191\u2193 to navigate):",source:async s=>{let c=(s??"").toLowerCase();return n.filter(u=>!c||u.name.toLowerCase().includes(c)||u.firstPrompt.toLowerCase().includes(c)).map(u=>({name:`${u.name} ${Lr(u.updatedAt)}`,value:u.uuid,description:u.firstPrompt.slice(0,100)}))}})),r=Ll(o);return r?{kind:"resume",uuid:r.uuid,name:r.name}:{kind:"continue"}}catch{return console.log(""),{kind:"continue"}}}function Nr(e){console.log("");for(let t of e.slice(0,15))console.log(` ${R(t.name)} ${f(Lr(t.updatedAt))}`),console.log(` ${f(t.firstPrompt.slice(0,80))}`);console.log("")}H();import{renameSession as Ul}from"document360-engine";function Zn(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 Ur(e,t){let n=Zn(e.join(" "));if(!n)return console.log(x("Usage: /rename <new name>")),{kind:"continue"};let o=t.currentUuid();return o?(Ul(o,n)?console.log(D(`\u2713 Session renamed to "${n}"`)):console.log(x("Could not find the current session record.")),{kind:"continue"}):(console.log(x("Nothing to rename yet \u2014 send a message first; sessions auto-save once the agent replies.")),{kind:"continue"})}import{knownEnvironments as Wr,readProjectConfig as Wl,writeProjectConfig as Fl}from"document360-engine";H();function eo(e,t,n){if(!t)return"Usage: /profile add <name> [environment]";let o=Wl(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 Wr().includes(r)?(o.profiles={...o.profiles,[t]:{connection:{environment:r},production:!1}},Fl(o,e),null):`Unknown environment "${r}". Known: ${Wr().join(", ")} (or add the profile with explicit URLs in .d360-writer.json).`}async function Fr(e,t){let n=e[0];if(!n)return Wt(t.cwd),{kind:"continue"};if(n==="add"){let o=eo(t.cwd,e[1],e[2]);return o?(console.log(x(o)),{kind:"continue"}):(console.log(D(`\u2713 Profile "${e[1]}" created (environment: ${e[2]??e[1]}).`)),console.log(` Switch + sign in: ${R(`/profile ${e[1]}`)} then ${R("/login")}`),{kind:"continue"})}return Ft(t.cwd,n),Ht(t.cwd,n),console.log(f(" Restarting agent for the new profile\u2026")),{kind:"clear"}}H();import{select as Hl}from"@inquirer/prompts";import{readProjectConfig as Bl,readUserConfig as Hr,resolveModelSetting as to,writeUserConfig as Br}from"document360-engine";var ge=[{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 Kt(e){if(e.model===null||e.source==="claude-settings")return 0;let t=e.model.toLowerCase(),n=ge.findIndex(o=>o.value!==null&&(o.value===t||o.label.toLowerCase()===t||t.includes(o.label.toLowerCase())));return n>=0?n:0}function ql(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 wt(e,t){let n=()=>{let s=to(e);return s.source==="project"||s.source==="user"||s.source==="env"?s.model??void 0:void 0};if(t==="default"){let s=Hr();return s.defaultModel?(delete s.defaultModel,Br(s),{lines:[`\u2713 Personal model override cleared \u2014 now: ${to(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}}Br({...Hr(),defaultModel:t});let o=[`\u2713 Personal model set to "${t}" (applies from your next message \u2014 conversation continues)`],r=Bl(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 qr(e,t){let n=e[0]?.trim();if(!n){let c=to(t.cwd);if(!process.stdin.isTTY)return console.log(`${ve("Model:")} ${R(c.model??"Claude Code default")}`),console.log(f(` source: ${ql(c)}`)),console.log(f(" change: /model <haiku|sonnet|opus|full-model-id> \xB7 reset: /model default")),{kind:"continue"};let u=Kt(c),g;try{g=await t.withPausedInput(()=>Hl({message:`Select model (current: ${c.model??"Claude Code default"})`,default:ge[u].value,choices:ge.map((b,v)=>({name:`${b.label}${v===u?" \u2714":""}`,value:b.value,description:b.desc}))}))}catch{return console.log(f("Cancelled.")),{kind:"continue"}}let{lines:m,changed:w,effective:j}=wt(t.cwd,g??"default");for(let b of m)console.log(b.startsWith("\u26A0")?L(b):b.startsWith("\u2713")?D(b):f(b));return w&&await t.setModel(j),{kind:"continue"}}let{lines:o,changed:r,effective:s}=wt(t.cwd,n);for(let c of o)console.log(c.startsWith("\u26A0")?L(c):c.startsWith("\u2713")?D(c):f(c));return r&&await t.setModel(s),{kind:"continue"}}Qt();async function Vr(e,t){return await t.withPausedInput(()=>Ze(t.cwd)),{kind:"clear"}}H();import{resolveActiveProfile as ta}from"document360-engine";async function Jr(e,t){let n=!1;try{n=ta(t.cwd).production}catch{}return n?(console.log(L("\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"})}H();var Kr=async(e,t)=>{try{await t.withPausedInput(()=>Ut({}))}catch(n){console.log(x(`Login failed: ${n.message}`))}return{kind:"continue"}};import{existsSync as Qr}from"node:fs";import{isAbsolute as na,join as oa,resolve as ra}from"node:path";import{readProjectConfig as sa,screenshotPlaceholderIds as ia}from"document360-engine";var Zr=e=>e.replace(/\\/g,"/").replace(/\/+$/,"");function oo(e){let t=!e.includes("--no-setup"),n=e.filter(r=>r!=="--no-setup");if(n[0]==="--list")return{mode:"list",scope:n[1]?Zr(n[1]):void 0};let o=n[0];return!o||o==="--all"?{mode:"all",setup:t}:/[\\/]/.test(o)||o.endsWith(".md")?{mode:"scope",scope:Zr(o),setup:t}:{mode:"single",id:o}}function ro(e,t){let n=sa(e),o=n?.captureDir??"user-docs/_capture",r=n?.outputDir??"user-docs/_screenshots",s=(c,u)=>na(c)?oa(c,u):ra(e,c,u);return ia(e,{scope:t}).map(({id:c,file:u})=>{let g=Qr(s(r,`${c}.png`))?"captured":Qr(s(o,`${c}.spec.ts`))?"spec":"placeholder";return{id:c,file:u,state:g}})}var la={placeholder:"\u25CB",spec:"\u25D0",captured:"\u25CF"},aa={placeholder:"placeholder only",spec:"spec written, not captured",captured:"captured"};function so(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(u=>u.file===s))r.push(` ${la[c.state]} ${c.id.padEnd(34)} ${aa[c.state]}`)}return r.push("","\u25CB placeholder only \u25D0 spec written \u25CF captured"),r}function es(e){return["Run the emit-screenshot-spec skill to author the document360-capture spec for EACH of these","SCREENSHOT placeholder ids:",e.paths.map(t=>`- ${t}`).join(`
|
|
51
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(`
|
|
52
|
-
`)}async function
|
|
53
|
-
`),display:`/screenshot ${
|
|
54
|
-
`),s=
|
|
55
|
-
`),display:"/capture-setup"}}var
|
|
56
|
-
`).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(
|
|
57
|
-
`);return{lines:s.slice(0,t).map(c=>
|
|
60
|
+
`)}async function Zt(e,t){let n=oo(e);if(n.mode==="list"){let c=t?.cwd??process.cwd();for(let u of so(ro(c,n.scope),n.scope))console.log(u);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 ca,readFileSync as ua}from"node:fs";import{isAbsolute as da,join as pa,resolve as fa}from"node:path";import{readProjectConfig as ma}from"document360-engine";async function xt(){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 ts="-".repeat(64);function io(e){let t="user-docs/_capture";try{let w=ma(e);w?.captureDir&&(t=w.captureDir.replace(/\\/g,"/").replace(/\/+$/,""))}catch{}let n=da(t)?pa(t,"CAPTURE-TESTID-REQUESTS.json"):fa(e,t,"CAPTURE-TESTID-REQUESTS.json");if(!ca(n))return null;let o;try{let w=JSON.parse(ua(n,"utf8").replace(/^/,""));o=Array.isArray(w?.requests)?w.requests:[]}catch{return null}if(o.length===0)return null;let r=new Set(o.map(w=>w.file).filter(Boolean)).size,s=o.length,c=`${s} data-testid attribute${s===1?"":"s"}`,u=`${r} source file${r===1?"":"s"}`,g=`${t}/CAPTURE-TESTID-REQUESTS.md`,m=`${t}/CAPTURE-TESTID-REQUESTS.json`;return[`\u{1F4CB} Developer hand-off \u2014 ${c} requested across ${u}.`,"\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.","",ts,`Read ${g} and ${m}. 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.",ts]}H();import{existsSync as ga,rmSync as ha}from"node:fs";import{basename as ns,resolve as os}from"node:path";import{input as ka}from"@inquirer/prompts";import{readProjectConfig as ya}from"document360-engine";var wa=[".d360-writer.json",".d360-writer","d360-category-map.json",".d360-capture.json",".d360-capture-cache"];function lo(e){let t=null;try{t=ya(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,...wa].filter(s=>ga(os(e,s)))}function ao(e,t){let n=[],o=[];for(let r of t)try{ha(os(e,r),{recursive:!0,force:!0}),n.push(r)}catch(s){o.push({path:r,error:s.message})}return{removed:n,failed:o}}function co(e,t){return t.length===0?["Nothing to reset \u2014 no d360-writer files found in this repo."]:["\u26A0 This permanently DELETES everything d360-writer created here:",...t.map(n=>` \u2022 ${n}`),"","Undo = git: committed files are restorable; untracked ones (most screenshots) are gone.",`To confirm, type the repo name: ${ns(e)}`]}async function rs(e,t){let n=lo(t.cwd);for(let u of co(t.cwd,n))console.log(n.length===0?f(u):u);if(n.length===0)return{kind:"continue"};let o=ns(t.cwd);if((await t.withPausedInput(()=>ka({message:`Type "${o}" to delete (anything else cancels):`}).catch(()=>""))).trim()!==o)return console.log(L("Reset cancelled \u2014 nothing deleted.")),{kind:"continue"};let{removed:s,failed:c}=ao(t.cwd,n);console.log(D(`\u2713 Reset complete \u2014 removed ${s.length} item${s.length===1?"":"s"}. The repo is back to its original state.`));for(let u of c)console.log(x(` \u2717 ${u.path}: ${u.error}`));return{kind:"continue"}}var ss={help:Tn,"?":Tn,clear:nr,exit:Rn,quit:Rn,init:ar,mcp:zt,publish:mr,audit:Xt,scope:hr,sync:vr,convert:Er,write:Mr,resume:Or,rename:Ur,profile:Fr,model:qr,doctor:$t,workspace:Vr,"allow-prod":Jr,login:Kr,screenshot:Zt,"capture-setup":xt,reset:rs};function is(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}H();var $a={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"},as=160,po=200,cs=40;function re(e,t){let n=e.replace(/\s+/g," ").trim();return n.length<=t?n:n.slice(0,t-1)+"\u2026"}function ls(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}: ${re(typeof r=="string"?r:JSON.stringify(r),cs)}`);return t.length>4&&n.push("\u2026"),re(n.join(", "),as)}var Ee=e=>typeof e=="string"&&e?e:null,uo=e=>typeof e=="string"&&e.length>=8?e.slice(0,8):null;function xa(e){let t=Ee(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 ba(e,t){let n=(o,r)=>({title:`Document360: ${o}`,sep:" ",arg:r});switch(e){case"d360_create_article":{let o=Ee(t.title);if(!o)return null;let r=xa(t.local_path);return n("Create article",`"${re(o,60)}"${r?` in ${r}`:""}`)}case"d360_update_article":{let o=Ee(t.title),r=uo(t.article_id);return n("Update article",o?`"${re(o,60)}"`:r?`id ${r}\u2026`:null)}case"d360_fork_article":return n("Fork article (new draft)",uo(t.article_id)?`id ${uo(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",Ee(t.name)?`"${re(Ee(t.name),60)}"`:null);case"d360_upload_drive_file":{let o=Ee(t.file_path);return n("Upload image",o?re(o.replace(/\\/g,"/").split("/").pop()??o,60):null)}case"d360_sync_status":return n("Check sync status",null);default:return null}}function en(e,t){if(e==="ToolSearch")return null;if(e.startsWith("mcp__")){let[,r="",...s]=e.split("__"),c=s.join("__");if(r==="document360"){let m=ba(c,t);if(m)return m}let u=c.replace(/^d360_/,"").replace(/_/g," ");return{title:`${r==="document360"?"Document360":r.charAt(0).toUpperCase()+r.slice(1)}: ${u}`,sep:" ",arg:ls(t)}}let n=$a[e],o=n?t[n]:void 0;return typeof o=="string"&&o?{title:e,sep:"",arg:re(o,as)}:{title:e,sep:"",arg:ls(t)}}function va(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 Ca(e){if(!/^[[{]/.test(e))return null;let t;try{t=JSON.parse(e)}catch{return null}if(Array.isArray(t)){let n=t.map(va).filter(r=>r!==null),o=`${t.length} item${t.length===1?"":"s"}`;return n.length===0?[o]:[o,...n.map(r=>re(r,po))]}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}: ${re(String(r),cs)}`);return n.length>0?[re(n.join(" \xB7 "),po)]:null}return null}function Pa(e){let t=Ee(e)?.replace(/\\/g,"/");if(!t)return null;let n=t.split("/").filter(Boolean);return n.length>1?n.slice(1).join("/"):t}function Sa(e,t,n){switch(e){case"d360_create_article":{let o=Pa(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=Ee(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 ${re(o[0],120)}`:""}`]}default:return null}}function tn(e,t=4,n,o){let r=e.replace(/\r\n/g,`
|
|
64
|
+
`).trimEnd();if(!r)return{lines:["(no output)"],hidden:0};if(n?.startsWith("mcp__document360__")){let c=Sa(n.slice(18),o,r);if(c)return{lines:c,hidden:0}}let s=Ca(r)??r.split(`
|
|
65
|
+
`);return{lines:s.slice(0,t).map(c=>re(c,po)),hidden:Math.max(0,s.length-t)}}function nn(e,t,n,o="en"){return`${e.replace(/\/$/,"")}/${t}/document/v1/${o}/${n}`}function on(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 rn(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 Oa=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Ua(e,t,n,o){if(Oa.test(e))try{let r=fs(o),s=typeof t.project_id=="string"&&t.project_id||r.project.projectId,c=on(t,n),u=rn(n);e.endsWith("publish_article")&&u&&console.log(R(` \u2B95 Live: ${u}`)),c&&s&&console.log(R(` \u2B95 Preview: ${nn(r.connection.portalUrl,s,c,r.project.languageCode??"en")}`))}catch{}}async function ms(e=process.cwd(),t="auto",n){let o=Ra(t);o.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(""),console.error(` ${R("export ANTHROPIC_API_KEY=sk-ant-...")} (macOS / Linux)`),console.error(` ${R('$env:ANTHROPIC_API_KEY="sk-ant-..."')} (PowerShell)`),console.error(""),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),console.error(""),process.exit(2)),Wa(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=fo({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1},u=Ta({input:process.stdin,output:process.stdout}),g=[],m=null,w=!1;u.on("line",v=>{if(m){let $=m;m=null,$(v)}else g.push(v)}),u.on("close",()=>{if(w=!0,m){let v=m;m=null,v(null)}});function j(){return g.length>0?Promise.resolve(g.shift()):w?Promise.resolve(null):(process.stdout.write(R("> ")),new Promise(v=>{m=v}))}let b={cwd:e,profileName:n,allowProdWrites:()=>r,restartAgent:()=>{s.close(),s=fo({cwd:e,profileName:n,allowProdWrites:r}),c={uuid:null,firstPrompt:null,titleFired:!1}},currentUuid:()=>c.uuid,setModel:async v=>s.setModel(v),withPausedInput:async v=>{u.pause();try{return await v()}finally{u.resume()}}};try{for(;;){let v=await j();if(v===null)break;let $=v.trim();if($){if($.startsWith("/")){let E=is($);if(!E)continue;let q=ss[E.name];if(!q){console.log(x(`Unknown command: /${E.name}`)),console.log(f("Type /help for the list."));continue}let G=await q(E.args,b);if(G.kind==="exit")break;if(G.kind==="clear"){b.restartAgent();continue}if(G.kind==="allow-prod"){r=!0,b.restartAgent(),console.log(D("\u2713 Production writes authorized for this session.")),console.log("");continue}if(G.kind==="resume"){s.close(),s=fo({cwd:e,resume:G.uuid,profileName:n,allowProdWrites:r});let ie=Aa(G.uuid);c={uuid:G.uuid,firstPrompt:ie?.firstPrompt??null,titleFired:!0},ps(G.uuid),console.log(D(`\u2713 Resumed "${G.name}"`)),console.log("");continue}G.kind==="forward-to-agent"&&(c.firstPrompt||(c.firstPrompt=G.display??G.prompt),await ds(s,G.prompt,o,c,e));continue}c.firstPrompt||(c.firstPrompt=$),await ds(s,$,o,c,e)}}}finally{s.close(),u.close()}}function Wa(e,t){console.log(""),console.log(pt("document360-writer")),console.log(f(` cwd: ${e}`));let n=_a(e);console.log(f(` model: ${n.model??"auto (engine right-sizes per task)"}${n.model?` (${n.source})`:""}`)),console.log(Fa(e,t)),Ma(e)||console.log(L(" 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 Fa(e,t){try{let n=fs(e,t),o=n.production?L(" \u26A0 PRODUCTION"):"",r=La(n.name);if(!r)return f(` Document360: profile "${n.name}"${o} \u2014 not logged in (d360-writer login)`);let s={...us(r.idToken)??{},...us(r.accessToken)??{}},c=s.email??s.preferred_username??"signed in";return Na(r)&&!r.refreshToken?L(` 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 Ha(){console.error(""),console.error(`Sign in with your Claude subscription: run ${R("claude")} once, then retry.`),console.error(` (No Claude Code? ${R("npm install -g @anthropic-ai/claude-code")})`),console.error(`Or set an API key: ${R("https://console.anthropic.com/settings/keys")}`)}function Ba(e,t,n){e.uuid=t;let o=new Date().toISOString();Da({uuid:t,name:Ea(e.firstPrompt??"session"),renamed:!1,titled:!1,cwd:n,firstPrompt:e.firstPrompt??"",createdAt:o,updatedAt:o})}function qa(e,t){e.titleFired=!0;let n=e.uuid,o=e.firstPrompt;!n||!o||Ia(o,t).then(r=>{r&&ja(n,r)}).catch(()=>{})}async function ds(e,t,n,o,r){let s=new Map;for await(let c of e.send(t))za(c,o,r,n,s)}function za(e,t,n,o,r){switch(e.type){case"session":t.uuid||Ba(t,e.sessionId,n);break;case"text":process.stdout.write(e.delta);break;case"tool":{let s=en(e.name,e.input);s&&(process.stdout.write(`
|
|
58
66
|
|
|
59
|
-
`),console.log(`${D("\u25CF")} ${
|
|
60
|
-
`),console.log(f(` (${e.inputTokens}\u2192${e.outputTokens} tokens`+(e.costUsd>0?`, $${e.costUsd<.01?e.costUsd.toFixed(4):e.costUsd.toFixed(2)}`:"")+")")),console.log(""),t.uuid&&(
|
|
61
|
-
`)){let c=s.trim().replace(
|
|
62
|
-
`)}function
|
|
63
|
-
`)||e.length>200}function
|
|
64
|
-
`).length;return`[Pasted text #${e} +${
|
|
65
|
-
`)){if(s.length===0)
|
|
66
|
-
`),s=0;for(let c=r.length-1;c>=0;c--){let
|
|
67
|
-
`),truncated:c>0||
|
|
68
|
-
`)){let s=
|
|
67
|
+
`),console.log(`${D("\u25CF")} ${ve(s.title)}${s.arg!==null?Q(`${s.sep}(${s.arg})`):""}`),r.set(e.id,{name:e.name,input:e.input}));break}case"article_diff":{let s=We(e.oldContent,e.newContent,Math.max(40,(process.stdout.columns??80)-1));if(!s)break;let c=u=>u===1?"":"s";console.log(Q(` \u23BF Added ${s.added} line${c(s.added)}, removed ${s.removed} line${c(s.removed)}`));for(let u of s.lines)console.log(u);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=tn(e.output,4,e.isError?void 0:s.name,s.input),u=e.isError?x:Q;c.lines.forEach((g,m)=>console.log(u((m===0?" \u23BF ":" ")+g))),c.hidden>0&&console.log(f(` \u2026 +${c.hidden} lines`)),e.isError||Ua(s.name,s.input,e.output,n);break}case"result":process.stdout.write(`
|
|
68
|
+
`),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&&(ps(t.uuid),t.titleFired||qa(t,n));break;case"error":console.error(""),console.error(x(`agent error: ${e.message}`)),o.kind==="subscription"&&e.kind==="auth"&&Ha();break}}import{render as Oc}from"ink";import{resolveAuth as Uc}from"document360-engine";import{useCallback as X,useEffect as Ie,useMemo as un,useRef as V,useState as W}from"react";import{Box as J,Text as P,useApp as tc,useInput as nc,useStdout as oc}from"ink";import{existsSync as js,readFileSync as Es,readdirSync as Ws}from"node:fs";import{basename as dn,isAbsolute as rc,join as vt}from"node:path";import{createSession as Ds,loginPkce as sc,toStoredTokens as ic,saveTokens as lc,getAccessToken as ac,resolveActiveProfile as he,resolveProjectId as cc,getArticle as uc,decodeJwtClaims as ot,isExpired as De,loadTokens as Fe,setTitle as dc,slugify as pc,touchSession as go,upsertSession as fc,generateTitle as Is,findByName as mc,listSessions as gc,renameSession as hc,suggestNextAction as kc,readProjectConfig as se,writeProjectConfig as yc,resolveModelSetting as $o,loadProfileMap as _s,applyPull as wc,computeSyncStatus as ho,planPull as $c,inventoryRepo as xc,knownEnvironments as bc,resolveEnvironment as vc,planPartitions as Ms,partitionEvenly as Cc,screenshotPlaceholderIds as Pc,trackedArticlePaths as Sc,runPartitioned as ko,estimateBulkCost as Ns,resolveModelForOperation as yo,readDocsPlan as Tc}from"document360-engine";import{existsSync as Ga,mkdirSync as Ya,readFileSync as Xa,writeFileSync as Va}from"node:fs";import{join as gs}from"node:path";import{writerDir as Ja}from"document360-engine";function hs(e){return gs(Ja(e),".sessions")}function ks(e,t){return gs(hs(e),`${t}.json`)}function ys(e,t,n){try{Ya(hs(e),{recursive:!0});let o=n.filter(r=>r.kind!=="banner");Va(ks(e,t),JSON.stringify({v:1,items:o}),"utf8")}catch{}}function mo(e,t){try{let n=ks(e,t);if(!Ga(n))return[];let o=JSON.parse(Xa(n,"utf8"));return Array.isArray(o.items)?o.items:[]}catch{return[]}}var Ka=e=>`\x1B]0;${e}\x07`,Qa=e=>`\x1B]9;4;${e};${e===1?100:0}\x07`;function ws(e){process.stdout.isTTY&&process.stdout.write(e)}function bt(e){ws(Ka(e))}function sn(e){ws(Qa(e))}var ln=["\xB7 ","\xB7\xB7 ","\xB7\xB7\xB7"];Qt();var Za=/^(?:\d+[.)]|[-*•])\s+/;function $s(e,t,n=4){let o=new Set(t.map(s=>s.toLowerCase())),r=[];for(let s of e.split(`
|
|
69
|
+
`)){let c=s.trim().replace(Za,""),u=c.match(/^`([^`]+)`$/);u&&(c=u[1].trim());let g=c.match(/^\/([a-z?][a-z0-9-]*)(?:\s+(\S.*?))?\s*$/i);if(!g||!o.has(g[1].toLowerCase()))continue;let m=`/${g[1].toLowerCase()}${g[2]?` ${g[2]}`:""}`;if(r.includes(m)||r.push(m),r.length>=n)break}return r}H();var ec=/\[Pasted text #\d+ \+\d+ lines?\]/g;function xs(e){return e.replace(/\r\n?/g,`
|
|
70
|
+
`)}function bs(e){return e.includes(`
|
|
71
|
+
`)||e.length>200}function vs(e,t){let n=t.split(`
|
|
72
|
+
`).length;return`[Pasted text #${e} +${n} line${n===1?"":"s"}]`}function Cs(e,t){return e.replace(ec,n=>t.get(n)??n)}function Ps(e){let t=e.match(/\[Pasted text #\d+ \+\d+ lines?\]$/);return t?e.slice(0,-t[0].length):null}function an(e,t){let n=Math.max(1,t),o=[],r=0;for(let s of e.split(`
|
|
73
|
+
`)){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 cn(e,t){let n=0;for(let o=0;o<e.length&&e[o].start<=t;o++)n=o;return n}function Ss(e,t,n){let o=cn(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 Ts(e,t,n){let o=e[cn(e,t)];return n==="start"?o.start:o.end}function Rs(e,t,n){let o=Math.max(1,n),r=e.split(`
|
|
74
|
+
`),s=0;for(let c=r.length-1;c>=0;c--){let u=r[c];if(s+=Math.max(1,Math.ceil(u.length/o)),s>=t){let g=s-t;return{text:[g>0?u.slice(g*o):u,...r.slice(c+1)].join(`
|
|
75
|
+
`),truncated:c>0||g>0}}}return{text:e,truncated:!1}}function As(e){let t=!1,n=0,o=0;for(let r of e.split(`
|
|
76
|
+
`)){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 wo,jsx as S,jsxs as F}from"react/jsx-runtime";var Rc={project:".d360-writer.json",user:"/model",env:"ANTHROPIC_MODEL","claude-settings":"Claude Code settings","claude-default":""};function Ac(e,t,n,o){let r=n.kind==="api"?"API key":n.kind==="subscription"?"subscription":"not configured",s=$o(e),c=se(e),u=(c?.docsDir??"user-docs").replace(/\/+$/,""),g=c?.mode==="engineer"?"engineer \xB7 full source access (dogfooding)":`writer \xB7 edits limited to ${u}/ + config`,m={version:t,claude:r,model:s.model??"Claude Code default model",modelSource:Rc[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 m;try{let w=he(e,o);m.profile=w.name,m.apiUrl=w.connection.apiUrl,m.prod=w.production,m.project=w.project.projectId??"(chosen at login)";let j=Fe(w.name);if(j){let b={...ot(j.idToken)??{},...ot(j.accessToken)??{}},v=b.email??b.preferred_username??"signed in";De(j)?j.refreshToken&&(m.who=v,m.loggedOut=!1,m.sessionHint="session expired \u2014 refreshing\u2026"):(m.who=v,m.loggedOut=!1,m.sessionHint=`session valid until ${new Date(j.expiresAt).toLocaleString(void 0,{hour:"2-digit",minute:"2-digit",day:"2-digit",month:"short"})}`)}}catch{}return m}function jc(e,t){try{let n=he(e,t),o=Fe(n.name);if(!o)return{text:`profile "${n.name}" \u2014 not logged in (/login)`,prod:n.production};let r={...ot(o.idToken)??{},...ot(o.accessToken)??{}},s=r.email??r.preferred_username??"signed in";return De(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 Ls=["Drafting","Composing","Outlining","Researching","Documenting","Structuring","Polishing","Synthesizing","Curating","Distilling","Weaving","Wrangling","Pondering"],fn=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],Ec="Ask me to write or update an article\u2026";function pn({ch:e,dim:t}){let[n,o]=W(!0);return Ie(()=>{let r=setInterval(()=>o(s=>!s),530);return()=>clearInterval(r)},[]),S(P,{inverse:n,color:t&&!n?"gray":void 0,children:e})}var Dc=/^mcp__document360__d360_(create_article|update_article|fork_article|publish_article)$/;function Os(e){let t=e.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n/);return t?e.slice(t[0].length):e}var Ic=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Us(e){try{return Ws(e,{withFileTypes:!0}).filter(t=>t.isDirectory()&&!t.name.startsWith(".")).length>6}catch{return!1}}function _c(e,t){let n=t??[];return n.length===0?["src","api","services","packages","modules"].some(o=>Us(vt(e,o))):n.some(o=>!o.includes("/")&&!o.endsWith(".md")&&Us(vt(e,o)))}function Mc({startTime:e,chars:t}){let[n,o]=W(0);Ie(()=>{let g=setInterval(()=>o(m=>m+1),120);return()=>clearInterval(g)},[]);let r=fn[n%fn.length],s=Ls[Math.floor(n/16)%Ls.length],c=Math.floor((Date.now()-e)/1e3),u=Math.round(t/4);return F(J,{children:[S(P,{color:z,children:` ${r} ${s}\u2026 `}),S(P,{color:"gray",children:`(${yt(c)} \xB7 ~${u} tokens \xB7 esc to interrupt)`})]})}var Nc=12e4;function Lc({p:e}){let[t,n]=W(0);Ie(()=>{let g=setInterval(()=>n(m=>m+1),150);return()=>clearInterval(g)},[]);let o=fn[t%fn.length],r=Math.floor((Date.now()-e.startedAt)/1e3),s=Math.floor((Date.now()-e.lastAt)/1e3),c=Math.round(e.chars/4),u=Date.now()-e.lastAt>=Nc;return F(J,{flexDirection:"column",children:[F(J,{children:[S(P,{color:z,children:` ${o} ${e.verb??"Converting"} `}),S(P,{color:"gray",children:`${e.done}/${e.total} \xB7 ${e.tools} tool call${e.tools===1?"":"s"} \xB7 ~${c} tokens \xB7 ${yt(r)} \xB7 esc to stop`})]}),e.active.length>0&&S(P,{dimColor:!0,children:` \u25B8 ${e.active.join(", ")} \xB7 last activity ${s}s ago`}),u&&S(P,{color:"yellow",children:` \u26A0 no activity for ${yt(s)} \u2014 press esc to stop if it's wedged`})]})}function Fs({cwd:e,auth:t,profileName:n,version:o}){let{exit:r}=tc(),[s,c]=W(n),[u,g]=W(null),[m,w]=W({text:"",pos:0}),j=m.text,b=X(a=>{w(l=>{let i=typeof a=="function"?a(l.text):a;return{text:i,pos:i.length}})},[]),v=X(a=>{w(l=>({text:l.text.slice(0,l.pos)+a+l.text.slice(l.pos),pos:l.pos+a.length}))},[]),$=X(()=>{w(a=>{if(a.pos===0)return a;let l=a.text.slice(0,a.pos),i=Ps(l)??l.slice(0,-1);return{text:i+a.text.slice(a.pos),pos:i.length}})},[]),[E,q]=W(!1),[G,ie]=W(!1),[de,Pe]=W(0),[pe,fe]=W(null),[rt,K]=W([]),Ct=V(0),[zs,mn]=W(!1),[Gs,st]=W(0),He=V(0),[it,Be]=W(0),[qe,Pt]=W(null),vo=V(new Map),Ys=V(0),ze=V([]),le=V(null);le.current===null&&(le.current=Ds({cwd:e,profileName:s,allowProdWrites:!1}));let ke=V({uuid:null,firstPrompt:null,titleFired:!1}),St=V(new Map),lt=V(!1),gn=V([]),Se=V([]),hn=V(null),[Co,Ge]=W(null),[_e,Me]=W(null),[ye,Ye]=W(null),[Te,Xe]=W(null),[ne,Ve]=W(null),[we,Je]=W(null),[ae,Ne]=W(null),[oe,Ke]=W(null),[Po,kn]=W([]),Le=V([]),Tt=V(!1),$e=V(null),[So,Z]=W(null),Rt=V(!1),At=V(null),[ce,Oe]=W(null),[To,at]=W(0),[Ro,ct]=W(0),[Xs,Ao]=W(0),yn=un(()=>{try{return $o(e).model??"auto"}catch{return null}},[e,Xs]),{stdout:me}=oc(),[,Vs]=W(0),jo=V(`${me.columns??80}x${me.rows??24}`),Re=X(()=>Math.max(20,(me.columns??80)-1),[me]),d=X(a=>{ze.current.push(a),console.log(On(a,Re()))},[Re]);Ie(()=>{let a=`d360-writer \xB7 ${Co??dn(e)}`;if(!E){sn(1),bt(a);return}sn(3);let l=0;bt(`${ln[0]} ${a}`);let i=setInterval(()=>{l=(l+1)%ln.length,bt(`${ln[l]} ${a}`)},450);return()=>clearInterval(i)},[E,e,Co]),Ie(()=>()=>sn(0),[]),Ie(()=>{if(d({kind:"banner",info:Ac(e,o,t,s)}),!se(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:
|
|
69
77
|
1. /init \u2014 pick your Document360 environment & scaffold config
|
|
70
78
|
2. /login signs in to that environment
|
|
71
79
|
3. /workspace picks where articles go
|
|
72
|
-
Press 1 to start.`}),
|
|
73
|
-
`)});return;case"exit":case"quit":
|
|
74
|
-
`:"",
|
|
75
|
-
Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;
|
|
76
|
-
`)+
|
|
77
|
-
`)})}}catch(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
`)})
|
|
81
|
-
`)})}return}case"
|
|
82
|
-
`)});return}default:u({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,At,u,he,ke]),jt=G(a=>{let l=(a??A).trim();if($(""),wt(null),We(0),l.startsWith("/")){let m=k=>k.trim().split(/\s+/).slice(1).join(" "),p=m(l);J(k=>k.filter(y=>y.trim()!==l&&!(p&&m(y)===p)))}if(!l||Tn(l)||Sn(l))return;fo.current.push(l);let i=us(l,fn.current);l.startsWith("/")?Pn(i):ke(i,{display:l})},[A,Tn,Sn,Pn,ke]),Rn=G(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=ls(a);if(as(l)){let i=cs(++Is.current,l);fn.current.set(i,l),j(i)}else j(l);return}j(a)},[j]),Ve=Math.max(10,Ne-6),An=lo(()=>so(g.text,Ve),[g.text,Ve]),$o=G(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),Et=G(a=>w(l=>({...l,pos:ps(so(l.text,Ve),l.pos,a)})),[Ve]),rt=G(a=>w(l=>({...l,pos:fs(so(l.text,Ve),l.pos,a)})),[Ve]),Ns=["\x1B[H","\x1B[1~","\x1BOH"],Ls=["\x1B[F","\x1B[4~","\x1BOF"],jn=G((a,l)=>l.leftArrow?($o(-1),!0):l.rightArrow?($o(1),!0):a&&Ns.includes(a)?(rt("start"),!0):a&&Ls.includes(a)?(rt("end"),!0):l.ctrl&&a==="a"?(rt("start"),!0):l.ctrl&&a==="e"?(rt("end"),!0):!1,[$o,rt]);return Da((a,l)=>{if(l.ctrl&&a==="c"){te.current?.close(),r();return}if(!z){if(M){if(l.escape){if(_e.current){_e.current.signal.aborted||(u({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),_e.current.abort());return}bt.current||(bt.current=!0,u({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),te.current?.interrupt());return}if(l.return){let i=A.trim();if(!i)return;Ie.current.push(i),go([...Ie.current]),$("");return}if(jn(a,l))return;if(l.upArrow){Et(-1);return}if(l.downArrow){Et(1);return}if(l.backspace||l.delete){P();return}a&&!l.ctrl&&!l.meta&&Rn(a);return}if(je){if(l.upArrow){Ee(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ee(i=>i&&{...i,cursor:Math.min(de.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=de.length){Ee(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=de[je.cursor];Ee(null);let{lines:m,changed:p,effective:k}=pt(e,i.value??"default");for(let y of m)u({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(wn(y=>y+1),te.current?.setModel(k));return}if(a==="s"){let i=de[je.cursor];Ee(null),te.current?.setModel(i.value??void 0),u({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Ee(null);return}return}if(me){if(l.upArrow){qe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){qe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=me.rows.length){qe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=me.rows[me.cursor];qe(null),xo(i.name,l.return===!0);return}if(l.escape){qe(null);return}return}if(ve){if(l.upArrow){ze(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){ze(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ve.rows.length){ze(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ve.rows[ve.cursor].name;if(ze(null),!Jn(e,i).created){u({kind:"note",tone:"error",text:"Could not scaffold \u2014 .d360-writer.json already exists."});return}u({kind:"note",tone:"ok",text:`\u2713 Wrote .d360-writer.json (environment "${i}").`});let p=!1;try{let k=Ue(i);p=!!k&&!(Re(k)&&!k.refreshToken)}catch{}p?(u({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),J(["/workspace"])):(u({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),J(["/login"])),$e(k=>k+1);return}if(l.escape){ze(null);return}return}if(K){if(l.upArrow){Ge(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ge(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=K.rows.length){Ge(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=K.rows[K.cursor],{profile:m,projectId:p}=K;Ge(null),Cn(m,p,i);return}if(l.escape){Ge(null);return}return}if(Q){if(l.upArrow){Xe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Xe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Xe(i=>i&&{...i,rows:i.rows.map((m,p)=>p===i.cursor?{...m,checked:!m.checked}:m)});return}if(l.return){let i=Q.rows.filter(m=>m.checked).map(m=>m.path);if(Xe(null),i.length===0){u({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}Ao(e,i),u({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let m of i)u({kind:"note",tone:"info",text:` ${m}`});u({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Xe(null);return}return}if(ge){if(l.upArrow){Ye(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ye(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ge.rows.length){Ye(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge.rows[ge.cursor],m=ge.rows.filter(p=>p.path!=="--all").map(p=>p.path);Ye(null),ke(i.path==="--all"?ct(m):qt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Ye(null);return}return}if(oe){if(l.escape){De(null);return}if(l.upArrow){De(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){De(i=>i&&{...i,cursor:Math.min(Math.max(0,Pt.length-1),i.cursor+1)});return}if(l.return){let i=Pt[oe.cursor];if(i){De(null);try{u({kind:"preview",name:ao(i),text:Cs(ks(ht(e,i),"utf8"))})}catch(m){u({kind:"note",tone:"error",text:`Could not read ${i}: ${m.message}`})}}return}if(l.backspace||l.delete){De(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){De(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ne){if(l.escape){Me(null);return}if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(Math.max(0,Rt.length-1),i.cursor+1)});return}if(l.return){let i=Rt[ne.cursor];if(i){Me(null),he(i.uuid),fe.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Be(i.name),ln(i.uuid),He.current=[];let m=sn(e,i.uuid);for(let p of m)u(p);u({kind:"note",tone:"ok",text:m.length?`Resumed "${i.name}" \u2014 restored ${m.length} message(s); continue where you left off.`:`Resumed "${i.name}" (agent memory reconnected; no saved transcript to replay).`})}return}if(l.backspace||l.delete){Me(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Me(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!A&&le){$(le),ae(null),kt.current++;return}if(!A&&et.length>0&&a&&/^[1-9]$/.test(a)){let i=et[Number(a)-1];if(i){$(i);return}}if(!jn(a,l)){if(bn){if(l.upArrow){We(i=>Math.max(0,i-1));return}if(l.downArrow){We(i=>Math.min(nt.length-1,i+1));return}if(l.tab){$("/"+(nt[ot]?.name??"")+" "),We(0);return}if(l.return){let i=nt[ot];if(i){let m=A.trim().slice(1).split(/\s+/).slice(1).join(" ");if(zn(i.usage)&&!m){$("/"+i.name+" "),We(0);return}jt("/"+i.name+(m?" "+m:""));return}}}else{if(l.upArrow){if(A!==""&&Fe===null){Et(-1);return}let i=fo.current;if(!i.length)return;let m=Fe===null?i.length-1:Math.max(0,Fe-1);wt(m),$(i[m]??"");return}if(l.downArrow){if(A!==""&&Fe===null){Et(1);return}let i=fo.current;if(Fe===null)return;let m=Fe+1;m>=i.length?(wt(null),$("")):(wt(m),$(i[m]??""));return}}if(l.return){jt();return}if(l.backspace||l.delete){P();return}if(l.escape){$(""),We(0),ae(null),J([]);return}a&&!l.ctrl&&!l.meta&&Rn(a)}}}),Ae(()=>{if(M||z)return;let a=Ie.current.shift();a!==void 0&&(go([...Ie.current]),jt(a))},[M,z,jt]),W(X,{flexDirection:"column",width:Ne,children:[yo!==null&&W(X,{marginTop:1,flexDirection:"column",children:[yo.truncated&&C(v,{dimColor:!0,children:"\u2026"}),C(v,{children:yo.text})]}),M&&(hn?C(mc,{p:hn}):C(pc,{startTime:tt.current,chars:Ds})),C(X,{borderStyle:"round",borderColor:$n.prod?"yellow":B,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:A?An.map((a,l)=>{let i=g.text.slice(a.start,a.end),m=l===io(An,g.pos),p=Math.min(g.pos,a.end)-a.start;return W(v,{children:[C(v,{color:B,children:l===0?"> ":" "}),m?W(cn,{children:[i.slice(0,p),C(co,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):W(v,{children:[C(v,{color:B,children:"> "}),le&&!M?W(cn,{children:[C(co,{ch:le[0],dim:!0}),C(v,{color:"gray",children:le.slice(1)}),C(v,{dimColor:!0,children:" (tab)"})]}):ko.isSetup||!Es?W(cn,{children:[C(co,{ch:ko.text[0],dim:!0}),C(v,{color:"gray",children:ko.text.slice(1)})]}):C(co,{ch:" "})]})}),gn.length>0&&C(X,{flexDirection:"column",paddingX:1,children:gn.map((a,l)=>C(v,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),je?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Select model"}),C(v,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),de.map((a,l)=>W(v,{color:l===je.cursor?B:void 0,children:[l===je.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===je.current?" \u2714":""}`.padEnd(16),C(v,{color:"gray",children:a.desc})]},a.label)),C(v,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):me?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Switch connection profile"}),me.rows.map((a,l)=>W(v,{color:l===me.cursor?B:void 0,children:[l===me.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===me.current?" \u2714":""}`.padEnd(20),C(v,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?C(v,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),C(v,{dimColor:!0,children:"enter switch (saved as default) \xB7 s this session only \xB7 esc cancel"})]}):ve?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Pick your Document360 environment"}),ve.rows.map((a,l)=>W(v,{color:l===ve.cursor?B:void 0,children:[l===ve.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),C(v,{color:"gray",children:a.apiUrl})]},a.name)),C(v,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):K?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Switch workspace"}),C(v,{dimColor:!0,children:`environment ${K.environment} \xB7 project ${K.projectId.slice(0,8)}\u2026`}),K.rows.map((a,l)=>W(v,{color:l===K.cursor?B:void 0,children:[l===K.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===K.current?" \u2714":""}`.padEnd(30),C(v,{color:"gray",children:a.type??""})]},a.id)),C(v,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):Q?(()=>{let l=Math.min(Math.max(0,Q.cursor-Math.floor(7)),Math.max(0,Q.rows.length-14)),i=Q.rows.slice(l,l+14),m=Q.rows.filter(p=>p.checked).length;return W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Which folders back the user docs? (${m} selected of ${Q.rows.length})`}),l>0?C(v,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,k)=>{let y=l+k;return W(v,{color:y===Q.cursor?B:void 0,children:[y===Q.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,Ne-34)),C(v,{color:"gray",children:Gt(p)})]},p.path)}),l+14<Q.rows.length?C(v,{dimColor:!0,children:` \u2193 ${Q.rows.length-l-14} more`}):null,C(v,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():ge?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:"Publish which article?"}),ge.rows.map((a,l)=>W(v,{color:l===ge.cursor?B:void 0,children:[l===ge.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Ne-30)),C(v,{color:"gray",children:a.label})]},a.path)),C(v,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):oe?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Preview article${oe.query?` \u2014 filter: ${oe.query}`:" (type to filter)"}`}),Pt.length===0?C(v,{color:"gray",children:"no articles match"}):Pt.map((a,l)=>W(v,{color:l===oe.cursor?B:void 0,children:[l===oe.cursor?"\u276F ":" ",a]},a)),C(v,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ne?W(X,{flexDirection:"column",paddingX:1,children:[C(v,{color:B,bold:!0,children:`Resume session${ne.query?` \u2014 filter: ${ne.query}`:" (type to filter)"}`}),Rt.length===0?C(v,{color:"gray",children:"no sessions match"}):Rt.map((a,l)=>W(v,{color:l===ne.cursor?B:void 0,children:[l===ne.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),C(v,{color:l===ne.cursor?B:"gray",children:a.firstPrompt.slice(0,Math.max(10,Ne-40))})]},a.uuid)),C(v,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):bn?C(X,{flexDirection:"column",children:nt.map((a,l)=>W(v,{color:l===ot?B:void 0,children:[l===ot?"\u276F ":" ",a.usage.padEnd(22)," ",C(v,{color:l===ot?B:"gray",children:a.name==="model"&&ho?`${a.desc} (currently ${ho})`:a.desc})]},a.name))}):!A&&et.length>0?W(X,{flexDirection:"column",paddingX:1,children:[et.map((a,l)=>W(v,{children:[C(v,{color:B,children:l+1})," ",a.slice(0,Math.max(20,Ne-5))]},a)),C(v,{dimColor:!0,children:`press 1-${et.length} to fill the command \xB7 esc dismiss`})]}):C(X,{paddingX:1,children:W(v,{color:"gray",children:[$n.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${ho??"model n/a"}${t.kind==="api"?yn>0?` \xB7 ${Oe(yn)}`:"":kn>0?` \xB7 ${Xt(kn)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as kc}from"react/jsx-runtime";async function Rs(e=process.cwd(),t="auto",o,n="0.0.0"){let r=hc(t);r.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${S("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${S("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=gc(kc(Ps,{cwd:e,auth:r,profileName:o,version:n}));await s(),process.stdout.write(`
|
|
83
|
-
`)
|
|
80
|
+
Press 1 to start.`}),K(["/init"]);return}try{let a=he(e,s),l=Fe(a.name);l&&De(l)&&l.refreshToken?ac({profile:a.name,connection:a.connection}).then(()=>{d({kind:"note",tone:"ok",text:"\u2713 Document360 session refreshed."}),Pe(i=>i+1)}).catch(()=>{d({kind:"note",tone:"warn",text:"Document360 session refresh failed \u2014 do you want to log in now? (press 1)"}),K(["/login"])}):(!l||De(l))&&(d({kind:"note",tone:"warn",text:`Profile "${a.name}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"]))}catch{}try{let a=se(e),l=he(e,s),i=Fe(l.name);a&&i&&!De(i)&&_c(e,a.authoritativeSourceFiles)&&(d({kind:"note",tone:"info",text:"Large repo \u2014 the docs scope isn\u2019t set yet. Run /scope to choose which folders back your docs."}),K(["/scope"]))}catch{}},[]),Ie(()=>{let a=null,l=null,i=()=>{a&&clearTimeout(a),a=setTimeout(()=>{a=null;let h=`${me.columns??80}x${me.rows??24}`;h!==jo.current&&(jo.current=h,Vs(p=>p+1),l&&clearTimeout(l),l=setTimeout(()=>{l=null,console.log("\x1B[?2026h\x1B[H\x1B[2J"+jr(ze.current,Re())+"\x1B[?2026l")},80))},400)};return me.on("resize",i),()=>{a&&clearTimeout(a),l&&clearTimeout(l),me.off("resize",i)}},[me,Re]);let Ue=Math.max(20,(me.columns??80)-1),Eo=un(()=>jc(e,s),[e,s,de]),wn=un(()=>{let a=se(e);if(!a)return{text:"Press 1 to set up this repo, or /help\u2026",isSetup:!0};try{let h=he(e,s),p=Fe(h.name);if(!(!!p&&!(De(p)&&!p.refreshToken)))return{text:`Press 1 to sign in to Document360 (profile "${h.name}")\u2026`,isSetup:!0};if(!h.project.workspaceId)return{text:"Press 1 to pick a workspace\u2026",isSetup:!0}}catch{}let l=vt(e,(a.docsDir??"user-docs").replace(/\/+$/,""));return(()=>{try{return js(l)&&Ws(l).length>0}catch{return!1}})()?{text:Ec,isSetup:!1}:{text:"Let's get started \u2014 try: write the docs for this repo",isSetup:!1}},[e,s,de]),ut=er(j),Do=ut.length>0&&!E,$n=u!==null?Rs(u,8,Ue):null,jt=ae?ae.paths.filter(a=>!ae.query||a.toLowerCase().includes(ae.query.toLowerCase())).slice(0,8):[],Et=ce?ce.sessions.filter(a=>{let l=ce.query.toLowerCase();return!l||a.name.toLowerCase().includes(l)||a.firstPrompt.toLowerCase().includes(l)}).slice(0,8):[],xe=X((a,l)=>{le.current?.close(),le.current=Ds({cwd:e,resume:a,profileName:l??s,allowProdWrites:lt.current}),a||(ke.current={uuid:null,firstPrompt:null,titleFired:!1},Ge(null)),at(0),ct(0)},[e,s]),Io=X((a,l,i)=>{if(Dc.test(a))try{let h=he(e,s),p=typeof l.project_id=="string"&&l.project_id||h.project.projectId,k=on(l,i),y=[],I=rn(i);a.endsWith("publish_article")&&I&&y.push(`Live: ${I}`),k&&p&&y.push(`Preview: ${nn(h.connection.portalUrl,p,k,h.project.languageCode??"en")}`),y.length>0&&d({kind:"link",lines:y})}catch{}},[e,s,d]),be=X(async(a,l)=>{mn(!0),fe(null),K([]);let i=++Ct.current;d({kind:"user",text:l?.echoDisplay&&l.display?l.display:a});let h=ke.current;h.firstPrompt||(h.firstPrompt=l?.display??a),He.current=Date.now(),st(0),q(!0),St.current.clear();let p="",k="",y=null,I=()=>{y||(y=setTimeout(()=>{y=null,g(k.length>0?k:null)},60))},M=()=>{y&&clearTimeout(y),y=null,g(null)},N=()=>{if(k.trim()){let A=k.trimEnd();d({kind:"assistant",text:A})}k="",M()};try{for await(let A of le.current.send(a))if(A.type==="session"){if(!h.uuid){h.uuid=A.sessionId;let _=new Date().toISOString(),C=pc(h.firstPrompt??"session");fc({uuid:A.sessionId,name:C,renamed:!1,titled:!1,cwd:e,firstPrompt:h.firstPrompt??"",createdAt:_,updatedAt:_}),Ge(C)}}else if(A.type==="text"){k+=A.delta,p+=A.delta;let _=As(k);if(_>0){let C=k.slice(0,_).trimEnd();C&&d({kind:"assistant",text:C}),k=k.slice(_)}I(),st(C=>C+A.delta.length)}else if(A.type==="tool"){let _=en(A.name,A.input);_&&(N(),d({kind:"tool",title:_.title,sep:_.sep,arg:_.arg}),St.current.set(A.id,{name:A.name,input:A.input}))}else if(A.type==="article_diff"){let _=We(A.oldContent,A.newContent,Re());_&&(N(),d({kind:"diff",added:_.added,removed:_.removed,lines:_.lines,hidden:_.hidden}))}else if(A.type==="tool_result"){A.isError&&/run \/login|not logged in|session expired|rejected the token/i.test(A.output)&&(Rt.current=!0);let _=St.current.get(A.id);if(_){St.current.delete(A.id),N();let C=tn(A.output,4,A.isError?void 0:_.name,_.input);d({kind:"tool-result",lines:C.lines,hidden:C.hidden,isError:A.isError}),A.isError||Io(_.name,_.input,A.output)}}else if(A.type==="result"){N(),at(C=>C+A.outputTokens),ct(C=>C+A.costUsd),d({kind:"done",seconds:Math.round((Date.now()-He.current)/1e3),tokens:A.outputTokens,costUsd:A.costUsd,ok:A.ok});let _=A.ok?$s(p,mt.map(C=>C.name)):[];if(_.length>0?K(_):A.ok&&p.trim()&&kc(a,p,e).then(C=>{C&&Ct.current===i&&fe(C)}).catch(()=>{}),h.uuid&&(go(h.uuid),!h.titleFired)){h.titleFired=!0;let C=h.uuid,T=h.firstPrompt;T&&Is(T,e).then(U=>{U&&(dc(C,U),Ge(U))}).catch(()=>{})}}else A.type==="error"&&(N(),A.kind==="auth"&&(Rt.current=!0),d({kind:"note",text:`agent error: ${A.message}`,tone:"error"}))}finally{q(!1),M(),ke.current.uuid&&ys(e,ke.current.uuid,ze.current),Rt.current&&(Rt.current=!1,At.current=l?.display??a,K(["/login"])),Tt.current&&(Tt.current=!1,Le.current.length>0&&(d({kind:"note",tone:"info",text:`(${Le.current.length} queued message(s) discarded)`}),Le.current=[],kn([])),d({kind:"note",tone:"warn",text:"Interrupted. What do you want to do next?"}))}},[e,d,Io,Re]),xn=X(a=>{let l=Fe(a);if(!l||De(l)&&!l.refreshToken)return null;let i={...ot(l.idToken)??{},...ot(l.accessToken)??{}};return i.email??i.preferred_username??"signed in"},[]),bn=X((a,l)=>{if(l){let i=se(e);i&&(i.defaultProfile=a,yc(i,e))}c(a),xe(void 0,a),d({kind:"note",tone:"ok",text:`\u2713 Switched to profile "${a}"${l?" (saved as default)":" (this session only)"} \u2014 agent restarted.`}),xn(a)||(d({kind:"note",tone:"warn",text:`Profile "${a}" is not signed in \u2014 do you want to log in now? (press 1)`}),K(["/login"])),Pe(i=>i+1)},[e,d,xe,xn]),_o=X((a,l,i)=>{Nt(e,a,l,i.id,i.name),d({kind:"note",tone:"ok",text:`Switched to workspace "${i.name??i.id}" (agent restarted).`}),xe(),_s(e,a)||(d({kind:"note",tone:"info",text:"Setup complete. Press tab to start with a docs analysis."}),fe("analyze this repo and propose a docs structure"))},[e,d,xe]),Dt=X(()=>{let a=Se.current[0];if(!a)return;d({kind:"note",tone:"info",text:`\u25CF ${a.title} (${a.path})`});for(let i of a.notes)d({kind:"note",tone:"warn",text:`\u26A0 ${i}`});a.overwritesLocalChanges&&d({kind:"note",tone:"warn",text:"\u26A0 This OVERWRITES local edits made since the last sync."});let l=We(a.oldContent,a.newContent,Re());d(l?{kind:"diff",added:l.added,removed:l.removed,lines:l.lines,hidden:l.hidden}:{kind:"note",tone:"info",text:"Local file already matches the remote content \u2014 applying only advances the sync base."}),d({kind:"note",tone:"info",text:`Write ${a.path}? (y/n \u2014 anything else cancels)`})},[d,Re]),Mo=X(a=>{if(Se.current.length===0)return!1;let l=a.trim().toLowerCase();if(l==="y"||l==="yes"){let i=Se.current.shift();try{wc({cwd:e,profileName:s},i),d({kind:"note",tone:"ok",text:`\u2713 Pulled ${i.path} (sync base advanced).`}),Se.current.length===0&&K(h=>h.length>0?h:["/sync"])}catch(h){d({kind:"note",tone:"error",text:`Pull failed: ${h.message}`})}}else if(l==="n"||l==="no"){let i=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 Dt(),!0},[e,s,Dt,d]),No=X(a=>{let l=hn.current;if(!l)return!1;if(hn.current=null,a.trim()!==l.repoName)return d({kind:"note",tone:"warn",text:"Reset cancelled \u2014 nothing deleted."}),!0;let{removed:i,failed:h}=ao(e,l.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 p of h)d({kind:"note",tone:"error",text:` \u2717 ${p.path}: ${p.error}`});return!0},[e,d]),Lo=X(async a=>{let l=a.slice(1).trim().split(/\s+/),i=(l[0]??"").toLowerCase(),h=l.slice(1);switch(mn(!0),i){case"help":d({kind:"note",tone:"info",text:Bt().join(`
|
|
81
|
+
`)});return;case"exit":case"quit":le.current?.close(),r();return;case"clear":xe(),ze.current=[],mn(!1),fe(null),Ct.current++,d({kind:"note",tone:"info",text:"Conversation reset (the previous session is still resumable via /resume)."});return;case"login":{let p;try{p=he(e,s)}catch(k){d({kind:"note",tone:"error",text:k.message});return}d({kind:"note",tone:"info",text:`Profile "${p.name}" \u2192 ${p.connection.name} (${p.connection.apiUrl})${p.production?" \u26A0 PRODUCTION":""}`}),ie(!0);try{let k=await sc(p.connection,{promptForRedirect:()=>Promise.reject(new Error("Manual login is CLI-only. Run: d360-writer login --manual"))},I=>d({kind:"note",tone:"info",text:I})),y=ic(p.name,k);lc(y),Sn(y,p.name,I=>d({kind:"note",tone:"info",text:I})),d({kind:"note",tone:"ok",text:`\u2713 Logged in to "${p.name}" as ${Ot(y)}`}),p.project.workspaceId||(d({kind:"note",tone:"info",text:"Next: pick the workspace your articles publish to."}),K(["/workspace"])),At.current&&(fe(At.current),At.current=null,d({kind:"note",tone:"info",text:"Press tab to re-send your last prompt."}))}catch(k){d({kind:"note",tone:"error",text:`Login failed: ${k.message}`})}finally{ie(!1),Pe(k=>k+1)}return}case"allow-prod":{let p=!1;try{p=he(e,s).production}catch{}if(!p){d({kind:"note",tone:"info",text:"Current profile is not production \u2014 writes are already allowed."});return}lt.current=!0,xe(),d({kind:"note",tone:"warn",text:"\u26A0 Production writes authorized for this session."});return}case"rename":{let p=Zn(h.join(" ")),k=ke.current.uuid;if(!k){d({kind:"note",tone:"error",text:"Send a message first \u2014 sessions save once the agent replies."});return}if(!p){d({kind:"note",tone:"info",text:"Thinking of a name\u2026"});let y=ke.current.firstPrompt??"";Is(y,e).then(I=>{I?(fe(`/rename ${I}`),d({kind:"note",tone:"info",text:`Suggestion: "${I}" \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}hc(k,p),Ge(p),bt(`d360-writer \xB7 ${p}`),d({kind:"note",tone:"ok",text:`Session renamed to "${p}".`});return}case"profile":{let p=h[0],k=se(e);if(!p){let y=Object.entries(k?.profiles??{});if(y.length===0){d({kind:"note",tone:"info",text:"No profiles. Run /init first."});return}let I=y.map(([A,_])=>({name:A,env:_.connection?.environment??"custom",prod:_.production===!0,who:xn(A)})),M=s??k?.defaultProfile,N=Math.max(0,I.findIndex(A=>A.name===M));Ye({cursor:N,current:N,rows:I});return}if(p==="add"){let y=eo(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]}).`}),bn(h[1],!1);return}if(!k?.profiles?.[p]){d({kind:"note",tone:"error",text:`Unknown profile "${p}". Create it: /profile add ${p} <environment>`});return}bn(p,!0);return}case"doctor":await $t(h,{cwd:e});return;case"mcp":await zt(h);return;case"init":{if(se(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 p=bc().map(k=>({name:k,apiUrl:vc(k).apiUrl}));Xe({cursor:0,rows:p});return}case"resume":{let p=h.join(" ").trim(),k=gc(e).filter(M=>M.uuid!==ke.current.uuid);if(!p){if(!k.length){d({kind:"note",tone:"info",text:"No saved sessions for this repo yet."});return}Oe({query:"",cursor:0,sessions:k});return}let y=mc(e,p);if(!y){d({kind:"note",tone:"error",text:`No session matches "${p}".`});return}xe(y.uuid),ke.current={uuid:y.uuid,firstPrompt:y.firstPrompt,titleFired:!0},Ge(y.name),go(y.uuid),ze.current=[];let I=mo(e,y.uuid);for(let M of I)d(M);d({kind:"note",tone:"ok",text:I.length?`Resumed "${y.name}" \u2014 restored ${I.length} message(s); continue where you left off.`:`Resumed "${y.name}" (agent memory reconnected; no saved transcript to replay).`});return}case"workspace":{let p=h.join(" ").trim(),k;try{k=await Mt(e,s)}catch(I){d({kind:"note",tone:"error",text:`Could not list workspaces: ${I.message}`});return}if(!p){let I=k.workspaces.map(N=>({id:N.id,name:N.name??N.id,type:N.workspace_type}));if(I.length===0){d({kind:"note",tone:"info",text:"No workspaces in this project."});return}let M=Math.max(0,I.findIndex(N=>N.id===k.current));Ve({cursor:M,current:M,rows:I,profile:k.profile,projectId:k.projectId,environment:k.environment});return}let y=Pn(k.workspaces,p);if(!y){d({kind:"note",tone:"error",text:`No workspace matches "${p}".`});return}_o(k.profile,k.projectId,y);return}case"publish":{if(h[0]&&h[0]!=="--all"){await be(Yt(h[0]),{display:`/publish ${h[0]}`,echoDisplay:!0});return}let p=h[0]==="--all";d({kind:"note",tone:"info",text:"Checking what needs publishing\u2026"});try{let k=await ho({cwd:e,profileName:s}),y=Gt(k.entries);if(y.length===0){d({kind:"note",tone:"ok",text:"\u2713 Nothing is ahead of Document360 \u2014 no publish candidates."});let M=k.counts["unknown-base"]??0;M>0&&d({kind:"note",tone:"info",text:`(${M} article(s) have no sync base yet \u2014 publish those by path if needed.)`});return}if(p){await be(ht(y.map(M=>M.path)),{display:"/publish --all",echoDisplay:!0});return}let I=y.length>1?[{path:"--all",label:`publish all ${y.length} candidates in one run`},...y]:y;Je({cursor:0,rows:I})}catch(k){d({kind:"note",tone:"error",text:`Could not compute sync status: ${k.message}`}),d({kind:"note",tone:"info",text:"Publish a specific article: /publish <article-path>"})}return}case"preview":{let p=h.join(" ").trim();if(!p){let y=[];try{y=Object.keys(_s(e,he(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}Ne({query:"",cursor:0,paths:y});return}let k=rc(p)?p:vt(e,p);if(js(k)){try{d({kind:"preview",name:dn(k),text:Os(Es(k,"utf8"))})}catch(y){d({kind:"note",tone:"error",text:`Could not read ${k}: ${y.message}`})}return}if(Ic.test(p)){try{let y=he(e,s),I={profile:y.name,connection:y.connection},M=y.project.projectId??cc(I),N=await uc(I,M,p);d({kind:"preview",name:N.title??p,text:N.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:`"${p}" is neither a file (relative to ${e}) nor an article id.`});return}case"model":{let p=h[0]?.trim();if(!p){let M=Kt($o(e));Me({cursor:M,current:M});return}let{lines:k,changed:y,effective:I}=wt(e,p);for(let M of k)d({kind:"note",tone:M.startsWith("\u26A0")?"warn":M.startsWith("\u2713")?"ok":"info",text:M});y&&(Ao(M=>M+1),le.current?.setModel(I));return}case"convert":{if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let{scope:p,run:k}=Wn(h),y=Sc(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 I=Fn(y,p);if(I.length===0){d({kind:"note",tone:"error",text:`No tracked articles under "${p}". (${y.length} are tracked overall.)`});return}let M=Ms(I),N=3,A=`/convert${p?` --scope ${p}`:""} --run`,_=yo(e,"light");if(!k){let T=Ns({files:Hn(e,I),op:"convert",model:_.model}),U=p?`Scope: ${p} (${I.length} of ${y.length} tracked)
|
|
82
|
+
`:"",ee=`
|
|
83
|
+
Model: ${_.model}${_.forced?" (forced)":" \u2014 mechanical work; /model to override"}`;d({kind:"note",tone:"info",text:U+qn(M,T,N).join(`
|
|
84
|
+
`)+ee}),K([A]);return}He.current=Date.now(),st(0),q(!0);let C=new AbortController;$e.current=C,Z({total:M.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Converting ${I.length} articles across ${M.length} partitions (\u2264${N} agents at once) on ${_.model}\u2026 (esc to stop)`});try{for await(let T of ko({cwd:e,partitions:M,promptFor:Bn,concurrency:N,profileName:s,allowProdWrites:lt.current,model:_.model,signal:C.signal}))if(T.type==="partition_status")T.status==="running"?(Z(U=>U&&{...U,active:[...U.active,T.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${T.label} \u2014 converting\u2026`})):(Z(U=>U&&{...U,active:U.active.filter(ee=>ee!==T.label),done:U.done+1,lastAt:Date.now()}),d({kind:"note",tone:T.status==="done"?"ok":"error",text:` ${T.status==="done"?"\u2713":"\u2717"} ${T.label}`}));else if(T.type==="partition_event")Z(U=>{if(!U)return U;let ee={...U,lastAt:Date.now()};return T.event.type==="tool"?ee.tools=U.tools+1:T.event.type==="text"&&(ee.chars=U.chars+T.event.delta.length),ee});else if(T.type==="run_done"){ct(O=>O+T.totalCostUsd),at(O=>O+T.results.reduce((Y,te)=>Y+te.outputTokens,0));let U=T.aborted?"Stopped. ":"",ee=t.kind==="api"?"api":"subscription";d({kind:"note",tone:T.aborted?"warn":T.ok?"ok":"warn",text:U+zn(T.results,M,ee).join(`
|
|
85
|
+
`)})}}catch(T){d({kind:"note",tone:"error",text:`Convert run failed: ${T.message}`})}finally{$e.current=null,Z(null),q(!1)}return}case"write":{if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=Tc(e);if(p.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:k,path:y,run:I}=Yn(h),{docsDir:M,targets:N,reason:A}=Qn(e,p,{scope:k,path:y});if(N.length===0){d({kind:"note",tone:"warn",text:A??"Nothing to write."});return}let _=Ms(N),C=5,T=yo(e,"standard");if(!I&&!!!y){let O=Ns({files:Xn(e,p,N),op:"write",model:T.model}),Y=k?`Scope: ${k} (${N.length} pending)
|
|
86
|
+
`:"",te=`
|
|
87
|
+
Model: ${T.model}${T.forced?" (forced)":" \u2014 authoring; /model to override"}`;d({kind:"note",tone:"info",text:Y+Jn(_,O,C).join(`
|
|
88
|
+
`)+te}),K([`/write${k?` --scope ${k}`:" --all"} --run`]);return}He.current=Date.now(),st(0),q(!0);let ee=new AbortController;$e.current=ee,Z({verb:"Writing",total:_.length,done:0,active:[],tools:0,chars:0,lastAt:Date.now(),startedAt:Date.now()}),d({kind:"note",tone:"info",text:`Writing ${N.length} article${N.length===1?"":"s"} across ${_.length} partition${_.length===1?"":"s"} (\u2264${C} agents at once) on ${T.model}${T.forced?" (forced)":""}\u2026 (esc to stop)`});try{for await(let O of ko({cwd:e,partitions:_,promptFor:Y=>Vn(Y,M),concurrency:C,profileName:s,allowProdWrites:lt.current,model:T.model,signal:ee.signal}))if(O.type==="partition_status")O.status==="running"?(Z(Y=>Y&&{...Y,active:[...Y.active,O.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${O.label} \u2014 writing\u2026`})):(Z(Y=>Y&&{...Y,active:Y.active.filter(te=>te!==O.label),done:Y.done+1,lastAt:Date.now()}),d({kind:"note",tone:O.status==="done"?"ok":"error",text:` ${O.status==="done"?"\u2713":"\u2717"} ${O.label}`}));else if(O.type==="partition_event")Z(Y=>{if(!Y)return Y;let te={...Y,lastAt:Date.now()};return O.event.type==="tool"?te.tools=Y.tools+1:O.event.type==="text"&&(te.chars=Y.chars+O.event.delta.length),te});else if(O.type==="run_done"){ct(Cn=>Cn+O.totalCostUsd),at(Cn=>Cn+O.results.reduce((Qs,Zs)=>Qs+Zs.outputTokens,0));let Y=O.aborted?"Stopped. ":"",te=t.kind==="api"?"api":"subscription";d({kind:"note",tone:O.aborted?"warn":O.ok?"ok":"warn",text:Y+Kn(O.results,_,te).join(`
|
|
89
|
+
`)})}}catch(O){d({kind:"note",tone:"error",text:`Write run failed: ${O.message}`})}finally{$e.current=null,Z(null),q(!1)}return}case"sync":{let p=(h[0]??"status").toLowerCase();try{if(p==="status"){d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"});let k=await ho({cwd:e,profileName:s});d({kind:"note",tone:"info",text:Jt(k).join(`
|
|
90
|
+
`)});return}if(p==="pull"){let k=h[1];if(!k){d({kind:"note",tone:"error",text:"Usage: /sync pull <article-path> | --all"});return}let y;if(k==="--all"){if(d({kind:"note",tone:"info",text:"Checking Document360 for drift\u2026"}),y=(await ho({cwd:e,profileName:s})).entries.filter(N=>N.status==="remote-ahead"&&N.path).map(N=>N.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=[k.replace(/\\/g,"/")];let I=[];for(let M of y)I.push(await $c({cwd:e,profileName:s,relPath:M}));Se.current=I,Dt();return}d({kind:"note",tone:"error",text:`Unknown subcommand: /sync ${p} \u2014 use /sync or /sync pull <path>|--all.`})}catch(k){d({kind:"note",tone:"error",text:`Sync failed: ${k.message}`})}return}case"scope":{if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let p=xc(e);if(p.length===0){d({kind:"note",tone:"info",text:'No candidate source folders found. Set "authoritativeSourceFiles" in .d360-writer.json manually.'});return}Ke({cursor:0,rows:p.map(k=>({...k,checked:k.recommended}))});return}case"audit":case"capture-setup":{let k=await(i==="audit"?Xt:xt)(h,void 0);if(k.kind==="forward-to-agent"&&k.prompt&&await be(k.prompt,{display:k.display,echoDisplay:!0}),i==="capture-setup"){let y=io(e);y&&d({kind:"note",tone:"info",text:y.join(`
|
|
91
|
+
`)})}return}case"screenshot":{let p=oo(h);if(p.mode==="list"){if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}d({kind:"note",tone:"info",text:so(ro(e,p.scope),p.scope).join(`
|
|
92
|
+
`)});return}if(p.mode==="single"){let C=await Zt(h);C.kind==="forward-to-agent"&&C.prompt&&await be(C.prompt,{display:C.display,echoDisplay:!0});return}if(!se(e)){d({kind:"note",tone:"error",text:"No d360-writer config here. Run /init first."});return}let k=p.mode==="scope"?p.scope:void 0,y=Pc(e,{scope:k}).map(C=>C.id);if(y.length===0){d({kind:"note",tone:"info",text:k?`No screenshot placeholders under ${k}.`:"No screenshot placeholders found in the docs."});return}let I=6,M=Cc(y,3),N=yo(e,"standard");He.current=Date.now(),st(0),q(!0);let A=new AbortController;$e.current=A,Z({verb:"Authoring",total:M.length,done:0,active:[],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 ${M.length} partition${M.length===1?"":"s"} (\u2264${I} agents at once) on ${N.model}${N.forced?" (forced)":""}\u2026 (esc to stop)`});let _=!1;try{for await(let C of ko({cwd:e,partitions:M,promptFor:es,concurrency:I,profileName:s,allowProdWrites:lt.current,model:N.model,signal:A.signal}))if(C.type==="partition_status")C.status==="running"?(Z(T=>T&&{...T,active:[...T.active,C.label],lastAt:Date.now()}),d({kind:"note",tone:"info",text:` \u25B8 ${C.label} \u2014 authoring\u2026`})):(Z(T=>T&&{...T,active:T.active.filter(U=>U!==C.label),done:T.done+1,lastAt:Date.now()}),d({kind:"note",tone:C.status==="done"?"ok":"error",text:` ${C.status==="done"?"\u2713":"\u2717"} ${C.label}`}));else if(C.type==="partition_event")Z(T=>{if(!T)return T;let U={...T,lastAt:Date.now()};return C.event.type==="tool"?U.tools=T.tools+1:C.event.type==="text"&&(U.chars=T.chars+C.event.delta.length),U});else if(C.type==="run_done"){ct(O=>O+C.totalCostUsd),at(O=>O+C.results.reduce((Y,te)=>Y+te.outputTokens,0));let T=C.results.filter(O=>O.ok).length,U=C.aborted?"Stopped. ":"";_=!C.aborted&&p.setup;let ee=C.aborted?"":_?" Refreshing the capture-setup checklist\u2026":" Next: /capture-setup, then d360-capture capture.";d({kind:"note",tone:C.aborted?"warn":C.ok?"ok":"warn",text:`${U}Authored specs \u2014 ${T}/${C.results.length} batches ok.${ee}`})}}catch(C){d({kind:"note",tone:"error",text:`Spec authoring failed: ${C.message}`})}finally{$e.current=null,Z(null),q(!1)}if(_){let C=await xt();C.kind==="forward-to-agent"&&C.prompt&&await be(C.prompt,{display:C.display,echoDisplay:!0});let T=io(e);T&&d({kind:"note",tone:"info",text:T.join(`
|
|
93
|
+
`)})}return}case"reset":{let p=lo(e);if(p.length===0){d({kind:"note",tone:"info",text:"Nothing to reset \u2014 no d360-writer files found in this repo."});return}hn.current={repoName:dn(e),targets:p},d({kind:"note",tone:"warn",text:co(e,p).join(`
|
|
94
|
+
`)});return}default:d({kind:"note",tone:"error",text:`Unknown command: /${i} \u2014 type /help.`})}},[e,r,s,Dt,d,xe,be]),It=X(a=>{let l=(a??j).trim();if(b(""),Pt(null),Be(0),l.startsWith("/")){let h=k=>k.trim().split(/\s+/).slice(1).join(" "),p=h(l);K(k=>k.filter(y=>y.trim()!==l&&!(p&&h(y)===p)))}if(!l||No(l)||Mo(l))return;gn.current.push(l);let i=Cs(l,vo.current);l.startsWith("/")?Lo(i):be(i,{display:l})},[j,No,Mo,Lo,be]),Oo=X(a=>{if(a.length>1){if(a.includes("\x1B"))return;let l=xs(a);if(bs(l)){let i=vs(++Ys.current,l);vo.current.set(i,l),v(i)}else v(l);return}v(a)},[v]),Qe=Math.max(10,Ue-6),Uo=un(()=>an(m.text,Qe),[m.text,Qe]),vn=X(a=>w(l=>({...l,pos:Math.max(0,Math.min(l.text.length,l.pos+a))})),[]),_t=X(a=>w(l=>({...l,pos:Ss(an(l.text,Qe),l.pos,a)})),[Qe]),dt=X(a=>w(l=>({...l,pos:Ts(an(l.text,Qe),l.pos,a)})),[Qe]),Js=["\x1B[H","\x1B[1~","\x1BOH"],Ks=["\x1B[F","\x1B[4~","\x1BOF"],Wo=X((a,l)=>l.leftArrow?(vn(-1),!0):l.rightArrow?(vn(1),!0):a&&Js.includes(a)?(dt("start"),!0):a&&Ks.includes(a)?(dt("end"),!0):l.ctrl&&a==="a"?(dt("start"),!0):l.ctrl&&a==="e"?(dt("end"),!0):!1,[vn,dt]);return nc((a,l)=>{if(l.ctrl&&a==="c"){le.current?.close(),r();return}if(!G){if(E){if(l.escape){if($e.current){$e.current.signal.aborted||(d({kind:"note",tone:"warn",text:"\u238B Stopping the convert run (finishing in-flight articles)\u2026"}),$e.current.abort());return}Tt.current||(Tt.current=!0,d({kind:"note",tone:"warn",text:"\u238B Interrupting\u2026"}),le.current?.interrupt());return}if(l.return){let i=j.trim();if(!i)return;Le.current.push(i),kn([...Le.current]),b("");return}if(Wo(a,l))return;if(l.upArrow){_t(-1);return}if(l.downArrow){_t(1);return}if(l.backspace||l.delete){$();return}a&&!l.ctrl&&!l.meta&&Oo(a);return}if(_e){if(l.upArrow){Me(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Me(i=>i&&{...i,cursor:Math.min(ge.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ge.length){Me(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ge[_e.cursor];Me(null);let{lines:h,changed:p,effective:k}=wt(e,i.value??"default");for(let y of h)d({kind:"note",tone:y.startsWith("\u26A0")?"warn":y.startsWith("\u2713")?"ok":"info",text:y});p&&(Ao(y=>y+1),le.current?.setModel(k));return}if(a==="s"){let i=ge[_e.cursor];Me(null),le.current?.setModel(i.value??void 0),d({kind:"note",tone:"ok",text:`\u2713 Using ${i.label} for this session only (your saved default is unchanged).`});return}if(l.escape){Me(null);return}return}if(ye){if(l.upArrow){Ye(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ye(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ye.rows.length){Ye(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return||a==="s"){let i=ye.rows[ye.cursor];Ye(null),bn(i.name,l.return===!0);return}if(l.escape){Ye(null);return}return}if(Te){if(l.upArrow){Xe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Xe(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=Te.rows.length){Xe(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=Te.rows[Te.cursor].name;if(Xe(null),!ir(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 p=!1;try{let k=Fe(i);p=!!k&&!(De(k)&&!k.refreshToken)}catch{}p?(d({kind:"note",tone:"info",text:`Already signed in to ${i} \u2014 next: pick a workspace.`}),K(["/workspace"])):(d({kind:"note",tone:"info",text:`Next: sign in to Document360 (${i}).`}),K(["/login"])),Pe(k=>k+1);return}if(l.escape){Xe(null);return}return}if(ne){if(l.upArrow){Ve(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ve(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=ne.rows.length){Ve(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=ne.rows[ne.cursor],{profile:h,projectId:p}=ne;Ve(null),_o(h,p,i);return}if(l.escape){Ve(null);return}return}if(oe){if(l.upArrow){Ke(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ke(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a===" "){Ke(i=>i&&{...i,rows:i.rows.map((h,p)=>p===i.cursor?{...h,checked:!h.checked}:h)});return}if(l.return){let i=oe.rows.filter(h=>h.checked).map(h=>h.path);if(Ke(null),i.length===0){d({kind:"note",tone:"info",text:"Nothing selected \u2014 scope unchanged."});return}jn(e,i),d({kind:"note",tone:"ok",text:`\u2713 Scoped to ${i.length} folder(s) \u2014 written to .d360-writer.json`});for(let h of i)d({kind:"note",tone:"info",text:` ${h}`});d({kind:"note",tone:"info",text:"Next: ask me to analyze these folders and propose a docs structure."});return}if(l.escape){Ke(null);return}return}if(we){if(l.upArrow){Je(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Je(i=>i&&{...i,cursor:Math.min(i.rows.length-1,i.cursor+1)});return}if(a&&/^[1-9]$/.test(a)&&Number(a)<=we.rows.length){Je(i=>i&&{...i,cursor:Number(a)-1});return}if(l.return){let i=we.rows[we.cursor],h=we.rows.filter(p=>p.path!=="--all").map(p=>p.path);Je(null),be(i.path==="--all"?ht(h):Yt(i.path),{display:i.path==="--all"?"/publish --all":`/publish ${i.path}`,echoDisplay:!0});return}if(l.escape){Je(null);return}return}if(ae){if(l.escape){Ne(null);return}if(l.upArrow){Ne(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Ne(i=>i&&{...i,cursor:Math.min(Math.max(0,jt.length-1),i.cursor+1)});return}if(l.return){let i=jt[ae.cursor];if(i){Ne(null);try{d({kind:"preview",name:dn(i),text:Os(Es(vt(e,i),"utf8"))})}catch(h){d({kind:"note",tone:"error",text:`Could not read ${i}: ${h.message}`})}}return}if(l.backspace||l.delete){Ne(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Ne(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(ce){if(l.escape){Oe(null);return}if(l.upArrow){Oe(i=>i&&{...i,cursor:Math.max(0,i.cursor-1)});return}if(l.downArrow){Oe(i=>i&&{...i,cursor:Math.min(Math.max(0,Et.length-1),i.cursor+1)});return}if(l.return){let i=Et[ce.cursor];if(i){Oe(null),xe(i.uuid),ke.current={uuid:i.uuid,firstPrompt:i.firstPrompt,titleFired:!0},Ge(i.name),go(i.uuid),ze.current=[];let h=mo(e,i.uuid);for(let p of h)d(p);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(l.backspace||l.delete){Oe(i=>i&&{...i,query:i.query.slice(0,-1),cursor:0});return}if(a&&!l.ctrl&&!l.meta&&a.length===1){Oe(i=>i&&{...i,query:i.query+a,cursor:0});return}return}if(l.tab&&!j&&pe){b(pe),fe(null),Ct.current++;return}if(!j&&rt.length>0&&a&&/^[1-9]$/.test(a)){let i=rt[Number(a)-1];if(i){b(i);return}}if(!Wo(a,l)){if(Do){if(l.upArrow){Be(i=>Math.max(0,i-1));return}if(l.downArrow){Be(i=>Math.min(ut.length-1,i+1));return}if(l.tab){b("/"+(ut[it]?.name??"")+" "),Be(0);return}if(l.return){let i=ut[it];if(i){let h=j.trim().slice(1).split(/\s+/).slice(1).join(" ");if(tr(i.usage)&&!h){b("/"+i.name+" "),Be(0);return}It("/"+i.name+(h?" "+h:""));return}}}else{if(l.upArrow){if(j!==""&&qe===null){_t(-1);return}let i=gn.current;if(!i.length)return;let h=qe===null?i.length-1:Math.max(0,qe-1);Pt(h),b(i[h]??"");return}if(l.downArrow){if(j!==""&&qe===null){_t(1);return}let i=gn.current;if(qe===null)return;let h=qe+1;h>=i.length?(Pt(null),b("")):(Pt(h),b(i[h]??""));return}}if(l.return){It();return}if(l.backspace||l.delete){$();return}if(l.escape){b(""),Be(0),fe(null),K([]);return}a&&!l.ctrl&&!l.meta&&Oo(a)}}}),Ie(()=>{if(E||G)return;let a=Le.current.shift();a!==void 0&&(kn([...Le.current]),It(a))},[E,G,It]),F(J,{flexDirection:"column",width:Ue,children:[$n!==null&&F(J,{marginTop:1,flexDirection:"column",children:[$n.truncated&&S(P,{dimColor:!0,children:"\u2026"}),S(P,{children:$n.text})]}),E&&(So?S(Lc,{p:So}):S(Mc,{startTime:He.current,chars:Gs})),S(J,{borderStyle:"round",borderColor:Eo.prod?"yellow":z,borderTop:!0,borderBottom:!0,borderLeft:!1,borderRight:!1,marginTop:1,flexDirection:"column",children:j?Uo.map((a,l)=>{let i=m.text.slice(a.start,a.end),h=l===cn(Uo,m.pos),p=Math.min(m.pos,a.end)-a.start;return F(P,{children:[S(P,{color:z,children:l===0?"> ":" "}),h?F(wo,{children:[i.slice(0,p),S(pn,{ch:i[p]??" "}),i.slice(p+1)]}):i||" "]},`${l}-${a.start}`)}):F(P,{children:[S(P,{color:z,children:"> "}),pe&&!E?F(wo,{children:[S(pn,{ch:pe[0],dim:!0}),S(P,{color:"gray",children:pe.slice(1)}),S(P,{dimColor:!0,children:" (tab)"})]}):wn.isSetup||!zs?F(wo,{children:[S(pn,{ch:wn.text[0],dim:!0}),S(P,{color:"gray",children:wn.text.slice(1)})]}):S(pn,{ch:" "})]})}),Po.length>0&&S(J,{flexDirection:"column",paddingX:1,children:Po.map((a,l)=>S(P,{color:"gray",children:`\u29D7 queued: ${a}`},`${l}-${a.slice(0,24)}`))}),_e?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Select model"}),S(P,{color:"gray",children:"Your pick becomes your personal default for new sessions (team .d360-writer.json still wins)."}),ge.map((a,l)=>F(P,{color:l===_e.cursor?z:void 0,children:[l===_e.cursor?"\u276F ":" ",`${l+1}. ${a.label}${l===_e.current?" \u2714":""}`.padEnd(16),S(P,{color:"gray",children:a.desc})]},a.label)),S(P,{dimColor:!0,children:"enter set as default \xB7 s this session only \xB7 esc cancel"})]}):ye?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Switch connection profile"}),ye.rows.map((a,l)=>F(P,{color:l===ye.cursor?z:void 0,children:[l===ye.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ye.current?" \u2714":""}`.padEnd(20),S(P,{color:"gray",children:`${a.env} \xB7 ${a.who??"not signed in"}`}),a.prod?S(P,{color:"yellow",bold:!0,children:" \u26A0 PRODUCTION"}):null]},a.name)),S(P,{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(P,{color:z,bold:!0,children:"Pick your Document360 environment"}),Te.rows.map((a,l)=>F(P,{color:l===Te.cursor?z:void 0,children:[l===Te.cursor?"\u276F ":" ",`${l+1}. ${a.name}`.padEnd(16),S(P,{color:"gray",children:a.apiUrl})]},a.name)),S(P,{dimColor:!0,children:"enter select \xB7 esc cancel"})]}):ne?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Switch workspace"}),S(P,{dimColor:!0,children:`environment ${ne.environment} \xB7 project ${ne.projectId.slice(0,8)}\u2026`}),ne.rows.map((a,l)=>F(P,{color:l===ne.cursor?z:void 0,children:[l===ne.cursor?"\u276F ":" ",`${l+1}. ${a.name}${l===ne.current?" \u2714":""}`.padEnd(30),S(P,{color:"gray",children:a.type??""})]},a.id)),S(P,{dimColor:!0,children:"enter switch \xB7 esc cancel"})]}):oe?(()=>{let l=Math.min(Math.max(0,oe.cursor-Math.floor(7)),Math.max(0,oe.rows.length-14)),i=oe.rows.slice(l,l+14),h=oe.rows.filter(p=>p.checked).length;return F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:`Which folders back the user docs? (${h} selected of ${oe.rows.length})`}),l>0?S(P,{dimColor:!0,children:` \u2191 ${l} more`}):null,i.map((p,k)=>{let y=l+k;return F(P,{color:y===oe.cursor?z:void 0,children:[y===oe.cursor?"\u276F ":" ",p.checked?"\u25C9 ":"\u25CB ",p.path.padEnd(Math.min(48,Ue-34)),S(P,{color:"gray",children:Vt(p)})]},p.path)}),l+14<oe.rows.length?S(P,{dimColor:!0,children:` \u2193 ${oe.rows.length-l-14} more`}):null,S(P,{dimColor:!0,children:"space toggle \xB7 enter save \xB7 esc cancel"})]})})():we?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:"Publish which article?"}),we.rows.map((a,l)=>F(P,{color:l===we.cursor?z:void 0,children:[l===we.cursor?"\u276F ":" ",`${l+1}. ${a.path}`.padEnd(Math.min(56,Ue-30)),S(P,{color:"gray",children:a.label})]},a.path)),S(P,{dimColor:!0,children:"enter publish (draft) \xB7 esc cancel"})]}):ae?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:`Preview article${ae.query?` \u2014 filter: ${ae.query}`:" (type to filter)"}`}),jt.length===0?S(P,{color:"gray",children:"no articles match"}):jt.map((a,l)=>F(P,{color:l===ae.cursor?z:void 0,children:[l===ae.cursor?"\u276F ":" ",a]},a)),S(P,{dimColor:!0,children:"enter preview \xB7 esc cancel"})]}):ce?F(J,{flexDirection:"column",paddingX:1,children:[S(P,{color:z,bold:!0,children:`Resume session${ce.query?` \u2014 filter: ${ce.query}`:" (type to filter)"}`}),Et.length===0?S(P,{color:"gray",children:"no sessions match"}):Et.map((a,l)=>F(P,{color:l===ce.cursor?z:void 0,children:[l===ce.cursor?"\u276F ":" ",a.name.slice(0,28).padEnd(30),S(P,{color:l===ce.cursor?z:"gray",children:a.firstPrompt.slice(0,Math.max(10,Ue-40))})]},a.uuid)),S(P,{dimColor:!0,children:"enter resume \xB7 esc cancel"})]}):Do?S(J,{flexDirection:"column",children:ut.map((a,l)=>F(P,{color:l===it?z:void 0,children:[l===it?"\u276F ":" ",a.usage.padEnd(22)," ",S(P,{color:l===it?z:"gray",children:a.name==="model"&&yn?`${a.desc} (currently ${yn})`:a.desc})]},a.name))}):!j&&rt.length>0?F(J,{flexDirection:"column",paddingX:1,children:[rt.map((a,l)=>F(P,{children:[S(P,{color:z,children:l+1})," ",a.slice(0,Math.max(20,Ue-5))]},a)),S(P,{dimColor:!0,children:`press 1-${rt.length} to fill the command \xB7 esc dismiss`})]}):S(J,{paddingX:1,children:F(P,{color:"gray",children:[Eo.prod?"\u26A0 PRODUCTION \xB7 ":"",`/help \xB7 ${yn??"model n/a"}${t.kind==="api"?Ro>0?` \xB7 ${ue(Ro)}`:"":To>0?` \xB7 ${nt(To)}`:""} \xB7 \u2191 history \xB7 ctrl+c exit`]})})]})}H();import{jsx as Wc}from"react/jsx-runtime";async function Hs(e=process.cwd(),t="auto",n,o="0.0.0"){let r=Uc(t);r.kind==="none"&&(console.error(""),console.error(x("ANTHROPIC_API_KEY is not set (required for --auth api).")),console.error(`Get a key at ${R("https://console.anthropic.com/settings/keys")}`),console.error(`Or use your Claude subscription instead: ${R("d360-writer --auth subscription")}`),process.exit(2));let{waitUntilExit:s}=Oc(Wc(Fs,{cwd:e,auth:r,profileName:n,version:o}));await s(),process.stdout.write(`
|
|
95
|
+
`),process.exit(0)}var qc=Hc(import.meta.url),Bs=qc("../package.json"),Ce=new Fc;function xo(e){e.env&&(console.error("\u2717 --env was replaced by --profile (connection profiles). Use: --profile <name>"),process.exit(2))}Ce.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=>{xo(e),await Ut({profile:e.profile,manual:e.manual})});Ce.command("logout").description("Remove the stored Document360 session").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{xo(e),await Jo({profile:e.profile})});Ce.command("whoami").description("Show the current Document360 identity (refreshes if expired)").option("--profile <name>","Connection profile").option("--env <name>",!1).action(async e=>{xo(e),await Vo({profile:e.profile})});var bo=Ce.command("profile").description("Manage connection profiles for the current repo");bo.command("list",{isDefault:!0}).description("List profiles (\u25CF = default)").action(()=>Wt(process.cwd()));bo.command("use <name>").description("Set the default profile for this repo").action(e=>Ft(process.cwd(),e));bo.command("show [name]").description("Print the resolved profile (connection + project)").action(e=>Ht(process.cwd(),e));var qs=Ce.command("workspace").description("Choose the Document360 workspace for this repo (active profile's project)");qs.command("select",{isDefault:!0}).description("Interactively pick the workspace (lists in non-TTY)").option("--profile <name>","Connection profile").action(e=>Ze(process.cwd(),e.profile));qs.command("use <name>").description("Set the workspace by name (scriptable)").option("--profile <name>","Connection profile").action(async(e,t)=>{process.exitCode=await qo(process.cwd(),e,t.profile)});Ce.command("logs").description("Show the Document360 API log files (send these to support when reporting a problem)").action(()=>Qo());Ce.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(()=>(Qt(),Xr)),n=await t(process.cwd());for(let o of e(n))console.log(o);process.exitCode=n.some(o=>o.level==="fail")?1:0});Ce.name("d360-writer").description("Standalone documentation agent CLI. Reads your code, writes your docs.").version(Bs.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(Bc.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 Zo(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 ms(e.cwd,e.auth,e.profile):await Hs(e.cwd,e.auth,e.profile,Bs.version)});Ce.parseAsync(process.argv).catch(e=>{console.error(""),console.error(`\u2717 ${e.message}`),process.exit(1)});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type CostEstimate, type DocsPlanEntry, type Partition, type PartitionResult, type TargetFile } from 'document360-engine';
|
|
2
|
+
import type { ReplContext } from '../repl.js';
|
|
3
|
+
import type { SlashCommandResult } from './index.js';
|
|
4
|
+
/** Partitions running at once — local-only work (no D360 rate limit), so higher than /convert. */
|
|
5
|
+
export declare const WRITE_CONCURRENCY = 5;
|
|
6
|
+
/** Parse /write args: `--run`/`--yes`, `--scope <cat>` (or `--scope=…`), `--all`, or a bare `<path>`. */
|
|
7
|
+
export declare function parseWriteArgs(args: string[]): {
|
|
8
|
+
scope?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
run: boolean;
|
|
11
|
+
};
|
|
12
|
+
/** Drop a leading `<docsDir>/` so a path given repo-relative matches the docsDir-relative plan keys. */
|
|
13
|
+
export declare function toPlanRelative(p: string, docsDir: string): string;
|
|
14
|
+
/** Keep only plan paths under `scope` (a category prefix or an exact .md path). No scope → all. */
|
|
15
|
+
export declare function applyScope(paths: string[], scope?: string): string[];
|
|
16
|
+
/** Of the given docsDir-relative paths, the ones not yet written (file missing or empty). FS. */
|
|
17
|
+
export declare function pendingArticles(cwd: string, docsDir: string, paths: string[]): string[];
|
|
18
|
+
/** Size each target by the SUM of its plan SOURCE bytes (the article file doesn't exist yet). The
|
|
19
|
+
1-byte floor keeps every planned article in the estimate so its per-article overhead counts. */
|
|
20
|
+
export declare function sizeWriteTargets(cwd: string, entries: DocsPlanEntry[], paths: string[]): TargetFile[];
|
|
21
|
+
/** The agent prompt for one partition: author each listed article from the plan. Each is
|
|
22
|
+
independent; purpose + sources are looked up from the pinned plan, grounded in source. */
|
|
23
|
+
export declare function buildWritePrompt(partition: Partition, docsDir: string): string;
|
|
24
|
+
/** Preview lines for bare `/write`: the partition plan + the cost band, before any spend. */
|
|
25
|
+
export declare function previewLines(partitions: Partition[], estimate: CostEstimate, concurrency: number): string[];
|
|
26
|
+
/**
|
|
27
|
+
* Final summary, a bookend to the opening "Writing N articles across M partitions…" line. Usage
|
|
28
|
+
* shows dollars in API mode but OUTPUT TOKENS on a Claude subscription (per-token cost is
|
|
29
|
+
* meaningless there). `partitions` maps each result back to its article count (results carry only
|
|
30
|
+
* an index).
|
|
31
|
+
*/
|
|
32
|
+
export declare function summaryLines(results: PartitionResult[], partitions: Partition[], usage: 'api' | 'subscription'): string[];
|
|
33
|
+
/** Resolve the docsDir-relative article paths a /write invocation targets, from the pinned plan.
|
|
34
|
+
Returns the target list + context for messaging. Single path → that article (forced, even if it
|
|
35
|
+
exists); scope/all → not-yet-written articles only. FS-touching (existence checks). */
|
|
36
|
+
export declare function selectWriteTargets(cwd: string, entries: DocsPlanEntry[], args: {
|
|
37
|
+
scope?: string;
|
|
38
|
+
path?: string;
|
|
39
|
+
}): {
|
|
40
|
+
docsDir: string;
|
|
41
|
+
targets: string[];
|
|
42
|
+
planCount: number;
|
|
43
|
+
reason?: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Classic-REPL `/write` — parity with the TUI handler (App.tsx): bare `/write [--scope x]` previews
|
|
47
|
+
* (plan + cost band, no spend); `/write --run` fans the planned articles out across parallel agents.
|
|
48
|
+
* A single `<path>` writes that article immediately. Mid-run abort is TUI-only (Esc); Ctrl+C exits
|
|
49
|
+
* the REPL here.
|
|
50
|
+
*/
|
|
51
|
+
export declare function writeCommand(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.
|
|
3
|
+
"version": "0.4.35",
|
|
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.
|
|
37
|
+
"document360-engine": "^0.2.23",
|
|
38
38
|
"ink": "^5.2.1",
|
|
39
39
|
"picocolors": "^1.1.1",
|
|
40
40
|
"react": "^18.3.1",
|