openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.59 kB
JavaScript
import{dn as e,xt as t}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import"./openclaw-state-read-sqlite-worker-operation-admission-DdAi_0af.mjs";import{a as n,i as r,r as i}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{a}from"./openclaw-state-read-sqlite-transaction-Bt-IwVhZ.mjs";import{n as o,r as s,t as c}from"./openclaw-state-read-workspace-state-identity-CiZRdjod.mjs";import{n as l}from"./openclaw-state-read-state-domain-publication-BS5chgVs.mjs";import{t as u}from"./openclaw-state-read-state-repair-message-DYzJY5ec.mjs";import d from"node:fs";import f from"node:path";l({domain:`workspace-state`,keyOf:e=>e.identity.workspaceKey,isValue:e=>t(e)&&t(e.identity)&&typeof e.identity.workspaceKey==`string`&&typeof e.identity.workspacePath==`string`&&typeof e.setupExists==`boolean`&&t(e.setup)&&e.setup.version===1&&(e.attestation===void 0||t(e.attestation)&&typeof e.attestation.attestedAtMs==`number`&&e.attestation.generatedHashes instanceof Map)});const p=/^[a-f0-9]{64}$/u,m=/^[A-Za-z0-9._-]+\.md$/u,h=/^(?:con|prn|aux|nul|com[0-9]|lpt[0-9])$/iu;function isSafeWorkspaceAttestationFilename(e){return e.length<=255&&m.test(e)&&!e.startsWith(`.`)&&!h.test(e.split(`.`)[0]??``)}function isCanonicalIsoTimestamp(e){let t=new Date(e);return Number.isFinite(t.getTime())&&t.toISOString()===e}function assertCanonicalTimestamp(e,t){if(e!==null&&!isCanonicalIsoTimestamp(e))throw Error(`workspace ${t} timestamp is invalid`)}function assertCanonicalIntegerTimestamp(e,t){if(!Number.isSafeInteger(e)||e<0)throw Error(`workspace ${t} timestamp is invalid`)}function workspacePathEntryExists(t){try{return d.lstatSync(f.resolve(e(t))),!0}catch{return!1}}function resolveWorkspaceIdentityFromDatabase(e){let t=s(e.workspaceDir),r=t.at(-1),a=n(e.database.db),l=i(e.database.db,a.selectFrom(`workspace_path_aliases`).selectAll().where(`alias_key`,`in`,t.map(e=>e.workspaceKey))).rows,u=new Map(t.map(e=>[e.workspaceKey,e])),d;for(let n of l){let i=u.get(n.alias_key);if(!i||i.workspacePath!==n.alias_path)throw Error(`workspace path alias key collision`);let a=o(n.workspace_path);if(a.workspaceKey!==n.workspace_key)throw Error(`workspace path alias target is invalid`);if(workspacePathEntryExists(e.workspaceDir)&&a.workspaceKey!==r.workspaceKey)throw new c({aliasPath:t[0].workspacePath,storedWorkspacePath:a.workspacePath,currentWorkspacePath:r.workspacePath});if(d&&d.workspaceKey!==a.workspaceKey)throw Error(`workspace path aliases resolve to conflicting state`);d=a}let f=new Set(l.map(e=>e.alias_key));return{identity:d??r,aliases:t,missingAliasKeys:t.map(e=>e.workspaceKey).filter(e=>!f.has(e))}}function readWorkspaceStateSnapshotFromDatabase(e){let t=e.identity,a=n(e.database.db),o=r(e.database.db,a.selectFrom(`workspace_setup_state`).selectAll().where(`workspace_key`,`=`,t.workspaceKey));if(o?.workspace_path!=null&&o.workspace_path!==t.workspacePath)throw Error(`workspace state key collision`);if(o?.version!=null&&o.version!==1)throw Error(u(`unsupported workspace setup version ${o.version} in ${e.database.path} for ${t.workspacePath}`,`Use a compatible OpenClaw build that supports this workspace version; preserve the database unchanged.`));if(o?.version!=null){if(assertCanonicalTimestamp(o.bootstrap_seeded_at,`bootstrap seeded`),assertCanonicalTimestamp(o.setup_completed_at,`setup completed`),o.updated_at==null)throw Error(`workspace setup update timestamp is invalid`);assertCanonicalIntegerTimestamp(o.updated_at,`setup update`)}let s=o?.attested_at_ms!=null,c=/* @__PURE__ */ new Map;if(o&&s){assertCanonicalIntegerTimestamp(o.attested_at_ms,`attestation`);let n=i(e.database.db,a.selectFrom(`workspace_generated_bootstrap_hashes`).select([`filename`,`sha256`]).where(`workspace_key`,`=`,t.workspaceKey).orderBy(`filename`,`asc`)).rows;for(let e of n){if(!isSafeWorkspaceAttestationFilename(e.filename)||!p.test(e.sha256))throw Error(`workspace attestation hash row is invalid`);c.set(e.filename,e.sha256)}}let l=o?.version!=null;return{identity:t,setupExists:l,...l&&o?.updated_at!=null?{setupUpdatedAtMs:o.updated_at}:{},setup:{version:1,...o?.bootstrap_seeded_at?{bootstrapSeededAt:o.bootstrap_seeded_at}:{},...o?.setup_completed_at?{setupCompletedAt:o.setup_completed_at}:{}},...s?{attestation:{attestedAtMs:o.attested_at_ms,generatedHashes:c}}:{}}}function readWorkspaceStateSnapshotForDirectoryInDatabase(e){return a(e.database.db,()=>{let{identity:t}=resolveWorkspaceIdentityFromDatabase(e);return readWorkspaceStateSnapshotFromDatabase({identity:t,database:e.database})})}export{readWorkspaceStateSnapshotForDirectoryInDatabase};