openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 12.9 kB
JavaScript
import"./openclaw-state-read-openclaw-state-db-cache-BVY8IYkn.mjs";import"./openclaw-state-read-openclaw-state-db.paths-CFemYLpA.mjs";import"./openclaw-state-read-openclaw-state-db-async-lifecycle-B_Y330HB.mjs";import{a as e}from"./openclaw-state-read-record-coerce-D1s3ReAs.mjs";import"./openclaw-state-read-sqlite-post-commit-CEHMNIsB.mjs";import{a as t,i as n,r}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import"./openclaw-state-read-sqlite-schema-facts-DxLfPKu-.mjs";import{a as i}from"./openclaw-state-read-sqlite-transaction-V1rK3HH2.mjs";import{o as a,s as o}from"./openclaw-state-read-openclaw-state-db-schema-helpers-BkqUYEFY.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-CDG18cNK.mjs";import"./openclaw-state-read-openclaw-state-worker-context-Cvog3s4V.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-DvdEKDEH.mjs";import"./openclaw-state-read-user-preferences.store-CzrRAUJG.mjs";import{a as u,c as d,d as f,o as p,s as m,u as h}from"./openclaw-state-read-user-profiles-internal-D1K1kSBE.mjs";import"node:path";import{toUSVString as g}from"node:util";function normalizeGitCoauthorIdentity(e){if(typeof e!=`object`||!e||Array.isArray(e)||!(`name`in e)||!(`email`in e)||typeof e.name!=`string`||typeof e.email!=`string`||/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}<>]/u.test(e.name)||/[\p{Cc}\p{Cf}]/u.test(e.email))return;let t=e.name.trim(),n=e.email.trim();if(!(!t||t.length>256||n.length>254||!/^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+$/u.test(n)))return{name:t,email:n.toLowerCase()}}function normalizeGitCoauthorOrganizationIdentity(e){let t=normalizeGitCoauthorIdentity(e);if(!(!t||typeof e!=`object`||!e||!(`policy`in e)||typeof e.policy!=`string`||!e.policy||e.policy.length>2048))return{...t,policy:e.policy}}function githubAccountAvatarUrl(e,t){return e===`github.com`?`https://avatars.githubusercontent.com/u/${t}?v=4`:`https://${e}/avatars/u/${t}`}const _=/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;function normalizeGitHubLogin(e){let t=e.trim();return _.test(t)?t:void 0}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||!o(t,`user_profile_identities`,`verified_email_json`))return/* @__PURE__ */ new Map;let a=f(t).selectFrom(`user_profile_identities`).select([`provider`,`profile_id`,`subject`,`canonical_login`,`verified_email_json`]).where(`provider`,`like`,`github:%`);i&&(a=a.where(`provider`,`=`,`github:${i}`)),n&&(a=a.where(`profile_id`,`in`,[...n]));let s=r(t,a).rows;if(!s.length)return/* @__PURE__ */ new Map;let c=new Map(r(t,f(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?normalizeGitHubLogin(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),u=l.get(t.profile_id)??[];u.push({host:n.host,accountId:a,login:r,...s?{verifiedEmail:s}:{}}),l.set(t.profile_id,u)}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}function projectStoredGitHubIdentities(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=Number(n.subject),r=n.canonical_login?normalizeGitHubLogin(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 readUserProfileGitAttribution(e,n,i=`github.com`,l,u=!1){if(n.length===0||!a(e,`user_profiles`))return{identities:/* @__PURE__ */ new Map,canonicalProfileIds:[]};let d=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`),o(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]),f=a(e,`user_preferences`)?d.leftJoin(`user_preferences as preference`,e=>e.onRef(`preference.profile_id`,`=`,`profile.id`).on(`preference.pref_key`,`=`,s)).leftJoin(`user_preferences as organization`,e=>e.onRef(`organization.profile_id`,`=`,`profile.id`).on(`organization.pref_key`,`=`,`git.coauthor.organization`)).select([`preference.value_json`,`organization.value_json as organization_json`]):d.select(e=>[e.val(null).as(`value_json`),e.val(null).as(`organization_json`)]),p=r(e,i===`github.com`&&o(e,`user_profile_identities`,`canonical_login`)?f.leftJoin(`user_profile_identities as identity`,e=>e.onRef(`identity.profile_id`,`=`,`profile.id`).on(`identity.provider`,`=`,`github`).on(`identity.canonical_login`,`is not`,null)).select([`identity.subject`,`identity.canonical_login`]).orderBy(`identity.subject`,`asc`):f.select(e=>[e.val(null).as(`subject`),e.val(null).as(`canonical_login`)])).rows,m=new Map(p.map(e=>[e.source_id,e.profile_id])),h=[...new Set(m.values())],g=projectStoredGitHubIdentities(p.map(e=>({...e,profile_id:e.source_id}))),_=i===`github.com`?/* @__PURE__ */ new Map:selectEnterpriseGitHubIdentities(e,h,i),v=new Map(p.map(e=>[e.source_id,e.value_json===null?void 0:JSON.parse(e.value_json)])),y=new Map(p.map(e=>{let t=!l||e.organization_json===null?void 0:normalizeGitCoauthorOrganizationIdentity(JSON.parse(e.organization_json));return[e.source_id,t?.policy===l?t:void 0]}));return{identities:new Map([...m].map(([e,t])=>{if(t===`gateway-owner`)return[e,null];let n=_.get(t),r=i===`github.com`?g.get(e)?.primary??null:n??null,a=i!==`github.com`&&!n?.verifiedEmail?null:r,o=u?!0:v.get(e),s=u?void 0:y.get(e),l=s?o===!0?{name:s.name,email:s.email,...r?{accountId:r.accountId,githubLogin:r.login}:{}}:null:a;return[e,c(o)?l:null]})),canonicalProfileIds:h}}const v=`github`;function selectStoredGitHubIdentities(e,t){if(t?.length===0||!o(e,`user_profile_identities`,`canonical_login`))return/* @__PURE__ */ new Map;let n=f(e).selectFrom(`user_profile_identities`).innerJoin(`user_profiles`,`user_profiles.id`,`user_profile_identities.profile_id`).select([`profile_id`,`subject`,`canonical_login`]).select(t=>[o(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 selectProfileAccessEntries(e,t){let n=u(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(!a(e,`user_profiles`)||!o(e,`user_profile_identities`,`canonical_login`))return;let n=`login`in t?normalizeGitHubLogin(t.login)?.toLowerCase():void 0,r=`email`in t?t:void 0,i=r?.email.trim().toLowerCase(),s=n?selectGitHubProfileAlias(e,{kind:`github-login`,subject:githubAuthenticationSubject(n)}):i&&r&&Number.isSafeInteger(r.accountId)&&r.accountId>0&&a(e,`user_profile_emails`)?selectGitHubProfileAlias(e,{kind:`email`,email:i}):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 selectUserProfileGitHubIdentities(e,t){return projectUserProfileGitHubIdentities(selectStoredGitHubIdentities(e,t),selectEnterpriseGitHubIdentities(e,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:githubAccountAvatarUrl(i.host,i.accountId)});return n}function projectGitHubIdentity(e){return{login:e.login,profileUrl:`https://github.com/${e.login}`,avatarUrl:githubAccountAvatarUrl(`github.com`,e.accountId)}}function readUserProfileGitHubCommand(e,t){if(t.type===`userProfiles.githubIdentity.cached`)return i(e,()=>({type:t.type,identity:resolveCachedGitHubIdentityInDatabase(e,t)}));let read=()=>({type:t.type,...readUserProfileGitAttribution(e,t.profileIds,t.host,t.organizationDefaults,t.githubAvailability)});return!t.host||t.host===`github.com`?read():i(e,read)}function selectGitHubProfileAlias(e,t){return t.kind===`email`?d(e,t.email):n(e,f(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||!a(e,`user_profile_emails`))return[];let n=f(e).selectFrom(`user_profile_emails`).select([`email`,`profile_id`]).select(t=>[o(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`&&!a(e,`user_profiles`)?{type:t.type,profiles:[],emailBindings:readUserProfileEmailBindings(e)}:i(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,f(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,f(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=f(e).selectFrom(`user_profiles`),i=r(e,n.select(h).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,p(e,i,n.select(h)));return r}let a=selectUserProfileGitHubIdentities(e,i.filter(e=>!e.display_name?.trim()).map(e=>e.id)),o=new Map(i.map(e=>[e.id,{...e,...a.has(e.id)?{githubIdentity:a.get(e.id)}:{}}]));return new Map(t.map(e=>{let t=o.get(g(e));return[e,t?.merged_into?o.get(t.merged_into)??t:t]}))}export{readUserProfileGitHubCommand as a,selectUserProfileIdentityInDatabase as i,selectHasMultipleSessionSharingIdentities as n,selectStoredGitHubIdentities as o,selectUserProfileDisplaysInDatabase as r,selectUserProfileGitHubIdentities as s,readUserProfileSnapshotCommand as t};