UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.43 kB
import{Sn as e,c as t}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import"./openclaw-state-read-sqlite-database-admission-CrkztOzk.mjs";import"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import{c as n,d as r,g as i,h as a,i as o,n as s,s 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}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as f}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import"./openclaw-state-read-sqlite-number-5mfxKD9j.mjs";import"./openclaw-state-read-openclaw-state-db-B3dGXm7m.mjs";import{n as p,t as m}from"./openclaw-state-read-secret-store-validation-error-GNcGbCv5.mjs";import"./openclaw-state-read-openclaw-state-db-handle-CTlulhMi.mjs";import{t as h}from"./openclaw-state-read-github-identity-profile-id-BSDFrs4w.mjs";import{t as g}from"./openclaw-state-read-secret-store-sqlite-BAcpqt2W.mjs";import{o as _}from"./openclaw-state-read-user-profiles-internal-RPWRWaaD.mjs";import"node:crypto";function assertSecretStoreValueLength(e,t){if(Buffer.byteLength(e,`utf8`)>65536)throw new p(`SECRET_STORE_VALUE_TOO_LARGE`,`Secret store value exceeds ${m} UTF-8 bytes.`);if(t===`secret`&&e.length===0)throw new p(`SECRET_STORE_VALUE_EMPTY`,`Secret store value is empty. Secret entries require a value; check the command that produced it.`)}var PersonalGitHubStateError=class extends Error{constructor(){super(`Personal GitHub state is invalid; disconnect and reconnect My GitHub.`)}};function readPersonalGitHubSecret(e,n){return g(()=>{let r=d(e,u(e).selectFrom(`secret_store_entries`).select([`value`,`kind`,`allowed_hosts`]).where(`scope_kind`,`=`,`identity`).where(`scope_id`,`=`,n).where(`name`,`=`,`github-connection`).where(`deleted_at_ms`,`is`,null));if(r){if(r.kind!==`secret`||r.allowed_hosts!==null)throw new PersonalGitHubStateError;try{assertSecretStoreValueLength(r.value,`secret`)}catch(e){throw e instanceof p?new PersonalGitHubStateError:e}t(r.value)}return r?.value},void 0)}e(Symbol.for(`openclaw.userGitHubConnectionAuthority`),`close-and-restart`),e(Symbol.for(`openclaw.userGitHubProfileRetirement`),`close-and-restart`);const v=r().int().nonnegative().safe(),y=i().min(1).max(2048).regex(/^[^\r\n]+$/u),b=i().regex(h),x=o(i().min(1).max(64).regex(/^[a-z0-9:_-]+$/u)).max(32),S={accountId:r().int().positive().safe(),login:i().regex(/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u),refreshToken:y,accessExpiresAtMs:v,refreshExpiresAtMs:v,scopes:x},C={deviceCode:i().regex(/^[A-Za-z0-9_-]{40}$/u),userCode:i().regex(/^[A-Z0-9]{4}-[A-Z0-9]{4}$/u),verificationUri:n(`https://github.com/login/device`),createdAtMs:v,expiresAtMs:v,pollIntervalMs:r().int().min(1e3).max(6e4),nextPollAtMs:v};function validGitHubDeviceTiming(e){return e.expiresAtMs>e.createdAtMs&&e.expiresAtMs-e.createdAtMs<=9e5&&(e.nextPollAtMs===void 0||e.nextPollAtMs>=e.createdAtMs&&e.nextPollAtMs<=e.expiresAtMs)}const w=a({accessToken:y,refreshToken:y,tokenType:n(`bearer`),scopes:x,expiresInSeconds:r().int().positive().max(31622400),refreshTokenExpiresInSeconds:r().int().positive().max(31622400)}),T={requestId:i().uuid(),createdAtMs:v,expiresAtMs:v},E=a({...T,kind:n(`device`),...C,candidate:a({profileId:b,tokens:w,receivedAtMs:v}).optional()}),D=a({kind:n(`connected`),profileId:b,...S,refreshFailure:s([`expired`,`failed`]).optional(),refresh:a({operationId:i().uuid(),tokens:w.optional(),receivedAtMs:v.optional()}).optional()}),O=a({version:n(1),generation:i().uuid(),selection:c(`kind`,[a({kind:n(`disconnected`)}),D]),pending:c(`kind`,[a({...T,kind:n(`starting`)}),E]).optional()}).superRefine((e,t)=>{let n=e.pending;n&&!validGitHubDeviceTiming(n)&&t.addIssue({code:`custom`,message:`Invalid device timing`});let r=e.selection;r.kind===`connected`&&(r.refreshExpiresAtMs<=r.accessExpiresAtMs||!!r.refresh?.tokens!=(r.refresh?.receivedAtMs!==void 0))&&t.addIssue({code:`custom`,message:`Invalid refresh state`})});a({kind:n(`user-github-connection`),changedOwners:o(i()),retiredProfileIds:o(i())});function parseUserGitHubConnection(e){let t=O.safeParse(l(e));if(!t.success)throw new PersonalGitHubStateError;return registerUserGitHubConnectionSecrets(t.data)}function registerUserGitHubConnectionSecrets(e){return e.pending?.kind===`device`&&(t(e.pending.deviceCode),e.pending.candidate&&registerTokens(e.pending.candidate.tokens)),e.selection.kind===`connected`&&(t(e.selection.refreshToken),e.selection.refresh?.tokens&&registerTokens(e.selection.refresh.tokens)),e}function registerTokens(e){t(e.accessToken),t(e.refreshToken)}function resolvePersonalGitHubOwnerInDatabase(e,t){if(!f(t,`user_profiles`))return;let n=_(t,e);return n&&!n.merged_into?n.id:void 0}function requireOwner(e,t){if(resolvePersonalGitHubOwnerInDatabase(t,e)!==t)throw Error(`Personal GitHub owner changed; reconnect and try again.`)}function readConnection(e,t){let n=readPersonalGitHubSecret(e,t);return n===void 0?void 0:parseUserGitHubConnection(n)}function readUserGitHubConnectionInDatabase(e,t){return requireOwner(e,t),readConnection(e,t)}function readCanonicalUserGitHubConnectionInDatabase(e,t){let n=resolvePersonalGitHubOwnerInDatabase(t,e);return n?{owner:n,connection:readConnection(e,n)}:void 0}export{resolvePersonalGitHubOwnerInDatabase as i,readCanonicalUserGitHubConnectionInDatabase as n,readUserGitHubConnectionInDatabase as r,parseUserGitHubConnection as t};