1import{r as d,j as n}from"./react-vendor-Dc5dhXQW.js";import{y as ne,a9 as i,p as $}from"./index-DuU_YjY9.js";import{getLocalizedTestConnectionMessage as V}from"./testConnectionMessage-_eg2MXHW.js";import{u as ae}from"./i18n-vendor-CyU1p4Oa.js";import{S as j,n as G,j as Q,k as K,ar as oe,as as re,at as J,w as le,B as z,au as ie}from"./ui-vendor-CrBP-qRZ.js";import"./utils-vendor-DBzSxgRl.js";import"./markdown-vendor-elH94dlU.js";import"./dnd-vendor-zA92mhJ9.js";function ce(e){const r=[],b=/("(?:\\.|[^"\\])*")(\s*:)?|\btrue\b|\bfalse\b|\bnull\b|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|[{}\[\],:]/g;let g=0,u;for(;(u=b.exec(e))!==null;){const[t,y,U]=u;u.index>g&&r.push(e.slice(g,u.index)),y?r.push(n.jsx("span",{className:U?i.jsonEditorTokenKey:i.jsonEditorTokenString,children:t},`${u.index}-${t}`)):t==="true"||t==="false"?r.push(n.jsx("span",{className:i.jsonEditorTokenBoolean,children:t},`${u.index}-${t}`)):t==="null"?r.push(n.jsx("span",{className:i.jsonEditorTokenNull,children:t},`${u.index}-${t}`)):/^-?\d/.test(t)?r.push(n.jsx("span",{className:i.jsonEditorTokenNumber,children:t},`${u.index}-${t}`)):r.push(n.jsx("span",{className:i.jsonEditorTokenPunctuation,children:t},`${u.index}-${t}`)),g=u.index+t.length}return g<e.length&&r.push(e.slice(g)),r}function de({value:e="",onChange:r,placeholder:b,rows:g=8}){const t=d.useRef(null),y=d.useRef(null),U=f=>{t.current&&(t.current.scrollTop=f.currentTarget.scrollTop,t.current.scrollLeft=f.currentTarget.scrollLeft)},B=f=>{if(f.key!=="Tab")return;f.preventDefault();const P=f.currentTarget,l=P.selectionStart,p=P.selectionEnd,O=l!==p,E=e.slice(l,p);if(!O||!E.includes(` 2`)){if(f.shiftKey){const m=e.lastIndexOf(` 3`,l-1)+1;if(!e.slice(m,l).endsWith(" "))return;const T=e.slice(0,l-2)+e.slice(l);r==null||r(T),requestAnimationFrame(()=>{var h;(h=y.current)==null||h.setSelectionRange(l-2,l-2)});return}const k=e.slice(0,l)+" "+e.slice(p);r==null||r(k),requestAnimationFrame(()=>{var A;const m=l+2;(A=y.current)==null||A.setSelectionRange(m,m)});return}const _=e.lastIndexOf(` 4`,l-1)+1,w=e.slice(_,p).split(` 5`);if(f.shiftKey){const k=w.map(h=>h.startsWith(" ")?h.slice(2):h),m=w[0].startsWith(" ")?2:0,A=w.reduce((h,S)=>h+(S.startsWith(" ")?2:0),0),T=e.slice(0,_)+k.join(` 6`)+e.slice(p);r==null||r(T),requestAnimationFrame(()=>{var h;(h=y.current)==null||h.setSelectionRange(l-m,p-A)});return}const x=w.map(k=>` ${k}`),R=e.slice(0,_)+x.join(` 7`)+e.slice(p);r==null||r(R),requestAnimationFrame(()=>{var k;(k=y.current)==null||k.setSelectionRange(l+2,p+2*w.length)})};return n.jsxs("div",{className:i.jsonEditorContainer,children:[n.jsxs("div",{ref:t,"aria-hidden":"true",className:i.jsonEditorHighlight,children:[e?ce(e):b,!e&&n.jsx("span",{children:` 8`})]}),n.jsx("textarea",{ref:y,rows:g,value:e,onChange:f=>r==null?void 0:r(f.target.value),onKeyDown:B,onScroll:U,placeholder:b,spellCheck:!1,className:i.jsonEditorTextarea})]})}function xe({provider:e,activeModels:r,open:b,onClose:g,onSaved:u}){var D;const{t}=ae(),[y,U]=d.useState(!1),[B,f]=d.useState(!1),[P,l]=d.useState(!1),[p,O]=d.useState(!1),[E]=j.useForm(),{message:_}=ne(),[M,w]=d.useState(e.auth_mode??"api_key"),[x,R]=d.useState(Object.entries(e.custom_headers??{}).map(([a,s])=>({key:a,value:s}))),k=j.useWatch("chat_model",E),m=!e.freeze_url,A=d.useMemo(()=>{var s;const a=(s=e.meta)==null?void 0:s.base_url_options;return Array.isArray(a)?a.flatMap(o=>o&&typeof o=="object"&&typeof o.label=="string"&&typeof o.value=="string"?[o]:[]):[]},[e.meta]),T=m&&A.length>0,h=a=>{const s=a==null?void 0:a.trim();if(!s)return;let o;try{o=JSON.parse(s)}catch{throw new Error(t("models.generateConfigInvalidJson"))}if(!o||typeof o!="object"||Array.isArray(o))throw new Error(t("models.generateConfigMustBeObject"));return o},S=d.useMemo(()=>e.is_custom?k||e.chat_model||"OpenAIChatModel":e.chat_model,[e.chat_model,e.is_custom,k]),I=d.useMemo(()=>e.id==="anthropic"||e.chat_model==="AnthropicChatModel"||S==="AnthropicChatModel",[e.id,e.chat_model,S]),C=d.useMemo(()=>e.api_key_prefixes&&e.api_key_prefixes.length>0?e.api_key_prefixes:e.api_key_prefix?[e.api_key_prefix]:[],[e.api_key_prefix,e.api_key_prefixes]),X=d.useMemo(()=>e.api_key?t("models.leaveBlankKeep"):C.length>0?t("models.enterApiKey",{prefix:C.join(", ")}):t("models.enterApiKeyOptional"),[e.api_key,C,t]),Y=t(I&&M==="auth_token"?"models.authModeAuthToken":"models.apiKey"),Z=d.useMemo(()=>
8{if(m)return T?t("models.selectBaseURLHint"):e.id==="azure-openai"?t("models.azureEndpointHint"):e.id==="anthropic"?t("models.anthropicEndpointHint"):e.id==="openai"?t("models.openAIEndpoint"):e.id==="opencode"?t("models.openAICompatibleEndpoint"):e.id==="ollama"?t("models.ollamaEndpointHint"):e.id==="lmstudio"?t("models.lmstudioEndpointHint"):e.is_custom?t(S==="AnthropicChatModel"?"models.anthropicEndpointHint":"models.openAICompatibleEndpoint"):t("models.apiEndpointHint")},[m,T,e.id,e.is_custom,S,t]),v=d.useMemo(()=>m?e.id==="azure-openai"?"https://<resource>.openai.azure.com/openai/v1":e.id==="anthropic"?"https://api.anthropic.com":e.id==="openai"?"https://api.openai.com/v1":e.id==="opencode"?"https://opencode.ai/zen/v1":e.id==="ollama"?"http://localhost:11434":e.id==="lmstudio"?"http://localhost:1234/v1":e.is_custom&&S==="AnthropicChatModel"?"https://api.anthropic.com":"https://api.example.com":"",[m,e.id,e.is_custom,S]);d.useEffect(()=>{b&&(E.setFieldsValue({api_key:void 0,base_url:e.base_url||void 0,chat_model:e.chat_model||"OpenAIChatModel",generate_kwargs_text:e.generate_kwargs&&Object.keys(e.generate_kwargs).length>0?JSON.stringify(e.generate_kwargs,null,2):void 0}),O(!1),l(!1),w(e.auth_mode??"api_key"),R(Object.entries(e.custom_headers??{}).map(([a,s])=>({key:a,value:s}))))},[e,E,b]);const ee=async()=>{var a;try{const s=await E.validateFields();U(!0);const o=h(s.generate_kwargs_text),c=!!((a=s.generate_kwargs_text)!=null&&a.trim());if(e.support_connection_check){const H=x.filter(L=>L.key.trim()).reduce((L,W)=>(L[W.key.trim()]=W.value,L),{}),N=await $.testProviderConnection(e.id,{api_key:s.api_key,base_url:s.base_url,chat_model:s.chat_model,custom_headers:H,auth_mode:I?M:void 0});if(!N.success){_.error(V(N,t));return}}const F=x.filter(H=>H.key.trim()).reduce((H,N)=>(H[N.key.trim()]=N.value,H),{});await $.configureProvider(e.id,{api_key:s.api_key,base_url:s.base_url,chat_model:s.chat_model,generate_kwargs:c?o:{},custom_headers:F,auth_mode:I?M:void 0}),await u(),l(!1),g(),_.success(t("models.configurationSaved",{name:e.name}))}catch(s){if(s&&typeof s=="object"&&"errorFields"in s)return;const o=s instanceof Error?s.message:t("models.failedToSaveConfig");_.error(o)}finally{U(!1)}},te=async()=>{f(!0);try{const a=await E.validateFields(["api_key","base_url","chat_model"]),s=x.filter(c=>c.key.trim()).reduce((c,F)=>(c[F.key.trim()]=F.value,c),{}),o=await $.testProviderConnection(e.id,{api_key:a.api_key,base_url:a.base_url,chat_model:a.chat_model,custom_headers:s,auth_mode:I?M:void 0});o.success?_.success(V(o,t)):_.warning(V(o,t))}catch(a){if(a&&typeof a=="object"&&"errorFields"in a)return;const s=a instanceof Error?a.message:t("models.testConnectionError");_.error(s)}finally{f(!1)}},q=((D=r==null?void 0:r.active_llm)==null?void 0:D.provider_id)===e.id,se=()=>{const a=q?t("models.revokeConfirmContent",{name:e.name}):t("models.revokeConfirmSimple",{name:e.name});G.confirm({title:t("models.revokeAuthorization"),content:a,okText:t("models.revokeAuthorization"),okButtonProps:{danger:!0},cancelText:t("models.cancel"),onOk:async()=>{try{await $.configureProvider(e.id,{api_key:""}),await u(),g(),q?_.success(t("models.authorizationRevoked",{name:e.name})):_.success(t("models.authorizationRevokedSimple",{name:e.name}))}catch(s){const o=s instanceof Error?s.message:t("models.failedToRevoke");_.error(o)}}})};return n.jsx(G,{width:800,className:i.modelManageModal,title:t("models.configureProvider",{name:e.name}),open:b,onCancel:g,footer:n.jsxs("div",{className:i.modalFooter,children:[n.jsxs("div",{className:i.modalFooterLeft,children:[e.api_key&&n.jsx(z,{danger:!0,size:"small",onClick:se,children:t("models.revokeAuthorization")}),e.support_connection_check&&n.jsx(z,{size:"small",icon:n.jsx(ie,{}),onClick:te,loading:B,children:t("models.testConnection")})]}),n.jsxs("div",{className:i.modalFooterRight,children:[n.jsx(z,{onClick:g,children:t("models.cancel")}),n.jsx(z,{type:"primary",loading:y,disabled:!P,onClick:ee,children:t("models.save")})]})]}),destroyOnHidden:!0,children:n.jsxs(j,{form:E,layout:"vertical",initialValues:{base_url:e.base_url||void 0,chat_model:e.chat_model||"OpenAIChatModel",generate_kwargs_text:e.generate_kwargs&&Object.keys(e.generate_kwargs).length>0?JSON.stringify(e.generate_kwargs,null,2):void 0},onValuesChange:()=>l(!0),children:[e.is_custom&&n.jsx(j.Item,{name:"chat_model",label:t("models.protocol"),rules:[{required:!0,message:t("models.selectProtocol")}],extra:t("models.protocolHint"),children:n.jsx(Q,{disabled:!0,options:[{value:"OpenAIChatModel",label:t("models.protocolOpenAI")},{value:"OpenAIResponseModel",label:t("models.protocolOpenAIResponse")},{value:"AnthropicChatModel",label:t("models.protocolAnthropic")}]})}),n.jsx(j.Item,{name:"base_url",label:t("models.baseURL"),rules:m?[...e.freeze_url?[]:[{required:!0,message:t("models.pleaseEnterBaseURL")}],{validator:(a,s)=>{if(!s||!s.trim())return Promise.resolve();try{const o=new URL(s.trim());return["http:","https:"].includes(o.protocol)?Promise.resolve():Promise.reject(new Error(t("models.pleaseEnterValidURL")))}catch{return Promise.reject(new Error(t("models.pleaseEnterValidURL")))}}}]:[],extra:Z,children:T?n.jsx(Q,{options:A.map(a=>({label:`${a.label} â ${a.value}`,value:a.value})),placeholder:t("models.selectBaseURL")}):n.jsx(K,{placeholder:v,disabled:!m})}),n.jsx(j.Item,{name:"api_key",label:Y,rules:[{validator:(a,s)=>s&&C.length>0&&M!=="auth_token"&&!C.some(o=>s.startsWith(o))?Promise.reject(new Error(t("models.apiKeyShouldStart",{prefix:C.join(", ")}))):Promise.resolve()}],children:n.jsx(K.Password,{placeholder:X})}),n.jsxs("div",{className:i.advancedConfigSection,children:[n.jsx("button",{type:"button",className:i.advancedConfigToggle,onClick:()=>O(a=>!a),children:n.jsxs("span",{className:i.advancedConfigToggleLabel,children:[p?n.jsx(oe,{}):n.jsx(re,{}),t("models.advancedConfig")]})}),I&&p&&n.jsx(j.Item,{label:t("models.authMode"),children:n.jsxs(J.Group,{value:M,onChange:a=>{w(a.target.value),l(!0)},children:[n.jsx(J,{value:"api_key",children:t("models.authModeApiKey")}),n.jsx(J,{value:"auth_token",children:t("models.authModeAuthToken")})]})}),p&&n.jsx(j.Item,{label:t("models.customHeaders"),extra:t("models.customHeadersHint"),children:n.jsxs("div",{className:i.customHeadersSection,children:[x.map((a,s)=>n.jsxs("div",{className:i.customHeaderRow,children:[n.jsx(K,{className:i.customHeaderKey,placeholder:t("models.customHeaderKey"),value:a.key,onChange:o=>{const c=[...x];c[s]={...c[s],key:o.target.value},R(c),l(!0)}}),n.jsx(K,{className:i.customHeaderValue,placeholder:t("models.customHeaderValue"),value:a.value,onChange:o=>{const c=[...x];c[s]={...c[s],value:o.target.value},R(c),l(!0)}}),n.jsx(le,{className:i.customHeaderDelete,onClick:()=>{R(x.filter((o,c)=>c!==s)),l(!0)}})]},s)),n.jsx("button",{type:"button",className:i.addHeaderBtn,onClick:()=>{R([...x,{key:"",value:""}]),l(!0)},children:t("models.addHeader")})]})}),n.jsx(j.Item,{hidden:!p,name:"generate_kwargs_text",label:t("models.generateConfig"),extra:t("models.generateConfigHint"),rules:[{validator:(a,s)=>{try{return h(s),Promise.resolve()}catch(o){return Promise.reject(o instanceof Error?o:new Error(t("models.generateConfigInvalidJson")))}}}],children:n.jsx(de,{rows:8,placeholder:`Example: 9{ 10 "extra_body": { 11 "enable_thinking": false 12 }, 13 "max_tokens": 2048 14}`})})]})]})})}export{xe as ProviderConfigModal};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.