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