openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.8 kB
JavaScript
import{n as createRuntimeConfigReader}from"../../runtime-snapshot-Bg5K4JNu.mjs";import"../../runtime-config-snapshot-N0DER8v5.mjs";import{a as resolveXAccountKey,i as resolveXAccount,n as listXRuntimeAccountIds,t as listXAccountIds}from"../../accounts-CMHmwgQI.mjs";import{i as openXAllowlist,n as mergeXAllowlist,r as normalizeXUserId}from"../../allowlist-C2fjJq54.mjs";import{o as XBudgetExceededError,s as openXSpend,t as getXApi}from"../../client-BgKkr6c_.mjs";import{t as getXGuestStatus}from"../../guests-DyQDlRzr.mjs";import{n as getXGitHubStatus}from"../../verified-github-2J9xgDsZ.mjs";
//!extensions/x/src/admin.ts
var XAdminError=class extends Error{constructor(e,t){super(t);this.code=e}};function assertAdmin(e){e.signal?.throwIfAborted();e.client?.connectionSignal?.throwIfAborted();if(!e.client?.connect.scopes?.includes(`operator.admin`)||e.hasCurrentClientAuthority?.()===false)throw new XAdminError(`FORBIDDEN`,`X replies management requires an active administrator.`);e.sessionMutationAuthorization?.assertCurrent();e.sessionAccessAuthority?.assertCurrent();e.sessionMutationCommitGuard?.()}function hasXAccount(e,t){return listXAccountIds(e).includes(t)||resolveXAccountKey(e,t)!==void 0}function callerIdentity(e){const t=e.client;if(!t)throw new XAdminError(`FORBIDDEN`,`An authenticated administrator is required.`);const n=t.authenticatedUserId||t.connect.device?.id||t.connId;if(!n)throw new XAdminError(`FORBIDDEN`,`The administrator identity is unavailable.`);return n}function registerXAllowlistMethods(e){let t;const getAllowlist=()=>t??=openXAllowlist(e.runtime);const accountFor=(e,t=e.context.getRuntimeConfig())=>{const n=e.params.accountId;if(n!==void 0&&(typeof n!==`string`||!n.trim()))throw new XAdminError(`INVALID_REQUEST`,`accountId must be a nonempty string.`);const r=resolveXAccount(t,n);if(!hasXAccount(t,r.accountId))throw new XAdminError(`INVALID_REQUEST`,`The X account is not configured.`);return{cfg:t,account:r}};const snapshot=async(t,n=t.context.getRuntimeConfig(),r)=>{const i=r?resolveXAccount(n,r):accountFor(t,n).account;const a=createRuntimeConfigReader(n);const[o,s,c,l]=await Promise.all([getAllowlist().list(i.accountId),getXGuestStatus(e.runtime,i,n),openXSpend(e.runtime,i.accountId,()=>resolveXAccount(a(),i.accountId).costLimits).status(),getXGitHubStatus(e.runtime,i)]);assertAdmin(t);return{accountId:i.accountId,accounts:listXRuntimeAccountIds(n).map(e=>({accountId:e,username:resolveXAccount(n,e).username})),entries:mergeXAllowlist(i.config.allowFrom??[],o),guests:s,spend:c,verifiedFromGitHub:l}};const n={"x.allowlist.list":snapshot,async"x.guests.set"(e){const t=e.params.enabled;if(typeof t!==`boolean`)throw new XAdminError(`INVALID_REQUEST`,`enabled must be a boolean.`);const{account:n}=accountFor(e);const{mutateConfigFile:r}=await import(`../../plugin-sdk/config-mutation.js`);const i=await r({afterWrite:{mode:`auto`},writeOptions:{assertCurrent:()=>assertAdmin(e)},mutate:r=>{assertAdmin(e);const i=r.channels?.x;if(!i||!hasXAccount(r,n.accountId))throw new XAdminError(`INVALID_REQUEST`,`The X account is no longer configured.`);const a=resolveXAccountKey(r,n.accountId);const o=(a===void 0?void 0:i.accounts?.[a])??i;o.guests={...o.guests,enabled:t}}});return await snapshot(e,i.nextConfig,n.accountId)},async"x.allowlist.add"(e){const t=e.params.username;const n=typeof t===`string`?t.trim().replace(/^@/,``):``;if(!/^[A-Za-z0-9_]{1,15}$/.test(n))throw new XAdminError(`INVALID_REQUEST`,`username must be an X handle, with or without @.`);const{cfg:r,account:i}=accountFor(e);const a=await getXApi(i.accountId,r);assertAdmin(e);const o=await a.getUserByUsername(n,e.signal,()=>()=>assertAdmin(e));assertAdmin(e);await getAllowlist().put(i.accountId,{userId:o.id,username:o.username,name:o.name??o.username,addedBy:callerIdentity(e),addedAt:Date.now()},()=>assertAdmin(e));return await snapshot(e)},async"x.allowlist.remove"(e){const t=typeof e.params.userId===`string`?normalizeXUserId(e.params.userId):void 0;if(!t)throw new XAdminError(`INVALID_REQUEST`,`userId must be a numeric X user ID.`);const{account:n}=accountFor(e);await getAllowlist().remove(n.accountId,t,()=>assertAdmin(e));return await snapshot(e)}};for(const[t,r]of Object.entries(n))e.registerGatewayMethod(t,async n=>{try{assertAdmin(n);n.respond(true,await r(n))}catch(r){if(r instanceof XAdminError)n.respond(false,void 0,{code:r.code,message:r.message});else if(r instanceof XBudgetExceededError)n.respond(false,void 0,{code:`UNAVAILABLE`,message:r.message});else{e.logger.error(`X replies operation failed (${t}): ${r instanceof Error?r.message:String(r)}`);n.respond(false,void 0,{code:`UNAVAILABLE`,message:`X replies operation failed. Check the Gateway logs and try again.`})}}},{scope:`operator.admin`})}export{registerXAllowlistMethods};