UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 7 kB
import{Q as e,b as t}from"./openclaw-state-read-logger-Ix7jXPG1.mjs";import{_ as n,b as r,f as i,g as a,h as o,l as s,x as c}from"./openclaw-state-read-schemas-CLrAtqTl.mjs";import{t as l}from"./openclaw-state-read-json-coercion-DgTIp8OM.mjs";import{a as u,i as d,r as f}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import{r as p}from"./openclaw-state-read-sqlite-transaction-V1rK3HH2.mjs";import{o as m}from"./openclaw-state-read-openclaw-state-db-schema-helpers-BkqUYEFY.mjs";import"./openclaw-state-read-openclaw-state-db-DnrW7QY9.mjs";import"./openclaw-state-read-secret-store-validation-error-GNcGbCv5.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-CDG18cNK.mjs";import{i as h,n as g,r as _}from"./openclaw-state-read-user-model-account-authority-B9juYXXg.mjs";import"./openclaw-state-read-openclaw-state-worker-context-Cvog3s4V.mjs";import"./openclaw-state-read-openclaw-state-db-schema-additive-DRfDFLLT.mjs";import"./openclaw-state-read-user-profile-events-DvdEKDEH.mjs";import{d as v,o as y,s as b}from"./openclaw-state-read-user-profiles-internal-D1K1kSBE.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(!m(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||!m(e,`user_profiles`)||!m(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=a({version:s(1),links:o(n(),a({authProfileId:n().min(1),updatedAt:i()}).nullable())}),w=a({version:s(1),credential:S,usageStats:r().transform(_).optional(),authorizationId:c().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(!m(e,`user_profiles`))return;let n=y(e,t,v(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(e){e.type===`oauth`?(t(e.access),t(e.refresh),e.idToken&&t(e.idToken)):e.type===`token`?e.token!==void 0&&t(e.token):e.key!==void 0&&t(e.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(t,n,r){let{credential:i}=parseRecord(n,w),a=[i.email?.trim(),i.displayName?.trim()].filter(Boolean);return{authProfileId:t,provider:i.provider,label:e(x([...new Set(a)].join(` · `)||i.provider),256),authType:i.type,selected:r.links[i.provider]?.authProfileId===t}}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&&m(e,`user_profiles`)?b(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:p(e,()=>readPersonalAuthProfileAuthorizationInDatabase(e,t.authProfileId))}:t.type===`userModelAccounts.selection`?{type:t.type,selection:readUserModelAccountSelectionInDatabase(e,t)}:t.type===`userModelAccounts.summary`?{type:t.type,account:p(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};