openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 8.45 kB
JavaScript
import{a as e}from"./openclaw-state-read-record-coerce-D1s3ReAs.mjs";import{_ as t,g as n,x as r}from"./openclaw-state-read-schemas-CLrAtqTl.mjs";import"./openclaw-state-read-sqlite-worker-operation-settlement-ogK5IsJy.mjs";import{d as i}from"./openclaw-state-read-sqlite-post-commit-CEHMNIsB.mjs";import{u as a}from"./openclaw-state-read-kysely-sync-cache-state-PxpuKzoA.mjs";import{t as o}from"./openclaw-state-read-json-coercion-DgTIp8OM.mjs";import{i as s,r as c}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import{s as l}from"./openclaw-state-read-sqlite-schema-facts-DxLfPKu-.mjs";import"./openclaw-state-read-sqlite-worker-operation-admission-DTfv_SKu.mjs";import{a as u}from"./openclaw-state-read-sqlite-transaction-V1rK3HH2.mjs";import{o as d}from"./openclaw-state-read-openclaw-state-db-schema-helpers-BkqUYEFY.mjs";import"./openclaw-state-read-openclaw-state-db-DnrW7QY9.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-CDG18cNK.mjs";import{a as f}from"./openclaw-state-read-openclaw-state-db-admission--7YB7DUG.mjs";import"./openclaw-state-read-user-profile-constants-DC9CyEfK.mjs";import{r as p}from"./openclaw-state-read-config-machine-state-row-CTZWZ_ex.mjs";import{i as m}from"./openclaw-state-read-users-L85ihBFX.mjs";import"./openclaw-state-read-user-profile-events-DvdEKDEH.mjs";import"./openclaw-state-read-config-machine-state-write-CU2Cxsvy.mjs";import"./openclaw-state-read-config-machine-state-rtO7vE83.mjs";import"./openclaw-state-read-user-channel-authorization-reference-Cn8KvdR4.mjs";import"./openclaw-state-read-secure-random-Bwhk0Eun.mjs";import{a as h,i as g,o as _,r as v,s as y,t as b}from"./openclaw-state-read-user-profile-list-B7mW0dZX.mjs";import{d as x,i as S,l as C,m as w,n as T,p as E,r as D,s as O,t as k}from"./openclaw-state-read-user-profiles-internal-D1K1kSBE.mjs";import{isDeepStrictEqual as A}from"node:util";import{Check as j}from"typebox/value";function classifyTailscaleLogin(e){let t=e.trim(),n=t.lastIndexOf(`@`);if(n<=0||n===t.length-1)return{kind:`invalid`};let r=t.slice(0,n),i=t.slice(n+1);return i.includes(`.`)?{kind:`email`,email:t}:{kind:`provider`,provider:i.toLowerCase(),subject:r.toLowerCase()}}const M=`channel.identity`,N=n({fingerprint:t().length(43),id:r()});function splitPolicy(t){let{configuredOwnerPolicy:n,...r}=e(t)?t:{};return{policy:r,configured:N.safeParse(n).data}}const P=n({pluginId:t().min(1).max(128),grantId:r()}).nullable();function matchesPolicy(e,t){let n=p(e,`operator.channelPolicy`);return n!==void 0&&A(splitPolicy(JSON.parse(n.value_json)).policy,t)}function readAuthorization(e,t,n){if(!f(e,19)||!matchesPolicy(e,n))return;let r=s(e,x(e).selectFrom(`user_profile_identities`).select([`subject`,`authorization_basis_json`]).where(`provider`,`=`,M).where(`authorization_id`,`=`,t));if(!r?.authorization_basis_json||r.authorization_basis_json.length>1024)return;let i=P.safeParse(o(r.authorization_basis_json));return i.success?{subject:r.subject,reference:{version:1,id:t},grant:i.data}:void 0}var UserChannelIdentityConflictError=class extends Error{constructor(){super(`channel identity is linked to another profile; unlink it from that profile first`),this.name=`UserChannelIdentityConflictError`}};function userChannelIdentitySubject(e){if(!j(m,e))throw TypeError(`invalid channel identity`);return JSON.stringify([e.channelId,e.accountId,e.senderId])}function readIdentity(e){let t=o(e);if(!Array.isArray(t)||t.length!==3)return;let n={channelId:t[0],accountId:t[1],senderId:t[2]};return j(m,n)?n:void 0}function selectLink(e,t){return s(e,x(e).selectFrom(`user_profile_identities`).select(`profile_id`).where(`provider`,`=`,M).where(`subject`,`=`,t))}function requirePerson(e,t){let n=S(e,t);if(t===`gateway-owner`||n.id===`gateway-owner`)throw new w(`merge`);return n}function hasIdentityTables(e){return d(e,`user_profiles`)&&d(e,`user_profile_identities`)}function listUserChannelIdentitiesInDatabase(e,t){return u(e,()=>{if(!hasIdentityTables(e))return[];let n=requirePerson(e,t);return c(e,x(e).selectFrom(`user_profile_identities`).select(`subject`).where(`provider`,`=`,M).where(`profile_id`,`=`,n.id).orderBy(`subject`,`asc`)).rows.flatMap(({subject:e})=>{let t=readIdentity(e);return t?[{profileId:n.id,identity:t}]:[]})})}function resolveUserChannelIdentityInDatabase(e,t){return u(e,()=>{if(!hasIdentityTables(e))return;let n,r;if(`authorizationId`in t){if(n=readAuthorization(e,t.authorizationId,t.policy),!n)return;r=n.subject}else r=userChannelIdentitySubject(t);let i=selectLink(e,r),a=i?O(e,i.profile_id):void 0;if(!a||a.id===`gateway-owner`)return;let o=x(e),s=C(e,a.id),l=s.filter(e=>{let t=classifyTailscaleLogin(e);return t.kind!==`provider`||t.provider!==`github`}),u=c(e,o.selectFrom(`user_profile_identities`).select([`provider`,`subject`]).where(`profile_id`,`=`,a.id).where(`canonical_login`,`is`,null)).rows.filter(e=>e.provider!==`github`&&e.provider!==`github-attribution`&&!e.provider.includes(`.`)).map(e=>`${e.subject}@${e.provider}`),d=_(e,[a.id]).get(a.id),f=d?.accounts,p=f?.map(e=>`${e.login.toLowerCase()}@github`)??[];return{...n?{authorization:n}:{},profileId:a.id,displayName:a.display_name,role:a.role??null,emails:s,...f?.length?{githubAccountIds:f.map(({accountId:e})=>e)}:{},...d?.primary?{githubLogin:d.primary.login}:{},loginIdentities:[.../* @__PURE__ */ new Set([...l,...u,...p])].toSorted()}},{operationLabel:`user-channel-identities.resolve`})}function readUserChannelIdentityResult(e){try{return{ok:!0,value:e()}}catch(e){if(e instanceof UserChannelIdentityConflictError)return{ok:!1,kind:`conflict`};if(e instanceof E)return{ok:!1,kind:`not-found`};if(e instanceof w)return{ok:!1,kind:`owner`,code:e.code};throw e}}const F=/* @__PURE__ */ new WeakMap;function readDisplays(e,t){return d(e,`user_profiles`)?v(e,t):/* @__PURE__ */ new Map}function readIdentities(e,t){return d(e,`user_profile_identities`)?_(e,t):/* @__PURE__ */ new Map}function readProfiles(e,t,n){if(!t.length)return/* @__PURE__ */ new Map;let r=readDisplays(e,t),i=[...new Set(t.map(e=>r.get(e)?.id??e))],a=n?readIdentities(e,i):void 0;return new Map(t.map(e=>{let t=r.get(e),n=t?.id??e;return[e,{ok:!0,facts:{profileId:n,profile:t?T(t):void 0,github:a?.get(n)?.primary}}]}))}function readCatalogIdentity(e,t){let n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Map;if(t.kind===`source`)return{profiles:readProfiles(e,[...new Set(t.profileIds)],!0),accounts:n,owners:r};let i=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;for(let[t,n]of readIdentities(e))for(let e of n.accounts){let n=String(e.accountId),r=e.login.toLowerCase();i.has(n)||i.set(n,t),a.has(r)||a.set(r,t)}for(let e of t.accountIds){let t=i.get(e);t&&n.set(e,t)}for(let e of t.owners){let t=e.startsWith(`profile:`)?e.slice(8):e.startsWith(`github:`)?a.get(e.slice(7).toLowerCase()):void 0;t&&r.set(e,t)}return{profiles:readProfiles(e,[.../* @__PURE__ */ new Set([...r.values(),...n.values()])],!1),accounts:n,owners:r}}function readUserProfileCatalogIdentity(e,t){let n=l(e),r=JSON.stringify(t),o=F.get(e);if(n){o?(o.schema!==n.schema||o.writeRevision!==n.writeRevision||o.mutationRevision!==n.mutationRevision)&&(Object.assign(o,n),o.pages.clear()):(o={...n,pages:/* @__PURE__ */ new Map},F.set(e,o),a(i(e),()=>F.delete(e)));let t=o.pages.get(r);if(t)return t}let s=readCatalogIdentity(e,t);return n&&l(e)===n&&o&&s.profiles.size<=1e3&&(o.pages.size>=8&&o.pages.delete(o.pages.keys().next().value),o.pages.set(r,s)),s}function readUserProfileCommand(e,t){switch(t.type){case`userProfiles.identity`:return{type:t.type,profile:d(e,`user_profiles`)?u(e,()=>g(e,t.profileId)):void 0};case`userProfiles.catalogIdentity`:return{type:t.type,result:readUserProfileCatalogIdentity(e,t.input)};case`userProfiles.githubIdentity.cached`:case`userProfiles.githubAttribution.resolve`:return h(e,t);case`userProfiles.channelIdentity.list`:return{type:t.type,result:readUserChannelIdentityResult(()=>listUserChannelIdentitiesInDatabase(e,t.profileId))};case`userProfiles.channelIdentity.resolve`:return{type:t.type,linked:resolveUserChannelIdentityInDatabase(e,t.identity)};case`userProfiles.reconcile`:case`userProfiles.catalog`:return b(e,t);case`userProfiles.avatar.inspect`:return u(e,()=>{let n=k(e,t.profileId),r=n.profile?.id;return{type:t.type,inspection:{...n,githubAvatarUrl:r&&!n.avatar?y(e,[r]).get(r)?.avatarUrl:void 0}}});case`userProfiles.avatar.read`:return D(e,t)}throw Error(`Unsupported profile read command`)}export{readUserProfileCommand};