openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 9.25 kB
JavaScript
import{o as e}from"./transcript-record-coerce-eN4p5mcq.mjs";import{i as t}from"./transcript-global-singleton-DIJgobpU.mjs";import{n}from"./transcript-sql-connection-CRz-dHXM.mjs";import"./transcript-sqlite-post-commit-BDZjIt2B.mjs";import"./transcript-sqlite-schema-facts-C4Tsn8VX.mjs";import{s as r}from"./transcript-openclaw-state-db.paths-DYRyvCKa.mjs";import{a as i,h as a,i as o,l as s,r as c,t as l}from"./transcript-kysely-sync-DQ_Yn8jW.mjs";import"./transcript-sqlite-transaction-BxeoS_ke.mjs";import{d as u}from"./transcript-openclaw-state-db-cache-DY0tZ0IC.mjs";import{mt as d,xt as f}from"./transcript-openclaw-state-db-readonly-wvpVS-us.mjs";import"./transcript-session-row-changes-DzgVgiDT.mjs";import{a as p}from"./transcript-openclaw-state-db-schema-helpers-DTvLbSq6.mjs";import{t as m}from"./transcript-sqlite-schema-ensure-CzveatKm.mjs";import{f as h,n as g,t as _}from"./transcript-openclaw-state-db-DbakxDr-.mjs";import"./transcript-user-profile-constants-BHKSvmm8.mjs";import"./transcript-secure-random-DdqnSh8g.mjs";import v from"node:path";import{toUSVString as y}from"node:util";function githubAccountAvatarUrl(e,t){return e===`github.com`?`https://avatars.githubusercontent.com/u/${t}?v=4`:`https://${e}/avatars/u/${t}`}const b=/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;function normalizeGitHubLogin(e){let t=e.trim();return b.test(t)?t:void 0}function createOpenClawStateSchemaEnsurer(e){let t=h(e.table,{endMarker:e.endMarker??`
) STRICT;
`,errorMessage:`Canonical state schema markers are missing for ${e.table}`}),r=m(()=>t,{tables:[e.table,...e.additionalTables??[]],indexes:e.indexes});return(t={})=>{let{db:i}=_(t),runWrite=r=>g(({db:t})=>{n(t)?d(t,e.table):r(t)},t,{operationLabel:e.operationLabel});n(i)?f(i,()=>runWrite(r)):r(i,runWrite)}}createOpenClawStateSchemaEnsurer({table:`user_background_images`,operationLabel:`users.background.schema.ensure`}),t(Symbol.for(`openclaw.userPreferences.publications`),()=>{let e=/* @__PURE__ */ new Map;return u(t=>{t.kind!==`opened`&&t.identity&&e.delete(t.identity.key)}),e}),createOpenClawStateSchemaEnsurer({table:`user_preferences`,operationLabel:`users.preferences.schema.ensure`});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}const x=[`image/png`,`image/jpeg`,`image/webp`];function userProfileDisplayName(e,t,n){return t?.trim()||n?.trim()||(e===`gateway-owner`?void 0:`Person `+e.slice(0,8))}m(()=>h(`user_profiles`),{tables:[`user_profiles`,`user_profile_emails`,`user_profile_identities`],indexes:[`idx_user_profile_emails_profile_id`]}),m(()=>{let e=h(`user_profile_identities`,{endMarker:`ON user_profile_identities(authorization_id);`});return e.slice(e.indexOf(`CREATE INDEX`))},{tables:[],indexes:[`idx_user_profile_identities_profile_id`,`idx_user_profile_identities_authorization`]});var UserProfileNotFoundError=class extends Error{constructor(e){super(`user profile not found: ${e}`),this.profileId=e,this.name=`UserProfileNotFoundError`}};function userProfileEditableDisplayName(e){return e&&!/^https?:\/\/\S+#\S+$/iu.test(e.trim())?e:null}const S=a()(`avatar`,`is not`,null).as(`has_avatar`);function userProfilesDb(e){return i(e)}const C=[`id`,`display_name`,`avatar_mime`,`avatar_sha256`,`merged_into`,`updated_at`,S];function normalizeUserProfileAvatarMime(e){return x.find(t=>t===e)??null}function selectResolvedUserProfile(e,t,n){return readResolvedUserProfile(t,t=>o(e,typeof n==`function`?n(t):n.where(`id`,`=`,t)))}function readResolvedUserProfile(e,t){let n=t(e);return n?.merged_into?t(n.merged_into)??n:n}l(e=>s(e,t=>userProfilesDb(e).selectFrom(`user_profiles`).select(t=>[`id`,`display_name`,n(e)?t.val(null).as(`avatar`):t.case().when(t.fn(`typeof`,[`avatar`]),`=`,`blob`).then(null).else(t.ref(`avatar`)).end().as(`avatar`),`avatar_mime`,`avatar_sha256`,`merged_into`,`role`,`created_at`,`updated_at`]).where(`id`,`=`,t(e=>e))));function projectUserProfileDisplay(e){let t=normalizeUserProfileAvatarMime(e.avatar_mime);return{id:e.id,displayName:userProfileDisplayName(e.id,userProfileEditableDisplayName(e.display_name),e.githubIdentity?.login??e.githubLogin)??null,avatarRevision:e.avatar_sha256&&t?`${e.avatar_sha256}-${t.slice(6)}`:String(e.updated_at),hasAvatar:e.has_avatar===1}}function resolveCatalogProfile(e,t){let n=e.get(t);return e.get(n?.merged_into??t)??n}function selectVerifiedEnterpriseGitHubIdentities(t,n,r){if(n?.length===0||!p(t,`user_profile_identities`,`verified_email_json`))return/* @__PURE__ */ new Map;let i=userProfilesDb(t).selectFrom(`user_profile_identities`).select([`provider`,`profile_id`,`subject`,`canonical_login`,`verified_email_json`]).where(`provider`,`like`,`github:%`);r&&(i=i.where(`provider`,`=`,`github:${r}`)),n&&(i=i.where(`profile_id`,`in`,[...n]));let a=c(t,i).rows;if(!a.length)return/* @__PURE__ */ new Map;let o=new Map(c(t,userProfilesDb(t).selectFrom(`user_profile_emails`).select([`email`,`profile_id`,`binding_id`]).where(`email`,`in`,a.map(e=>`${e.provider}:${e.subject}`))).rows.map(e=>[e.email,e])),s=/* @__PURE__ */ new Map;for(let t of a){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,c=o.get(`${t.provider}:${a}`);if(c?.profile_id!==t.profile_id||c.binding_id!==i.bindingId)continue;let l=normalizeVerifiedEmail(i.email),u=s.get(t.profile_id)??[];u.push({host:n.host,accountId:a,login:r,...l?{verifiedEmail:l}:{}}),s.set(t.profile_id,u)}return s}function selectEnterpriseGitHubIdentities(e,t,n){return unambiguousEnterpriseGitHubIdentities(selectVerifiedEnterpriseGitHubIdentities(e,t,n))}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 selectStoredGitHubIdentities(e,t){if(t?.length===0||!p(e,`user_profile_identities`,`canonical_login`))return/* @__PURE__ */ new Map;let n=userProfilesDb(e).selectFrom(`user_profile_identities`).innerJoin(`user_profiles`,`user_profiles.id`,`user_profile_identities.profile_id`).select([`profile_id`,`subject`,`canonical_login`]).select(t=>[p(e,`user_profiles`,`primary_github_account_id`)?`user_profiles.primary_github_account_id`:t.val(null).as(`primary_github_account_id`)]).where(`provider`,`=`,`github`).where(`canonical_login`,`is not`,null).orderBy(`subject`,`asc`);return t&&(n=n.where(`profile_id`,`in`,[...t])),projectStoredGitHubIdentities(c(e,n).rows)}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)}}const profileCatalogPath=e=>v.resolve(e.path??r(e.env??process.env));function selectUserProfileDisplaysInDatabase(e,t){let n=userProfilesDb(e).selectFrom(`user_profiles`),r=c(e,n.select(C).where(e=>e.or([e(`id`,`in`,[...t]),e(`id`,`in`,n.select(`merged_into`).where(`id`,`in`,[...t]).where(`merged_into`,`!=`,``))]))).rows;if(r.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,selectResolvedUserProfile(e,i,n.select(C)));return r}let i=selectUserProfileGitHubIdentities(e,r.filter(e=>!e.display_name?.trim()).map(e=>e.id)),a=new Map(r.map(e=>[e.id,{...e,...i.has(e.id)?{githubIdentity:i.get(e.id)}:{}}]));return new Map(t.map(e=>{let t=a.get(y(e));return[e,t?.merged_into?a.get(t.merged_into)??t:t]}))}function projectUserProfileDisplays(e,t){return new Map(e.flatMap(e=>{let n=t(e);return n?[[e,projectUserProfileDisplay(n)]]:[]}))}export{resolveCatalogProfile as a,projectUserProfileDisplay as i,projectUserProfileDisplays as n,UserProfileNotFoundError as o,selectUserProfileDisplaysInDatabase as r,profileCatalogPath as t};