import{RbacFeatures as c}from"../../../constants/index.js";import{ASK_AI_API_URL as t}from"../../constants/common.js";import{canAccessFeature as p}from"../../utils/rbac.js";import{handleUnauthorizedApiRequest as u}from"../utils.js";import{isAiSearchEnabled as f}from"../../plugins/search/utils.js";function R(r){return async e=>{if(!t)return e.newResponse(null,404);const n=r.getConfig();if(!f(n))return e.newResponse(null,403,{});const o=e.get("auth");if(!p(c.AI_SEARCH,o,n.rbac,n.requiresLogin))return u(e);const s={"Content-Type":"application/json"};o.idpAccessToken&&(s.Cookie=`accessToken=${o.idpAccessToken}`);const i=await e.req.json(),a=await fetch(t,{method:"POST",body:JSON.stringify(i),headers:s});return e.newResponse(a.body,200,{"Content-Type":"text/event-stream"})}}export{R as askAiHandler};
