openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.13 kB
JavaScript
import{Jn as string,Nt as array,Yt as discriminatedUnion,lr as uuid,qn as strictObject,xn as literal}from"./schemas-DuCQi-Qb.mjs";import{l as openClawIdRecoveryPrincipalSchema}from"./tunnel-principal-CmwEB_uz.mjs";import{createHash}from"node:crypto";
//!src/state/github-publication-requester.ts
const requesterSchema=strictObject({version:literal(1),actor:discriminatedUnion(`kind`,[strictObject({kind:literal(`operator`),profileId:string().min(1).max(128)}),strictObject({kind:literal(`system`)})]),scopes:array(string().min(1)),openclawIdPrincipal:openClawIdRecoveryPrincipalSchema.optional(),grant:strictObject({pluginId:string().min(1).max(128),grantId:uuid(),aliasBindingIds:array(uuid())}).nullable(),origin:strictObject({agentId:string().min(1),sessionKey:string().min(1),sessionId:string().min(1),lifecycleRevision:string().min(1).nullable(),runId:string().min(1).optional()}).optional()}).refine(e=>e.actor.kind!==`system`||e.grant===null).refine(e=>!e.openclawIdPrincipal||e.actor.kind===`operator`&&e.openclawIdPrincipal.kind===`member`);const MAX_REQUESTER_BYTES=65536;function deriveGitHubPublicationTaskId(e,t){const n=createHash(`sha256`).update(JSON.stringify([`openclaw-github-publication-task`,e.agentId,e.sessionKey,e.sessionId,e.lifecycleRevision,t])).digest().subarray(0,16);n[6]=(n[6]??0)&15|128;n[8]=(n[8]??0)&63|128;const r=n.toString(`hex`);return`${r.slice(0,8)}-${r.slice(8,12)}-${r.slice(12,16)}-${r.slice(16,20)}-${r.slice(20)}`}function immutableRequester(e){return Object.freeze({version:1,actor:Object.freeze(e.actor),scopes:Object.freeze([...new Set(e.scopes)].toSorted()),...e.openclawIdPrincipal?{openclawIdPrincipal:Object.freeze({...e.openclawIdPrincipal,authority:Object.freeze({...e.openclawIdPrincipal.authority})})}:{},grant:e.grant?Object.freeze({...e.grant,aliasBindingIds:Object.freeze([...new Set(e.grant.aliasBindingIds)].toSorted())}):null,...e.origin?{origin:Object.freeze({agentId:e.origin.agentId,sessionKey:e.origin.sessionKey,sessionId:e.origin.sessionId,lifecycleRevision:e.origin.lifecycleRevision,...e.origin.runId?{runId:e.origin.runId}:{}})}:{}})}function encodeGitHubPublicationRequester(e){const t=JSON.stringify(immutableRequester(e));if(Buffer.byteLength(t,`utf8`)>MAX_REQUESTER_BYTES)throw new Error(`GitHub publication requester is too large.`);return t}function matchesGitHubPublicationRequester(e,t){return encodeGitHubPublicationRequester(e)===encodeGitHubPublicationRequester({...t,openclawIdPrincipal:e.openclawIdPrincipal?t.openclawIdPrincipal:void 0,grant:!e.openclawIdPrincipal&&t.openclawIdPrincipal&&t.grant===null?e.grant:t.grant&&e.grant?{...t.grant,aliasBindingIds:e.grant.aliasBindingIds}:t.grant,...t.origin&&e.origin?{origin:{...t.origin,runId:e.origin.runId}}:{}})}function decodeGitHubPublicationRequester(e){if(!e||Buffer.byteLength(e,`utf8`)>MAX_REQUESTER_BYTES)return;try{const t=requesterSchema.safeParse(JSON.parse(e));return t.success?immutableRequester(t.data):void 0}catch{return}}export{matchesGitHubPublicationRequester as i,deriveGitHubPublicationTaskId as n,encodeGitHubPublicationRequester as r,decodeGitHubPublicationRequester as t};