import{Or as e,Tr as t,t as n}from"./jsx-runtime-NwRKtVrk.js";import{t as r}from"./request-Blbag3Ot.js";import{t as i}from"./alert-DAtI4UaJ.js";import{t as a}from"./button-CM-rzMOH.js";import{t as o}from"./card-D7XvzVif.js";import{t as s}from"./XinForm-ihfVpzb6.js";import{d as c}from"./es-Do50EHua.js";import{t as l}from"./spin-DV35Mdw9.js";import{C as u,N as d,b as f,h as p,s as m,v as h}from"./index-B7MUL4ct.js";var g=e(t(),1);async function _(){return r({url:`/system/ai/list`,method:`get`})}async function v(){return r({url:`/system/ai/config`,method:`get`})}async function y(e){return r({url:`/system/ai/save`,method:`post`,data:e})}var b=n(),{Text:x,Title:S}=p,C=()=>{let{t:e}=m(),t=(0,g.useRef)(null),[n]=u.useForm(),[r,p]=(0,g.useState)({default:[]}),[C,w]=(0,g.useState)(!1),[T,E]=(0,g.useState)(null),[D,O]=(0,g.useState)(null),k=e=>({dependencies:[`default`],visible:t=>t.default===e}),A=t=>({dataIndex:[`providers`,t,`key`],valueType:`password`,title:e(`system.ai.provider.api_key`,{lab:t}),fieldProps:{placeholder:`sk-...`},colProps:{span:16},dependency:k(t)}),j=(t,n=``)=>({dataIndex:[`providers`,t,`url`],valueType:`text`,title:e(`system.ai.provider.url`,{lab:t}),fieldProps:{placeholder:e(`system.ai.provider.url.placeholder`,{url:n})},colProps:{span:8},dependency:k(t)}),M=[A(`anthropic`),j(`anthropic`,`https://api.anthropic.com/v1`)],N=[A(`openai`),j(`openai`,`https://api.openai.com/v1`)],P=[A(`gemini`),j(`gemini`,`https://generativelanguage.googleapis.com/v1beta/`)],F=[A(`ollama`),j(`ollama`,`http://localhost:11434`)],I=[A(`azure`),j(`azure`,`https://<resource>.openai.azure.com`),{dataIndex:[`providers`,`azure`,`api_version`],valueType:`text`,title:e(`system.ai.azure.api_version`),fieldProps:{placeholder:`2025-04-01-preview`},colProps:{span:8},dependency:k(`azure`)},{dataIndex:[`providers`,`azure`,`deployment`],valueType:`text`,title:e(`system.ai.azure.deployment`),fieldProps:{placeholder:`gpt-4o`},colProps:{span:8},dependency:k(`azure`)},{dataIndex:[`providers`,`azure`,`embedding_deployment`],valueType:`text`,title:e(`system.ai.azure.embedding_deployment`),fieldProps:{placeholder:`text-embedding-3-small`},colProps:{span:8},dependency:k(`azure`)},{dataIndex:[`providers`,`azure`,`image_deployment`],valueType:`text`,title:e(`system.ai.azure.image_deployment`),fieldProps:{placeholder:`gpt-image-1`},colProps:{span:12},dependency:k(`azure`)}],L=[{dataIndex:[`providers`,`bedrock`,`region`],valueType:`text`,title:e(`system.ai.bedrock.region`),fieldProps:{placeholder:`us-east-1`},colProps:{span:12},dependency:k(`bedrock`)},A(`bedrock`),{dataIndex:[`providers`,`bedrock`,`access_key_id`],valueType:`password`,title:e(`system.ai.bedrock.access_key_id`),fieldProps:{placeholder:e(`system.ai.provider.api_key.placeholder`)},colProps:{span:12},dependency:k(`bedrock`)},{dataIndex:[`providers`,`bedrock`,`secret_access_key`],valueType:`password`,title:e(`system.ai.bedrock.secret_access_key`),fieldProps:{placeholder:e(`system.ai.provider.api_key.placeholder`)},colProps:{span:12},dependency:k(`bedrock`)},{dataIndex:[`providers`,`bedrock`,`session_token`],valueType:`password`,title:e(`system.ai.bedrock.session_token`),fieldProps:{placeholder:e(`system.ai.provider.api_key.placeholder`)},colProps:{span:24},dependency:k(`bedrock`)}],R=[{dataIndex:`default`,valueType:`radio`,title:e(`system.ai.default`),tooltip:e(`system.ai.default.tooltip`),colProps:{span:24},fieldProps:{options:r.default.map(e=>({label:e,value:e}))}},...M,...N,...P,...I,...L,...F,A(`cohere`),A(`deepseek`),A(`eleven`),A(`groq`),A(`jina`),A(`mistral`),A(`openrouter`),A(`voyageai`),A(`xai`)];(0,g.useEffect)(()=>{z(),_().then(e=>e.data.data&&p(e.data.data))},[]);let z=async()=>{let{data:e}=await v();e.success&&n.setFieldsValue(e.data)};return(0,b.jsxs)(b.Fragment,{children:[(0,b.jsxs)(`div`,{className:`mb-5`,children:[(0,b.jsx)(S,{level:3,children:e(`system.ai.page.title`)}),(0,b.jsx)(x,{type:`secondary`,children:e(`system.ai.page.description`)})]}),(0,b.jsxs)(h,{gutter:24,children:[(0,b.jsx)(d,{span:16,children:(0,b.jsx)(o,{variant:`borderless`,children:(0,b.jsx)(s,{columns:R,layout:`vertical`,formRef:t,form:n,grid:!0,onFinish:async t=>{await y(t),f.success(e(`system.ai.save.success`))},rowProps:{gutter:[10,0]}})})}),(0,b.jsx)(d,{span:8,children:(0,b.jsx)(o,{title:e(`system.ai.test.title`),children:(0,b.jsxs)(`div`,{className:`flex flex-col gap-4`,children:[(0,b.jsx)(x,{type:`secondary`,children:e(`system.ai.test.hint`)}),(0,b.jsx)(a,{type:`primary`,block:!0,icon:(0,b.jsx)(c,{}),loading:C,onClick:async()=>{w(!0),E(null),O(null);try{let t=localStorage.getItem(`token`),n=await fetch(`/index.php/system/ai/test`,{method:`POST`,headers:{Authorization:`Bearer ${t}`,Accept:`text/event-stream`}});if(!n.ok){let e=await n.json().catch(()=>null);throw Error(e?.msg||`HTTP ${n.status}`)}let r=n.body?.getReader();if(!r)throw Error(`Stream not supported`);let i=new TextDecoder,a=``,o=``;for(;;){let{done:e,value:t}=await r.read();if(e)break;a+=i.decode(t,{stream:!0});let n=a.split(`
`);a=n.pop()||``;for(let e of n){if(!e.startsWith(`data: `))continue;let t=e.slice(6).trim();if(t!==`[DONE]`)try{let e=JSON.parse(t);e.type===`text_delta`&&e.delta&&(o+=e.delta,E(o))}catch{}}}f.success(e(`system.ai.test.success`))}catch(e){let t=e?.message||`Connection test failed`;O(t),f.error(t)}finally{w(!1)}},children:e(C?`system.ai.test.testing`:`system.ai.test.button`)}),C&&!T&&(0,b.jsx)(`div`,{className:`flex justify-center py-8`,children:(0,b.jsx)(l,{description:e(`system.ai.test.testing`)})}),T&&(0,b.jsx)(i,{type:`success`,title:e(`system.ai.test.success`),description:T,showIcon:!0}),D&&(0,b.jsx)(i,{type:`error`,title:e(`system.ai.test.error`),description:D,showIcon:!0})]})})})]})]})};export{C as default};