import{h as Ne,i as Ce,q as Ee,m as ke,u as _,r as v,j as n,g as S,S as te,I as Pe,d as Ie,B as N,n as Oe,R as _e,t as P,f as K,T as ne}from"./index-B48HCCvn.js";import{u as Se,a as Te,b as Ae,c as De,d as Le,e as Re,f as Fe}from"./use-providers-CSQgeUSp.js";import{C as Me}from"./confirm-dialog-Bit5jNx2.js";import{I as M}from"./input-CapacyJb.js";import{P as Z,T as le,M as H,a as Ue}from"./modal-B6TZLhQ2.js";import{S as ae}from"./select-YFykpbyQ.js";import{C as ze,S as Be}from"./switch-D5RS57YU.js";import{T as oe}from"./textarea-PjCFOxmD.js";import{A as w}from"./log-adapter-CtvxzS4j.js";import{C as ce}from"./chevron-down-DBS9-_Q3.js";import{S as Ge}from"./SettingsMasterDetail-CTXxGe3o.js";import{U as Ke,C as Xe}from"./upload-BkWqEmQd.js";import{P as Ve}from"./pencil-D0ZsDzqo.js";import{R as Ze}from"./rotate-ccw-Cm5Zx65C.js";import"./check-CadkXmJ-.js";import"./arrow-left-D8mdzO_t.js";const $e=[["path",{d:"M12 15V3",key:"m9g1x1"}],["path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4",key:"ih7n3h"}],["path",{d:"m7 10 5 5 5-5",key:"brsn70"}]],qe=Ne("download",$e);function He(e,t){let l=e.slice(0,t).split(/\r\n|\n|\r/g);return[l.length,l.pop().length+1]}function Je(e,t,l){let a=e.split(/\r\n|\n|\r/g),r="",i=(Math.log10(t+1)|0)+1;for(let s=t-1;s<=t+1;s++){let o=a[s-1];o&&(r+=s.toString().padEnd(i," "),r+=":  ",r+=o,r+=`
`,s===t&&(r+=" ".repeat(i+l+2),r+=`^
`))}return r}class p extends Error{line;column;codeblock;constructor(t,l){const[a,r]=He(l.toml,l.ptr),i=Je(l.toml,a,r);super(`Invalid TOML document: ${t}

${i}`,l),this.line=a,this.column=r,this.codeblock=i}}function Ye(e,t){let l=0;for(;e[t-++l]==="\\";);return--l&&l%2}function $(e,t=0,l=e.length){let a=e.indexOf(`
`,t);return e[a-1]==="\r"&&a--,a<=l?a:-1}function U(e,t){for(let l=t;l<e.length;l++){let a=e[l];if(a===`
`)return l;if(a==="\r"&&e[l+1]===`
`)return l+1;if(a<" "&&a!=="	"||a==="")throw new p("control characters are not allowed in comments",{toml:e,ptr:t})}return e.length}function O(e,t,l,a){let r;for(;(r=e[t])===" "||r==="	"||!l&&(r===`
`||r==="\r"&&e[t+1]===`
`);)t++;return a||r!=="#"?t:O(e,U(e,t),l)}function We(e,t,l,a,r=!1){if(!a)return t=$(e,t),t<0?e.length:t;for(let i=t;i<e.length;i++){let s=e[i];if(s==="#")i=$(e,i);else{if(s===l)return i+1;if(s===a||r&&(s===`
`||s==="\r"&&e[i+1]===`
`))return i}}throw new p("cannot find end of structure",{toml:e,ptr:t})}function de(e,t){let l=e[t],a=l===e[t+1]&&e[t+1]===e[t+2]?e.slice(t,t+3):l;t+=a.length-1;do t=e.indexOf(a,++t);while(t>-1&&l!=="'"&&Ye(e,t));return t>-1&&(t+=a.length,a.length>1&&(e[t]===l&&t++,e[t]===l&&t++)),t}let Qe=/^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[-+]\d{2}:\d{2})?$/i;class A extends Date{#t=!1;#n=!1;#e=null;constructor(t){let l=!0,a=!0,r="Z";if(typeof t=="string"){let i=t.match(Qe);i?(i[1]||(l=!1,t=`0000-01-01T${t}`),a=!!i[2],a&&t[10]===" "&&(t=t.replace(" ","T")),i[2]&&+i[2]>23?t="":(r=i[3]||null,t=t.toUpperCase(),!r&&a&&(t+="Z"))):t=""}super(t),isNaN(this.getTime())||(this.#t=l,this.#n=a,this.#e=r)}isDateTime(){return this.#t&&this.#n}isLocal(){return!this.#t||!this.#n||!this.#e}isDate(){return this.#t&&!this.#n}isTime(){return this.#n&&!this.#t}isValid(){return this.#t||this.#n}toISOString(){let t=super.toISOString();if(this.isDate())return t.slice(0,10);if(this.isTime())return t.slice(11,23);if(this.#e===null)return t.slice(0,-1);if(this.#e==="Z")return t;let l=+this.#e.slice(1,3)*60+ +this.#e.slice(4,6);return l=this.#e[0]==="-"?l:-l,new Date(this.getTime()-l*6e4).toISOString().slice(0,-1)+this.#e}static wrapAsOffsetDateTime(t,l="Z"){let a=new A(t);return a.#e=l,a}static wrapAsLocalDateTime(t){let l=new A(t);return l.#e=null,l}static wrapAsLocalDate(t){let l=new A(t);return l.#n=!1,l.#e=null,l}static wrapAsLocalTime(t){let l=new A(t);return l.#t=!1,l.#e=null,l}}let et=/^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/,tt=/^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/,nt=/^[+-]?0[0-9_]/,st=/^[0-9a-f]{2,8}$/i,se={b:"\b",t:"	",n:`
`,f:"\f",r:"\r",e:"\x1B",'"':'"',"\\":"\\"};function ue(e,t=0,l=e.length){let a=e[t]==="'",r=e[t++]===e[t]&&e[t]===e[t+1];r&&(l-=2,e[t+=2]==="\r"&&t++,e[t]===`
`&&t++);let i=0,s,o="",d=t;for(;t<l-1;){let c=e[t++];if(c===`
`||c==="\r"&&e[t]===`
`){if(!r)throw new p("newlines are not allowed in strings",{toml:e,ptr:t-1})}else if(c<" "&&c!=="	"||c==="")throw new p("control characters are not allowed in strings",{toml:e,ptr:t-1});if(s){if(s=!1,c==="x"||c==="u"||c==="U"){let f=e.slice(t,t+=c==="x"?2:c==="u"?4:8);if(!st.test(f))throw new p("invalid unicode escape",{toml:e,ptr:i});try{o+=String.fromCodePoint(parseInt(f,16))}catch{throw new p("invalid unicode escape",{toml:e,ptr:i})}}else if(r&&(c===`
`||c===" "||c==="	"||c==="\r")){if(t=O(e,t-1,!0),e[t]!==`
`&&e[t]!=="\r")throw new p("invalid escape: only line-ending whitespace may be escaped",{toml:e,ptr:i});t=O(e,t)}else if(c in se)o+=se[c];else throw new p("unrecognized escape sequence",{toml:e,ptr:i});d=t}else!a&&c==="\\"&&(i=t-1,s=!0,o+=e.slice(d,i))}return o+e.slice(d,l-1)}function rt(e,t,l,a){if(e==="true")return!0;if(e==="false")return!1;if(e==="-inf")return-1/0;if(e==="inf"||e==="+inf")return 1/0;if(e==="nan"||e==="+nan"||e==="-nan")return NaN;if(e==="-0")return a?0n:0;let r=et.test(e);if(r||tt.test(e)){if(nt.test(e))throw new p("leading zeroes are not allowed",{toml:t,ptr:l});e=e.replace(/_/g,"");let s=+e;if(isNaN(s))throw new p("invalid number",{toml:t,ptr:l});if(r){if((r=!Number.isSafeInteger(s))&&!a)throw new p("integer value cannot be represented losslessly",{toml:t,ptr:l});(r||a===!0)&&(s=BigInt(e))}return s}const i=new A(e);if(!i.isValid())throw new p("invalid value",{toml:t,ptr:l});return i}function it(e,t,l){let a=e.slice(t,l),r=a.indexOf("#");return r>-1&&(U(e,r),a=a.slice(0,r)),[a.trimEnd(),r]}function J(e,t,l,a,r){if(a===0)throw new p("document contains excessively nested structures. aborting.",{toml:e,ptr:t});let i=e[t];if(i==="["||i==="{"){let[d,c]=i==="["?ot(e,t,a,r):at(e,t,a,r);if(l){if(c=O(e,c),e[c]===",")c++;else if(e[c]!==l)throw new p("expected comma or end of structure",{toml:e,ptr:c})}return[d,c]}let s;if(i==='"'||i==="'"){s=de(e,t);let d=ue(e,t,s);if(l){if(s=O(e,s),e[s]&&e[s]!==","&&e[s]!==l&&e[s]!==`
`&&e[s]!=="\r")throw new p("unexpected character encountered",{toml:e,ptr:s});s+=+(e[s]===",")}return[d,s]}s=We(e,t,",",l);let o=it(e,t,s-+(e[s-1]===","));if(!o[0])throw new p("incomplete key-value declaration: no value specified",{toml:e,ptr:t});return l&&o[1]>-1&&(s=O(e,t+o[1]),s+=+(e[s]===",")),[rt(o[0],e,t,r),s]}let lt=/^[a-zA-Z0-9-_]+[ \t]*$/;function q(e,t,l="="){let a=t-1,r=[],i=e.indexOf(l,t);if(i<0)throw new p("incomplete key-value: cannot find end of key",{toml:e,ptr:t});do{let s=e[t=++a];if(s!==" "&&s!=="	")if(s==='"'||s==="'"){if(s===e[t+1]&&s===e[t+2])throw new p("multiline strings are not allowed in keys",{toml:e,ptr:t});let o=de(e,t);if(o<0)throw new p("unfinished string encountered",{toml:e,ptr:t});a=e.indexOf(".",o);let d=e.slice(o,a<0||a>i?i:a),c=$(d);if(c>-1)throw new p("newlines are not allowed in keys",{toml:e,ptr:t+a+c});if(d.trimStart())throw new p("found extra tokens after the string part",{toml:e,ptr:o});if(i<o&&(i=e.indexOf(l,o),i<0))throw new p("incomplete key-value: cannot find end of key",{toml:e,ptr:t});r.push(ue(e,t,o))}else{a=e.indexOf(".",t);let o=e.slice(t,a<0||a>i?i:a);if(!lt.test(o))throw new p("only letter, numbers, dashes and underscores are allowed in keys",{toml:e,ptr:t});r.push(o.trimEnd())}}while(a+1&&a<i);return[r,O(e,i+1,!0,!0)]}function at(e,t,l,a){let r={},i=new Set,s;for(t++;(s=e[t++])!=="}"&&s;){if(s===",")throw new p("expected value, found comma",{toml:e,ptr:t-1});if(s==="#")t=U(e,t);else if(s!==" "&&s!=="	"&&s!==`
`&&s!=="\r"){let o,d=r,c=!1,[f,j]=q(e,t-1);for(let x=0;x<f.length;x++){if(x&&(d=c?d[o]:d[o]={}),o=f[x],(c=Object.hasOwn(d,o))&&(typeof d[o]!="object"||i.has(d[o])))throw new p("trying to redefine an already defined value",{toml:e,ptr:t});!c&&o==="__proto__"&&Object.defineProperty(d,o,{enumerable:!0,configurable:!0,writable:!0})}if(c)throw new p("trying to redefine an already defined value",{toml:e,ptr:t});let[h,m]=J(e,j,"}",l-1,a);i.add(h),d[o]=h,t=m}}if(!s)throw new p("unfinished table encountered",{toml:e,ptr:t});return[r,t]}function ot(e,t,l,a){let r=[],i;for(t++;(i=e[t++])!=="]"&&i;){if(i===",")throw new p("expected value, found comma",{toml:e,ptr:t-1});if(i==="#")t=U(e,t);else if(i!==" "&&i!=="	"&&i!==`
`&&i!=="\r"){let s=J(e,t-1,"]",l-1,a);r.push(s[0]),t=s[1]}}if(!i)throw new p("unfinished array encountered",{toml:e,ptr:t});return[r,t]}function re(e,t,l,a){let r=t,i=l,s,o=!1,d;for(let c=0;c<e.length;c++){if(c){if(r=o?r[s]:r[s]={},i=(d=i[s]).c,a===0&&(d.t===1||d.t===2))return null;if(d.t===2){let f=r.length-1;r=r[f],i=i[f].c}}if(s=e[c],(o=Object.hasOwn(r,s))&&i[s]?.t===0&&i[s]?.d)return null;o||(s==="__proto__"&&(Object.defineProperty(r,s,{enumerable:!0,configurable:!0,writable:!0}),Object.defineProperty(i,s,{enumerable:!0,configurable:!0,writable:!0})),i[s]={t:c<e.length-1&&a===2?3:a,d:!1,i:0,c:{}})}if(d=i[s],d.t!==a&&!(a===1&&d.t===3)||(a===2&&(d.d||(d.d=!0,r[s]=[]),r[s].push(r={}),d.c[d.i++]=d={t:1,d:!1,i:0,c:{}}),d.d))return null;if(d.d=!0,a===1)r=o?r[s]:r[s]={};else if(a===0&&o)return null;return[s,r,d.c]}function ct(e,{maxDepth:t=1e3,integersAsBigInt:l}={}){let a={},r={},i=a,s=r;for(let o=O(e,0);o<e.length;){if(e[o]==="["){let d=e[++o]==="[",c=q(e,o+=+d,"]");if(d){if(e[c[1]-1]!=="]")throw new p("expected end of table declaration",{toml:e,ptr:c[1]-1});c[1]++}let f=re(c[0],a,r,d?2:1);if(!f)throw new p("trying to redefine an already defined table or value",{toml:e,ptr:o});s=f[2],i=f[1],o=c[1]}else{let d=q(e,o),c=re(d[0],i,s,0);if(!c)throw new p("trying to redefine an already defined table or value",{toml:e,ptr:o});let f=J(e,d[1],void 0,t,l);c[1][c[0]]=f[0],o=f[1]}if(o=O(e,o,!0),e[o]&&e[o]!==`
`&&e[o]!=="\r")throw new p("each key-value declaration must be followed by an end-of-line",{toml:e,ptr:o});o=O(e,o)}return a}function dt(){return Ce({queryKey:Ee.system.cursorAgentModels,queryFn:()=>ke.get("/system/cursor-agent-models"),staleTime:1e3*60*10})}function ut({value:e,onChange:t}){const{t:l}=_(),{data:a,isLoading:r,isError:i}=dt(),s=a?.models??[],[o,d]=v.useState(!1),[c,f]=v.useState(""),j=v.useMemo(()=>{const h=c.trim().toLowerCase();return h?s.filter(m=>m.id.toLowerCase().includes(h)||m.label.toLowerCase().includes(h)):s},[s,c]);return n.jsxs("div",{className:"flex flex-col gap-2 flex-1 min-w-0",children:[n.jsx("input",{type:"text",value:e,onChange:h=>{const m=h.target.value;t(m===""?void 0:m)},placeholder:l("留空为 auto；或直接输入模型 ID（与 cursor-agent --model 一致）"),className:"w-full px-3 py-1.5 text-sm border border-neutral-200 rounded focus:outline-none focus:ring-1 focus:ring-neutral-900 font-mono"}),n.jsxs("button",{type:"button",onClick:()=>d(h=>!h),disabled:r,className:"flex items-center gap-1 text-xs text-neutral-600 hover:text-neutral-900 disabled:opacity-50 w-fit",children:[n.jsx(ce,{size:14,className:S("transition-transform",o&&"rotate-180")}),r?l("正在加载 cursor-agent 模型列表…"):o?l("收起列表"):l("从本机 cursor-agent 选择（{count} 个）",{count:s.length})]}),o&&!r&&n.jsxs("div",{className:"rounded-lg border border-neutral-200 bg-white shadow-sm overflow-hidden",children:[n.jsx("input",{type:"text",value:c,onChange:h=>f(h.target.value),placeholder:l("筛选模型…"),className:"w-full px-3 py-2 text-sm border-b border-neutral-100 focus:outline-none focus:ring-1 focus:ring-inset focus:ring-neutral-300"}),n.jsxs("div",{className:"max-h-[min(50vh,22rem)] overflow-y-auto py-1",children:[n.jsx("button",{type:"button",onClick:()=>{t(void 0),d(!1),f("")},className:S("w-full px-3 py-2 text-left text-sm hover:bg-neutral-50",e?"text-neutral-700":"bg-neutral-50 font-medium"),children:l("默认 (auto)")}),j.map(h=>n.jsxs("button",{type:"button",onClick:()=>{t(h.id),d(!1),f("")},className:S("w-full px-3 py-2 text-left text-sm hover:bg-neutral-50 border-t border-neutral-100",e===h.id?"bg-blue-50/80":""),children:[n.jsx("div",{className:"font-mono text-xs text-neutral-900 break-all",children:h.id}),n.jsx("div",{className:"text-xs text-neutral-500 mt-0.5 break-words",children:h.label})]},h.id)),j.length===0&&n.jsx("div",{className:"px-3 py-4 text-xs text-neutral-400 text-center",children:l("无匹配项")})]})]}),(i||a?.error)&&n.jsxs("p",{className:"text-xs text-amber-700",children:[l("无法从本机加载模型列表（{error}）。仍可手动输入；或在安装 cursor-agent 的机器上运行",{error:a?.error??l("请求失败")}),n.jsx("code",{className:"mx-0.5 bg-neutral-100 px-1 rounded text-[11px]",children:"cursor-agent --list-models"}),l("查看 ID。")]})]})}const z={CLAUDE_CODE:"Claude Code",GEMINI_CLI:"Gemini CLI",CURSOR_AGENT:"Cursor Agent",CODEX:"Codex"},F={key:"appendPrompt",label:"追加 Prompt",type:"textarea",rows:3,placeholder:"追加到每次 prompt 末尾的文本"},Y={[w.CLAUDE_CODE]:[{key:"dangerouslySkipPermissions",label:"跳过权限确认",type:"switch"},{key:"model",label:"模型",type:"input",placeholder:"claude-sonnet-4-20250514"},{key:"effort",label:"推理强度",type:"select",options:[{value:"",label:"默认"},{value:"low",label:"Low"},{value:"medium",label:"Medium"},{value:"high",label:"High"},{value:"xhigh",label:"XHigh"},{value:"max",label:"Max"}]},F],[w.GEMINI_CLI]:[{key:"yolo",label:"跳过权限确认",type:"switch"},{key:"model",label:"模型",type:"input",placeholder:"gemini-2.5-pro"},F],[w.CURSOR_AGENT]:[{key:"force",label:"强制执行",type:"switch"},{key:"model",label:"模型",type:"cursor_model"},F],[w.CODEX]:[{key:"dangerouslyBypassApprovalsAndSandbox",label:"跳过所有确认和沙盒",type:"switch"},{key:"model",label:"模型",type:"input",placeholder:"o3"},{key:"profile",label:"Profile",type:"input",placeholder:"~/.codex/config.toml 中的 profile 名称"},F]};function ie(){return{}}function W(e,t){return{...t}}const mt=JSON.stringify({env:{ANTHROPIC_API_KEY:"",ANTHROPIC_BASE_URL:""}},null,2),xt=`# Codex config.toml 配置片段 — 通过 -c 参数注入，不会修改 ~/.codex/config.toml
# 参考: https://developers.openai.com/codex/config-sample

# ─── 模型与推理 ─────────────────────────────────────────────
# model_reasoning_effort = "medium"     # minimal | low | medium | high | xhigh
# model_reasoning_summary = "auto"      # auto | concise | detailed | none
# model_verbosity = "medium"            # low | medium | high
# service_tier = "flex"                 # fast | flex

# ─── 自定义 Model Provider ──────────────────────────────────
# model_provider = "azure"
#
# [model_providers.azure]
# name = "Azure OpenAI"
# base_url = "https://YOUR_PROJECT.openai.azure.com/openai"
# env_key = "AZURE_OPENAI_API_KEY"
# env_key_instructions = "Set AZURE_OPENAI_API_KEY in Provider env"
# wire_api = "responses"
# query_params = { api-version = "2025-04-01-preview" }

# ─── OpenAI 数据驻留 ────────────────────────────────────────
# [model_providers.openai-us]
# name = "OpenAI US"
# base_url = "https://us.api.openai.com/v1"
# wire_api = "responses"
# requires_openai_auth = true

# ─── 本地 OSS (Ollama) ──────────────────────────────────────
# [model_providers.ollama]
# name = "Ollama"
# base_url = "http://localhost:11434/v1"
# wire_api = "responses"
`;function X(e){return e===w.CLAUDE_CODE?mt:e===w.CODEX?xt:""}function ft(e){return e===w.CLAUDE_CODE||e===w.CODEX}const pt=Object.values(Y).flat().reduce((e,t)=>(e[t.key]||(e[t.key]=t.label),e),{}),ht=Object.values(Y).flat().reduce((e,t)=>(t.options&&(e[t.key]=Object.fromEntries(t.options.map(l=>[l.value,l.label]))),e),{});function gt(e,t){return typeof t=="boolean"?t?ne("是"):ne("否"):typeof t=="string"&&t?ht[e]?.[t]??t:String(t)}function T(e,t){return e instanceof Error?e.message:t}function bt(e){return`agent-tower-provider-backup-${e.replace(/[:.]/g,"-")}.json`}function vt(e,t){const l=new Blob([JSON.stringify(t,null,2)],{type:"application/json"}),a=URL.createObjectURL(l),r=document.createElement("a");r.href=a,r.download=e,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(a)}function yt(e){switch(e){case"CREATE":return{label:"新增",className:"bg-success/10 text-success"};case"OVERWRITE":return{label:"覆盖",className:"bg-warning/10 text-warning"};case"SKIP":return{label:"跳过",className:"bg-muted text-muted-foreground"}}}function jt({type:e}){const t=e==="LOGIN_DETECTED"||e==="INSTALLATION_FOUND";return n.jsx("span",{className:S("inline-block h-2 w-2 rounded-full shrink-0",t?"bg-success":"bg-border"),title:t?"Available":"Unavailable",children:n.jsx("span",{className:"sr-only",children:t?"Available":"Unavailable"})})}function wt({type:e}){const{t}=_();return e==="LOGIN_DETECTED"||e==="INSTALLATION_FOUND"?n.jsxs("span",{className:"inline-flex items-center gap-1 rounded-full bg-success/10 px-2 py-0.5 text-[11px] font-medium text-success",children:[n.jsx(ze,{size:11,"aria-hidden":"true"}),t("可用")]}):n.jsxs("span",{className:"inline-flex items-center gap-1 rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium text-muted-foreground",children:[n.jsx(Xe,{size:11,"aria-hidden":"true"}),t("不可用")]})}function V({title:e,defaultOpen:t=!1,children:l}){const[a,r]=v.useState(t);return n.jsxs("div",{className:"rounded-lg border border-border",children:[n.jsxs("button",{type:"button",onClick:()=>r(i=>!i),"aria-expanded":a,className:"w-full flex items-center justify-between rounded-lg px-4 py-2.5 text-sm font-medium text-foreground transition-colors hover:bg-muted/50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60 focus-visible:ring-inset",children:[e,n.jsx(ce,{size:14,"aria-hidden":"true",className:S("text-muted-foreground transition-transform motion-reduce:transition-none",a&&"rotate-180")})]}),a&&n.jsx("div",{className:"px-4 pb-4 space-y-3",children:l})]})}function Nt({agentType:e,config:t,onChange:l}){const{t:a}=_(),r=Y[e]??[];if(r.length===0)return n.jsx("p",{className:"text-xs text-muted-foreground",children:a("该类型暂无运行配置")});const i=(s,o)=>{l({...t,[s]:o})};return n.jsx("div",{className:"space-y-3",children:r.map(s=>s.type==="textarea"?n.jsxs("div",{children:[n.jsx("label",{className:"mb-1 block text-sm text-foreground",children:a(s.label)}),n.jsx(oe,{value:t[s.key]??"",onChange:o=>i(s.key,o.target.value||void 0),placeholder:s.placeholder?a(s.placeholder):void 0,rows:s.rows??3,className:"py-1.5"})]},s.key):s.type==="cursor_model"?n.jsxs("div",{className:"flex flex-col gap-2 sm:flex-row sm:items-start",children:[n.jsx("label",{className:"w-32 shrink-0 text-sm text-foreground sm:pt-2",children:a(s.label)}),n.jsx(ut,{value:t[s.key]??"",onChange:o=>i(s.key,o)})]},s.key):n.jsxs("div",{className:"flex items-center gap-3",children:[n.jsx("label",{className:"w-32 shrink-0 text-sm text-foreground",children:a(s.label)}),s.type==="switch"&&n.jsx(Be,{checked:!!t[s.key],onCheckedChange:o=>i(s.key,o),"aria-label":a(s.label)}),s.type==="input"&&n.jsx(M,{value:t[s.key]??"",onChange:o=>i(s.key,o.target.value||void 0),placeholder:s.placeholder?a(s.placeholder):void 0,className:"flex-1 py-1.5"}),s.type==="select"&&s.options&&n.jsx(ae,{value:t[s.key]??"",onChange:o=>i(s.key,o||void 0),options:s.options,placeholder:a("选择...")})]},s.key))})}function Ct({isOpen:e,onClose:t,initialData:l,onSave:a}){const{t:r}=_(),[i,s]=v.useState(l??{name:"",agentType:w.CLAUDE_CODE,config:ie(),settings:"",env:[],isDefault:!1}),[o,d]=v.useState("");v.useEffect(()=>{if(!l&&!i.settings){const m=X(i.agentType);m&&s(x=>({...x,settings:m}))}},[]);const c=m=>{s(x=>({...x,agentType:m,config:ie(),settings:X(m)}))},f=()=>{d("");const m=W(i.agentType,i.config),x={};for(const[I,k]of Object.entries(m))k!==void 0&&k!==""&&(x[I]=k);const y=i.settings.trim();if(y){if(i.agentType===w.CODEX)try{ct(y)}catch(I){d(r("TOML 语法错误: {message}",{message:I instanceof Error?I.message:String(I)}));return}else if(i.agentType===w.CLAUDE_CODE)try{JSON.parse(y)}catch{d(r("JSON 语法错误"));return}}const b={};for(const{key:I,value:k}of i.env){const D=I.trim();D&&(b[D]=k)}const E=!!l,B={name:i.name,agentType:i.agentType,config:x,settings:E?y||"":y||void 0,env:E||Object.keys(b).length>0?b:void 0,isDefault:i.isDefault};a(B)};if(!e)return null;const j=ft(i.agentType),h=i.agentType===w.CODEX;return n.jsx(H,{isOpen:e,onClose:t,title:r(l?"编辑 Provider":"新建 Provider"),className:"max-w-2xl",children:n.jsxs("div",{className:"space-y-4",children:[n.jsxs("div",{className:"grid grid-cols-1 gap-4 sm:grid-cols-2",children:[n.jsxs("div",{children:[n.jsx("label",{htmlFor:"provider-name",className:"mb-1 block text-xs font-medium text-foreground",children:r("名称")}),n.jsx(M,{id:"provider-name",value:i.name,onChange:m=>s(x=>({...x,name:m.target.value})),placeholder:r("例如: Claude Code (官方)")})]}),!l&&n.jsxs("div",{children:[n.jsx("label",{className:"mb-1 block text-xs font-medium text-foreground",children:r("Agent 类型")}),n.jsx(ae,{value:i.agentType,onChange:m=>c(m),options:Object.values(w).map(m=>({value:m,label:z[m]??m})),placeholder:r("选择 Agent 类型")})]})]}),n.jsxs("div",{className:"flex items-center gap-2",children:[n.jsx("input",{type:"checkbox",id:"isDefault",checked:i.isDefault,onChange:m=>s(x=>({...x,isDefault:m.target.checked})),className:"h-4 w-4 rounded border-border accent-primary"}),n.jsx("label",{htmlFor:"isDefault",className:"text-sm text-foreground",children:r("设为该类型的默认 Provider")})]}),n.jsx(V,{title:r("运行配置"),defaultOpen:!0,children:n.jsx(Nt,{agentType:i.agentType,config:i.config,onChange:m=>s(x=>({...x,config:m}))})}),n.jsxs(V,{title:r("环境变量"),defaultOpen:i.env.length>0,children:[n.jsxs("p",{className:"text-xs text-muted-foreground mb-2",children:[r("注入到 Agent 进程的环境变量。Codex 的")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"env_key"})," ",r("指定的是变量名，实际值需在此处设置。")]}),n.jsxs("div",{className:"space-y-2",children:[i.env.map((m,x)=>n.jsxs("div",{className:"flex gap-2 items-center",children:[n.jsx(M,{value:m.key,onChange:y=>{const b=[...i.env];b[x]={...b[x],key:y.target.value},s(E=>({...E,env:b}))},placeholder:r("变量名，如 AZURE_OPENAI_API_KEY"),className:"flex-1 py-1.5 font-mono"}),n.jsx(M,{value:m.value,onChange:y=>{const b=[...i.env];b[x]={...b[x],value:y.target.value},s(E=>({...E,env:b}))},placeholder:r("值"),className:"flex-1 py-1.5 font-mono"}),n.jsx("button",{type:"button",onClick:()=>{const y=i.env.filter((b,E)=>E!==x);s(b=>({...b,env:y}))},"aria-label":r("删除"),className:"rounded-md p-1.5 text-muted-foreground transition-colors hover:text-destructive focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60",children:n.jsx(le,{size:14})})]},x)),n.jsxs(N,{variant:"outline",size:"sm",onClick:()=>s(m=>({...m,env:[...m.env,{key:"",value:""}]})),children:[n.jsx(Z,{size:12,className:"mr-1"}),r("添加变量")]})]})]}),j&&n.jsxs(V,{title:r(h?"CLI 原生配置 (config.toml)":"CLI 原生配置 (settings.json)"),children:[n.jsx("p",{className:"text-xs text-muted-foreground mb-2",children:h?n.jsxs(n.Fragment,{children:[r("直接填写 Codex")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"config.toml"})," ",r("格式的配置片段，通过")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"-c"})," ",r("参数注入。不会修改你的")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"~/.codex/config.toml"})," ",r("文件。")]}):n.jsxs(n.Fragment,{children:[r("对应 Claude Code 的")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"~/.claude/settings.json"}),"，",r("通过")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"--settings"})," ",r("参数注入。在")," ",n.jsx("code",{className:"rounded bg-muted px-1",children:"env"})," ",r("中设置 ANTHROPIC_API_KEY、ANTHROPIC_BASE_URL 等。")]})}),n.jsx(oe,{value:i.settings,onChange:m=>{s(x=>({...x,settings:m.target.value})),d("")},rows:10,className:"font-mono",placeholder:X(i.agentType),"aria-invalid":!!o}),o&&n.jsx("p",{role:"alert",className:"mt-1 text-xs text-destructive",children:o})]}),n.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[n.jsx(N,{variant:"outline",onClick:t,children:r("取消")}),n.jsx(N,{onClick:f,disabled:!i.name.trim(),children:r("保存")})]})]})})}function Et({isOpen:e,onClose:t,onConfirm:l,acknowledged:a,onAcknowledgedChange:r,isLoading:i}){const{t:s}=_();return n.jsx(H,{isOpen:e,onClose:t,title:s("导出 Provider 备份"),className:"max-w-xl",action:n.jsxs(n.Fragment,{children:[n.jsx(N,{variant:"outline",onClick:t,disabled:i,children:s("取消")}),n.jsx(N,{onClick:l,disabled:!a||i,children:s(i?"导出中...":"导出备份")})]}),children:n.jsxs("div",{className:"space-y-4",children:[n.jsxs("div",{className:"flex items-start gap-2.5 rounded-lg border border-warning/30 bg-warning/10 px-4 py-3 text-sm text-foreground",children:[n.jsx(Ue,{size:15,className:"mt-0.5 shrink-0 text-warning","aria-hidden":"true"}),n.jsx("span",{children:s("导出的备份文件将包含完整的 Provider 配置，包括环境变量、CLI settings 等敏感信息。任何拿到文件的人都可能直接使用这些 Provider。")})]}),n.jsxs("div",{className:"space-y-2 text-sm text-muted-foreground",children:[n.jsx("p",{children:s("这个功能用于备份和迁移，不用于分享配置。")}),n.jsx("p",{children:s("导出内容只包含用户层配置：自定义 Provider，以及对内置 Provider 的覆盖。")})]}),n.jsxs("label",{className:"flex items-start gap-3 rounded-lg border border-border px-4 py-3 text-sm text-foreground",children:[n.jsx("input",{type:"checkbox",checked:a,onChange:o=>r(o.target.checked),className:"mt-0.5 h-4 w-4 rounded border-border accent-primary"}),n.jsx("span",{children:s("我已知晓该备份文件包含敏感信息，只会保存在安全位置。")})]})]})})}function kt({isOpen:e,onClose:t,preview:l,backup:a,onConfirm:r,isLoading:i}){const{t:s}=_();if(!l||!a)return null;const o=l.summary.create+l.summary.overwrite,d=[...l.items].sort((c,f)=>{const j={CREATE:0,OVERWRITE:1,SKIP:2};return j[c.action]-j[f.action]});return n.jsx(H,{isOpen:e,onClose:t,title:s("导入 Provider 备份"),className:"max-w-3xl",action:n.jsxs(n.Fragment,{children:[n.jsx(N,{variant:"outline",onClick:t,disabled:i,children:s("取消")}),n.jsx(N,{onClick:r,disabled:o===0||i,children:i?s("导入中..."):s("确认导入 {count} 项",{count:o})})]}),children:n.jsxs("div",{className:"space-y-4",children:[n.jsxs("div",{className:"rounded-lg border border-border bg-muted/40 px-4 py-3 text-sm text-muted-foreground",children:[n.jsx("div",{children:s("导出时间：{value}",{value:new Date(a.exportedAt).toLocaleString()})}),n.jsx("div",{children:s("模式：完整备份（含敏感信息）")}),n.jsx("div",{children:s("文件内 Provider 数量：{count}",{count:a.providers.length})})]}),n.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[n.jsxs("div",{className:"rounded-lg border border-success/25 bg-success/10 px-4 py-3",children:[n.jsx("div",{className:"text-xs font-medium text-success",children:s("新增")}),n.jsx("div",{className:"text-lg font-semibold text-foreground",children:l.summary.create})]}),n.jsxs("div",{className:"rounded-lg border border-warning/25 bg-warning/10 px-4 py-3",children:[n.jsx("div",{className:"text-xs font-medium text-warning",children:s("覆盖")}),n.jsx("div",{className:"text-lg font-semibold text-foreground",children:l.summary.overwrite})]}),n.jsxs("div",{className:"rounded-lg border border-border bg-muted/40 px-4 py-3",children:[n.jsx("div",{className:"text-xs font-medium text-muted-foreground",children:s("跳过")}),n.jsx("div",{className:"text-lg font-semibold text-foreground",children:l.summary.skip})]})]}),n.jsx("div",{className:"max-h-[420px] overflow-y-auto space-y-3 pr-1",children:d.map(c=>{const f=yt(c.action);return n.jsxs("div",{className:"rounded-lg border border-border px-4 py-3",children:[n.jsx("div",{className:"flex items-center justify-between gap-3",children:n.jsxs("div",{className:"min-w-0",children:[n.jsxs("div",{className:"flex items-center gap-2",children:[n.jsx("h4",{className:"truncate text-sm font-medium text-foreground",children:c.incoming.name}),n.jsx("span",{className:`inline-flex rounded-full px-2 py-0.5 text-[11px] font-medium ${f.className}`,children:s(f.label)})]}),n.jsxs("div",{className:"mt-1 text-xs text-muted-foreground",children:[z[c.incoming.agentType]??c.incoming.agentType," · ",n.jsx("code",{className:"rounded bg-muted px-1 py-0.5",children:c.incoming.id})]})]})}),c.action==="OVERWRITE"&&c.existing&&n.jsx("p",{className:"mt-2 text-xs text-muted-foreground",children:s("将覆盖当前已有的 Provider：{name}",{name:c.existing.name})}),c.action==="SKIP"&&n.jsx("p",{className:"mt-2 text-xs text-muted-foreground",children:s("当前同 ID Provider 配置一致，本次不会重复写入。")}),c.action==="CREATE"&&n.jsx("p",{className:"mt-2 text-xs text-muted-foreground",children:s("当前不存在同 ID Provider，将直接新增。")})]},c.incoming.id)})})]})})}function Pt({item:e,onEdit:t,onDelete:l}){const{t:a}=_(),r=e.provider,i=e.availability,s=W(r.agentType,r.config),o=Object.entries(s).filter(([c])=>c!=="cmd"),d=Object.keys(r.env||{});return n.jsxs("div",{className:"space-y-6",children:[n.jsxs("div",{className:"flex items-start justify-between gap-4",children:[n.jsxs("div",{children:[n.jsxs("div",{className:"flex items-center gap-2.5",children:[n.jsx("h3",{className:"text-base font-semibold text-foreground",children:r.name}),n.jsx(wt,{type:i.type})]}),n.jsxs("div",{className:"mt-1 flex flex-wrap items-center gap-2 text-xs text-muted-foreground",children:[n.jsx("span",{children:z[r.agentType]??r.agentType}),r.isDefault&&n.jsx("span",{className:"rounded-full bg-primary/[0.06] px-2 py-0.5 text-[11px] font-medium text-primary",children:a("默认")}),r.builtIn&&n.jsx("span",{className:"rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium text-muted-foreground",children:a("内置")})]})]}),n.jsxs("div",{className:"flex items-center gap-1.5",children:[n.jsxs(N,{size:"sm",variant:"outline",onClick:t,children:[n.jsx(Ve,{size:13}),a("编辑")]}),n.jsxs(N,{size:"sm",variant:r.builtIn?"outline":"destructive",onClick:l,disabled:r.deletable===!1,children:[r.builtIn?n.jsx(Ze,{size:13}):n.jsx(le,{size:13}),r.builtIn?a("恢复默认"):a("删除")]})]})]}),o.length>0&&n.jsxs("div",{children:[n.jsx(K,{className:"mb-3",children:a("运行配置")}),n.jsx("div",{className:"grid gap-px overflow-hidden rounded-lg border border-border bg-border/60",children:o.map(([c,f])=>n.jsxs("div",{className:"flex items-center gap-4 bg-background px-4 py-2.5",children:[n.jsx("span",{className:"w-32 shrink-0 text-[13px] text-muted-foreground",children:a(pt[c]??c)}),n.jsx("span",{className:"font-mono text-[13px] text-foreground",children:gt(c,f)})]},c))})]}),d.length>0&&n.jsxs("div",{children:[n.jsx(K,{className:"mb-3",children:a("环境变量")}),n.jsx("div",{className:"grid gap-px overflow-hidden rounded-lg border border-border bg-border/60",children:d.map(c=>n.jsxs("div",{className:"flex items-center gap-4 bg-background px-4 py-2.5",children:[n.jsx("span",{className:"font-mono text-[13px] text-foreground",children:c}),n.jsx("span",{className:"text-[13px] text-muted-foreground","aria-label":"hidden",children:"•••••"})]},c))})]}),r.settings?.trim()&&n.jsxs("div",{children:[n.jsx(K,{className:"mb-3",children:a("CLI 配置")}),n.jsx("div",{className:"rounded-lg border border-border bg-muted/40 p-4",children:n.jsx("pre",{className:"max-h-[200px] overflow-y-auto whitespace-pre-wrap break-all font-mono text-xs text-muted-foreground",children:r.settings})})]}),o.length===0&&d.length===0&&!r.settings?.trim()&&n.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:a('该 Provider 未配置额外参数。点击"编辑"添加运行配置、环境变量或 CLI 配置。')})]})}function Xt(){const{t:e}=_(),{data:t,isLoading:l}=Se(),a=Te(),r=Ae(),i=De(),s=Le(),o=Re(),d=Fe(),c=v.useRef(null),[f,j]=v.useState(null),[h,m]=v.useState(null),[x,y]=v.useState(null),[b,E]=v.useState(!1),[B,I]=v.useState(!1),[k,D]=v.useState(null),[me,G]=v.useState(!1),L=t??[];v.useEffect(()=>{!f&&L.length>0&&j(L[0].provider.id)},[L,f]);const xe=u=>{a.mutate(u,{onSuccess:()=>m(null),onError:g=>P.error(T(g,e("创建 Provider 失败")))})},fe=(u,g)=>{r.mutate({id:u,data:g},{onSuccess:()=>m(null),onError:C=>P.error(T(C,e("更新 Provider 失败")))})},pe=u=>{if(!(u.deletable??!u.builtIn)){P.error(e("系统内置 Provider 不可删除"));return}y({id:u.id,name:u.name,builtIn:u.builtIn})},he=()=>{x&&i.mutate(x.id,{onSuccess:()=>{P.success(x.builtIn?e("已恢复默认 Provider 配置"):e("Provider 已删除")),f===x.id&&j(null),y(null),G(!1)},onError:u=>P.error(T(u,e("删除 Provider 失败")))})},Q=()=>{E(!1),I(!1)},ee=()=>D(null),ge=()=>{s.mutate(void 0,{onSuccess:u=>{vt(bt(u.exportedAt),u),P.success(e("Provider 备份已导出")),Q()},onError:u=>P.error(T(u,e("导出 Provider 备份失败")))})},be=()=>{c.current&&(c.current.value="",c.current.click())},ve=async u=>{const g=u.target.files?.[0];if(u.target.value="",!g)return;let C;try{C=JSON.parse(await g.text())}catch{P.error(e("备份文件不是有效的 JSON"));return}o.mutate(C,{onSuccess:R=>D({backup:C,preview:R}),onError:R=>P.error(T(R,e("导入预览失败")))})},ye=()=>{k&&d.mutate(k.backup,{onSuccess:u=>{const g=u.summary.create+u.summary.overwrite;P.success(g===0?e("导入完成，当前配置无需变更"):e("导入完成：新增 {create}，覆盖 {overwrite}，跳过 {skip}",{create:u.summary.create,overwrite:u.summary.overwrite,skip:u.summary.skip})),ee()},onError:u=>P.error(T(u,e("导入 Provider 备份失败")))})},je=u=>{const g=u.provider,C=g.env?Object.entries(g.env).map(([R,we])=>({key:R,value:we})):[];m({id:g.id,data:{name:g.name,agentType:g.agentType,config:W(g.agentType,g.config),settings:g.settings??"",env:C,isDefault:g.isDefault}})};return l?n.jsx(te,{className:"max-w-5xl",children:n.jsx(Pe,{})}):n.jsxs(te,{className:"max-w-5xl",children:[n.jsx("input",{ref:c,type:"file",accept:".json,application/json",className:"hidden",onChange:ve}),n.jsx(Ie,{title:e("Agent 配置"),actions:n.jsxs(n.Fragment,{children:[n.jsxs(N,{variant:"outline",size:"sm",onClick:be,disabled:o.isPending||d.isPending,children:[n.jsx(Ke,{size:13}),e("导入")]}),n.jsxs(N,{variant:"outline",size:"sm",onClick:()=>E(!0),disabled:s.isPending,children:[n.jsx(qe,{size:13}),e("导出")]}),n.jsxs(N,{size:"sm",onClick:()=>m({}),children:[n.jsx(Z,{size:13,className:"mr-1"}),e("新建")]})]})}),L.length===0?n.jsx(Oe,{icon:_e,message:e("暂无 Agent 配置"),action:n.jsxs(N,{size:"sm",onClick:()=>m({}),children:[n.jsx(Z,{size:13,className:"mr-1"}),e("新建配置")]})}):n.jsx(Ge,{items:L,selectedId:f,onSelectItem:u=>{j(u),G(!0)},getItemId:u=>u.provider.id,mobileShowDetail:me,onMobileBack:()=>G(!1),renderListItem:(u,g)=>{const C=u.provider;return n.jsxs(n.Fragment,{children:[n.jsx(jt,{type:u.availability.type}),n.jsxs("div",{className:"min-w-0 flex-1",children:[n.jsx("div",{className:"truncate text-[13px] font-medium",children:C.name}),n.jsx("div",{className:S("truncate text-[11px]",g?"text-primary-foreground/60":"text-muted-foreground"),children:z[C.agentType]??C.agentType})]}),C.isDefault&&n.jsx("span",{className:S("shrink-0 rounded px-1.5 py-0.5 text-[10px] font-medium",g?"bg-white/20 text-primary-foreground":"bg-primary/[0.06] text-primary"),children:e("默认")})]})},renderDetail:u=>u?n.jsx("div",{className:"p-5",children:n.jsx(Pt,{item:u,onEdit:()=>je(u),onDelete:()=>pe(u.provider)})}):n.jsx("div",{className:"py-16 text-center text-sm text-muted-foreground",children:e("选择一个 Provider 查看详情")})}),h&&n.jsx(Ct,{isOpen:!0,onClose:()=>m(null),initialData:h.data,onSave:u=>{h.id?fe(h.id,u):xe(u)}}),n.jsx(Me,{isOpen:x!==null,onClose:()=>{i.isPending||y(null)},onConfirm:he,title:x?.builtIn?e("恢复默认 Provider"):e("删除 Provider"),description:x?.builtIn?e('确定删除 "{name}" 的自定义覆盖，并恢复系统默认配置？',{name:x?.name}):e('确定删除 "{name}"？此操作不可撤销。',{name:x?.name}),confirmText:x?.builtIn?e("恢复默认"):e("删除"),cancelText:e("取消"),variant:"danger",isLoading:i.isPending}),n.jsx(Et,{isOpen:b,onClose:Q,onConfirm:ge,acknowledged:B,onAcknowledgedChange:I,isLoading:s.isPending}),n.jsx(kt,{isOpen:!!k,onClose:ee,preview:k?.preview??null,backup:k?.backup??null,onConfirm:ye,isLoading:d.isPending})]})}export{Xt as ProviderSettingsPage};
