import{R as u}from"./vendor-ClLR6ncd.js";import{_ as T}from"./lodash-ZyeDnt0e.js";import{j as B,ap as L,i as c,f as d,E as m,d as R,ar as _,g}from"./index-OtVJl4GC.js";import{a as x}from"./model-DgFcCvKB.js";import{u as C}from"./model-C9Idzwgo.js";import{p as l}from"./model-DAvH-x8m.js";function J({effective:e=!1}={}){return B([...L(),e],async t=>x("name",await c({url:d(t,"query-options"),params:{type:"any",effective:e.toString()}})))}class y extends Error{constructor(t){var n;(n=t==null?void 0:t.error)!=null&&n.message&&(t.error.message=`An error from OpenAI:
${t.error.message}`),super(JSON.stringify(t||{error:{message:"An OpenAi Error"}}))}}function Y({onSuccess:e,...t}={}){return R(async n=>{const{apiKey:r,repoSpec:s,withTokenVerification:a}=T({},n,t,{withTokenVerification:!0});a&&await M(r);const i=new URLSearchParams({"api-key":r});await c({url:d(g(l(s)),"set-api-key"),method:"put",data:i})},{onSuccess:(n,r)=>{const{catalogId:s,repositoryId:a}=l((r==null?void 0:r.repoSpec)||(t==null?void 0:t.repoSpec));_.invalidateQueries([s,a,"vector-api-key"]),e&&e()}})}async function M(e){var t;try{return await c({method:"post",url:"https://api.openai.com/v1/chat/completions",headers:{Authorization:`Bearer ${e}`},data:{model:"gpt-3.5-turbo",max_tokens:1,messages:[{role:"user",content:"Hi GPT"}],temperature:.7}},()=>y)}catch(n){const r=n instanceof y&&((t=JSON.parse(n.message).error)!=null&&t.message)?JSON.parse(n.message).error.message:"Unexpected error during token verification. Try again later.";throw new Error(r)}}const h=new String("");function F(){return m(["lookup-embedder"],async()=>(await c({url:"/lookup-llm"})).filter(({"embedder-models":e})=>Array.isArray(e)&&e.length>0).map(({vendor:e,"embedder-models":t,fields:n})=>({value:e,label:e,models:t,fields:n,options:t.map(({model:r})=>({value:r,label:r})),defaultOptions:t[0].model})))}const N=e=>{try{return new URL(e)}catch{return null}};function Z({embedder:e,embedderOptionsQuery:t,endpointUrl:n,scheme:r,host:s,port:a,model:i}){if(e==="databricks"){const o=n||z(t,e,i);return`${e} endpoint-url ${encodeURIComponent(o)}`}if(e==="ollama"){const o=A(t,e,i),p=N(`${r||(o==null?void 0:o.scheme)}://${s||(o==null?void 0:o.host)}:${a||(o==null?void 0:o.port)}`);return p?`${e} port ${p.port} host ${p.hostname} scheme ${p.protocol.slice(0,-1)}`:e}return e}function A(e,t,n){var r,s,a,i;return(i=(a=(s=(r=e.data)==null?void 0:r.find(({value:o})=>o===t))==null?void 0:s.models)==null?void 0:a.find(o=>o.model===n))==null?void 0:i.props}function z(e,t,n){return(A(e,t,n)||{"endpoint-url":""})["endpoint-url"]}function j(e,t,n){var r,s,a,i,o;return!!((o=(i=(a=(s=(r=e.data)==null?void 0:r.find(({value:p})=>p===t))==null?void 0:s.models)==null?void 0:a.find(p=>p.model===n))==null?void 0:i.props)!=null&&o.needsapikey)}const f=e=>{var t,n;return(n=(t=e.data)==null?void 0:t.find(r=>r.name==="openaiApiKey"))==null?void 0:n.value};function P(){var o;const e=J({effective:!0}),t=f(e),{catalogId:n,allRepositories:r}=C(),s=(o=r==null?void 0:r[0])==null?void 0:o.id,a=m(["global","query-options"],async()=>c({url:d(g({catalogId:n,repositoryId:s}),"query-options"),params:{type:"default",effective:!1}}),{enabled:!!s}),i=f(a);return{value:t||i,isLoading:e.isLoading||a.isLoading}}function ee(e=h){const[t,n]=u.useState(e);return[t,n]}function te({initModel:e="",initEmbedder:t="",initEndpointUrl:n="",initHost:r="",initPort:s="",initScheme:a="",initApiKey:i=h}={}){const[o,p]=u.useState(t),[O,S]=u.useState(e),[k,K]=u.useState(i),[E,Q]=u.useState(n),[$,v]=u.useState(r),[U,w]=u.useState(s),[I,b]=u.useState(a),q=F();return{embedder:o,setEmbedder:p,model:O,setModel:S,apiKey:k,setApiKey:K,endpointUrl:E,setEndpointUrl:Q,host:$,setHost:v,port:U,setPort:w,scheme:I,setScheme:b,embedderOptionsQuery:q,isOpenAiEmbedder:o==="openai"}}function ne(e,t,n="user"){return c({method:"put",url:d(e,"query-options","franzOption_openaiApiKey"),params:{type:n},data:t})}export{P as a,te as b,J as c,Z as d,h as e,N as f,F as g,Y as h,A as m,j as n,z as p,ne as s,ee as u,M as v};
