openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 10.9 kB
JavaScript
import{xt as e}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import"./openclaw-state-read-openclaw-state-db.paths-7XLpcdiq.mjs";import"./openclaw-state-read-runtime-worker-generation-DIZIqsAo.mjs";import"./openclaw-state-read-sqlite-schema-facts-Dtolu7-X.mjs";import{a as t,i as n,r}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as i,s as a}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{a as o}from"./openclaw-state-read-sqlite-transaction-Bt-IwVhZ.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-Bv32Owin.mjs";import{n as s,r as c,t as l}from"./openclaw-state-read-user-profile-constants-DC9CyEfK.mjs";import"./openclaw-state-read-user-profile-events-D1aryhLR.mjs";import{t as u}from"./openclaw-state-read-github-login-EEU6tBiG.mjs";import"./openclaw-state-read-user-preferences.store-Bfv_XrVt.mjs";import{a as d,i as f,l as p,o as m,s as h,u as g}from"./openclaw-state-read-user-profiles-internal-RPWRWaaD.mjs";import"node:path";import{toUSVString as _}from"node:util";function parseEnterpriseGitHubPrincipal(e){let t=/^github:([a-z0-9](?:[a-z0-9.-]*[a-z0-9])?):([1-9][0-9]*)$/u.exec(e),n=t?.[1],r=t?.[2];if(!n||!r||n===`github.com`||n.includes(`..`))return;let i=Number(r);return Number.isSafeInteger(i)?{host:n,accountId:i}:void 0}function normalizeVerifiedEmail(e){if(typeof e!=`string`)return;let t=e.trim();return t.length<=254&&/^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+$/u.test(t)?t.toLowerCase():void 0}function selectVerifiedEnterpriseGitHubIdentities(t,n,i){if(n?.length===0||!a(t,`user_profile_identities`,`verified_email_json`))return/* @__PURE__ */ new Map;let o=g(t).selectFrom(`user_profile_identities`).select([`provider`,`profile_id`,`subject`,`canonical_login`,`verified_email_json`]).where(`provider`,`like`,`github:%`);i&&(o=o.where(`provider`,`=`,`github:${i}`)),n&&(o=o.where(`profile_id`,`in`,[...n]));let s=r(t,o).rows;if(!s.length)return/* @__PURE__ */ new Map;let c=new Map(r(t,g(t).selectFrom(`user_profile_emails`).select([`email`,`profile_id`,`binding_id`]).where(`email`,`in`,s.map(e=>`${e.provider}:${e.subject}`))).rows.map(e=>[e.email,e])),l=/* @__PURE__ */ new Map;for(let t of s){let n=parseEnterpriseGitHubPrincipal(`${t.provider}:${t.subject}`),r=t.canonical_login?u(t.canonical_login):void 0,i;try{if(!t.verified_email_json||t.verified_email_json.length>1024)continue;i=JSON.parse(t.verified_email_json)}catch{continue}if(!n||!e(i)||i.version!==1||i.profileId!==t.profile_id||!r)continue;let{accountId:a}=n,o=c.get(`${t.provider}:${a}`);if(o?.profile_id!==t.profile_id||o.binding_id!==i.bindingId)continue;let s=normalizeVerifiedEmail(i.email),d=l.get(t.profile_id)??[];d.push({host:n.host,accountId:a,login:r,...s?{verifiedEmail:s}:{}}),l.set(t.profile_id,d)}return l}function selectEnterpriseGitHubIdentities(e,t,n){return unambiguousEnterpriseGitHubIdentities(selectVerifiedEnterpriseGitHubIdentities(e,t,n))}function enterpriseGitHubAccountPairs(e){return e.map(({host:e,accountId:t})=>({host:e,accountId:t})).toSorted((e,t)=>e.host.localeCompare(t.host)||e.accountId-t.accountId)}function unambiguousEnterpriseGitHubIdentities(e){let t=/* @__PURE__ */ new Map;for(let[n,r]of e){let[e]=r;e&&r.length===1&&t.set(n,e)}return t}const v=`github`;function selectStoredGitHubIdentities(e,t){if(t?.length===0||!a(e,`user_profile_identities`,`canonical_login`))return/* @__PURE__ */ new Map;let n=g(e).selectFrom(`user_profile_identities`).innerJoin(`user_profiles`,`user_profiles.id`,`user_profile_identities.profile_id`).select([`profile_id`,`subject`,`canonical_login`]).select(t=>[a(e,`user_profiles`,`primary_github_account_id`)?`user_profiles.primary_github_account_id`:t.val(null).as(`primary_github_account_id`)]).where(`provider`,`=`,v).where(`canonical_login`,`is not`,null).orderBy(`subject`,`asc`);return t&&(n=n.where(`profile_id`,`in`,[...t])),projectStoredGitHubIdentities(r(e,n).rows)}function projectStoredGitHubIdentities(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=Number(n.subject),r=n.canonical_login?u(n.canonical_login):void 0;if(!r||!Number.isSafeInteger(e)||e<=0)continue;let i=t.get(n.profile_id)??{accounts:[],primaryId:n.primary_github_account_id??null};i.accounts.push({accountId:e,login:r}),t.set(n.profile_id,i)}return new Map([...t].map(([e,{accounts:t,primaryId:n}])=>[e,{accounts:t,primary:n===null&&t.length===1?t[0]:t.find(e=>e.accountId===n)}]))}function selectProfileAccessEntries(e,t){let n=f(e,t);if(n.length===0)return n;let r=selectStoredGitHubIdentities(e,t),i=selectVerifiedEnterpriseGitHubIdentities(e,t),a=projectUserProfileGitHubIdentities(r,unambiguousEnterpriseGitHubIdentities(i));return n.map(([e,t])=>{let n=r.get(e),o=n?.accounts,s=i.get(e);return[e,{...t,...o?.length?{githubAccountIds:o.map(({accountId:e})=>e),githubLogin:n?.primary?.login??null}:{},githubIdentity:a.get(e)??null,...s?.length?{enterpriseGitHubAccounts:enterpriseGitHubAccountPairs(s)}:{}}]})}function resolveCachedGitHubIdentityInDatabase(e,t){if(!i(e,`user_profiles`)||!a(e,`user_profile_identities`,`canonical_login`))return;let n=`login`in t?u(t.login)?.toLowerCase():void 0,r=`email`in t?t:void 0,o=r?.email.trim().toLowerCase(),s=n?selectGitHubProfileAlias(e,{kind:`github-login`,subject:githubAuthenticationSubject(n)}):o&&r&&Number.isSafeInteger(r.accountId)&&r.accountId>0&&i(e,`user_profile_emails`)?selectGitHubProfileAlias(e,{kind:`email`,email:o}):void 0,c=s?m(e,s.profile_id):void 0,l=c?selectStoredGitHubIdentities(e,[c.id]).get(c.id)?.accounts:void 0;return c&&l?.some(e=>n?e.login.toLowerCase()===n:e.accountId===r?.accountId)?{profileId:c.id,updatedAt:c.updated_at}:void 0}function githubAuthenticationSubject(e){let t=e.trim().toLowerCase();if(!t)throw TypeError(`GitHub login is invalid`);return`login:${t}`}function projectUserProfileGitHubIdentities(e,t){let n=/* @__PURE__ */ new Map;for(let[t,{primary:r}]of e)r&&n.set(t,projectGitHubIdentity(r));for(let[r,i]of t)e.has(r)||n.set(r,{host:i.host,login:i.login,profileUrl:`https://${i.host}/${i.login}`,avatarUrl:`https://${i.host}/avatars/u/${i.accountId}`});return n}function projectGitHubIdentity(e){return{login:e.login,profileUrl:`https://github.com/${e.login}`,avatarUrl:`https://avatars.githubusercontent.com/u/${e.accountId}?v=4`}}function resolveUserProfileGitHubAttributionInDatabase(e,n,o=`github.com`){if(n.length===0||!i(e,`user_profiles`))return{identities:/* @__PURE__ */ new Map,canonicalProfileIds:[]};let l=t(e).selectFrom(`user_profiles as requested`).leftJoin(`user_profiles as profile`,e=>e.on(e=>e(`profile.id`,`=`,e.fn.coalesce(`requested.merged_into`,`requested.id`)))).select([`requested.id as source_id`]).select(t=>[t.fn.coalesce(`requested.merged_into`,`requested.id`).as(`profile_id`),a(e,`user_profiles`,`primary_github_account_id`)?`profile.primary_github_account_id`:t.val(null).as(`primary_github_account_id`)]).where(`requested.id`,`in`,[...n]),u=i(e,`user_preferences`)?l.leftJoin(`user_preferences as preference`,e=>e.onRef(`preference.profile_id`,`=`,`profile.id`).on(`preference.pref_key`,`=`,s)).select(`preference.value_json`):l.select(e=>e.val(null).as(`value_json`)),d=r(e,o===`github.com`&&a(e,`user_profile_identities`,`canonical_login`)?u.leftJoin(`user_profile_identities as identity`,e=>e.onRef(`identity.profile_id`,`=`,`profile.id`).on(`identity.provider`,`=`,v).on(`identity.canonical_login`,`is not`,null)).select([`identity.subject`,`identity.canonical_login`]).orderBy(`identity.subject`,`asc`):u.select(e=>[e.val(null).as(`subject`),e.val(null).as(`canonical_login`)])).rows,f=new Map(d.map(e=>[e.source_id,e.profile_id])),p=[...new Set(f.values())],m=projectStoredGitHubIdentities(d.map(e=>({...e,profile_id:e.source_id}))),h=o===`github.com`?/* @__PURE__ */ new Map:selectEnterpriseGitHubIdentities(e,p,o),g=new Map(d.map(e=>[e.source_id,e.value_json===null?void 0:JSON.parse(e.value_json)]));return{identities:new Map([...f].map(([e,t])=>{let n=h.get(t),r=o===`github.com`?m.get(e)?.primary??null:n?.verifiedEmail?n:null;return[e,c(g.get(e))?r:null]})),canonicalProfileIds:p}}function readUserProfileGitHubCommand(e,t){if(t.type===`userProfiles.githubIdentity.cached`)return o(e,()=>({type:t.type,identity:resolveCachedGitHubIdentityInDatabase(e,t)}));let read=()=>({type:t.type,...resolveUserProfileGitHubAttributionInDatabase(e,t.profileIds,t.host)});return!t.host||t.host===`github.com`?read():o(e,read)}function selectGitHubProfileAlias(e,t){return t.kind===`email`?h(e,t.email):n(e,g(e).selectFrom(`user_profile_identities`).select(`profile_id`).where(`provider`,`=`,v).where(`subject`,`=`,t.subject).where(`canonical_login`,`is`,null))}function readUserProfileEmailBindings(e,t){if(Array.isArray(t)&&t.length===0||!i(e,`user_profile_emails`))return[];let n=g(e).selectFrom(`user_profile_emails`).select([`email`,`profile_id`]).select(t=>[a(e,`user_profile_emails`,`binding_id`)?`binding_id`:t.val(null).as(`binding_id`)]).orderBy(`email`,`asc`);return r(e,t===void 0?n:typeof t==`string`?n.where(`profile_id`,`=`,t):n.where(`profile_id`,`in`,[...t])).rows.map(({email:e,profile_id:t,binding_id:n})=>({email:e,profileId:t,bindingId:n}))}function readUserProfileSnapshotCommand(e,t){return t.type===`userProfiles.catalog`&&!i(e,`user_profiles`)?{type:t.type,profiles:[],emailBindings:readUserProfileEmailBindings(e)}:o(e,()=>t.type===`userProfiles.reconcile`?{type:t.type,profile:selectProfileAccessEntries(e,[t.profileId])[0]?.[1],emailBindings:readUserProfileEmailBindings(e,t.profileId)}:{type:t.type,profiles:selectProfileAccessEntries(e),emailBindings:readUserProfileEmailBindings(e)})}function selectHasMultipleSessionSharingIdentities(e){return r(e,g(e).selectFrom(`user_profiles`).select(`id`).where(`merged_into`,`is`,null).where(`id`,`!=`,l).limit(2)).rows.length>=2}function selectUserProfileIdentityInDatabase(e,t){let n=m(e,t);return n&&{profileId:n.id,role:n.role??null,aliases:new Set(r(e,g(e).selectFrom(`user_profiles`).select(`id`).where(e=>e.or([e(`id`,`=`,n.id),e(`merged_into`,`=`,n.id)]))).rows.map(e=>e.id))}}function selectUserProfileDisplaysInDatabase(e,t){let n=g(e).selectFrom(`user_profiles`),i=r(e,n.select(p).where(e=>e.or([e(`id`,`in`,[...t]),e(`id`,`in`,n.select(`merged_into`).where(`id`,`in`,[...t]).where(`merged_into`,`!=`,``))]))).rows;if(i.some(e=>typeof e.id!=`string`||e.merged_into!==null&&typeof e.merged_into!=`string`)){let r=/* @__PURE__ */ new Map;for(let i of t)r.set(i,d(e,i,n.select(p)));return r}let a=new Map(i.map(e=>[e.id,e]));return new Map(t.map(e=>{let t=a.get(_(e));return[e,t?.merged_into?a.get(t.merged_into)??t:t]}))}export{readUserProfileGitHubCommand as a,selectUserProfileIdentityInDatabase as i,selectHasMultipleSessionSharingIdentities as n,selectStoredGitHubIdentities as o,selectUserProfileDisplaysInDatabase as r,readUserProfileSnapshotCommand as t};