UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.5 kB
import{_ as e}from"./openclaw-state-read-home-dir-BQZX1pD2.mjs";import"./openclaw-state-read-sqlite-database-admission-Ck6VG7rE.mjs";import{b as t}from"./openclaw-state-read-logger-Ix7jXPG1.mjs";import{_ as n,a as r,c as i,f as a,g as o,l as s,n as c}from"./openclaw-state-read-schemas-CLrAtqTl.mjs";import"./openclaw-state-read-sqlite-post-commit-CEHMNIsB.mjs";import{t as l}from"./openclaw-state-read-json-coercion-DgTIp8OM.mjs";import{a as u,i as d}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import{o as f}from"./openclaw-state-read-openclaw-state-db-schema-helpers-BkqUYEFY.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-D9nuaODy.mjs";import"./openclaw-state-read-sqlite-number-CpW6Oa2f.mjs";import"./openclaw-state-read-openclaw-state-db-schema-additive-DRfDFLLT.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-dCEtSFdy.mjs";import{s as _}from"./openclaw-state-read-user-profiles-internal-D1K1kSBE.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=a().int().nonnegative().safe(),y=n().min(1).max(2048).regex(/^[^\r\n]+$/u),b=n().regex(h),x=r(n().min(1).max(64).regex(/^[a-z0-9:_-]+$/u)).max(32),S={accountId:a().int().positive().safe(),login:n().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:n().regex(/^[A-Za-z0-9_-]{40}$/u),userCode:n().regex(/^[A-Z0-9]{4}-[A-Z0-9]{4}$/u),verificationUri:s(`https://github.com/login/device`),createdAtMs:v,expiresAtMs:v,pollIntervalMs:a().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=o({accessToken:y,refreshToken:y,tokenType:s(`bearer`),scopes:x,expiresInSeconds:a().int().positive().max(31622400),refreshTokenExpiresInSeconds:a().int().positive().max(31622400)}),T={requestId:n().uuid(),createdAtMs:v,expiresAtMs:v},E=o({...T,kind:s(`device`),...C,candidate:o({profileId:b,tokens:w,receivedAtMs:v}).optional()}),D=o({kind:s(`connected`),profileId:b,...S,refreshFailure:c([`expired`,`failed`]).optional(),refresh:o({operationId:n().uuid(),tokens:w.optional(),receivedAtMs:v.optional()}).optional()}),O=o({version:s(1),generation:n().uuid(),selection:i(`kind`,[o({kind:s(`disconnected`)}),D]),pending:i(`kind`,[o({...T,kind:s(`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`})});o({kind:s(`user-github-connection`),changedOwners:r(n()),retiredProfileIds:r(n())});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};