openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 22.8 kB
JavaScript
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{Ot as t,Qn as n,Qt as r,Xt as i,Zt as a,ac as o,au as s,dc as c,el as l,et as u,fc as d,jt as f,rc as p,tt as m,wa as h,xa as g}from"./control-ui-connection-Bfj9beF_.js";import{A as _,CO as v,D as y,Ev as b,FM as x,Fk as S,Hk as C,Jk as ee,Kk as w,Lk as te,M as T,N as E,NM as D,Nk as O,O as k,Pk as A,R as j,Tv as M,Uk as N,Vk as P,Wk as ne,Yk as re,bD as F,j as I,k as L,vD as ie,wO as ae,z as R}from"./control-ui-boot-shared-DFTSUQQ0.js";import{_ as oe,a as z,c as B,d as V,f as se,g as ce,h as H,i as U,l as W,m as G,n as K,o as q,p as J,r as Y,t as X,u as le,v as ue}from"./config-draft-model-CMYQuqkj.js";async function de(e,t,n,r){let i=e.configFormMode===`form`&&e.configFormDirty?z(e.configSnapshot):null,a=e.client,o=S(e);await t()&&a&&N(e,a,o)&&i!==null&&z(e.configSnapshot)===i&&(e.configDraftBaseHash=e.configSnapshot?.hash??e.configDraftBaseHash,n()),r()}async function fe(e,t,n,r,a,o,s){if(!N(e,t,n))return{ok:!1,reason:`unavailable`,error:`Connection changed before the configuration update started.`};if(a.canDispatch&&!a.canDispatch())return{ok:!1,reason:`unavailable`,error:a.dispatchError??`Access changed before the configuration update started.`};let c={operation:`independent`,raw:e.configFormDirty?e.configRawOriginal:H(e),form:e.configFormOriginal,independentSnapshot:e.configSnapshot},l;try{l=await r(t)}catch(r){if(!N(e,t,n))return{ok:!1,reason:`unavailable`,error:`Connection changed before the configuration update completed.`};let i=I(e,r);return{ok:!1,reason:i.status===`conflict`?`conflict`:E(r)?`rejected`:`error`,error:i.message}}let u=e=>({ok:!0,value:l,refresh:{ok:!1,error:e}});try{let r=a.configWriteAck?.(l);if(r&&r.noop!==!0&&(s({...c,ack:r}),N(e,t,n)&&X(e,c,r)),!N(e,t,n))return u(`Connection changed before the configuration update was refreshed.`);if(r?.noop!==!0&&a.shouldRefresh&&!a.shouldRefresh(l))return{ok:!0,value:l,refresh:{ok:!0}};let i=await o();if(!N(e,t,n))return u(`Connection changed before the configuration update was refreshed.`);if(!i.ok)return u(i.error);if(r?.noop===!0||!r&&e.configFormMode===`form`&&e.configFormDirty){let t=e.configSnapshot,n=w(t);if(!n||!t?.hash)throw Error(`Config hash missing; refresh and retry.`);X(e,{raw:e.configRawOriginal,form:e.configFormOriginal,independentSnapshot:t},{config:n,hash:t.hash},{raw:t.raw})}return{ok:!0,value:l,refresh:{ok:!0}}}catch(e){return u(i(e))}}function Z(e,t={},n=()=>!0){return L(e,{...t,snapshotOwner:t.draftWrites??{hasUnacknowledgedDraftWrite:()=>!1,applySnapshot:(t,n)=>K(e,t,n)}},n)}function pe(e,t){return _(e,{...t,snapshotOwner:t.draftWrites??{hasUnacknowledgedDraftWrite:()=>!1,applySnapshot:(t,n)=>K(e,t,n)}})}async function Q(e){let n=e.client;if(!n||!e.connected||e.configSchemaLoading||!t(n.auth))return o(void 0);let r=S(e);e.configSchemaLoading=!0;try{let t=await n.request(`config.schema`,{});return N(e,n,r)?(e.configSchema=t.schema??null,e.configUiHints=t.uiHints??{},e.configSchemaVersion=t.version??null,o(void 0)):p(`The schema refresh was superseded.`)}catch(t){let a=i(t);return N(e,n,r)&&(e.lastError=a,e.configAutoSaveStatus===`rejected`&&(e.configAutoSaveStatus=`error`)),p(a)}finally{N(e,n,r)&&(e.configSchemaLoading=!1)}}async function me(e,t,n,r){let i=e.client,a=()=>t!==`auto`||e.configFormDirty&&e.configFormMode===`form`;if(!i||!e.connected||!a()||e.configRecoveryError!==null)return!1;let o=S(e),s=()=>N(e,i,o),c=t===`auto`?null:t===`apply`?`configApplying`:`configSaving`;c&&(e[c]=!0,e.lastError=null,e.chatError=null);let l=null,u=null;try{if(e.configRawOriginalParsePending&&await e.configRawOriginalParsePending,!s()||!a())return!1;Y(e);let o=H(e),c={operation:t===`apply`?`apply`:`save`,raw:o,form:q(e),independentSnapshot:null};l=e.configFormMode===`form`?o:null;let d=e.configDraftBaseHash??e.configSnapshot?.hash;if(!d)return e.lastError=`Config hash missing; reload and retry.`,e.configAutoSaveStatus=`error`,!1;if(!r())return!1;t===`auto`&&(e.configAutoSaveStatus=`saving`,e.lastError=null,e.chatError=null),u={...c,ack:null},n?.(u);let f=await i.request(t===`apply`?`config.apply`:`config.set`,{raw:o,baseHash:d,...t===`apply`?{sessionKey:e.applySessionKey}:{}});return n?.({...c,ack:f}),!s()||(X(e,c,f),e.configNeedsApply=t!==`apply`,t!==`auto`&&(await Z(e),!s()))?!1:(t!==`apply`&&e.configAutoSaveStatus!==`conflict`&&(e.configAutoSaveStatus=e.configFormDirty?`idle`:`saved`),!0)}catch(t){if(s()){let r=I(e,t,l);u&&e.configRecoveryError===null&&E(t)&&n?.({...u,rejected:!0}),e.lastError=r.message,e.configAutoSaveStatus=r.status}return!1}finally{c&&s()&&(e[c]=!1)}}function he(e,t,n,r,i){if(X(e,n,r),!i()||!e.configFormDirty)return;try{Y(e)}catch(t){let n=I(e,t);e.lastError=n.message,e.configAutoSaveStatus=n.status;return}let a={raw:H(e),form:q(e),independentSnapshot:null};t.request(`config.set`,{raw:a.raw,baseHash:r.hash}).then(t=>X(e,a,t)).catch(()=>void 0)}async function ge(e,t,n){let r=e.client,i=e.configSnapshot;if(!r||!e.connected||!i)return!1;let a=S(e),o=i.hash,c=w(i);if(!o||!c)return e.lastError=`Config hash missing; refresh and retry.`,e.configAutoSaveStatus=`conflict`,!1;if(t.canDispatch&&!t.canDispatch())return!1;let l=e.configFormDirty?e.configAutoSaveStatus:`idle`,f={operation:`independent`,raw:e.configRawOriginal,form:e.configFormOriginal,independentSnapshot:i};e.configAutoSaveStatus=`saving`,e.lastError=null,e.chatError=null;try{let s=await r.request(`config.patch`,{baseHash:o,raw:typeof t.raw==`string`?t.raw:JSON.stringify(t.raw),sessionKey:e.applySessionKey,note:t.note,...t.replacePaths?.length?{replacePaths:t.replacePaths}:{}}),u={config:s.noop===!0?c:s.config,hash:s.noop===!0?o:s.hash};if(n({...f,ack:u}),!N(e,r,a))return!1;let d=X(e,f,u,{raw:s.noop===!0?i.raw:void 0});return s.noop!==!0&&(e.configNeedsApply=!0),e.configAutoSaveStatus=d===`conflict`?`conflict`:e.configFormDirty&&l===`paused`?`paused`:e.configFormDirty?`idle`:`saved`,!0}catch(t){if(N(e,r,a)){if(t instanceof u&&d(t)&&t.gatewayCode===D.UNAVAILABLE&&s(t.details)&&t.details.publication!==`partial`&&t.details.publication!==`complete`&&le(t.details.persistedConfig)){n({...f,ack:t.details.persistedConfig});let r=X(e,f,t.details.persistedConfig);if(e.configNeedsApply=!0,r===`conflict`)return!1}let r=I(e,t);e.lastError=r.message,e.configAutoSaveStatus=r.status}return!1}}async function _e(e,t){let n=e.client;if(!n||!e.connected)return null;let r=S(e);try{let i=await n.request(`config.schema.lookup`,{path:t});return N(e,n,r)?i:null}catch(t){if(!N(e,n,r))return null;throw t}}async function ve(e){let t=e.client;if(!t||!e.connected)return;let n=S(e),r=()=>N(e,t,n);e.configAutoSaveStatus!==`rejected`&&(e.lastError=null),e.chatError=null;let o=async(t,n)=>{if(!r())return;let i=t;n&&(i+=await M(n)?`\n\nFile path copied to clipboard: ${n}`:`\n\nFile path: ${n}`),r()&&(e.lastError=a(i),e.configAutoSaveStatus===`rejected`&&(e.configAutoSaveStatus=`error`),F({message:e.lastError}))};try{let n=await t.request(`config.openFile`,{});if(!r())return;if(!n.ok){await o(a(n.error,`Failed to open config file`),n.path||e.configSnapshot?.path);return}F({message:h(`configView.fileOpenedOnGateway`)})}catch(t){await o(i(t),e.configSnapshot?.path)}}function $(){return($=e((()=>{x(),c(),m(),f(),g(),b(),r(),ie(),W(),k(),T(),P()})))()}function ye(e){let{state:t}=e,n=async t=>{let n=e.holdAutoSave();try{e.hasInFlightWrite()&&await e.drainPendingWrites()}finally{n(!t())}};return async r=>{let i=e.currentDraftRevision(),a=()=>e.currentDraftRevision()===i;if(e.invalidateFieldDiscards(),await n(a),a()){if(e.clearPatches(),t.connected&&t.client){e.appliedRefresh.cancel();try{await e.run(()=>Z(t,{discardPendingChanges:!0},a),`config`)&&e.clearDraftConnection()}finally{e.appliedRefresh.reconcile()}}else r?.reloadOnly||t.configRecoveryError!==null||(e.mutate(()=>{J(t),t.configAutoSaveStatus!==`conflict`&&(t.configAutoSaveStatus=`idle`,t.lastError=null)}),e.clearDraftConnection())}}}function be(){return(be=e((()=>{W(),$()})))()}function xe(e){let{state:t}=e,n=new Set;return{invalidate:e=>{for(let t of n)(!e||e.slice(0,Math.min(e.length,t.path.length)).every((e,n)=>e===t.path[n]))&&(t.current=!1)},discard:async r=>{let i=t.client,a=S(t);if(!i||!t.connected||t.configFormMode!==`form`)return!1;let o={path:[...r],current:!0};n.add(o);let s=e.holdAutoSave(),c=!1,l=()=>o.current&&!e.isDisposed()&&N(t,i,a);try{return c=await e.serialize(async()=>!l()||!await e.readSnapshot()||!l()||t.configRecoveryError!==null?!1:e.hasUnacknowledgedDraftWrite()?(t.lastError=h(`configView.discardUnconfirmed`),t.configAutoSaveStatus=`error`,!1):B(t,o.path)),c}finally{n.delete(o),e.reconcileDraft(),s(c),e.publish()}}}}function Se(){return(Se=e((()=>{g(),W(),P()})))()}function Ce(e){let{state:t}=e,n=null,r=r=>(e.appliedRefresh.cancel(),e.dispatch(async i=>{e.appliedRefresh.cancel();let a=t.client,o=S(t);try{let s=r();if(`error`in s)return t.lastError=s.error,t.configAutoSaveStatus=`error`,n=r,!1;if(!a||!t.configSnapshot||s.options.canDispatch?.()===!1)return!1;let c=await ge(t,s.options,i);return N(t,a,o)&&(n=c?null:r,c&&e.reconcileDraft()),c}finally{e.appliedRefresh.reconcile()}}).finally(e.scheduleAutoSave));return{queue:r,retry:e=>n?r(n):e(),clear:()=>{n=null}}}function we(){return(we=e((()=>{$(),P()})))()}function Te({state:e,getFlight:t,invalidateConfigLoad:n,refreshConnectionState:r,refreshSnapshot:i,isDisposed:a,pauseAutoSaveDraftConnection:o,clearAutoSaveDraftConnection:s,publish:c,reconcileAppliedRefresh:u}){let d=null,f=!1,p=null,m=()=>{f=!1,p=null},g=()=>{let e=d??p;return e&&!e.ack&&!e.rejected&&e.operation!==`independent`?e:null},_=()=>g()!==null,v=(e,t)=>t!==null&&(e.gatewayUrl===t.gatewayUrl||l(e.gatewayUrl)===l(t.gatewayUrl)),y=()=>{let t=g();return!t||v(t,e.client)?!0:(e.client&&(e.configAutoSaveStatus=`error`,e.lastError=h(`configView.writeGatewayChanged`)),!1)},b=(n,r={})=>{let i=g();if(K(e,n,{...r,preservePendingChanges:i!==null||r.preservePendingChanges===!0}),r.discardPendingChanges){d=null,m();return}let a=w(n),o=n.configRevisionHash??n.hash,s=i?.form&&i.raw===e.configRawOriginal?z(n):n.raw;if(i&&v(i,e.client)&&t()?.submission!==i&&s===i.raw&&n.hash&&a){let t={config:a,hash:n.hash},r=i.operation===`save`||!!o&&n.appliedConfigHash===o,s=e.configAutoSaveStatus,c=e.lastError;X(e,i,t,{raw:n.raw}),r&&(d={...i,ack:t},m()),(!r||e.configFormDirty)&&e.configAutoSaveStatus!==`conflict`?(e.configAutoSaveStatus=s===`conflict`?`error`:s,e.lastError=c):r&&!e.configFormDirty&&(e.lastError=null)}};return{applySnapshot:b,canWriteDraft:y,unacknowledgedDraftWrite:g,hasUnacknowledgedDraftWrite:_,get latestSubmission(){return d??p},get interrupted(){return f},createFlight(){let r=g(),i={promise:Promise.resolve(),submission:null,snapshotAtDispatch:e.configSnapshot},o=e.client,s=S(e);return{flight:i,onSubmitted:c=>{if(!o)return;let l={...c,gatewayUrl:o.gatewayUrl};!c.ack&&!c.rejected&&(i.snapshotAtDispatch=e.configSnapshot),i.submission=l,!a()&&N(e,o,s)&&(d=c.rejected&&r?r:l,c.ack&&m()),c.ack&&!a()&&t()===i&&n()}}},reconcileSettledFlight(t,n,r){n&&e.configSnapshot&&e.configSnapshot!==t.snapshotAtDispatch&&b(e.configSnapshot),r?.(),t.submission?.rejected&&!_()&&!e.configFormDirty&&e.configSnapshot?(K(e,e.configSnapshot),t.submission.operation===`save`&&(G(e),c())):t.submission?.rejected&&_()&&(e.configAutoSaveStatus===`rejected`&&(e.configAutoSaveStatus=`error`,e.lastError=h(`configView.writeUnconfirmed`)),i())},flushDisposedFlight(t,n,r){n.promise.then(()=>{let i=n.submission,a=i?.ack??null;a&&i&&v(i,t)&&he(e,t,i,a,r)})},retireConnection(){d=null},interrupt(e){f=!0,p=e?.ack?null:e},refreshInterrupted(){let t=p,n=t?.raw??null;t&&!t.ack&&!t.rejected&&(d=t);let i=()=>({form:e.configForm??{},raw:e.configRaw,mode:e.configFormMode,originalRaw:e.configRawOriginal,dirty:e.configFormDirty,submittedRaw:e.configFormDirty?H(e):e.configRaw}),l=null;r(()=>{l=i()},n!==null).then(r=>{if(!a()){if(!r||!e.connected)u();else{if(y()){if(m(),n!==null&&e.configSnapshot?.raw!==n){let t=e.configSnapshot?.raw,n=l&&t===l.originalRaw;l&&n&&!l.dirty&&J(e);let r=typeof t==`string`&&!n;r&&(e.configFormDirty=!0),e.configAutoSaveStatus=r?`conflict`:`error`,e.lastError=r?`config changed since last load; re-run config.get and retry`:h(`configView.writeUnconfirmed`)}else if(t?.operation===`apply`&&g()===t)e.configAutoSaveStatus=`error`,e.lastError??=h(`configView.writeUnconfirmed`);else if(n!==null&&e.configSnapshot?.raw===n){e.configSnapshot.appliedConfigHash===void 0&&(e.configNeedsApply=!0);let t=e.configFormDirty?i():l;t&&t.submittedRaw!==n?(V(e),e.configForm=t.form,e.configRaw=t.raw,e.configFormMode=t.mode,e.configFormDirty=!0,o()):(J(e),e.configAutoSaveStatus=`idle`,s())}}c(),u()}}})},clear(){d=null,m()}}}function Ee(){return(Ee=e((()=>{g(),W(),$(),P()})))()}function De({state:e,gateway:n,publish:r,run:i,mutate:a,resetLoads:o,resetConfigLoad:s,refreshConnectionState:c,canCallConfigMethod:l,appliedRefresh:u,isDisposed:d}){let f=null,p=null,m=!1,g=null,_=!1,v=0,y=0,b=ee(),x=!1,T,E=null,D=t=>{let n=l(t);return!n&&e.connected&&(e.lastError=h(`configView.adminRequired`),e.configAutoSaveStatus===`rejected`&&(e.configAutoSaveStatus=`error`),r()),n&&t!==`config.patch`&&!M.canWriteDraft()?(r(),!1):n},O=()=>{g=null,_=!1,e.configAutoSaveStatus===`paused`&&(e.configAutoSaveStatus=`idle`)},k=()=>{_=!0,e.configFormMode===`form`&&e.configAutoSaveStatus!==`conflict`&&(e.configAutoSaveStatus=`paused`)},A=(t=!1)=>{!t&&_?k():e.client&&e.connected&&e.configFormMode===`form`&&(t||!g&&e.configFormDirty)&&(g={client:e.client,epoch:S(e)},t&&(_=!1,e.configAutoSaveStatus===`paused`&&(e.configAutoSaveStatus=`idle`)))},j=()=>e.configAutoSaveStatus!==`conflict`&&e.configRecoveryError===null&&M.canWriteDraft()&&!_&&g!==null&&g.client===e.client&&g.epoch===S(e),M=Te({state:e,getFlight:()=>p,invalidateConfigLoad:()=>ie(),refreshConnectionState:c,refreshSnapshot:()=>i(()=>Z(e,{background:!0,draftWrites:Q}),`config`),isDisposed:d,pauseAutoSaveDraftConnection:k,clearAutoSaveDraftConnection:O,publish:r,reconcileAppliedRefresh:u.reconcile}),{applySnapshot:P,unacknowledgedDraftWrite:re,hasUnacknowledgedDraftWrite:F}=M,I=()=>{e.configFormDirty?A():p===null&&O()},L=()=>{f&&=(clearTimeout(f),null),m=!1},ie=()=>{s(),ne(e),e.configLoading=!1},ae=(t,n=!1)=>{let{flight:r,onSubmitted:i}=M.createFlight(),a=t(i);return r.promise=a.catch(()=>!1).then(t=>{if(p!==r)return;let i=!t&&F();i&&L(),p=null,M.reconcileSettledFlight(r,i,n?I:void 0);let a=m||n&&t&&e.configFormDirty&&e.configFormMode===`form`&&f===null;m=!1,a&&!d()?R():u.reconcile()}),p=r,a},R=()=>{d()||v||x||!j()||!l(`config.set`)||(p?m=!0:(u.cancel(),ae(t=>i(()=>me(e,`auto`,t,()=>D(`config.set`)?(Y.clear(),!0):!1)),!0).catch(()=>void 0)))},z=()=>{f&&(clearTimeout(f),f=null,R())},B=()=>{!d()&&!x&&j()&&l(`config.set`)&&e.configFormDirty&&e.configFormMode===`form`&&(u.cancel(),f&&clearTimeout(f),f=setTimeout(()=>{f=null,R()},Oe))},V=async(e=!1)=>{for(;;){e&&z();let t=p;if(!t||(await Promise.race([t.promise,b.promise]),d()))return;e||L()}},H=()=>(v++,L(),e=>{v--,e&&B()}),U=null,W=(t,n,r={})=>{if(x&&!T)return Promise.resolve(n());let a=e.client,o=S(e),s=r.flushScheduledDraft?z:L;s();let c=()=>i(async()=>{x&&T&&!d()&&(await T(),x||s()),p&&await V(r.flushScheduledDraft);let i=M.interrupted&&r.flushScheduledDraft?te(e):null;i&&await i.settled;let c=a&&N(e,a,o);return x||d()||!c?n():e.configRecoveryError!==null||r.canDispatch?.()===!1?n(e.configRecoveryError??void 0):r.flushScheduledDraft&&F()?(e.lastError=h(`configView.writeUnconfirmed`),e.configAutoSaveStatus=`error`,n(e.lastError)):await ae(t)}),l=U?U.then(c):c(),u=l.catch(()=>!1).then(()=>{U===u&&(U=null)});return U=u,l},G=xe({state:e,serialize:e=>W(e,()=>!1),readSnapshot:()=>i(()=>Z(e,{draftWrites:Q}),`config`),hasUnacknowledgedDraftWrite:F,holdAutoSave:H,isDisposed:d,publish:r,reconcileDraft:I}),K=n.snapshot.pluginCapabilities?.generation,q=t(n.snapshot.hello?.auth??null),J=n.subscribe(n=>{let i=e.client!==n.client,a=e.connected!==(n.phase===`connected`),s=t(n.hello?.auth??null),l=n.phase===`connected`&&q&&!s;n.phase===`connected`&&(q=s);let d=n.pluginCapabilities?.generation,f=d!==void 0&&d!==K;if(K=d,e.client=n.client,e.connected=n.phase===`connected`,e.applySessionKey=n.sessionKey,i||a||l){let t=re();G.invalidate(),M.retireConnection(),Y.clear();let n=e.configFormDirty||p!==null||t!==null;(i||p!==null||t!==null)&&(g=null),o(),U=null,C(e),L(),u.cancel(),n&&k(),(p!==null||t!==null)&&(M.interrupt(p?.submission??t),p=null,m=!1);let a=b;if(b=ee(),a.resolve(),e.configLoading=!1,e.configSchemaLoading=!1,e.configSaving=!1,e.configApplying=!1,e.configAutoSaveStatus===`saving`&&(e.configAutoSaveStatus=`idle`),e.connected&&e.client&&!l){if(M.interrupted)M.refreshInterrupted();else if(e.configSnapshot||e.configFormDirty||!e.bootstrapConfig){let t=g,n=e.configDraftBaseHash??e.configSnapshot?.hash??null,i=e.client,a=S(e);de(e,async()=>{let o=await c();return o&&N(e,i,a)&&t!==null&&g===t&&(t.client===i&&n!==null&&n===e.configSnapshot?.hash?(A(!0),B(),r()):g=null),o},r,u.reconcile)}}}else f&&e.connected&&e.configSnapshot&&de(e,c,r,u.reconcile);r()}),Y=Ce({state:e,reconcileDraft:I,dispatch:e=>W(e,()=>!1,{flushScheduledDraft:!0,canDispatch:()=>D(`config.patch`)}),appliedRefresh:u,scheduleAutoSave:()=>{F()||B()}}),X=(e,t)=>{y++,G.invalidate(t),a(e),I(),B()},le=(t,n)=>n()?W(async r=>{A(!0),u.cancel();try{if(t===`apply`&&e.configFormDirty&&e.configFormMode===`raw`)return e.configAutoSaveStatus=`error`,e.lastError=h(`configView.rawDraftBlocksApply`),!1;let i=await me(e,t,e=>{t===`save`&&e.ack===null&&Y.clear(),r(e)},()=>n()?(t===`apply`&&Y.clear(),!0):!1);return I(),i}finally{u.reconcile()}},()=>!1,{canDispatch:n}):Promise.resolve(!1),Q={hasUnacknowledgedDraftWrite:F,applySnapshot:P,patchForm:(t,n)=>X(()=>oe(e,t,n),t),removeFormValue:t=>X(()=>se(e,t),t),setRaw:t=>X(()=>ue(e,t,F())),discardFormValue:e=>(y++,G.discard(e)),discardDraft:ye({state:e,currentDraftRevision:()=>y,invalidateFieldDiscards:G.invalidate,hasInFlightWrite:()=>p!==null,holdAutoSave:H,drainPendingWrites:V,clearPatches:Y.clear,run:i,mutate:a,clearDraftConnection:()=>{M.clear(),O()},appliedRefresh:u}),setWritesSuspended:(e,t)=>{if(T=t,x!==e){if(x=e,e)L(),E=ee();else{let e=E;E=null,e?.resolve(),F()||B()}}},waitForPendingWrites:()=>V(!0),flushFormChanges:async()=>{let t=e.client,n=S(e);return!t||!l(`config.set`)||e.configFormMode!==`form`?!1:(await V(!0),!d()&&N(e,t,n)&&!e.configFormDirty&&e.configRecoveryError===null&&(e.configAutoSaveStatus===`saved`||e.configAutoSaveStatus===`idle`))},save:(e={})=>le(`save`,()=>D(`config.set`)&&(e.canDispatch?.()??!0)),retry:()=>Y.retry(()=>M.latestSubmission?.operation===`apply`?Q.apply():Q.save()),apply:()=>le(`apply`,()=>D(`config.apply`)),stageDefaultAgent:t=>{if(!D(`config.set`))return!1;G.invalidate([`agents`]),y++;let n=ce(e,t);return r(),I(),B(),n},patch:e=>D(`config.patch`)&&(e.canDispatch?.()??!0)?Y.queue(()=>({options:e})):Promise.resolve(!1),patchFromSnapshot:t=>D(`config.patch`)?Y.queue(()=>{let n=w(e.configSnapshot);return n?t(n):{error:`Configuration is unavailable; refresh and try again.`}}):Promise.resolve(!1),runExternalMutation:async(t,n={})=>{let r=e.client,a=S(e);for(;;){if(n.waitForWritesResumed&&x&&!d()&&(await T?.(),x&&!d()&&await E?.promise),!r||!N(e,r,a))return{ok:!1,reason:`unavailable`,error:`Connection changed before the configuration update started.`};let o=await W(o=>fe(e,r,a,t,n,()=>i(()=>pe(e,{draftWrites:Q}),`config`),o),e=>({ok:!1,reason:x?`suspended`:`unavailable`,error:e??(x?`Configuration writes are temporarily suspended.`:`Configuration is unavailable; reconnect and try again.`)}),{flushScheduledDraft:!0});if(!n.waitForWritesResumed||d()||o.ok||o.reason!==`suspended`&&!x)return o}},dispose(){G.invalidate(),Y.clear(),E?.resolve(),E=null,b.resolve();let t=e.client,n=e.connected&&t!==null&&!x&&l(`config.set`),r=p;L(),u.dispose(),t&&r?M.flushDisposedFlight(t,r,()=>n&&!_&&l(`config.set`)):n&&!F()&&j()&&e.configFormDirty&&e.configFormMode===`form`&&me(e,`auto`,void 0,()=>l(`config.set`)),C(e),e.connected=!1,e.configLoading=!1,e.configSchemaLoading=!1,e.configSaving=!1,e.configApplying=!1,J(),M.clear()}};return Q}var Oe;function ke(){return(ke=e((()=>{re(),f(),g(),be(),W(),Se(),$(),we(),P(),Ee(),Oe=800})))()}function Ae(e,r,i,a,o){e.configSnapshot&&K(e,e.configSnapshot);let s=ae(()=>!e.configFormDirty&&!e.configSaving&&!e.configApplying&&!x.hasUnacknowledgedDraftWrite(),()=>F({message:h(`configView.reloadBlocked`)})),c=new Set,l=new Map,u=null,d=!1,f=()=>{if(!d)for(let t of c)t(e)},p=a?.subscribe(f),m=async(e,t)=>{let n;try{return n=e(),t&&l.set(t,n),f(),await n}finally{try{f()}finally{t&&l.get(t)===n&&l.delete(t)}}},g=e=>{e(),f()},_=e=>l.get(`config`)??m(e,`config`);o&&m(()=>o,`config`);let v=()=>l.has(`schema`)&&u?u:m(()=>u=Q(e),`schema`),b=j({shouldRefresh:()=>!d&&e.connected&&e.configNeedsApply&&e.configSnapshot?.appliedConfigHash!==void 0,refresh:t=>_(()=>Z(e,{background:!0,draftWrites:x},t))}),x=De({state:e,gateway:r,publish:f,run:m,mutate:g,resetLoads:()=>{l.clear(),u=null},resetConfigLoad:()=>{l.delete(`config`)},refreshConnectionState:(t,n=!1)=>{let r=S(e),i=l.get(`schema`),a=m(()=>Z(e,{beforeApplySnapshot:t,preservePendingChanges:n,draftWrites:x}),`config`);return(e.configSchemaVersion!==null||i)&&C()&&a.then(async()=>{await i,S(e)===r&&await v()}),a},canCallConfigMethod:i,appliedRefresh:b,isDisposed:()=>d}),C=()=>{let e=r.snapshot;return!e.client||e.phase!==`connected`?!1:t(e.hello?.auth??null)};return{snapshotOwner:x,get state(){return e},get schemaRead(){return u},get canSet(){return i(`config.set`)},get canApply(){return i(`config.apply`)},get canPatch(){return i(`config.patch`)},get canOpenFile(){return i(`config.openFile`,{requireAdvertisement:!1})},ensureLoaded:async()=>{!e.configSnapshot&&(l.has(`config`)||y(e))&&await _(()=>Z(e,{draftWrites:x})),b.reconcile()},ensureSchemaLoaded:async()=>{!e.configSchema&&C()&&await v()},refresh:async t=>{b.cancel();try{await m(()=>Z(e,{...t,draftWrites:x}),`config`)}finally{b.reconcile()}},refreshSchema:v,patchForm:x.patchForm,removeFormValue:x.removeFormValue,setRaw:x.setRaw,discardDraft:x.discardDraft,discardFormValue:x.discardFormValue,setWritesSuspended:x.setWritesSuspended,waitForPendingWrites:x.waitForPendingWrites,flushFormChanges:x.flushFormChanges,save:x.save,retry:x.retry,apply:x.apply,openFile:()=>i(`config.openFile`,{requireAdvertisement:!1})?m(()=>ve(e)):Promise.resolve(),agentEntry:(t,n)=>O(e,t,n),stageDefaultAgent:x.stageDefaultAgent,patch:x.patch,patchFromSnapshot:x.patchFromSnapshot,runExternalMutation:x.runExternalMutation,lookupSchemaPath:t=>m(()=>_e(e,t)),subscribe(e){return n(c,e)},dispose(){p?.(),s(),d=!0,x.dispose(),c.clear(),A(e),U(e)}}}function je(){return(je=e((()=>{v(),f(),g(),ie(),R(),W(),$(),k(),P(),ke()})))()}je();export{Ae as createRuntimeConfigEditor};