document360-engine 0.2.3 → 0.2.4

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/index.js CHANGED
@@ -1,21 +1,21 @@
1
- import{readdirSync as Jr,readFileSync as jt,existsSync as Ae,statSync as Vr}from"node:fs";import{join as Ee}from"node:path";import{query as Gr}from"@anthropic-ai/claude-agent-sdk";import{existsSync as $t}from"node:fs";import{isAbsolute as Ot,relative as Mt,resolve as Ue}from"node:path";var Ne=new Set(["Edit","Write","MultiEdit","NotebookEdit"]);function de(e,t,r){let n=Ot(t)?t:Ue(e,t),o=Mt(e,n).replace(/\\/g,"/");return o===""||o.startsWith("..")?!1:/\.(md|markdown)$/i.test(o)||o===".d360-writer.json"||o==="d360-category-map.json"||o===".d360-writer"||o.startsWith(".d360-writer/")?!0:[r?.captureDir,r?.outputDir].filter(i=>typeof i=="string"&&i.length>0).map(i=>i.replace(/\\/g,"/").replace(/\/+$/,"")).some(i=>o===i||o.startsWith(`${i}/`))}var Ut=/^(\/dev\/null|nul|null)$/i,X=e=>e.replace(/^['"]/,"").replace(/['"]$/,""),Nt=new Set(["rm","rmdir","mv","truncate","touch","mkdir","chmod","chown"]),qt=new Set(["cp","ln","install"]);function qe(e,t,r){let n=i=>{let c=X(i);return!c||c.startsWith("&")||c.startsWith("-")||Ut.test(c)?!1:!de(t,c,r)},o=/(?:^|[^0-9>])>>?\s*("[^"]+"|'[^']+'|[^\s|&;<>]+)/g,s;for(;s=o.exec(e);)if(n(s[1]))return X(s[1]);for(let i of e.split(/&&|\|\||[;|]/)){let c=i.trim().split(/\s+/).filter(Boolean);if(c.length===0)continue;let u=c[0].replace(/.*[/\\]/,""),d=c.slice(1),l=d.filter(f=>!f.startsWith("-")),p=[];Nt.has(u)?p=l:qt.has(u)?p=l.slice(-1):u==="tee"?p=l:u==="dd"?p=d.filter(f=>f.startsWith("of=")).map(f=>f.slice(3)):(u==="sed"||u==="perl")&&d.some(f=>/^-i/.test(f)||f==="--in-place")&&(p=l.filter(f=>$t(Ue(t,X(f)))));for(let f of p)if(n(f))return X(f)}return null}function ue(e){return`Writer mode: "${e}" is outside the documentation scope (markdown, capture specs, d360 config). Note the needed change for the engineering team instead of making it. Repos that want the agent to modify source code set "mode": "engineer" in .d360-writer.json.`}import{homedir as Lt}from"node:os";import{join as A,dirname as Be}from"node:path";import{fileURLToPath as Bt}from"node:url";import{existsSync as Fe,mkdirSync as Ft}from"node:fs";var Ht=Bt(import.meta.url),Le=Be(Ht);function Wt(){let e=Le;for(let t=0;t<4;t++){if(Fe(A(e,"package.json")))return e;e=Be(e)}return A(Le,"..","..")}function He(){return A(Wt(),"skills")}function j(){return A(Lt(),".document360-writer")}function pe(){return A(j(),"config.json")}function fe(){return A(j(),"mcp.json")}function ge(){return A(j(),"sessions.json")}function me(e=process.cwd()){return A(e,".d360-writer.json")}function We(e=process.cwd()){return A(e,".d360-writer","skills")}function E(e){Fe(e)||Ft(e,{recursive:!0})}import{readFileSync as ee,writeFileSync as he,existsSync as ye}from"node:fs";import{homedir as zt}from"node:os";import{join as Jt}from"node:path";var Y=class extends Error{};function ze(e,t){if(!e?.profiles||Object.keys(e.profiles).length===0)throw new Y("No connection profiles in .d360-writer.json. This version requires a `profiles` map + `defaultProfile` (the old d360.environment/projectId shape is no longer supported). Run `d360-writer init` to scaffold the new shape.");let r=t??e.defaultProfile;if(!r)throw new Y(`No profile specified and no defaultProfile set. Available: ${Object.keys(e.profiles).join(", ")}`);let n=e.profiles[r];if(!n)throw new Y(`Unknown profile "${r}". Available: ${Object.keys(e.profiles).join(", ")}`);return{name:r,profile:n}}function N(e=process.cwd()){let t=me(e);return ye(t)?JSON.parse(ee(t,"utf8")):null}function Je(e,t=process.cwd()){he(me(t),JSON.stringify(e,null,2)+`
1
+ import{readdirSync as Jr,readFileSync as jt,existsSync as Ae,statSync as Vr}from"node:fs";import{join as Ee}from"node:path";import{query as Gr}from"@anthropic-ai/claude-agent-sdk";import{existsSync as $t}from"node:fs";import{isAbsolute as Ot,relative as Ut,resolve as Me}from"node:path";var Ne=new Set(["Edit","Write","MultiEdit","NotebookEdit"]);function de(e,t,r){let n=Ot(t)?t:Me(e,t),o=Ut(e,n).replace(/\\/g,"/");return o===""||o.startsWith("..")?!1:/\.(md|markdown)$/i.test(o)||o===".d360-writer.json"||o==="d360-category-map.json"||o===".d360-writer"||o.startsWith(".d360-writer/")?!0:[r?.captureDir,r?.outputDir].filter(i=>typeof i=="string"&&i.length>0).map(i=>i.replace(/\\/g,"/").replace(/\/+$/,"")).some(i=>o===i||o.startsWith(`${i}/`))}var Mt=/^(\/dev\/null|nul|null)$/i,X=e=>e.replace(/^['"]/,"").replace(/['"]$/,""),Nt=new Set(["rm","rmdir","mv","truncate","touch","mkdir","chmod","chown"]),qt=new Set(["cp","ln","install"]);function qe(e,t,r){let n=i=>{let c=X(i);return!c||c.startsWith("&")||c.startsWith("-")||Mt.test(c)?!1:!de(t,c,r)},o=/(?:^|[^0-9>])>>?\s*("[^"]+"|'[^']+'|[^\s|&;<>]+)/g,s;for(;s=o.exec(e);)if(n(s[1]))return X(s[1]);for(let i of e.split(/&&|\|\||[;|]/)){let c=i.trim().split(/\s+/).filter(Boolean);if(c.length===0)continue;let u=c[0].replace(/.*[/\\]/,""),d=c.slice(1),l=d.filter(f=>!f.startsWith("-")),p=[];Nt.has(u)?p=l:qt.has(u)?p=l.slice(-1):u==="tee"?p=l:u==="dd"?p=d.filter(f=>f.startsWith("of=")).map(f=>f.slice(3)):(u==="sed"||u==="perl")&&d.some(f=>/^-i/.test(f)||f==="--in-place")&&(p=l.filter(f=>$t(Me(t,X(f)))));for(let f of p)if(n(f))return X(f)}return null}function ue(e){return`Writer mode: "${e}" is outside the documentation scope (markdown, capture specs, d360 config). Note the needed change for the engineering team instead of making it. Repos that want the agent to modify source code set "mode": "engineer" in .d360-writer.json.`}import{homedir as Lt}from"node:os";import{join as A,dirname as Be}from"node:path";import{fileURLToPath as Bt}from"node:url";import{existsSync as Fe,mkdirSync as Ft}from"node:fs";var Ht=Bt(import.meta.url),Le=Be(Ht);function Wt(){let e=Le;for(let t=0;t<4;t++){if(Fe(A(e,"package.json")))return e;e=Be(e)}return A(Le,"..","..")}function He(){return A(Wt(),"skills")}function j(){return A(Lt(),".document360-writer")}function pe(){return A(j(),"config.json")}function fe(){return A(j(),"mcp.json")}function ge(){return A(j(),"sessions.json")}function me(e=process.cwd()){return A(e,".d360-writer.json")}function We(e=process.cwd()){return A(e,".d360-writer","skills")}function E(e){Fe(e)||Ft(e,{recursive:!0})}import{readFileSync as ee,writeFileSync as he,existsSync as ye}from"node:fs";import{homedir as zt}from"node:os";import{join as Jt}from"node:path";var Y=class extends Error{};function ze(e,t){if(!e?.profiles||Object.keys(e.profiles).length===0)throw new Y("No connection profiles in .d360-writer.json. This version requires a `profiles` map + `defaultProfile` (the old d360.environment/projectId shape is no longer supported). Run `d360-writer init` to scaffold the new shape.");let r=t??e.defaultProfile;if(!r)throw new Y(`No profile specified and no defaultProfile set. Available: ${Object.keys(e.profiles).join(", ")}`);let n=e.profiles[r];if(!n)throw new Y(`Unknown profile "${r}". Available: ${Object.keys(e.profiles).join(", ")}`);return{name:r,profile:n}}function N(e=process.cwd()){let t=me(e);return ye(t)?JSON.parse(ee(t,"utf8")):null}function Je(e,t=process.cwd()){he(me(t),JSON.stringify(e,null,2)+`
2
2
  `,"utf8")}function we(){let e=pe();if(!ye(e))return{};try{return JSON.parse(ee(e,"utf8"))}catch{return{}}}function qn(e){E(j()),he(pe(),JSON.stringify(e,null,2)+`
3
3
  `,"utf8")}function Vt(e,t,r,n){return e?{model:e,source:"project"}:t?{model:t,source:"user"}:r?{model:r,source:"env"}:n?{model:n,source:"claude-settings"}:{model:null,source:"claude-default"}}function Gt(){try{let e=JSON.parse(ee(Jt(zt(),".claude","settings.json"),"utf8"));return typeof e.model=="string"&&e.model?e.model:void 0}catch{return}}function Ln(e=process.cwd()){return Vt(N(e)?.defaultModel,we().defaultModel,process.env.ANTHROPIC_MODEL,Gt())}function Ve(){let e=fe();if(!ye(e))return{servers:{}};try{return JSON.parse(ee(e,"utf8"))}catch{return{servers:{}}}}function Bn(e){E(j()),he(fe(),JSON.stringify(e,null,2)+`
4
- `,"utf8")}var te=class{resolvers=[];buffer=[];closed=!1;push(t){if(this.closed)return;let r={type:"user",message:{role:"user",content:t},parent_tool_use_id:null},n=this.resolvers.shift();n?n(r):this.buffer.push(r)}close(){this.closed=!0;for(let t of this.resolvers)t(null);this.resolvers=[]}async*[Symbol.asyncIterator](){for(;;){if(this.buffer.length>0){yield this.buffer.shift();continue}if(this.closed)return;let t=await new Promise(r=>this.resolvers.push(r));if(t===null)return;yield t}}};import{existsSync as Fr,readFileSync as _t}from"node:fs";import{basename as Hr,isAbsolute as Wr,resolve as vt}from"node:path";import{createSdkMcpServer as zr,tool as b}from"@anthropic-ai/claude-agent-sdk";import{z as g}from"zod";import{createHash as Kt,randomBytes as Ge}from"node:crypto";import{createServer as Yt}from"node:http";import{spawn as ke}from"node:child_process";var Ke=600*1e3;function _e(e){return e.toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function Qt(e){let t=_e(Ge(32)),r=_e(Kt("sha256").update(t).digest()),n=_e(Ge(16)),o=new URL(e.authorizationUrl);return o.searchParams.set("client_id",e.clientId),o.searchParams.set("response_type","code"),o.searchParams.set("redirect_uri",e.redirectUri),o.searchParams.set("scope",e.scopes.join(" ")),o.searchParams.set("state",n),o.searchParams.set("code_challenge",r),o.searchParams.set("code_challenge_method","S256"),e.acrValues&&o.searchParams.set("acr_values",e.acrValues),e.prompt&&o.searchParams.set("prompt",e.prompt),{url:o.toString(),verifier:t,state:n}}function Zt(e){if(process.env.D360_NO_BROWSER)return;let t=process.platform;if(t==="win32"){let r=e.replace(/'/g,"''");ke("powershell",["-NoProfile","-NonInteractive","-Command",`Start-Process '${r}'`],{detached:!0,stdio:"ignore"}).unref()}else t==="darwin"?ke("open",[e],{detached:!0,stdio:"ignore"}).unref():ke("xdg-open",[e],{detached:!0,stdio:"ignore"}).unref()}function Xt(e,t){let r=new URL(e),n,o=new Promise((s,i)=>{let c=setTimeout(()=>{i(new Error(`Timed out after ${Ke/6e4} minutes waiting for the browser login.`)),n.close()},Ke);n=Yt((u,d)=>{let l=new URL(u.url??"/",`http://${r.host}`);if(l.pathname!==r.pathname){d.writeHead(404).end();return}let p=l.searchParams.get("error"),f=l.searchParams.get("code"),h=l.searchParams.get("state"),_=x=>{d.writeHead(400,{"content-type":"text/html"}),d.end(`<html><body><h3>Login failed</h3><p>${x}</p><p>Return to the terminal.</p></body></html>`),clearTimeout(c),i(new Error(x)),n.close()};if(p)return _(`${p}: ${l.searchParams.get("error_description")??""}`);if(!f)return _("No authorization code in the callback.");if(h!==t)return _("State mismatch \u2014 possible CSRF; try again.");d.writeHead(200,{"content-type":"text/html"}),d.end("<html><body><h3>\u2713 Document360 login complete</h3><p>You can close this tab and return to the terminal.</p></body></html>"),clearTimeout(c),s(f),n.close()}),n.on("error",u=>{clearTimeout(c),i(new Error(`Could not listen on ${r.host} (${u.message}). Another process may hold the port \u2014 set D360_REDIRECT_URI to a free port and retry.`))}),n.listen(Number(r.port)||80,r.hostname)});return{server:n,result:o}}async function Qe(e,t){let r=await fetch(e.tokenUrl,{method:"POST",headers:{"content-type":"application/x-www-form-urlencoded"},body:t}),n=await r.text();if(!r.ok)throw new Error(`Token endpoint ${r.status}: ${n.slice(0,400)}`);return JSON.parse(n)}async function Ye(e,t,r){return Qe(e,new URLSearchParams({grant_type:"authorization_code",code:t,redirect_uri:e.redirectUri,client_id:e.clientId,code_verifier:r}))}async function Ze(e,t){return Qe(e,new URLSearchParams({grant_type:"refresh_token",refresh_token:t,client_id:e.clientId}))}function Xe(e,t){let r=Date.now();return{profile:e,accessToken:t.access_token,refreshToken:t.refresh_token,idToken:t.id_token,scope:t.scope,obtainedAt:new Date(r).toISOString(),expiresAt:new Date(r+(t.expires_in??3600)*1e3).toISOString()}}async function Vn(e,t,r){let{url:n,verifier:o,state:s}=Qt(e);if(t.manual){r("Open this URL in a browser and sign in:"),r(n);let u=await t.promptForRedirect("Paste the full URL you were redirected to (it contains ?code=...):"),d=new URL(u.trim()),l=d.searchParams.get("code"),p=d.searchParams.get("state");if(!l)throw new Error("No code parameter found in the pasted URL.");if(p!==s)throw new Error("State mismatch in the pasted URL \u2014 restart the login.");return Ye(e,l,o)}let{result:i}=Xt(e.redirectUri,s);r("Opening your browser for Document360 sign-in\u2026"),r(`If it did not open, visit:
5
- ${n}`),Zt(n);let c=await i;return Ye(e,c,o)}import{existsSync as et,readFileSync as er,unlinkSync as tr,writeFileSync as rr}from"node:fs";import{join as tt}from"node:path";function rt(){return tt(j(),"auth")}function ve(e){return tt(rt(),`${e}.json`)}function nt(e){E(rt()),rr(ve(e.profile),JSON.stringify(e,null,2))}function be(e){let t=ve(e);if(!et(t))return null;try{return JSON.parse(er(t,"utf8"))}catch{return null}}function Zn(e){let t=ve(e);return et(t)?(tr(t),!0):!1}function ot(e,t=60){return Date.now()>=new Date(e.expiresAt).getTime()-t*1e3}function st(e){if(!e)return null;let t=e.split(".");if(t.length!==3)return null;try{let r=Buffer.from(t[1].replace(/-/g,"+").replace(/_/g,"/"),"base64").toString("utf8");return JSON.parse(r)}catch{return null}}import{appendFileSync as nr,readdirSync as or,unlinkSync as sr}from"node:fs";import{join as Se}from"node:path";var ir=14,ar=4096;function cr(){return Se(j(),"logs")}function lr(e){return e.replace(/Bearer\s+[A-Za-z0-9._~+/=-]{8,}/g,"Bearer [redacted]").replace(/eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]+/g,"[redacted-jwt]")}function dr(e,t=ar){return e.length<=t?e:`${e.slice(0,t)}\u2026 [+${e.length-t} chars]`}function re(e){return e===void 0?void 0:dr(lr(e))}var xe=()=>process.env.D360_LOG_BODIES==="1",ur=/^d360-api-(\d{4}-\d{2}-\d{2})\.jsonl$/;function pr(e){let t=new Date(Date.now()-ir*24*60*60*1e3).toISOString().slice(0,10);for(let r of or(e)){let n=r.match(ur);if(n&&n[1]<t)try{sr(Se(e,r))}catch{}}}var it=!1;function Pe(e){try{let t=cr();E(t),it||(it=!0,pr(t));let r=Se(t,`d360-api-${e.ts.slice(0,10)}.jsonl`);nr(r,JSON.stringify(e)+`
6
- `,"utf8")}catch{}}var I=class extends Error{},ne=class extends Error{constructor(r,n,o){super(r);this.status=n;this.requestId=o}status;requestId};async function at(e){let t=be(e.profile);if(!t)throw new I(`Not logged in to Document360 (profile "${e.profile}"). Run /login (or: d360-writer login --profile ${e.profile})`);if(!ot(t))return t.accessToken;if(!t.refreshToken)throw new I(`Document360 session expired (profile "${e.profile}"). Run /login (or: d360-writer login --profile ${e.profile})`);try{let r=Xe(e.profile,await Ze(e.connection,t.refreshToken));return nt(r),r.accessToken}catch(r){throw new I(`Document360 session expired and refresh failed (${r.message.slice(0,120)}). Run /login (or: d360-writer login --profile ${e.profile})`)}}function B(e,t){if(t)return t;let r=be(e.profile),o=(r?st(r.accessToken)??{}:{}).doc360_project_id;if(typeof o=="string"&&o)return o;throw new I("No Document360 project resolved. Log in (the project is chosen during login) or set the profile's project.projectId in .d360-writer.json.")}async function oe(e,t,r,n={}){let o=await at(e),s=new URL(r.startsWith("http")?r:`${e.connection.apiUrl}${r}`);for(let[f,h]of Object.entries(n.query??{}))h!==void 0&&s.searchParams.set(f,String(h));let i=n.body!==void 0?JSON.stringify(n.body):void 0,c=Date.now(),u=f=>Pe({ts:new Date().toISOString(),profile:e.profile,method:t,url:s.toString(),ms:Date.now()-c,ok:f.ok,status:f.status,requestId:f.requestId,error:f.error,...f.withBodies?{requestBody:re(i),responseBody:re(f.responseBody)}:{}}),d;try{d=await fetch(s,{method:t,headers:{authorization:`Bearer ${o}`,accept:"application/json",...i!==void 0?{"content-type":"application/json"}:{}},body:i})}catch(f){throw u({ok:!1,error:`network: ${f.message}`,withBodies:!0}),f}let l=await d.text(),p={};if(l)try{p=JSON.parse(l)}catch{}if(!d.ok||p.success===!1){let f=p.errors?.map(_=>_.message??_.code).filter(Boolean).join("; ")||l.slice(0,300)||d.statusText;if(u({ok:!1,status:d.status,requestId:p.request_id,error:f,responseBody:l,withBodies:!0}),d.status===401)throw new I(`Document360 rejected the token (401). Run /login (or: d360-writer login --profile ${e.profile})`);let h=p.request_id?` [request_id: ${p.request_id}]`:"";throw new ne(`Document360 API ${d.status}: ${f}${h}`,d.status,p.request_id)}return u({ok:!0,status:d.status,requestId:p.request_id,responseBody:l,withBodies:xe()}),p}async function R(e,t,r){return(await oe(e,"GET",t,r)).data}async function F(e,t,r){return(await oe(e,"POST",t,r)).data}async function ct(e,t,r){return(await oe(e,"PATCH",t,r)).data}async function lt(e,t,r){let n=await at(e),o=`${e.connection.apiUrl}${t}`,s=Date.now(),i=l=>Pe({ts:new Date().toISOString(),profile:e.profile,method:"POST",url:o,ms:Date.now()-s,ok:l.ok,status:l.status,requestId:l.requestId,error:l.error,...l.withBodies?{requestBody:"[multipart form-data]",responseBody:re(l.responseBody)}:{}}),c;try{c=await fetch(o,{method:"POST",headers:{authorization:`Bearer ${n}`,accept:"application/json"},body:r})}catch(l){throw i({ok:!1,error:`network: ${l.message}`,withBodies:!0}),l}let u=await c.text(),d={};if(u)try{d=JSON.parse(u)}catch{}if(!c.ok||d.success===!1){let l=d.errors?.map(f=>f.message??f.code).filter(Boolean).join("; ")||u.slice(0,300)||c.statusText;if(i({ok:!1,status:c.status,requestId:d.request_id,error:l,responseBody:u,withBodies:!0}),c.status===401)throw new I(`Document360 rejected the token (401). Run /login (or: d360-writer login --profile ${e.profile})`);let p=d.request_id?` [request_id: ${d.request_id}]`:"";throw new ne(`Document360 upload ${c.status}: ${l}${p}`,c.status,d.request_id)}return i({ok:!0,status:c.status,requestId:d.request_id,responseBody:u,withBodies:xe()}),d.data}async function $(e,t,r={}){let n=[],o;for(let s=0;s<50;s++){let i=await oe(e,"GET",t,{...r,query:{...r.query,cursor:o}});if(Array.isArray(i.data)&&n.push(...i.data),!i.pagination?.has_more||!i.pagination.next_cursor)break;o=i.pagination.next_cursor}return n}async function ao(e,t){return $(e,`/v3/projects/${t}/workspaces`)}async function co(e,t,r){return R(e,`/v3/projects/${t}/articles/${r}`,{query:{content_mode:"raw"}})}import{existsSync as fr,readFileSync as gr,writeFileSync as mr}from"node:fs";import{join as hr}from"node:path";function dt(e){return hr(e,"d360-category-map.json")}function ut(e){let t=dt(e);if(!fr(t))return null;try{return JSON.parse(gr(t,"utf8"))}catch{return null}}function pt(e){return typeof e=="string"?{id:e}:{...e}}function H(e,t){let n=ut(e)?.[t];if(!n)return null;let o={};for(let[s,i]of Object.entries(n.articles??{}))o[s]=pt(i);return{environment:n.environment,projectId:n.projectId,workspaceId:n.workspaceId,lastAnalyzedCommit:n.lastAnalyzedCommit,categories:{...n.categories??{}},articles:o}}function ft(e,t){for(let[r,n]of Object.entries(e.articles))if(n.id===t)return r;return null}function se(e,t,r,n){let o=ut(e),s=o?.[t];if(!o||!s)return!1;s.articles??={};let i=s.articles[r],c=i!==void 0?pt(i):null;return s.articles[r]={...c??{},...n,lastSyncedAt:new Date().toISOString()},mr(dt(e),JSON.stringify(o,null,2)+`
4
+ `,"utf8")}var te=class{resolvers=[];buffer=[];closed=!1;push(t){if(this.closed)return;let r={type:"user",message:{role:"user",content:t},parent_tool_use_id:null},n=this.resolvers.shift();n?n(r):this.buffer.push(r)}close(){this.closed=!0;for(let t of this.resolvers)t(null);this.resolvers=[]}async*[Symbol.asyncIterator](){for(;;){if(this.buffer.length>0){yield this.buffer.shift();continue}if(this.closed)return;let t=await new Promise(r=>this.resolvers.push(r));if(t===null)return;yield t}}};import{existsSync as Fr,readFileSync as kt}from"node:fs";import{basename as Hr,isAbsolute as Wr,resolve as bt}from"node:path";import{createSdkMcpServer as zr,tool as v}from"@anthropic-ai/claude-agent-sdk";import{z as g}from"zod";import{createHash as Kt,randomBytes as Ge}from"node:crypto";import{createServer as Yt}from"node:http";import{spawn as _e}from"node:child_process";var Ke=600*1e3;function ke(e){return e.toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function Qt(e){let t=ke(Ge(32)),r=ke(Kt("sha256").update(t).digest()),n=ke(Ge(16)),o=new URL(e.authorizationUrl);return o.searchParams.set("client_id",e.clientId),o.searchParams.set("response_type","code"),o.searchParams.set("redirect_uri",e.redirectUri),o.searchParams.set("scope",e.scopes.join(" ")),o.searchParams.set("state",n),o.searchParams.set("code_challenge",r),o.searchParams.set("code_challenge_method","S256"),e.acrValues&&o.searchParams.set("acr_values",e.acrValues),e.prompt&&o.searchParams.set("prompt",e.prompt),{url:o.toString(),verifier:t,state:n}}function Zt(e){if(process.env.D360_NO_BROWSER)return;let t=process.platform;if(t==="win32"){let r=e.replace(/'/g,"''");_e("powershell",["-NoProfile","-NonInteractive","-Command",`Start-Process '${r}'`],{detached:!0,stdio:"ignore"}).unref()}else t==="darwin"?_e("open",[e],{detached:!0,stdio:"ignore"}).unref():_e("xdg-open",[e],{detached:!0,stdio:"ignore"}).unref()}function Xt(e,t){let r=new URL(e),n,o=new Promise((s,i)=>{let c=setTimeout(()=>{i(new Error(`Timed out after ${Ke/6e4} minutes waiting for the browser login.`)),n.close()},Ke);n=Yt((u,d)=>{let l=new URL(u.url??"/",`http://${r.host}`);if(l.pathname!==r.pathname){d.writeHead(404).end();return}let p=l.searchParams.get("error"),f=l.searchParams.get("code"),h=l.searchParams.get("state"),k=x=>{d.writeHead(400,{"content-type":"text/html"}),d.end(`<html><body><h3>Login failed</h3><p>${x}</p><p>Return to the terminal.</p></body></html>`),clearTimeout(c),i(new Error(x)),n.close()};if(p)return k(`${p}: ${l.searchParams.get("error_description")??""}`);if(!f)return k("No authorization code in the callback.");if(h!==t)return k("State mismatch \u2014 possible CSRF; try again.");d.writeHead(200,{"content-type":"text/html"}),d.end("<html><body><h3>\u2713 Document360 login complete</h3><p>You can close this tab and return to the terminal.</p></body></html>"),clearTimeout(c),s(f),n.close()}),n.on("error",u=>{clearTimeout(c),i(new Error(`Could not listen on ${r.host} (${u.message}). Another process may hold the port \u2014 set D360_REDIRECT_URI to a free port and retry.`))}),n.listen(Number(r.port)||80,r.hostname)});return{server:n,result:o}}async function Qe(e,t){let r=await fetch(e.tokenUrl,{method:"POST",headers:{"content-type":"application/x-www-form-urlencoded"},body:t}),n=await r.text();if(!r.ok)throw new Error(`Token endpoint ${r.status}: ${n.slice(0,400)}`);return JSON.parse(n)}async function Ye(e,t,r){return Qe(e,new URLSearchParams({grant_type:"authorization_code",code:t,redirect_uri:e.redirectUri,client_id:e.clientId,code_verifier:r}))}async function Ze(e,t){return Qe(e,new URLSearchParams({grant_type:"refresh_token",refresh_token:t,client_id:e.clientId}))}function Xe(e,t){let r=Date.now();return{profile:e,accessToken:t.access_token,refreshToken:t.refresh_token,idToken:t.id_token,scope:t.scope,obtainedAt:new Date(r).toISOString(),expiresAt:new Date(r+(t.expires_in??3600)*1e3).toISOString()}}async function Vn(e,t,r){let{url:n,verifier:o,state:s}=Qt(e);if(t.manual){r("Open this URL in a browser and sign in:"),r(n);let u=await t.promptForRedirect("Paste the full URL you were redirected to (it contains ?code=...):"),d=new URL(u.trim()),l=d.searchParams.get("code"),p=d.searchParams.get("state");if(!l)throw new Error("No code parameter found in the pasted URL.");if(p!==s)throw new Error("State mismatch in the pasted URL \u2014 restart the login.");return Ye(e,l,o)}let{result:i}=Xt(e.redirectUri,s);r("Opening your browser for Document360 sign-in\u2026"),r(`If it did not open, visit:
5
+ ${n}`),Zt(n);let c=await i;return Ye(e,c,o)}import{existsSync as et,readFileSync as er,unlinkSync as tr,writeFileSync as rr}from"node:fs";import{join as tt}from"node:path";function rt(){return tt(j(),"auth")}function be(e){return tt(rt(),`${e}.json`)}function nt(e){E(rt()),rr(be(e.profile),JSON.stringify(e,null,2))}function ve(e){let t=be(e);if(!et(t))return null;try{return JSON.parse(er(t,"utf8"))}catch{return null}}function Zn(e){let t=be(e);return et(t)?(tr(t),!0):!1}function ot(e,t=60){return Date.now()>=new Date(e.expiresAt).getTime()-t*1e3}function st(e){if(!e)return null;let t=e.split(".");if(t.length!==3)return null;try{let r=Buffer.from(t[1].replace(/-/g,"+").replace(/_/g,"/"),"base64").toString("utf8");return JSON.parse(r)}catch{return null}}import{appendFileSync as nr,readdirSync as or,unlinkSync as sr}from"node:fs";import{join as Se}from"node:path";var ir=14,ar=4096;function cr(){return Se(j(),"logs")}function lr(e){return e.replace(/Bearer\s+[A-Za-z0-9._~+/=-]{8,}/g,"Bearer [redacted]").replace(/eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]+/g,"[redacted-jwt]")}function dr(e,t=ar){return e.length<=t?e:`${e.slice(0,t)}\u2026 [+${e.length-t} chars]`}function re(e){return e===void 0?void 0:dr(lr(e))}var xe=()=>process.env.D360_LOG_BODIES==="1",ur=/^d360-api-(\d{4}-\d{2}-\d{2})\.jsonl$/;function pr(e){let t=new Date(Date.now()-ir*24*60*60*1e3).toISOString().slice(0,10);for(let r of or(e)){let n=r.match(ur);if(n&&n[1]<t)try{sr(Se(e,r))}catch{}}}var it=!1;function Pe(e){try{let t=cr();E(t),it||(it=!0,pr(t));let r=Se(t,`d360-api-${e.ts.slice(0,10)}.jsonl`);nr(r,JSON.stringify(e)+`
6
+ `,"utf8")}catch{}}var I=class extends Error{},ne=class extends Error{constructor(r,n,o){super(r);this.status=n;this.requestId=o}status;requestId};async function at(e){let t=ve(e.profile);if(!t)throw new I(`Not logged in to Document360 (profile "${e.profile}"). Run /login (or: d360-writer login --profile ${e.profile})`);if(!ot(t))return t.accessToken;if(!t.refreshToken)throw new I(`Document360 session expired (profile "${e.profile}"). Run /login (or: d360-writer login --profile ${e.profile})`);try{let r=Xe(e.profile,await Ze(e.connection,t.refreshToken));return nt(r),r.accessToken}catch(r){throw new I(`Document360 session expired and refresh failed (${r.message.slice(0,120)}). Run /login (or: d360-writer login --profile ${e.profile})`)}}function B(e,t){if(t)return t;let r=ve(e.profile),o=(r?st(r.accessToken)??{}:{}).doc360_project_id;if(typeof o=="string"&&o)return o;throw new I("No Document360 project resolved. Log in (the project is chosen during login) or set the profile's project.projectId in .d360-writer.json.")}async function oe(e,t,r,n={}){let o=await at(e),s=new URL(r.startsWith("http")?r:`${e.connection.apiUrl}${r}`);for(let[f,h]of Object.entries(n.query??{}))h!==void 0&&s.searchParams.set(f,String(h));let i=n.body!==void 0?JSON.stringify(n.body):void 0,c=Date.now(),u=f=>Pe({ts:new Date().toISOString(),profile:e.profile,method:t,url:s.toString(),ms:Date.now()-c,ok:f.ok,status:f.status,requestId:f.requestId,error:f.error,...f.withBodies?{requestBody:re(i),responseBody:re(f.responseBody)}:{}}),d;try{d=await fetch(s,{method:t,headers:{authorization:`Bearer ${o}`,accept:"application/json",...i!==void 0?{"content-type":"application/json"}:{}},body:i})}catch(f){throw u({ok:!1,error:`network: ${f.message}`,withBodies:!0}),f}let l=await d.text(),p={};if(l)try{p=JSON.parse(l)}catch{}if(!d.ok||p.success===!1){let f=p.errors?.map(k=>k.message??k.code).filter(Boolean).join("; ")||l.slice(0,300)||d.statusText;if(u({ok:!1,status:d.status,requestId:p.request_id,error:f,responseBody:l,withBodies:!0}),d.status===401)throw new I(`Document360 rejected the token (401). Run /login (or: d360-writer login --profile ${e.profile})`);let h=p.request_id?` [request_id: ${p.request_id}]`:"";throw new ne(`Document360 API ${d.status}: ${f}${h}`,d.status,p.request_id)}return u({ok:!0,status:d.status,requestId:p.request_id,responseBody:l,withBodies:xe()}),p}async function R(e,t,r){return(await oe(e,"GET",t,r)).data}async function F(e,t,r){return(await oe(e,"POST",t,r)).data}async function ct(e,t,r){return(await oe(e,"PATCH",t,r)).data}async function lt(e,t,r){let n=await at(e),o=`${e.connection.apiUrl}${t}`,s=Date.now(),i=l=>Pe({ts:new Date().toISOString(),profile:e.profile,method:"POST",url:o,ms:Date.now()-s,ok:l.ok,status:l.status,requestId:l.requestId,error:l.error,...l.withBodies?{requestBody:"[multipart form-data]",responseBody:re(l.responseBody)}:{}}),c;try{c=await fetch(o,{method:"POST",headers:{authorization:`Bearer ${n}`,accept:"application/json"},body:r})}catch(l){throw i({ok:!1,error:`network: ${l.message}`,withBodies:!0}),l}let u=await c.text(),d={};if(u)try{d=JSON.parse(u)}catch{}if(!c.ok||d.success===!1){let l=d.errors?.map(f=>f.message??f.code).filter(Boolean).join("; ")||u.slice(0,300)||c.statusText;if(i({ok:!1,status:c.status,requestId:d.request_id,error:l,responseBody:u,withBodies:!0}),c.status===401)throw new I(`Document360 rejected the token (401). Run /login (or: d360-writer login --profile ${e.profile})`);let p=d.request_id?` [request_id: ${d.request_id}]`:"";throw new ne(`Document360 upload ${c.status}: ${l}${p}`,c.status,d.request_id)}return i({ok:!0,status:c.status,requestId:d.request_id,responseBody:u,withBodies:xe()}),d.data}async function $(e,t,r={}){let n=[],o;for(let s=0;s<50;s++){let i=await oe(e,"GET",t,{...r,query:{...r.query,cursor:o}});if(Array.isArray(i.data)&&n.push(...i.data),!i.pagination?.has_more||!i.pagination.next_cursor)break;o=i.pagination.next_cursor}return n}async function ao(e,t){return $(e,`/v3/projects/${t}/workspaces`)}async function co(e,t,r){return R(e,`/v3/projects/${t}/articles/${r}`,{query:{content_mode:"raw"}})}import{existsSync as fr,readFileSync as gr,writeFileSync as mr}from"node:fs";import{join as hr}from"node:path";function dt(e){return hr(e,"d360-category-map.json")}function ut(e){let t=dt(e);if(!fr(t))return null;try{return JSON.parse(gr(t,"utf8"))}catch{return null}}function pt(e){return typeof e=="string"?{id:e}:{...e}}function H(e,t){let n=ut(e)?.[t];if(!n)return null;let o={};for(let[s,i]of Object.entries(n.articles??{}))o[s]=pt(i);return{environment:n.environment,projectId:n.projectId,workspaceId:n.workspaceId,lastAnalyzedCommit:n.lastAnalyzedCommit,categories:{...n.categories??{}},articles:o}}function ft(e,t){for(let[r,n]of Object.entries(e.articles))if(n.id===t)return r;return null}function se(e,t,r,n){let o=ut(e),s=o?.[t];if(!o||!s)return!1;s.articles??={};let i=s.articles[r],c=i!==void 0?pt(i):null;return s.articles[r]={...c??{},...n,lastSyncedAt:new Date().toISOString()},mr(dt(e),JSON.stringify(o,null,2)+`
7
7
  `,"utf8"),!0}import{createHash as yr}from"node:crypto";function wr(e){let t=e.replace(/^\uFEFF/,"").replace(/\r\n/g,`
8
8
  `);return t=t.replace(/\n*$/,""),t===""?"":t+`
9
9
  `}function q(e){return"sha256:"+yr("sha256").update(wr(e),"utf8").digest("hex")}function W(e,t){return q(`${e??""}
10
- ${t??""}`)}import{existsSync as vr,readdirSync as br,readFileSync as Sr,statSync as xr}from"node:fs";import{join as je}from"node:path";var gt={berlin:{apiUrl:"https://apihub.berlin.document360.net",portalUrl:"https://portal.berlin.document360.net",authorizationUrl:"https://identity.berlin.document360.net/connect/authorize",tokenUrl:"https://identity.berlin.document360.net/connect/token",clientId:"d360WriterAgentClient",scopes:["openid","profile","email","customerApi","offline_access"],acrValues:"project_select",redirectUri:"http://127.0.0.1:3223/callback",prompt:"login"},sharjah:{apiUrl:"https://apihub.sharjah.document360.net",portalUrl:"https://portal.sharjah.document360.net",authorizationUrl:"https://identity.sharjah.document360.net/connect/authorize",tokenUrl:"https://identity.sharjah.document360.net/connect/token",clientId:"d360WriterAgentClient",scopes:["openid","profile","email","customerApi","offline_access"],acrValues:"project_select",redirectUri:"http://127.0.0.1:3223/callback",prompt:"login"}};function kr(){return Object.keys(gt)}function ho(e="berlin"){return ie({environment:e})}function ie(e){let t=e.environment??"berlin",r=gt[t];if(!r)throw new Error(`Unknown Document360 environment "${t}". Known: ${kr().join(", ")}`);let n=process.env.D360_SCOPES?.split(/[\s,]+/).filter(Boolean);return{name:t,apiUrl:process.env.D360_API_URL??e.apiUrl??r.apiUrl,portalUrl:process.env.D360_PORTAL_URL??e.portalUrl??r.portalUrl,authorizationUrl:process.env.D360_AUTHORIZATION_URL??e.authorizationUrl??r.authorizationUrl,tokenUrl:process.env.D360_TOKEN_URL??e.tokenUrl??r.tokenUrl,clientId:process.env.D360_CLIENT_ID??e.clientId??r.clientId,scopes:n??e.scopes??r.scopes,acrValues:process.env.D360_ACR_VALUES??e.acrValues??r.acrValues,redirectUri:process.env.D360_REDIRECT_URI??e.redirectUri??r.redirectUri,prompt:process.env.D360_PROMPT??e.prompt??r.prompt}}function z(e,t){let r=N(e);if(r===null){let s=t??"berlin";return{name:s,connection:ie({environment:s}),project:{},production:!1}}let{name:n,profile:o}=ze(r,t);return{name:n,connection:ie(o.connection),project:o.project??{},production:o.production===!0}}function _o(e,t,r){let n=N(e);n?.profiles?.[t]&&(n.profiles[t].project={...n.profiles[t].project,...r},Je(n,e))}function mt(e,t){if(e?.docsDir)return e.docsDir.replace(/\\/g,"/").replace(/\/+$/,"");let r=new Set(Object.keys(t?.articles??{}).map(n=>n.split("/")[0]).filter(Boolean));if(r.size===1)return[...r][0];throw new Error(r.size===0?'Cannot locate the docs folder: no mapped articles yet. Set "docsDir" in .d360-writer.json.':`Cannot locate the docs folder: mapped articles span multiple roots (${[...r].join(", ")}). Set "docsDir" in .d360-writer.json.`)}var _r=4;function ht(e,t,r,n={getAll:$,get:R}){let o=`/v3/projects/${t}`;return{async fetch(s){let[i,c]=await Promise.all([n.getAll(e,`${o}/workspaces/${r}/articles`),n.getAll(e,`${o}/workspaces/${r}/categories`)]),u=new Map;for(let h of i)u.set(h.id,{id:h.id,title:h.title,categoryId:h.category_id,hidden:h.hidden});let d=new Map;for(let h of c)d.set(h.id,{id:h.id,name:h.name,parentId:h.parent_category_id});let l=s.filter(h=>u.has(h)),p=0,f=async()=>{for(;p<l.length;){let h=l[p++],_=await n.get(e,`${o}/articles/${h}`,{query:{content_mode:"raw"}}),x=u.get(h);x.title=_.title??x.title,x.content=_.content,x.latestVersion=_.latest_version,x.modifiedAt=_.modified_at,x.contentHash=W(_.title??x.title,_.content)}};return await Promise.all(Array.from({length:Math.min(_r,l.length)},f)),{articles:u,categories:d}}}}function Pr(e){if(!e.mapped)return e.localExists?"untracked-local":"untracked-remote";if(!e.localExists&&!e.remoteExists)return"orphaned";if(!e.localExists)return"deleted-local";if(!e.remoteExists)return"deleted-remote";if(!e.hasBase)return"unknown-base";let t=e.localHashNow!==e.baseLocalHash,r=e.remoteContentHashNow!==e.baseRemoteContentHash;return t&&r?"conflict":t?"local-ahead":r?"remote-ahead":"in-sync"}function jr(e,t){let r=je(e,t);if(!vr(r))return[];let n=[],o=(s,i)=>{for(let c of br(s)){let u=je(s,c),d=i?`${i}/${c}`:c;xr(u).isDirectory()?o(u,d):c.endsWith(".md")&&n.push(d)}};return o(r,t.replace(/\\/g,"/")),n}function Cr(e,t){let r=new Set(Object.values(e.categories)),n=new Map;for(let i of t.categories.values()){if(!i.parentId)continue;let c=n.get(i.parentId)??[];c.push(i.id),n.set(i.parentId,c)}let o=new Set,s=[...r];for(;s.length>0;){let i=s.shift();o.has(i)||(o.add(i),s.push(...n.get(i)??[]))}return o}async function yt(e){let t=z(e.cwd,e.profileName),r=N(e.cwd),n=H(e.cwd,t.name);if(!n)throw new Error(`No d360-category-map.json section for profile "${t.name}". Publish at least one article first (/publish).`);let o={profile:t.name,connection:t.connection},s=n.projectId??t.project.projectId??B(o),i=n.workspaceId??t.project.workspaceId;if(!i)throw new Error(`No workspace recorded for profile "${t.name}". Run /workspace to select one.`);let c=mt(r,n),u=Object.values(n.articles).map(v=>v.id),l=await(e.provider??ht(o,s,i)).fetch(u),p=new Set(jr(e.cwd,c)),f=[];for(let[v,O]of Object.entries(n.articles)){let Q=p.has(v);p.delete(v);let T=l.articles.get(O.id),le=!!(O.localHash&&O.remoteContentHash),G=Pr({mapped:!0,localExists:Q,remoteExists:T!==void 0,hasBase:le,localHashNow:Q?q(Sr(je(e.cwd,v),"utf8")):void 0,baseLocalHash:O.localHash,remoteContentHashNow:T?.contentHash,baseRemoteContentHash:O.remoteContentHash}),J=[];if(G==="remote-ahead"||G==="conflict"){let Z=T?.modifiedAt?.slice(0,10);J.push(`remote edited${Z?` ${Z}`:""}${T?.latestVersion?` (v${T.latestVersion})`:""}`)}T?.hidden&&J.push("hidden on D360"),f.push({path:v,articleId:O.id,title:T?.title,status:G,detail:J.length>0?J.join("; "):void 0})}for(let v of[...p].sort())f.push({path:v,articleId:null,status:"untracked-local"});let h=new Set(u),_=Cr(n,l);for(let v of l.articles.values())h.has(v.id)||!v.categoryId||!_.has(v.categoryId)||f.push({path:null,articleId:v.id,title:v.title,status:"untracked-remote",detail:v.hidden?"hidden on D360":void 0});let x={};for(let v of f)x[v.status]=(x[v.status]??0)+1;return{profile:t.name,projectId:s,workspaceId:i,docsRoot:c,entries:f,counts:x,generatedAt:new Date().toISOString()}}import{posix as Ce}from"node:path";var Dr=/\[([^\]]+)\]\(([^)\s]+)\)/g;function De(e,t,r){if(!r)return{content:e,resolved:0,unresolved:0};let n=Ce.dirname(t.replace(/\\/g,"/")),o=0,s=0;return{content:e.replace(Dr,(c,u,d)=>{if(/^(https?:|mailto:|#|\/)/i.test(d)||!/\.(md|markdown)(#[^)]*)?$/i.test(d))return c;let l=d.indexOf("#"),p=l===-1?d:d.slice(0,l),f=l===-1?"":d.slice(l),h=Ce.normalize(Ce.join(n,p)).replace(/^\.\//,""),_=r.articles[h]?.url;return _?(o++,`[${u}](${_}${f})`):(s++,u)}),resolved:o,unresolved:s}}import{readdirSync as Rr}from"node:fs";import{join as ae}from"node:path";var Tr=new Set([".git","node_modules","dist","build","bin","obj","out","target",".vs",".vscode",".idea",".next","coverage",".turbo",".cache","packages"]),Ar=40,Er=3,Re=5e3,Ir=/\.(csproj|vbproj|fsproj)$|^(package\.json|go\.mod|Cargo\.toml|pyproject\.toml|setup\.py|pom\.xml|build\.gradle(\.kts)?)$/i,$r=/\.sln$|^(Directory\.(Build|Packages)\.props|global\.json|go\.work)$/i;function Or(e){return/\.(csproj|vbproj|fsproj|sln)$/i.test(e)||/^(Directory\.(Build|Packages)\.props|global\.json)$/i.test(e)?".NET":/^(package\.json|angular\.json|tsconfig\.json)$/i.test(e)?"JS/TS":/^(pyproject\.toml|requirements\.txt|setup\.py)$/i.test(e)?"Python":/^(go\.mod|go\.work)$/i.test(e)?"Go":/^Cargo\.toml$/i.test(e)?"Rust":/^(pom\.xml|build\.gradle(\.kts)?)$/i.test(e)?"Java":null}var Mr=/(?:^|[.\-_])(web|portal|ui|app|apps|client|site|widget|admin|dashboard|frontend|api)(?:$|[.\-_])/i,Ur=/(?:^|[.\-_])(tests?|specs?|helm|docker|migrations?|functions?|ci|builds?|scripts|tools|proxy|coverage|release|libs?|redis|signalr|websocket|dataaccess|nuget|sdk|infra|infrastructure|deploy|deployment|e2e)(?:$|[.\-_])/i;function Te(e){let t=[],r=[];try{for(let n of Rr(e,{withFileTypes:!0}))n.isDirectory()?!n.name.startsWith(".")&&!Tr.has(n.name)&&t.push(n.name):n.isFile()&&r.push(n.name)}catch{}return{dirs:t,files:r}}function Nr(e){let t=new Set;for(let r of e){let n=Or(r);n&&t.add(n)}return t}var wt=e=>e.some(t=>Ir.test(t)),qr=e=>e.some(t=>$r.test(t));function Lr(e){let t=0,r=n=>{if(t>=Re)return;let{dirs:o,files:s}=Te(n);t+=s.length;for(let i of o){if(t>=Re)return;r(ae(n,i))}};return r(e),Math.min(t,Re)}function Br(e,t){let r=e.split("/").pop()??e,n=t.size?[...t].join("+"):"";return Ur.test(r)?{recommended:!1,reason:`tests/infrastructure${n?` \xB7 ${n}`:""}`}:Mr.test(r)?{recommended:!0,reason:`user-facing surface${n?` \xB7 ${n}`:""}`}:{recommended:!1,reason:n?`${n} project`:"source folder"}}function kt(e){let t=[],r=(s,i)=>{let c=Nr(i.files),{recommended:u,reason:d}=Br(s,c);t.push({path:s,fileCount:Lr(ae(e,s)),stacks:[...c],recommended:u,reason:d})},n=(s,i)=>{let c=s?ae(e,s):e,u=Te(c);if(s&&wt(u.files)){r(s,u);return}if(s&&u.dirs.length===0){r(s,u);return}let d=p=>wt(Te(ae(c,p)).files);if((qr(u.files)||u.dirs.some(d)||s==="")&&i<Er){for(let p of u.dirs)n(s?`${s}/${p}`:p,i+1);return}s&&r(s,u)};n("",0),t.sort((s,i)=>Number(i.recommended)-Number(s.recommended)||s.path.localeCompare(i.path));let o=t.filter(s=>s.recommended);return t.slice(0,Math.max(Ar,o.length))}var bt="/v3/projects",P=e=>`${bt}/${e}`;function S(e){return{content:[{type:"text",text:JSON.stringify(e,null,2)??'{"success":true}'}]}}function w(e){return{content:[{type:"text",text:e instanceof Error?e.message:String(e)}],isError:!0}}function St(e,t){let r={profile:e.name,connection:e.connection},n=e.project.projectId,o=e.project.workspaceId,s=()=>B(r,n),i=a=>{let m=a??o;if(!m)throw new Error("No workspace_id given and none in the profile. Call d360_list_workspaces first.");return m},c=(a,m)=>{if(t.cwd)try{let y=vt(t.cwd,a);se(t.cwd,e.name,a.replace(/\\/g,"/"),{...m,...Fr(y)?{localHash:q(_t(y,"utf8"))}:{}})}catch{}},u=()=>t.writesAllowed?null:w(`Refusing to write to PRODUCTION profile "${e.name}". Authorize this session first: run /allow-prod in the REPL, or pass --yes in one-shot/CI.`),d=b("d360_context","Report the active Document360 connection: profile name, environment, project/workspace id, and whether it is a production profile. Call this before maintaining d360-category-map.json so you scope IDs to the right profile.",{},async()=>{try{return S({profile:e.name,environment:e.connection.name,production:e.production,projectId:n??B(r),workspaceId:o??null})}catch(a){return w(a)}}),l=b("d360_list_projects","List all Document360 projects the signed-in user can access (id, name, sub-domain, status).",{},async()=>{try{return S(await $(r,bt))}catch(a){return w(a)}}),p=b("d360_list_workspaces","List workspaces (project versions) for the current project. Each has id, name, slug, is_default, workspace_type.",{project_id:g.string().optional().describe("Defaults to the logged-in/config project.")},async a=>{try{return S(await $(r,`${P(a.project_id??s())}/workspaces`))}catch(m){return w(m)}}),f=b("d360_list_categories","List categories in a workspace (the folder structure for articles).",{workspace_id:g.string().optional().describe("Defaults to d360.workspaceId in config."),project_id:g.string().optional()},async a=>{try{let m=a.workspace_id??o;return m?S(await $(r,`${P(a.project_id??s())}/workspaces/${m}/categories`)):w("No workspace_id given and none in config. Call d360_list_workspaces first.")}catch(m){return w(m)}}),h=b("d360_list_articles","List articles in a workspace (id, title, status, category). Use to see existing docs before writing.",{workspace_id:g.string().optional().describe("Defaults to d360.workspaceId in config."),project_id:g.string().optional()},async a=>{try{let m=a.workspace_id??o;return m?S(await $(r,`${P(a.project_id??s())}/workspaces/${m}/articles`)):w("No workspace_id given and none in config. Call d360_list_workspaces first.")}catch(m){return w(m)}}),_=b("d360_get_article","Get a single article including its content. content_mode=raw returns the stored markdown source (best for editing); display returns processed content.",{article_id:g.string(),content_mode:g.enum(["raw","display"]).optional().describe("raw = stored markdown source (default), display = processed."),published:g.boolean().optional().describe("Read the published version instead of the latest draft."),project_id:g.string().optional()},async a=>{try{return S(await R(r,`${P(a.project_id??s())}/articles/${a.article_id}`,{query:{content_mode:a.content_mode,published:a.published}}))}catch(m){return w(m)}}),x=b("d360_ai_query","Ask Document360's AI search over a workspace's published content. Returns an answer grounded in existing articles \u2014 use to check what's already documented.",{query:g.string(),workspace_id:g.string().optional().describe("Defaults to d360.workspaceId in config."),project_id:g.string().optional()},async a=>{try{let m=a.workspace_id??o;return m?S(await F(r,`${P(a.project_id??s())}/workspaces/ai/query`,{body:{query:a.query,workspace_id:m}})):w("No workspace_id given and none in config. Call d360_list_workspaces first.")}catch(m){return w(m)}}),v=b("d360_sync_status","Deterministic drift report: local docs tree vs Document360, classified against the sync bases in d360-category-map.json. Call this BEFORE any docs gap/coverage analysis \u2014 it proves whether the local docs tree can be trusted as the Document360 inventory (no need to fetch article content). Statuses: local-ahead (push pending), remote-ahead (portal edit to pull), conflict, untracked-local/remote, deleted-local/remote, unknown-base (no base recorded yet).",{},async()=>{try{if(!t.cwd)return w("Sync status needs a repo context (no cwd configured for this session).");let a=await yt({cwd:t.cwd,profileName:e.name});return S({profile:a.profile,docsRoot:a.docsRoot,generatedAt:a.generatedAt,counts:a.counts,attention:a.entries.filter(m=>m.status!=="in-sync")})}catch(a){return w(a)}}),O=b("d360_repo_inventory","Deterministic inventory of candidate source folders for documentation scope (no file reads of your own needed). Returns project-level folders with: path, fileCount, detected stacks, a `recommended` flag (user-facing surfaces pre-recommended; tests/infrastructure not), and a one-line reason. Use this when analyzing a large/multi-project repo to decide which folders back the docs \u2014 it is the same data the /scope picker pre-ticks, so your recommendation and the picker agree. Cheap; safe to call before reading any source.",{},async()=>{try{return t.cwd?S(kt(t.cwd)):w("Repo inventory needs a repo context (no cwd configured for this session).")}catch(a){return w(a)}}),Q=b("d360_create_category","Create a category (a docs folder). Returns the new category id.",{name:g.string(),workspace_id:g.string().optional(),parent_category_id:g.string().optional().describe("Omit for a top-level category."),content:g.string().optional(),slug:g.string().optional(),order:g.number().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/categories`,{body:{name:a.name,workspace_id:i(a.workspace_id),parent_category_id:a.parent_category_id,content:a.content,slug:a.slug,order:a.order,content_type:"markdown"}}))}catch(y){return w(y)}}),T=b("d360_create_article","Create a DRAFT article in a category. The body is always Markdown (product rule \u2014 we never create WYSIWYG/Block articles). Returns the new article id. Does not publish.",{title:g.string(),category_id:g.string(),content:g.string().optional().describe("Markdown body."),workspace_id:g.string().optional(),slug:g.string().optional(),order:g.number().optional(),project_id:g.string().optional(),local_path:g.string().optional().describe("Repo-relative path of the local .md this article mirrors (e.g. user-docs/01-intro/01-a.md). When given, the new article id + sync base are recorded into d360-category-map.json automatically \u2014 do not edit the articles map yourself.")},async a=>{let m=u();if(m)return m;try{let y=a.project_id??s(),D=a.content;typeof D=="string"&&a.local_path&&t.cwd&&(D=De(D,a.local_path,H(t.cwd,e.name)).content);let k=await F(r,`${P(y)}/articles/bulk`,{body:{articles:[{title:a.title,category_id:a.category_id,workspace_id:i(a.workspace_id),content:D,slug:a.slug,order:a.order,content_type:"markdown"}]}}),M=Array.isArray(k)?k[0]:void 0;if(a.local_path&&typeof M?.id=="string"){let C;try{let U=await R(r,`${P(y)}/articles/${M.id}`,{});C=typeof U?.url=="string"?U.url:void 0}catch{}c(a.local_path,{id:M.id,remoteContentHash:W(a.title,D),remoteVersion:1,...C?{url:C}:{}})}return S(k)}catch(y){return w(y)}}),le=b("d360_update_article","Update an article's title/content/category. Edits the latest draft by default; set auto_fork to safely edit a published article (creates a new draft version).",{article_id:g.string(),title:g.string().optional(),content:g.string().optional().describe("Markdown body."),category_id:g.string().optional(),hidden:g.boolean().optional(),version_number:g.number().optional(),auto_fork:g.boolean().optional().describe("If the target version is published, fork a new draft instead of erroring."),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{let{article_id:y,project_id:D,...k}=a,M=D??s(),C=typeof k.content=="string"&&t.cwd?H(t.cwd,e.name):null,U=C?ft(C,y):null;typeof k.content=="string"&&U&&C&&(k.content=De(k.content,U,C).content);let L=null;if(typeof k.content=="string"&&t.onUiEvent)try{L=await R(r,`${P(M)}/articles/${y}`,{query:{content_mode:"raw"}})}catch{}let $e=e.project.languageCode??L?.lang_code,Oe=await ct(r,`${P(M)}/articles/${y}`,{body:k,...$e?{query:{lang_code:$e}}:{}});if(L&&typeof k.content=="string"&&(L.content??"")!==k.content&&t.onUiEvent?.({type:"article_diff",articleId:y,title:L.title??null,oldContent:L.content??"",newContent:k.content}),typeof k.content=="string"&&U)try{let K=Oe,Me=k.title??K?.title??L?.title;typeof Me=="string"&&c(U,{id:y,remoteContentHash:W(Me,k.content),...typeof K?.latest_version=="number"?{remoteVersion:K.latest_version}:{},...typeof K?.modified_at=="string"?{remoteModifiedAt:K.modified_at}:{}})}catch{}return S(Oe)}catch(y){return w(y)}}),G=b("d360_fork_article","Fork a published article into a new draft version \u2014 the safe way to start editing live content.",{article_id:g.string(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/articles/${a.article_id}/fork`))}catch(y){return w(y)}}),J=b("d360_publish_article","Publish an article version. This makes the draft live to readers \u2014 only call when the user explicitly asks to publish.",{article_id:g.string(),version_number:g.number(),workspace_id:g.string().optional(),message:g.string().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/articles/${a.article_id}/publish`,{body:{workspace_id:i(a.workspace_id),version_number:a.version_number,message:a.message}}))}catch(y){return w(y)}}),Z=b("d360_unpublish_article","Unpublish an article version, reverting it to draft (removes it from readers).",{article_id:g.string(),version_number:g.number().describe("The published version to unpublish (see d360_get_article)."),workspace_id:g.string().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/articles/${a.article_id}/unpublish`,{body:{workspace_id:i(a.workspace_id),version_number:a.version_number}}))}catch(y){return w(y)}}),Et=b("d360_upload_drive_file","Upload a local file (e.g. a captured screenshot PNG) to Drive and return its URL for embedding in an article. Uploads to the default folder unless folder_id is given.",{file_path:g.string().describe("Local path to the file (absolute, or relative to the repo)."),folder_id:g.string().optional(),title:g.string().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{let y=a.project_id??s(),D=Wr(a.file_path)?a.file_path:vt(process.cwd(),a.file_path),k=a.folder_id;if(!k&&(k=(await R(r,`${P(y)}/drive/folders/default`))?.id,!k))return w("Could not resolve the default Drive folder.");let M=_t(D),C=new FormData;return C.append("file",new Blob([M]),a.title??Hr(D)),S(await lt(r,`${P(y)}/drive/folders/${k}/files`,C))}catch(y){return w(y)}}),It=b("d360_search_drive","Search Drive files (e.g. to reuse an already-uploaded screenshot instead of uploading a duplicate).",{search_keyword:g.string().optional(),allow_images_only:g.boolean().optional(),project_id:g.string().optional()},async a=>{try{return S(await $(r,`${P(a.project_id??s())}/drive/search`,{query:{search_keyword:a.search_keyword,allow_images_only:a.allow_images_only}}))}catch(m){return w(m)}});return zr({name:"document360",version:"0.2.0",instructions:"First-party Document360 tools. The signed-in user's permissions apply server-side. Project/workspace default from the active profile; pass ids explicitly to override. Create articles as DRAFTS; only call d360_publish_article when the user explicitly asks to publish. All articles are Markdown \u2014 the tools enforce this; never attempt WYSIWYG/Block content. To edit a published article, fork it (d360_fork_article) or update with auto_fork. Before any docs gap/coverage analysis, call d360_sync_status to verify the local docs tree matches Document360. When scoping a large repo (which folders back the docs), call d360_repo_inventory for the candidate folders. Auth errors mean the session expired \u2014 tell the user to run /login (works inside this session).",tools:[d,l,p,f,h,_,x,v,O,Q,T,le,G,J,Z,Et,It]})}var Ct="CLAUDE.md";function xt(e){if(!Ae(e))return[];let t=[];for(let r of Jr(e)){let n=Ee(e,r);if(r===Ct||!Vr(n).isDirectory())continue;let o=Ee(n,"SKILL.md");Ae(o)&&t.push({name:r,body:jt(o,"utf8")})}return t}function Pt(e){let t=Ee(e,Ct);return Ae(t)?jt(t,"utf8"):null}function Kr(e,t){let r=He(),n=We(e),o=Pt(r),s=Pt(n),i=xt(r),c=xt(n),u=new Set(c.map(p=>p.name)),d=[...i.filter(p=>!u.has(p.name)),...c],l=[];if(o&&l.push(o),s&&l.push(`# Project addendum
10
+ ${t??""}`)}import{existsSync as br,readdirSync as vr,readFileSync as Sr,statSync as xr}from"node:fs";import{join as je}from"node:path";var gt={berlin:{apiUrl:"https://apihub.berlin.document360.net",portalUrl:"https://portal.berlin.document360.net",authorizationUrl:"https://identity.berlin.document360.net/connect/authorize",tokenUrl:"https://identity.berlin.document360.net/connect/token",clientId:"d360WriterAgentClient",scopes:["openid","profile","email","customerApi","offline_access"],acrValues:"project_select",redirectUri:"http://127.0.0.1:3223/callback",prompt:"login"},sharjah:{apiUrl:"https://apihub.sharjah.document360.net",portalUrl:"https://portal.sharjah.document360.net",authorizationUrl:"https://identity.sharjah.document360.net/connect/authorize",tokenUrl:"https://identity.sharjah.document360.net/connect/token",clientId:"d360WriterAgentClient",scopes:["openid","profile","email","customerApi","offline_access"],acrValues:"project_select",redirectUri:"http://127.0.0.1:3223/callback",prompt:"login"}};function _r(){return Object.keys(gt)}function ho(e="berlin"){return ie({environment:e})}function ie(e){let t=e.environment??"berlin",r=gt[t];if(!r)throw new Error(`Unknown Document360 environment "${t}". Known: ${_r().join(", ")}`);let n=process.env.D360_SCOPES?.split(/[\s,]+/).filter(Boolean);return{name:t,apiUrl:process.env.D360_API_URL??e.apiUrl??r.apiUrl,portalUrl:process.env.D360_PORTAL_URL??e.portalUrl??r.portalUrl,authorizationUrl:process.env.D360_AUTHORIZATION_URL??e.authorizationUrl??r.authorizationUrl,tokenUrl:process.env.D360_TOKEN_URL??e.tokenUrl??r.tokenUrl,clientId:process.env.D360_CLIENT_ID??e.clientId??r.clientId,scopes:n??e.scopes??r.scopes,acrValues:process.env.D360_ACR_VALUES??e.acrValues??r.acrValues,redirectUri:process.env.D360_REDIRECT_URI??e.redirectUri??r.redirectUri,prompt:process.env.D360_PROMPT??e.prompt??r.prompt}}function z(e,t){let r=N(e);if(r===null){let s=t??"berlin";return{name:s,connection:ie({environment:s}),project:{},production:!1}}let{name:n,profile:o}=ze(r,t);return{name:n,connection:ie(o.connection),project:o.project??{},production:o.production===!0}}function ko(e,t,r){let n=N(e);n?.profiles?.[t]&&(n.profiles[t].project={...n.profiles[t].project,...r},Je(n,e))}function mt(e,t){if(e?.docsDir)return e.docsDir.replace(/\\/g,"/").replace(/\/+$/,"");let r=new Set(Object.keys(t?.articles??{}).map(n=>n.split("/")[0]).filter(Boolean));if(r.size===1)return[...r][0];throw new Error(r.size===0?'Cannot locate the docs folder: no mapped articles yet. Set "docsDir" in .d360-writer.json.':`Cannot locate the docs folder: mapped articles span multiple roots (${[...r].join(", ")}). Set "docsDir" in .d360-writer.json.`)}var kr=4;function ht(e,t,r,n={getAll:$,get:R}){let o=`/v3/projects/${t}`;return{async fetch(s){let[i,c]=await Promise.all([n.getAll(e,`${o}/workspaces/${r}/articles`),n.getAll(e,`${o}/workspaces/${r}/categories`)]),u=new Map;for(let h of i)u.set(h.id,{id:h.id,title:h.title,categoryId:h.category_id,hidden:h.hidden});let d=new Map;for(let h of c)d.set(h.id,{id:h.id,name:h.name,parentId:h.parent_category_id});let l=s.filter(h=>u.has(h)),p=0,f=async()=>{for(;p<l.length;){let h=l[p++],k=await n.get(e,`${o}/articles/${h}`,{query:{content_mode:"raw"}}),x=u.get(h);x.title=k.title??x.title,x.content=k.content,x.latestVersion=k.latest_version,x.modifiedAt=k.modified_at,x.contentHash=W(k.title??x.title,k.content)}};return await Promise.all(Array.from({length:Math.min(kr,l.length)},f)),{articles:u,categories:d}}}}function Pr(e){if(!e.mapped)return e.localExists?"untracked-local":"untracked-remote";if(!e.localExists&&!e.remoteExists)return"orphaned";if(!e.localExists)return"deleted-local";if(!e.remoteExists)return"deleted-remote";if(!e.hasBase)return"unknown-base";let t=e.localHashNow!==e.baseLocalHash,r=e.remoteContentHashNow!==e.baseRemoteContentHash;return t&&r?"conflict":t?"local-ahead":r?"remote-ahead":"in-sync"}function jr(e,t){let r=je(e,t);if(!br(r))return[];let n=[],o=(s,i)=>{for(let c of vr(s)){let u=je(s,c),d=i?`${i}/${c}`:c;xr(u).isDirectory()?o(u,d):c.endsWith(".md")&&n.push(d)}};return o(r,t.replace(/\\/g,"/")),n}function Cr(e,t){let r=new Set(Object.values(e.categories)),n=new Map;for(let i of t.categories.values()){if(!i.parentId)continue;let c=n.get(i.parentId)??[];c.push(i.id),n.set(i.parentId,c)}let o=new Set,s=[...r];for(;s.length>0;){let i=s.shift();o.has(i)||(o.add(i),s.push(...n.get(i)??[]))}return o}async function yt(e){let t=z(e.cwd,e.profileName),r=N(e.cwd),n=H(e.cwd,t.name);if(!n)throw new Error(`No d360-category-map.json section for profile "${t.name}". Publish at least one article first (/publish).`);let o={profile:t.name,connection:t.connection},s=n.projectId??t.project.projectId??B(o),i=n.workspaceId??t.project.workspaceId;if(!i)throw new Error(`No workspace recorded for profile "${t.name}". Run /workspace to select one.`);let c=mt(r,n),u=Object.values(n.articles).map(b=>b.id),l=await(e.provider??ht(o,s,i)).fetch(u),p=new Set(jr(e.cwd,c)),f=[];for(let[b,O]of Object.entries(n.articles)){let Q=p.has(b);p.delete(b);let T=l.articles.get(O.id),le=!!(O.localHash&&O.remoteContentHash),G=Pr({mapped:!0,localExists:Q,remoteExists:T!==void 0,hasBase:le,localHashNow:Q?q(Sr(je(e.cwd,b),"utf8")):void 0,baseLocalHash:O.localHash,remoteContentHashNow:T?.contentHash,baseRemoteContentHash:O.remoteContentHash}),J=[];if(G==="remote-ahead"||G==="conflict"){let Z=T?.modifiedAt?.slice(0,10);J.push(`remote edited${Z?` ${Z}`:""}${T?.latestVersion?` (v${T.latestVersion})`:""}`)}T?.hidden&&J.push("hidden on D360"),f.push({path:b,articleId:O.id,title:T?.title,status:G,detail:J.length>0?J.join("; "):void 0})}for(let b of[...p].sort())f.push({path:b,articleId:null,status:"untracked-local"});let h=new Set(u),k=Cr(n,l);for(let b of l.articles.values())h.has(b.id)||!b.categoryId||!k.has(b.categoryId)||f.push({path:null,articleId:b.id,title:b.title,status:"untracked-remote",detail:b.hidden?"hidden on D360":void 0});let x={};for(let b of f)x[b.status]=(x[b.status]??0)+1;return{profile:t.name,projectId:s,workspaceId:i,docsRoot:c,entries:f,counts:x,generatedAt:new Date().toISOString()}}import{posix as Ce}from"node:path";var Dr=/\[([^\]]+)\]\(([^)\s]+)\)/g;function De(e,t,r){if(!r)return{content:e,resolved:0,unresolved:0};let n=Ce.dirname(t.replace(/\\/g,"/")),o=0,s=0;return{content:e.replace(Dr,(c,u,d)=>{if(/^(https?:|mailto:|#|\/)/i.test(d)||!/\.(md|markdown)(#[^)]*)?$/i.test(d))return c;let l=d.indexOf("#"),p=l===-1?d:d.slice(0,l),f=l===-1?"":d.slice(l),h=Ce.normalize(Ce.join(n,p)).replace(/^\.\//,""),k=r.articles[h]?.url;return k?(o++,`[${u}](${k}${f})`):(s++,u)}),resolved:o,unresolved:s}}import{readdirSync as Rr}from"node:fs";import{join as ae}from"node:path";var Tr=new Set([".git","node_modules","dist","build","bin","obj","out","target",".vs",".vscode",".idea",".next","coverage",".turbo",".cache","packages"]),Ar=40,Er=3,Re=5e3,Ir=/\.(csproj|vbproj|fsproj)$|^(package\.json|go\.mod|Cargo\.toml|pyproject\.toml|setup\.py|pom\.xml|build\.gradle(\.kts)?)$/i,$r=/\.sln$|^(Directory\.(Build|Packages)\.props|global\.json|go\.work)$/i;function Or(e){return/\.(csproj|vbproj|fsproj|sln)$/i.test(e)||/^(Directory\.(Build|Packages)\.props|global\.json)$/i.test(e)?".NET":/^(package\.json|angular\.json|tsconfig\.json)$/i.test(e)?"JS/TS":/^(pyproject\.toml|requirements\.txt|setup\.py)$/i.test(e)?"Python":/^(go\.mod|go\.work)$/i.test(e)?"Go":/^Cargo\.toml$/i.test(e)?"Rust":/^(pom\.xml|build\.gradle(\.kts)?)$/i.test(e)?"Java":null}var Ur=/(?:^|[.\-_])(web|portal|ui|app|apps|client|site|widget|admin|dashboard|frontend|api)(?:$|[.\-_])/i,Mr=/(?:^|[.\-_])(tests?|specs?|helm|docker|migrations?|functions?|ci|builds?|scripts|tools|proxy|coverage|release|libs?|redis|signalr|websocket|dataaccess|nuget|sdk|infra|infrastructure|deploy|deployment|e2e)(?:$|[.\-_])/i;function Te(e){let t=[],r=[];try{for(let n of Rr(e,{withFileTypes:!0}))n.isDirectory()?!n.name.startsWith(".")&&!Tr.has(n.name)&&t.push(n.name):n.isFile()&&r.push(n.name)}catch{}return{dirs:t,files:r}}function Nr(e){let t=new Set;for(let r of e){let n=Or(r);n&&t.add(n)}return t}var wt=e=>e.some(t=>Ir.test(t)),qr=e=>e.some(t=>$r.test(t));function Lr(e){let t=0,r=n=>{if(t>=Re)return;let{dirs:o,files:s}=Te(n);t+=s.length;for(let i of o){if(t>=Re)return;r(ae(n,i))}};return r(e),Math.min(t,Re)}function Br(e,t){let r=e.split("/").pop()??e,n=t.size?[...t].join("+"):"";return Mr.test(r)?{recommended:!1,reason:`tests/infrastructure${n?` \xB7 ${n}`:""}`}:Ur.test(r)?{recommended:!0,reason:`user-facing surface${n?` \xB7 ${n}`:""}`}:{recommended:!1,reason:n?`${n} project`:"source folder"}}function _t(e){let t=[],r=(s,i)=>{let c=Nr(i.files),{recommended:u,reason:d}=Br(s,c);t.push({path:s,fileCount:Lr(ae(e,s)),stacks:[...c],recommended:u,reason:d})},n=(s,i)=>{let c=s?ae(e,s):e,u=Te(c);if(s&&wt(u.files)){r(s,u);return}if(s&&u.dirs.length===0){r(s,u);return}let d=p=>wt(Te(ae(c,p)).files);if((qr(u.files)||u.dirs.some(d)||s==="")&&i<Er){for(let p of u.dirs)n(s?`${s}/${p}`:p,i+1);return}s&&r(s,u)};n("",0),t.sort((s,i)=>Number(i.recommended)-Number(s.recommended)||s.path.localeCompare(i.path));let o=t.filter(s=>s.recommended);return t.slice(0,Math.max(Ar,o.length))}var vt="/v3/projects",P=e=>`${vt}/${e}`;function S(e){return{content:[{type:"text",text:JSON.stringify(e,null,2)??'{"success":true}'}]}}function w(e){return{content:[{type:"text",text:e instanceof Error?e.message:String(e)}],isError:!0}}function St(e,t){let r={profile:e.name,connection:e.connection},n=e.project.projectId,o=e.project.workspaceId,s=()=>B(r,n),i=a=>{let m=a??o;if(!m)throw new Error("No workspace_id given and none in the profile. Call d360_list_workspaces first.");return m},c=(a,m)=>{if(t.cwd)try{let y=bt(t.cwd,a);se(t.cwd,e.name,a.replace(/\\/g,"/"),{...m,...Fr(y)?{localHash:q(kt(y,"utf8"))}:{}})}catch{}},u=()=>t.writesAllowed?null:w(`Refusing to write to PRODUCTION profile "${e.name}". Authorize this session first: run /allow-prod in the REPL, or pass --yes in one-shot/CI.`),d=v("d360_context","Report the active Document360 connection: profile name, environment, project/workspace id, and whether it is a production profile. Call this before maintaining d360-category-map.json so you scope IDs to the right profile.",{},async()=>{try{return S({profile:e.name,environment:e.connection.name,production:e.production,projectId:n??B(r),workspaceId:o??null})}catch(a){return w(a)}}),l=v("d360_list_projects","List all Document360 projects the signed-in user can access (id, name, sub-domain, status).",{},async()=>{try{return S(await $(r,vt))}catch(a){return w(a)}}),p=v("d360_list_workspaces","List workspaces (project versions) for the current project. Each has id, name, slug, is_default, workspace_type.",{project_id:g.string().optional().describe("Defaults to the logged-in/config project.")},async a=>{try{return S(await $(r,`${P(a.project_id??s())}/workspaces`))}catch(m){return w(m)}}),f=v("d360_list_categories","List categories in a workspace (the folder structure for articles).",{workspace_id:g.string().optional().describe("Defaults to d360.workspaceId in config."),project_id:g.string().optional()},async a=>{try{let m=a.workspace_id??o;return m?S(await $(r,`${P(a.project_id??s())}/workspaces/${m}/categories`)):w("No workspace_id given and none in config. Call d360_list_workspaces first.")}catch(m){return w(m)}}),h=v("d360_list_articles","List articles in a workspace (id, title, status, category). Use to see existing docs before writing.",{workspace_id:g.string().optional().describe("Defaults to d360.workspaceId in config."),project_id:g.string().optional()},async a=>{try{let m=a.workspace_id??o;return m?S(await $(r,`${P(a.project_id??s())}/workspaces/${m}/articles`)):w("No workspace_id given and none in config. Call d360_list_workspaces first.")}catch(m){return w(m)}}),k=v("d360_get_article","Get a single article including its content. content_mode=raw returns the stored markdown source (best for editing); display returns processed content.",{article_id:g.string(),content_mode:g.enum(["raw","display"]).optional().describe("raw = stored markdown source (default), display = processed."),published:g.boolean().optional().describe("Read the published version instead of the latest draft."),project_id:g.string().optional()},async a=>{try{return S(await R(r,`${P(a.project_id??s())}/articles/${a.article_id}`,{query:{content_mode:a.content_mode,published:a.published}}))}catch(m){return w(m)}}),x=v("d360_ai_query","Ask Document360's AI search over a workspace's published content. Returns an answer grounded in existing articles \u2014 use to check what's already documented.",{query:g.string(),workspace_id:g.string().optional().describe("Defaults to d360.workspaceId in config."),project_id:g.string().optional()},async a=>{try{let m=a.workspace_id??o;return m?S(await F(r,`${P(a.project_id??s())}/workspaces/ai/query`,{body:{query:a.query,workspace_id:m}})):w("No workspace_id given and none in config. Call d360_list_workspaces first.")}catch(m){return w(m)}}),b=v("d360_sync_status","Deterministic drift report: local docs tree vs Document360, classified against the sync bases in d360-category-map.json. Call this BEFORE any docs gap/coverage analysis \u2014 it proves whether the local docs tree can be trusted as the Document360 inventory (no need to fetch article content). Statuses: local-ahead (push pending), remote-ahead (portal edit to pull), conflict, untracked-local/remote, deleted-local/remote, unknown-base (no base recorded yet).",{},async()=>{try{if(!t.cwd)return w("Sync status needs a repo context (no cwd configured for this session).");let a=await yt({cwd:t.cwd,profileName:e.name});return S({profile:a.profile,docsRoot:a.docsRoot,generatedAt:a.generatedAt,counts:a.counts,attention:a.entries.filter(m=>m.status!=="in-sync")})}catch(a){return w(a)}}),O=v("d360_repo_inventory","Deterministic inventory of candidate source folders for documentation scope (no file reads of your own needed). Returns project-level folders with: path, fileCount, detected stacks, a `recommended` flag (user-facing surfaces pre-recommended; tests/infrastructure not), and a one-line reason. Use this when analyzing a large/multi-project repo to decide which folders back the docs \u2014 it is the same data the /scope picker pre-ticks, so your recommendation and the picker agree. Cheap; safe to call before reading any source.",{},async()=>{try{return t.cwd?S(_t(t.cwd)):w("Repo inventory needs a repo context (no cwd configured for this session).")}catch(a){return w(a)}}),Q=v("d360_create_category","Create a category (a docs folder). Returns the new category id.",{name:g.string(),workspace_id:g.string().optional(),parent_category_id:g.string().optional().describe("Omit for a top-level category."),content:g.string().optional(),slug:g.string().optional(),order:g.number().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/categories`,{body:{name:a.name,workspace_id:i(a.workspace_id),parent_category_id:a.parent_category_id,content:a.content,slug:a.slug,order:a.order,content_type:"markdown"}}))}catch(y){return w(y)}}),T=v("d360_create_article","Create a DRAFT article in a category. The body is always Markdown (product rule \u2014 we never create WYSIWYG/Block articles). Returns the new article id. Does not publish.",{title:g.string(),category_id:g.string(),content:g.string().optional().describe("Markdown body."),workspace_id:g.string().optional(),slug:g.string().optional(),order:g.number().optional(),project_id:g.string().optional(),local_path:g.string().optional().describe("Repo-relative path of the local .md this article mirrors (e.g. user-docs/01-intro/01-a.md). When given, the new article id + sync base are recorded into d360-category-map.json automatically \u2014 do not edit the articles map yourself.")},async a=>{let m=u();if(m)return m;try{let y=a.project_id??s(),D=a.content;typeof D=="string"&&a.local_path&&t.cwd&&(D=De(D,a.local_path,H(t.cwd,e.name)).content);let _=await F(r,`${P(y)}/articles/bulk`,{body:{articles:[{title:a.title,category_id:a.category_id,workspace_id:i(a.workspace_id),content:D,slug:a.slug,order:a.order,content_type:"markdown"}]}}),U=Array.isArray(_)?_[0]:void 0;if(a.local_path&&typeof U?.id=="string"){let C;try{let M=await R(r,`${P(y)}/articles/${U.id}`,{});C=typeof M?.url=="string"?M.url:void 0}catch{}c(a.local_path,{id:U.id,remoteContentHash:W(a.title,D),remoteVersion:1,...C?{url:C}:{}})}return S(_)}catch(y){return w(y)}}),le=v("d360_update_article","Update an article's title/content/category. Edits the latest draft by default; set auto_fork to safely edit a published article (creates a new draft version).",{article_id:g.string(),title:g.string().optional(),content:g.string().optional().describe("Markdown body."),category_id:g.string().optional(),hidden:g.boolean().optional(),version_number:g.number().optional(),auto_fork:g.boolean().optional().describe("If the target version is published, fork a new draft instead of erroring."),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{let{article_id:y,project_id:D,..._}=a,U=D??s(),C=typeof _.content=="string"&&t.cwd?H(t.cwd,e.name):null,M=C?ft(C,y):null;typeof _.content=="string"&&M&&C&&(_.content=De(_.content,M,C).content);let L=null;if(typeof _.content=="string"&&t.onUiEvent)try{L=await R(r,`${P(U)}/articles/${y}`,{query:{content_mode:"raw"}})}catch{}let $e=e.project.languageCode??L?.lang_code,Oe=await ct(r,`${P(U)}/articles/${y}`,{body:_,...$e?{query:{lang_code:$e}}:{}});if(L&&typeof _.content=="string"&&(L.content??"")!==_.content&&t.onUiEvent?.({type:"article_diff",articleId:y,title:L.title??null,oldContent:L.content??"",newContent:_.content}),typeof _.content=="string"&&M)try{let K=Oe,Ue=_.title??K?.title??L?.title;typeof Ue=="string"&&c(M,{id:y,remoteContentHash:W(Ue,_.content),...typeof K?.latest_version=="number"?{remoteVersion:K.latest_version}:{},...typeof K?.modified_at=="string"?{remoteModifiedAt:K.modified_at}:{}})}catch{}return S(Oe)}catch(y){return w(y)}}),G=v("d360_fork_article","Fork a published article into a new draft version \u2014 the safe way to start editing live content.",{article_id:g.string(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/articles/${a.article_id}/fork`))}catch(y){return w(y)}}),J=v("d360_publish_article","Publish an article version. This makes the draft live to readers \u2014 only call when the user explicitly asks to publish.",{article_id:g.string(),version_number:g.number(),workspace_id:g.string().optional(),message:g.string().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/articles/${a.article_id}/publish`,{body:{workspace_id:i(a.workspace_id),version_number:a.version_number,message:a.message}}))}catch(y){return w(y)}}),Z=v("d360_unpublish_article","Unpublish an article version, reverting it to draft (removes it from readers).",{article_id:g.string(),version_number:g.number().describe("The published version to unpublish (see d360_get_article)."),workspace_id:g.string().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{return S(await F(r,`${P(a.project_id??s())}/articles/${a.article_id}/unpublish`,{body:{workspace_id:i(a.workspace_id),version_number:a.version_number}}))}catch(y){return w(y)}}),Et=v("d360_upload_drive_file","Upload a local file (e.g. a captured screenshot PNG) to Drive and return its URL for embedding in an article. Uploads to the default folder unless folder_id is given.",{file_path:g.string().describe("Local path to the file (absolute, or relative to the repo)."),folder_id:g.string().optional(),title:g.string().optional(),project_id:g.string().optional()},async a=>{let m=u();if(m)return m;try{let y=a.project_id??s(),D=Wr(a.file_path)?a.file_path:bt(process.cwd(),a.file_path),_=a.folder_id;if(!_&&(_=(await R(r,`${P(y)}/drive/folders/default`))?.id,!_))return w("Could not resolve the default Drive folder.");let U=kt(D),C=new FormData;return C.append("file",new Blob([U]),a.title??Hr(D)),S(await lt(r,`${P(y)}/drive/folders/${_}/files`,C))}catch(y){return w(y)}}),It=v("d360_search_drive","Search Drive files (e.g. to reuse an already-uploaded screenshot instead of uploading a duplicate).",{search_keyword:g.string().optional(),allow_images_only:g.boolean().optional(),project_id:g.string().optional()},async a=>{try{return S(await $(r,`${P(a.project_id??s())}/drive/search`,{query:{search_keyword:a.search_keyword,allow_images_only:a.allow_images_only}}))}catch(m){return w(m)}});return zr({name:"document360",version:"0.2.0",instructions:"First-party Document360 tools. The signed-in user's permissions apply server-side. Project/workspace default from the active profile; pass ids explicitly to override. Create articles as DRAFTS; only call d360_publish_article when the user explicitly asks to publish. All articles are Markdown \u2014 the tools enforce this; never attempt WYSIWYG/Block content. To edit a published article, fork it (d360_fork_article) or update with auto_fork. Before any docs gap/coverage analysis, call d360_sync_status to verify the local docs tree matches Document360. When scoping a large repo (which folders back the docs), call d360_repo_inventory for the candidate folders. Auth errors mean the session expired \u2014 tell the user to run /login (works inside this session).",tools:[d,l,p,f,h,k,x,b,O,Q,T,le,G,J,Z,Et,It]})}var Ct="CLAUDE.md";function xt(e){if(!Ae(e))return[];let t=[];for(let r of Jr(e)){let n=Ee(e,r);if(r===Ct||!Vr(n).isDirectory())continue;let o=Ee(n,"SKILL.md");Ae(o)&&t.push({name:r,body:jt(o,"utf8")})}return t}function Pt(e){let t=Ee(e,Ct);return Ae(t)?jt(t,"utf8"):null}function Kr(e,t){let r=He(),n=We(e),o=Pt(r),s=Pt(n),i=xt(r),c=xt(n),u=new Set(c.map(p=>p.name)),d=[...i.filter(p=>!u.has(p.name)),...c],l=[];if(o&&l.push(o),s&&l.push(`# Project addendum
11
11
 
12
12
  ${s}`),t&&(l.push("# Project configuration"),l.push("```json"),l.push(JSON.stringify(t,null,2)),l.push("```")),d.length>0){l.push("# Capabilities"),l.push('You have the following specialized documentation skills. Activate the one whose preconditions match the user request. If multiple match, run them in order. These skills are INLINE INSTRUCTIONS in this prompt \u2014 "activating" one means following its steps directly. They are NOT registered with any Skill tool; never attempt to invoke them via a Skill/skill-runner tool.');for(let p of d)l.push(`## Skill: ${p.name}`),l.push(p.body)}return l.join(`
13
13
 
14
- `)}function Yr(){let e=Ve(),t={};for(let[r,n]of Object.entries(e.servers))n.type==="stdio"?t[r]={type:"stdio",command:n.command,args:n.args,env:n.env}:n.type==="http"?t[r]={type:"http",url:n.url,headers:n.headers}:n.type==="sse"&&(t[r]={type:"sse",url:n.url,headers:n.headers});return t}var Qr=/auth|api key|login|credential|401|forbidden/i,Ie=class{queue;iterator;sdkQuery;uiEvents=[];sessionId=null;constructor(t={}){let r=t.cwd??process.cwd(),n=N(r),o=Kr(r,n),s=Yr();try{let l=z(r,t.profileName),p=!l.production||t.allowProdWrites===!0;s.document360=St(l,{writesAllowed:p,cwd:r,onUiEvent:f=>this.uiEvents.push(f)})}catch{}this.queue=new te;let i=async(l,p)=>{if(Ne.has(l)){let f=p.file_path??p.notebook_path;if(f&&!de(r,f,n))return{behavior:"deny",message:ue(f)}}if(l==="Bash"||l==="PowerShell"){let f=p.command;if(typeof f=="string"){let h=qe(f,r,n);if(h)return{behavior:"deny",message:ue(h)}}}return{behavior:"allow",updatedInput:p}},c=n?.mode==="engineer",u={cwd:r,systemPrompt:o,mcpServers:s,...c?{permissionMode:"bypassPermissions",allowDangerouslySkipPermissions:!0}:{permissionMode:"default",canUseTool:i},disallowedTools:["Skill"],includePartialMessages:!0,settingSources:[],model:n?.defaultModel??we().defaultModel,resume:t.resume},d=Gr({prompt:this.queue,options:u});this.sdkQuery=d,this.iterator=d[Symbol.asyncIterator]()}async interrupt(){try{await this.sdkQuery.interrupt()}catch{}}async setModel(t){await this.sdkQuery.setModel(t)}async*send(t){for(this.queue.push(t);;){let r;try{r=await this.iterator.next()}catch(s){let i=s.message;yield{type:"error",kind:Qr.test(i)?"auth":"agent",message:i};return}if(r.done)return;for(;this.uiEvents.length>0;)yield this.uiEvents.shift();let n=r.value?.session_id;n&&!this.sessionId&&(this.sessionId=n,yield{type:"session",sessionId:n});let o=Xr(r.value);if(o){for(let s of o)yield s;if(o.some(s=>s.type==="result"))return}}}close(){this.queue.close()}};function as(e={}){return new Ie(e)}function Zr(e){return typeof e=="string"?e:Array.isArray(e)?e.map(t=>{let r=t;return r.type==="text"&&typeof r.text=="string"?r.text:""}).filter(Boolean).join(`
15
- `):""}function Xr(e){if(!e||typeof e!="object")return null;let t=e;if(t.type==="stream_event"){let r=t.event;return r?.delta&&typeof r.delta.text=="string"?[{type:"text",delta:r.delta.text}]:null}if(t.type==="assistant"){let r=t.message,n=[];for(let o of r?.content??[]){let s=o;s.type==="tool_use"&&n.push({type:"tool",id:s.id??"",name:s.name??"tool",input:s.input??{}})}return n.length>0?n:null}if(t.type==="user"){let r=t.message;if(!Array.isArray(r?.content))return null;let n=[];for(let o of r.content){let s=o;s.type==="tool_result"&&n.push({type:"tool_result",id:s.tool_use_id??"",output:Zr(s.content),isError:s.is_error===!0})}return n.length>0?n:null}if(t.type==="result"){let r=t;return[{type:"result",inputTokens:r.usage?.input_tokens??0,outputTokens:r.usage?.output_tokens??0,ok:r.subtype==="success"}]}return null}import{existsSync as en}from"node:fs";import{homedir as tn}from"node:os";import{join as rn}from"node:path";var ps=["auto","api","subscription"];function Dt(){return process.env.CLAUDE_CODE_OAUTH_TOKEN?!0:en(rn(tn(),".claude",".credentials.json"))}function fs(e){let t=!!process.env.ANTHROPIC_API_KEY;return e==="api"?t?{kind:"api"}:{kind:"none"}:e==="subscription"?(delete process.env.ANTHROPIC_API_KEY,{kind:"subscription",stored:Dt()}):t?{kind:"api"}:{kind:"subscription",stored:Dt()}}import{existsSync as nn,readFileSync as on,writeFileSync as sn}from"node:fs";var an=100;function V(){let e=ge();if(!nn(e))return[];try{let t=JSON.parse(on(e,"utf8"));return Array.isArray(t)?t:[]}catch{return[]}}function ce(e){E(j());let t=[...e].sort((r,n)=>n.updatedAt.localeCompare(r.updatedAt)).slice(0,an);sn(ge(),JSON.stringify(t,null,2))}function ys(e){let t=V().filter(r=>r.uuid!==e.uuid);t.push(e),ce(t)}function ws(e){return V().find(t=>t.uuid===e)}function cn(e){return V().filter(t=>t.cwd===e).sort((t,r)=>r.updatedAt.localeCompare(t.updatedAt))}function ks(e,t){let r=cn(e),n=t.toLowerCase(),o=r.find(c=>c.name.toLowerCase()===n);if(o)return o;let s=r.filter(c=>c.name.toLowerCase().startsWith(n));if(s.length===1)return s[0];let i=r.filter(c=>c.uuid.startsWith(t));if(i.length===1)return i[0]}function _s(e){let t=V(),r=t.find(n=>n.uuid===e);r&&(r.updatedAt=new Date().toISOString(),ce(t))}function vs(e,t){let r=V(),n=r.find(o=>o.uuid===e);return n?(n.name=t,n.renamed=!0,n.updatedAt=new Date().toISOString(),ce(r),!0):!1}function bs(e,t){let r=V(),n=r.find(o=>o.uuid===e);!n||n.renamed||(n.name=t,n.titled=!0,ce(r))}function Ss(e,t=6){return e.toLowerCase().replace(/[^a-z0-9\s-]/g,"").split(/\s+/).filter(Boolean).slice(0,t).join("-")||"session"}function xs(e){let t=Date.now()-new Date(e).getTime(),r=Math.floor(t/6e4);if(r<1)return"just now";if(r<60)return`${r}m ago`;let n=Math.floor(r/60);return n<24?`${n}h ago`:`${Math.floor(n/24)}d ago`}import{query as ln}from"@anthropic-ai/claude-agent-sdk";var dn=6e4;async function Cs(e,t){try{return await Promise.race([un(e,t),pn()])}catch{return null}}async function un(e,t){let r=ln({prompt:["Generate a short session title for a documentation-writing session that started with this request:","",e.slice(0,500),"","Reply with ONLY the title: 3-6 words, lowercase, kebab-case, no quotes, no punctuation."].join(`
14
+ `)}function Yr(){let e=Ve(),t={};for(let[r,n]of Object.entries(e.servers))n.type==="stdio"?t[r]={type:"stdio",command:n.command,args:n.args,env:n.env}:n.type==="http"?t[r]={type:"http",url:n.url,headers:n.headers}:n.type==="sse"&&(t[r]={type:"sse",url:n.url,headers:n.headers});return t}var Qr=/auth|api key|login|credential|401|forbidden/i,Ie=class{queue;iterator;sdkQuery;uiEvents=[];sessionId=null;constructor(t={}){let r=t.cwd??process.cwd(),n=N(r),o=Kr(r,n),s=Yr();try{let l=z(r,t.profileName),p=!l.production||t.allowProdWrites===!0;s.document360=St(l,{writesAllowed:p,cwd:r,onUiEvent:f=>this.uiEvents.push(f)})}catch{}this.queue=new te;let i=async(l,p)=>{if(Ne.has(l)){let f=p.file_path??p.notebook_path;if(f&&!de(r,f,n))return{behavior:"deny",message:ue(f)}}if(l==="Bash"||l==="PowerShell"){let f=p.command;if(typeof f=="string"){let h=qe(f,r,n);if(h)return{behavior:"deny",message:ue(h)}}}return{behavior:"allow",updatedInput:p}},c=n?.mode==="engineer",u={cwd:r,systemPrompt:o,mcpServers:s,...c?{permissionMode:"bypassPermissions",allowDangerouslySkipPermissions:!0}:{permissionMode:"default",canUseTool:i},disallowedTools:["Skill","AskUserQuestion"],includePartialMessages:!0,settingSources:[],model:n?.defaultModel??we().defaultModel,resume:t.resume},d=Gr({prompt:this.queue,options:u});this.sdkQuery=d,this.iterator=d[Symbol.asyncIterator]()}async interrupt(){try{await this.sdkQuery.interrupt()}catch{}}async setModel(t){await this.sdkQuery.setModel(t)}async*send(t){for(this.queue.push(t);;){let r;try{r=await this.iterator.next()}catch(s){let i=s.message;yield{type:"error",kind:Qr.test(i)?"auth":"agent",message:i};return}if(r.done)return;for(;this.uiEvents.length>0;)yield this.uiEvents.shift();let n=r.value?.session_id;n&&!this.sessionId&&(this.sessionId=n,yield{type:"session",sessionId:n});let o=Xr(r.value);if(o){for(let s of o)yield s;if(o.some(s=>s.type==="result"))return}}}close(){this.queue.close()}};function as(e={}){return new Ie(e)}function Zr(e){return typeof e=="string"?e:Array.isArray(e)?e.map(t=>{let r=t;return r.type==="text"&&typeof r.text=="string"?r.text:""}).filter(Boolean).join(`
15
+ `):""}function Xr(e){if(!e||typeof e!="object")return null;let t=e;if(t.type==="stream_event"){let r=t.event;return r?.delta&&typeof r.delta.text=="string"?[{type:"text",delta:r.delta.text}]:null}if(t.type==="assistant"){let r=t.message,n=[];for(let o of r?.content??[]){let s=o;s.type==="tool_use"&&n.push({type:"tool",id:s.id??"",name:s.name??"tool",input:s.input??{}})}return n.length>0?n:null}if(t.type==="user"){let r=t.message;if(!Array.isArray(r?.content))return null;let n=[];for(let o of r.content){let s=o;s.type==="tool_result"&&n.push({type:"tool_result",id:s.tool_use_id??"",output:Zr(s.content),isError:s.is_error===!0})}return n.length>0?n:null}if(t.type==="result"){let r=t;return[{type:"result",inputTokens:r.usage?.input_tokens??0,outputTokens:r.usage?.output_tokens??0,cacheReadTokens:r.usage?.cache_read_input_tokens??0,cacheCreationTokens:r.usage?.cache_creation_input_tokens??0,costUsd:r.total_cost_usd??0,ok:r.subtype==="success"}]}return null}import{existsSync as en}from"node:fs";import{homedir as tn}from"node:os";import{join as rn}from"node:path";var ps=["auto","api","subscription"];function Dt(){return process.env.CLAUDE_CODE_OAUTH_TOKEN?!0:en(rn(tn(),".claude",".credentials.json"))}function fs(e){let t=!!process.env.ANTHROPIC_API_KEY;return e==="api"?t?{kind:"api"}:{kind:"none"}:e==="subscription"?(delete process.env.ANTHROPIC_API_KEY,{kind:"subscription",stored:Dt()}):t?{kind:"api"}:{kind:"subscription",stored:Dt()}}import{existsSync as nn,readFileSync as on,writeFileSync as sn}from"node:fs";var an=100;function V(){let e=ge();if(!nn(e))return[];try{let t=JSON.parse(on(e,"utf8"));return Array.isArray(t)?t:[]}catch{return[]}}function ce(e){E(j());let t=[...e].sort((r,n)=>n.updatedAt.localeCompare(r.updatedAt)).slice(0,an);sn(ge(),JSON.stringify(t,null,2))}function ys(e){let t=V().filter(r=>r.uuid!==e.uuid);t.push(e),ce(t)}function ws(e){return V().find(t=>t.uuid===e)}function cn(e){return V().filter(t=>t.cwd===e).sort((t,r)=>r.updatedAt.localeCompare(t.updatedAt))}function _s(e,t){let r=cn(e),n=t.toLowerCase(),o=r.find(c=>c.name.toLowerCase()===n);if(o)return o;let s=r.filter(c=>c.name.toLowerCase().startsWith(n));if(s.length===1)return s[0];let i=r.filter(c=>c.uuid.startsWith(t));if(i.length===1)return i[0]}function ks(e){let t=V(),r=t.find(n=>n.uuid===e);r&&(r.updatedAt=new Date().toISOString(),ce(t))}function bs(e,t){let r=V(),n=r.find(o=>o.uuid===e);return n?(n.name=t,n.renamed=!0,n.updatedAt=new Date().toISOString(),ce(r),!0):!1}function vs(e,t){let r=V(),n=r.find(o=>o.uuid===e);!n||n.renamed||(n.name=t,n.titled=!0,ce(r))}function Ss(e,t=6){return e.toLowerCase().replace(/[^a-z0-9\s-]/g,"").split(/\s+/).filter(Boolean).slice(0,t).join("-")||"session"}function xs(e){let t=Date.now()-new Date(e).getTime(),r=Math.floor(t/6e4);if(r<1)return"just now";if(r<60)return`${r}m ago`;let n=Math.floor(r/60);return n<24?`${n}h ago`:`${Math.floor(n/24)}d ago`}import{query as ln}from"@anthropic-ai/claude-agent-sdk";var dn=6e4;async function Cs(e,t){try{return await Promise.race([un(e,t),pn()])}catch{return null}}async function un(e,t){let r=ln({prompt:["Generate a short session title for a documentation-writing session that started with this request:","",e.slice(0,500),"","Reply with ONLY the title: 3-6 words, lowercase, kebab-case, no quotes, no punctuation."].join(`
16
16
  `),options:{cwd:t,model:"haiku",maxTurns:1,settingSources:[],systemPrompt:"You generate terse kebab-case session titles. Output only the title, nothing else.",allowedTools:[]}});for await(let n of r){let o=n;if(o.type==="result")return o.subtype==="success"&&typeof o.result=="string"?fn(o.result):null}return null}function pn(){return new Promise(e=>setTimeout(()=>e(null),dn).unref())}function fn(e){let t=e.trim().toLowerCase().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"").split("-").slice(0,8).join("-").slice(0,60);return t.length>=3?t:null}import{query as gn}from"@anthropic-ai/claude-agent-sdk";var mn=3e4;async function Ts(e,t,r){try{return await Promise.race([hn(e,t,r),yn()])}catch{return null}}async function hn(e,t,r){let n=gn({prompt:["In a documentation-writing session, the user asked:","",e.slice(0,800),"","The assistant's reply ended with:","",t.slice(-4e3),"","Suggest the user's most likely next prompt. Reply with ONLY that prompt:","imperative, specific, at most 15 words, no quotes \u2014 a complete, grammatical","sentence the user could send verbatim. If the assistant asked a question,",'answer it. If the assistant offered options AND recommended one ("the right','approach", "recommended", "I suggest"), your suggestion must accept the',"RECOMMENDED option \u2014 never an alternative the assistant advised against or","deferred. If there is no clear next action, reply with exactly: NONE"].join(`
17
17
  `),options:{cwd:r,model:"haiku",maxTurns:1,settingSources:[],systemPrompt:"You propose the next user prompt for a docs-writing CLI. Output only the prompt, or NONE.",allowedTools:[]}});for await(let o of n){let s=o;if(s.type==="result")return s.subtype==="success"&&typeof s.result=="string"?wn(s.result):null}return null}function yn(){return new Promise(e=>setTimeout(()=>e(null),mn).unref())}function wn(e){let t=e.trim().split(`
18
- `)[0].replace(/^["'`]+|["'`]+$/g,"").trim();return!t||/^none\b/i.test(t)||t.length<4||t.length>100?null:t}var kn=/^---\r?\n[\s\S]*?\r?\n---\r?\n/;function Rt(e){let t=e.match(kn);return t?{frontmatter:t[0],body:e.slice(t[0].length)}:{frontmatter:null,body:e}}import{existsSync as _n,mkdirSync as vn,readFileSync as bn,writeFileSync as Sn}from"node:fs";import{dirname as xn,join as Tt}from"node:path";var At=/^\[Screenshot: [^\]]+\]\s*$/;function Pn(e){let t=e.replace(/\r\n/g,`
18
+ `)[0].replace(/^["'`]+|["'`]+$/g,"").trim();return!t||/^none\b/i.test(t)||t.length<4||t.length>100?null:t}var _n=/^---\r?\n[\s\S]*?\r?\n---\r?\n/;function Rt(e){let t=e.match(_n);return t?{frontmatter:t[0],body:e.slice(t[0].length)}:{frontmatter:null,body:e}}import{existsSync as kn,mkdirSync as bn,readFileSync as vn,writeFileSync as Sn}from"node:fs";import{dirname as xn,join as Tt}from"node:path";var At=/^\[Screenshot: [^\]]+\]\s*$/;function Pn(e){let t=e.replace(/\r\n/g,`
19
19
  `).split(`
20
20
  `),r=new Map;for(let n=0;n<t.length;n++){if(!t[n].startsWith("<!-- SCREENSHOT"))continue;let o=n;for(;n<t.length&&!t[n].includes("-->");)n++;if(n>=t.length)break;let s=t.slice(o,n+1),i=n+1;for(;i<t.length&&t[i].trim()==="";)i++;i<t.length&&At.test(t[i])&&r.set(t[i].trim(),[...s,...t.slice(n+1,i)])}return r}function jn(e,t,r){let n=[],o=t.replace(/\r\n/g,`
21
21
  `).replace(/^\n+/,""),s=r?Rt(r).frontmatter:null,i=o,c=0;if(r){let d=Pn(r);d.size>0&&(i=o.split(`
@@ -23,6 +23,6 @@ ${s}`),t&&(l.push("# Project configuration"),l.push("```json"),l.push(JSON.strin
23
23
  `))}return c>0&&n.push(`${c} screenshot spec comment(s) re-attached from the existing local file.`),r&&/\]\((\.\.?\/)[^)]+\.md\)/.test(r)&&n.push("The existing local file has relative cross-article links; publishing flattened them to plain text on Document360, so this pull does not restore them."),/!\[[^\]]*\]\(https?:\/\/[^)]*\)/.test(o)&&n.push("Remote content embeds uploaded images (Drive URLs); they are kept as-is locally."),s&&n.push("Existing frontmatter preserved."),{content:`${s??""}# ${e}
24
24
 
25
25
  ${i}`.replace(/\n*$/,`
26
- `),notes:n}}async function Ls(e,t=R){let r=z(e.cwd,e.profileName),n=H(e.cwd,r.name),o=n?.articles[e.relPath];if(!n||!o)throw new Error(`"${e.relPath}" is not in d360-category-map.json for profile "${r.name}". Run /sync to see tracked articles.`);let s={profile:r.name,connection:r.connection},i=n.projectId??r.project.projectId??B(s),c=await t(s,`/v3/projects/${i}/articles/${o.id}`,{query:{content_mode:"raw"}}),u=Tt(e.cwd,e.relPath),d=_n(u)?bn(u,"utf8"):null,l=c.title??"",{content:p,notes:f}=jn(l,c.content??"",d);return{path:e.relPath,articleId:o.id,title:l,oldContent:d??"",newContent:p,remoteContentHash:W(c.title,c.content),latestVersion:c.latest_version,modifiedAt:c.modified_at,overwritesLocalChanges:d!==null&&o.localHash!==void 0&&q(d)!==o.localHash,notes:f}}function Bs(e,t){let r=z(e.cwd,e.profileName),n=Tt(e.cwd,t.path),o=t.oldContent.includes(`\r
26
+ `),notes:n}}async function Ls(e,t=R){let r=z(e.cwd,e.profileName),n=H(e.cwd,r.name),o=n?.articles[e.relPath];if(!n||!o)throw new Error(`"${e.relPath}" is not in d360-category-map.json for profile "${r.name}". Run /sync to see tracked articles.`);let s={profile:r.name,connection:r.connection},i=n.projectId??r.project.projectId??B(s),c=await t(s,`/v3/projects/${i}/articles/${o.id}`,{query:{content_mode:"raw"}}),u=Tt(e.cwd,e.relPath),d=kn(u)?vn(u,"utf8"):null,l=c.title??"",{content:p,notes:f}=jn(l,c.content??"",d);return{path:e.relPath,articleId:o.id,title:l,oldContent:d??"",newContent:p,remoteContentHash:W(c.title,c.content),latestVersion:c.latest_version,modifiedAt:c.modified_at,overwritesLocalChanges:d!==null&&o.localHash!==void 0&&q(d)!==o.localHash,notes:f}}function Bs(e,t){let r=z(e.cwd,e.profileName),n=Tt(e.cwd,t.path),o=t.oldContent.includes(`\r
27
27
  `)?t.newContent.replace(/\n/g,`\r
28
- `):t.newContent;vn(xn(n),{recursive:!0}),Sn(n,o,"utf8"),se(e.cwd,r.name,t.path,{id:t.articleId,localHash:q(o),remoteContentHash:t.remoteContentHash,remoteVersion:t.latestVersion,remoteModifiedAt:t.modifiedAt})}export{ps as AUTH_MODES,ne as D360ApiError,I as D360AuthError,Ie as EngineSession,Y as ProfileConfigError,cr as apiLogDir,Bs as applyPull,Qt as buildAuthorizationUrl,dt as categoryMapPath,Pr as classify,Zn as clearTokens,yt as computeSyncStatus,as as createSession,ht as createV3RemoteProvider,R as d360Get,$ as d360GetAll,ct as d360Patch,F as d360Post,lt as d360Upload,st as decodeJwtClaims,E as ensureDir,Ye as exchangeCode,ks as findByName,ft as findPathByArticleId,Cs as generateTitle,at as getAccessToken,co as getArticle,ws as getSession,q as hashContent,W as hashRemote,kt as inventoryRepo,ot as isExpired,kr as knownEnvironments,cn as listSessions,ao as listWorkspaces,H as loadProfileMap,V as loadSessions,be as loadTokens,Pe as logApiCall,xe as logBodiesAlways,re as loggableBody,Vn as loginPkce,wr as normalizeMarkdown,Wt as packageRoot,He as packageSkillsDir,Vt as pickModel,Ls as planPull,me as projectConfigPath,We as projectSkillsDir,Ve as readMcpConfig,N as readProjectConfig,we as readUserConfig,jn as reconstructLocalMarkdown,se as recordSyncBase,lr as redactSecrets,Ze as refreshTokens,xs as relativeTime,vs as renameSession,z as resolveActiveProfile,fs as resolveAuth,ie as resolveConnection,De as resolveCrossLinks,mt as resolveDocsRoot,ho as resolveEnvironment,Ln as resolveModelSetting,ze as resolveProfile,B as resolveProjectId,nt as saveTokens,ge as sessionsFilePath,_o as setProfileProject,bs as setTitle,Ss as slugify,Rt as splitFrontmatter,Ts as suggestNextAction,Xe as toStoredTokens,_s as touchSession,dr as truncateBody,ys as upsertSession,pe as userConfigPath,j as userDir,fe as userMcpConfigPath,Bn as writeMcpConfig,Je as writeProjectConfig,qn as writeUserConfig};
28
+ `):t.newContent;bn(xn(n),{recursive:!0}),Sn(n,o,"utf8"),se(e.cwd,r.name,t.path,{id:t.articleId,localHash:q(o),remoteContentHash:t.remoteContentHash,remoteVersion:t.latestVersion,remoteModifiedAt:t.modifiedAt})}export{ps as AUTH_MODES,ne as D360ApiError,I as D360AuthError,Ie as EngineSession,Y as ProfileConfigError,cr as apiLogDir,Bs as applyPull,Qt as buildAuthorizationUrl,dt as categoryMapPath,Pr as classify,Zn as clearTokens,yt as computeSyncStatus,as as createSession,ht as createV3RemoteProvider,R as d360Get,$ as d360GetAll,ct as d360Patch,F as d360Post,lt as d360Upload,st as decodeJwtClaims,E as ensureDir,Ye as exchangeCode,_s as findByName,ft as findPathByArticleId,Cs as generateTitle,at as getAccessToken,co as getArticle,ws as getSession,q as hashContent,W as hashRemote,_t as inventoryRepo,ot as isExpired,_r as knownEnvironments,cn as listSessions,ao as listWorkspaces,H as loadProfileMap,V as loadSessions,ve as loadTokens,Pe as logApiCall,xe as logBodiesAlways,re as loggableBody,Vn as loginPkce,wr as normalizeMarkdown,Wt as packageRoot,He as packageSkillsDir,Vt as pickModel,Ls as planPull,me as projectConfigPath,We as projectSkillsDir,Ve as readMcpConfig,N as readProjectConfig,we as readUserConfig,jn as reconstructLocalMarkdown,se as recordSyncBase,lr as redactSecrets,Ze as refreshTokens,xs as relativeTime,bs as renameSession,z as resolveActiveProfile,fs as resolveAuth,ie as resolveConnection,De as resolveCrossLinks,mt as resolveDocsRoot,ho as resolveEnvironment,Ln as resolveModelSetting,ze as resolveProfile,B as resolveProjectId,nt as saveTokens,ge as sessionsFilePath,ko as setProfileProject,vs as setTitle,Ss as slugify,Rt as splitFrontmatter,Ts as suggestNextAction,Xe as toStoredTokens,ks as touchSession,dr as truncateBody,ys as upsertSession,pe as userConfigPath,j as userDir,fe as userMcpConfigPath,Bn as writeMcpConfig,Je as writeProjectConfig,qn as writeUserConfig};
package/dist/session.d.ts CHANGED
@@ -23,6 +23,12 @@ export type EngineEvent = {
23
23
  type: 'result';
24
24
  inputTokens: number;
25
25
  outputTokens: number;
26
+ /** Tokens served from cache this turn (~0.1x input price). */
27
+ cacheReadTokens: number;
28
+ /** Tokens written to cache this turn (~1.25x input price). */
29
+ cacheCreationTokens: number;
30
+ /** Authoritative turn cost from the SDK (`total_cost_usd`); 0 if unreported. */
31
+ costUsd: number;
26
32
  ok: boolean;
27
33
  } | {
28
34
  type: 'error';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document360-engine",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Headless documentation agent engine for document360-writer / -desktop. Emits a typed event stream; no UI.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/skills/CLAUDE.md CHANGED
@@ -17,6 +17,16 @@ You are a documentation engineer working from inside the user's source repositor
17
17
  - Taking screenshots — you write placeholder instructions an intern (or a paired CLI) can act on.
18
18
  - Publishing past the draft stage by default — every D360 write is a DRAFT unless the user explicitly asks to publish specific articles. Even then, publishing is gated on production profiles (refused until the user authorizes). Default to drafts; let the human review in the portal.
19
19
 
20
+ ## Confirm before bulk or irreversible actions
21
+
22
+ Before an action that writes, publishes, overwrites, or converts MANY articles at once — or any single step that is hard to undo (bulk publish, full-repo re-convert + re-push, mass update, delete) — STOP and get an explicit go-ahead first:
23
+
24
+ - State the scope concretely before asking: how many articles, which categories, draft vs publish, and the token/cost estimate if you have it.
25
+ - Lay out the options as a numbered list (see "Offering next actions") and END YOUR TURN there.
26
+ - Then wait. Do NOT proceed on a default, an assumption, or silence — **no answer is not a yes.** Act only on the user's next message.
27
+
28
+ There is no tool for asking a blocking question; you ask in prose and the turn ends naturally. A single-article draft is routine and needs no gate — the bar is *bulk or irreversible*. When unsure whether something clears that bar, ask.
29
+
20
30
  ## Voice and style
21
31
 
22
32
  - Second person ("you"). Imperative ("Click Save", not "The user should click Save").