openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.85 kB
JavaScript
import{c as e,rt as t}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{b as n,c as r,d as i,g as a,h as o,m as s,y as c}from"./openclaw-state-read-schemas-YhOPoIHM.mjs";import{t as l}from"./openclaw-state-read-json-coercion-CAL-_nfN.mjs";import{a as u,i as d,r as f}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as p}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{r as m}from"./openclaw-state-read-sqlite-transaction-Bt-IwVhZ.mjs";import"./openclaw-state-read-openclaw-state-db-B3dGXm7m.mjs";import"./openclaw-state-read-secret-store-validation-error-GNcGbCv5.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-Bv32Owin.mjs";import{i as h,n as g,r as _}from"./openclaw-state-read-user-model-account-authority-D8rlDXD4.mjs";import"./openclaw-state-read-user-profile-events-D1aryhLR.mjs";import{a as v,o as y,u as b}from"./openclaw-state-read-user-profiles-internal-RPWRWaaD.mjs";import"node:crypto";import{toUSVString as x}from"node:util";function invalidUserModelAccounts(){return/* @__PURE__ */ Error(`Personal model account state is invalid; restore a verified state backup.`)}function readUserModelAccountRecord(e,t,n){if(!p(e,`secret_store_entries`))return;let r=d(e,u(e).selectFrom(`secret_store_entries`).select([`value`,`kind`,`allowed_hosts`]).where(`scope_kind`,`=`,`identity`).where(`scope_id`,`=`,t).where(`name`,`=`,n).where(`deleted_at_ms`,`is`,null));return r?userModelAccountRecordValue(r):void 0}function userModelAccountRecordValue(e){if(e.kind!==`secret`||e.allowed_hosts!==null||e.value===null)throw invalidUserModelAccounts();return e.value}function readUserModelAccountRecords(e,t){return t.length===0||!p(e,`user_profiles`)||!p(e,`secret_store_entries`)?[]:f(e,u(e).selectFrom(`user_profiles as source`).leftJoin(`user_profiles as target`,`target.id`,`source.merged_into`).innerJoin(`secret_store_entries`,e=>e.on(e=>e(`secret_store_entries.scope_id`,`=`,e.case().when(e.or([e(`source.merged_into`,`is`,null),e(`source.merged_into`,`=`,``)])).then(e.ref(`source.id`)).else(e.ref(`target.id`)).end()))).select([`name`,`value`,`kind`,`allowed_hosts`]).where(`scope_kind`,`=`,`identity`).where(`deleted_at_ms`,`is`,null).where(e=>e.or([e(`source.merged_into`,`is`,null),e(`source.merged_into`,`=`,``),e.and([e(`target.id`,`is not`,null),e.or([e(`target.merged_into`,`is`,null),e(`target.merged_into`,`=`,``)])])])).where(e=>e.or(t.map(({owner:t,name:n})=>e.and([e(`source.id`,`=`,t),e(`name`,`=`,n)]))))).rows}function readUserModelAccountLinksRecord(e,t){let n=readUserModelAccountRecords(e,[{owner:t,name:`model-accounts`}])[0];return n?userModelAccountRecordValue(n):void 0}function readSelectedUserModelAccountRecords(e,t){let n=[...new Set(t)].flatMap(e=>{let t=g(e);return t?[{id:e,owner:t.ownerProfileId}]:[]}),r=readUserModelAccountRecords(e,n.map(({id:e,owner:t})=>({owner:t,name:`model-account:${e}`}))),i=new Map(r.map(e=>[e.name,e]));return n.flatMap(({id:e})=>{let t=i.get(`model-account:${e}`);return t?[{id:e,...t}]:[]})}const S=h.refine(e=>e.copyToAgents!==!0,`Personal model accounts cannot be copied to agent stores.`),C=o({version:r(1),links:s(a(),o({authProfileId:a().min(1),updatedAt:i()}).nullable())}),w=o({version:r(1),credential:S,usageStats:c().transform(_).optional(),authorizationId:n().optional()});function parseRecord(e,t){if(Buffer.byteLength(e,`utf8`)>65536)throw invalidUserModelAccounts();let n=t.safeParse(l(e));if(!n.success)throw invalidUserModelAccounts();return n.data}function resolveOwner(e,t){if(!p(e,`user_profiles`))return;let n=v(e,t,b(e).selectFrom(`user_profiles`).select([`id`,`merged_into`]));return n&&!n.merged_into?n.id:void 0}function readLinks(e,t){let n=readUserModelAccountRecord(e,t,`model-accounts`);return n===void 0?{version:1,links:{}}:parseRecord(n,C)}function parseProfileRecord(e){if(e===void 0)return;let{credential:t,usageStats:n}=parseRecord(e,w);return registerUserModelAuthProfileSecrets(t),{credential:t,usageStats:n}}function registerUserModelAuthProfileSecrets(t){t.type===`oauth`?(e(t.access),e(t.refresh),t.idToken&&e(t.idToken)):t.type===`token`?t.token!==void 0&&e(t.token):t.key!==void 0&&e(t.key)}function accountLinks(e){return Object.entries(e.links).toSorted(([e],[t])=>e.localeCompare(t)).flatMap(([e,t])=>t?[{provider:e,...t}]:[])}function credentialOwner(e,t){let n=g(t);return n?resolveOwner(e,n.ownerProfileId):void 0}function accountSummary(e,n,r){let{credential:i}=parseRecord(n,w),a=[i.email?.trim(),i.displayName?.trim()].filter(Boolean);return{authProfileId:e,provider:i.provider,label:t(x([...new Set(a)].join(` · `)||i.provider),256),authType:i.type,selected:r.links[i.provider]?.authProfileId===e}}function readUserModelAccountSummaryInDatabase(e,t){let n=resolveOwner(e,t.profileId);if(!n||credentialOwner(e,t.authProfileId)!==n)return;let r=readUserModelAccountRecord(e,n,`model-account:${t.authProfileId}`);return r===void 0?void 0:accountSummary(t.authProfileId,r,readLinks(e,n))}function readUserModelAccountSelectionInDatabase(e,t){let n=g(t.authProfileId),r=t.profileId&&t.profileId!==n?.ownerProfileId?resolveOwner(e,t.profileId):void 0,i=n&&p(e,`user_profiles`)?y(e,n.ownerProfileId):void 0,a=i&&!i.merged_into&&(t.profileId===n?.ownerProfileId||r===i.id)?readUserModelAccountRecord(e,i.id,`model-account:${t.authProfileId}`):void 0;return{personal:a!==void 0&&i?accountSummary(t.authProfileId,a,readLinks(e,i.id)):void 0,owner:i?{profileId:i.id,displayName:i.display_name}:void 0}}function readPersonalCatalogProfilesInDatabase(e,t){let n=`requesterProfileId`in t?listUserProfileAuthLinksInDatabase(e,t.requesterProfileId):[],r=`profileId`in t?[t.profileId]:n.map(e=>e.authProfileId),i={};for(let{id:t,...n}of readSelectedUserModelAccountRecords(e,r)){let e=parseProfileRecord(userModelAccountRecordValue(n));e&&(i[t]=e)}return{links:n,profiles:i}}function readPersonalAuthProfileAuthorizationInDatabase(e,t){let n=credentialOwner(e,t),r=n&&readUserModelAccountRecord(e,n,`model-account:${t}`);if(n&&r!==void 0)return{ownerProfileId:n,authorizationId:parseRecord(r,w).authorizationId}}function listUserProfileAuthLinksInDatabase(e,t){let n=readUserModelAccountLinksRecord(e,t);return n===void 0?[]:accountLinks(parseRecord(n,C))}function readUserModelAccountCommand(e,t){return t.type===`userModelAccounts.authorization`?{type:t.type,authorization:m(e,()=>readPersonalAuthProfileAuthorizationInDatabase(e,t.authProfileId))}:t.type===`userModelAccounts.selection`?{type:t.type,selection:readUserModelAccountSelectionInDatabase(e,t)}:t.type===`userModelAccounts.summary`?{type:t.type,account:m(e,()=>readUserModelAccountSummaryInDatabase(e,t))}:t.type===`userModelAccounts.links`?{type:t.type,links:listUserProfileAuthLinksInDatabase(e,t.profileId)}:{type:t.type,catalog:readPersonalCatalogProfilesInDatabase(e,t.selection)}}export{readUserModelAccountCommand};