hololake-system-architecture/product-source/guanghu-knowledge-base/server/domain-runtime-handoff-registry.ts

161 lines
5.9 KiB
TypeScript

import type { DomainNodeType } from './domain-access.js';
const REGISTRY_SCHEMA = 'gh-aios.domain-runtime-handoff-endpoints/v1' as const;
const IDENTIFIER_PATTERN = /^[A-Z0-9][A-Z0-9._:-]{1,159}$/;
const COMMIT_PATTERN = /^[a-f0-9]{40}(?:[a-f0-9]{24})?$/;
const VERSION_PATTERN = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/;
const registeredEndpoints = new WeakSet<object>();
export interface DomainRuntimeHandoffEndpoint {
domainIds: readonly string[];
endpointId: string;
nodeTypes: readonly DomainNodeType[];
status: 'ACTIVE' | 'REVOKED';
url: string;
}
export interface DomainRuntimeHandoffEndpointRegistrySource {
repositoryId: 'REPO-012';
sourceCommit: string;
sourceUrl: string;
}
export interface DomainRuntimeHandoffEndpointRegistry {
endpoints: readonly DomainRuntimeHandoffEndpoint[];
registryId: 'GH-AIOS-DOMAIN-RUNTIME-HANDOFF-ENDPOINTS-001';
schema: typeof REGISTRY_SCHEMA;
source: Readonly<DomainRuntimeHandoffEndpointRegistrySource>;
state: 'CURRENT';
version: string;
}
export interface DomainRuntimeHandoffEndpointLookup {
domainId: string;
nodeType: DomainNodeType;
}
function invalidRegistry(): never {
throw new Error('domain_runtime_handoff_endpoint_registry_invalid');
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function hasExactKeys(record: Record<string, unknown>, keys: string[]): boolean {
const expected = [...keys].sort();
const actual = Object.keys(record).sort();
return actual.length === expected.length && actual.every((key, index) => key === expected[index]);
}
function isIdentifier(value: unknown): value is string {
return typeof value === 'string' && IDENTIFIER_PATTERN.test(value);
}
function parseEndpointUrl(value: unknown): string {
if (typeof value !== 'string') invalidRegistry();
let url: URL;
try {
url = new URL(value);
} catch {
invalidRegistry();
}
if (url.protocol !== 'https:'
|| url.hostname !== 'guanghulab.com'
|| url.port
|| url.username
|| url.password
|| url.search
|| url.hash
|| !url.pathname.startsWith('/api/ai/v1/')) invalidRegistry();
return url.toString();
}
function parseEndpoint(input: unknown): DomainRuntimeHandoffEndpoint {
if (!isRecord(input)
|| !hasExactKeys(input, ['domainIds', 'endpointId', 'nodeTypes', 'status', 'url'])
|| !isIdentifier(input.endpointId)
|| (input.status !== 'ACTIVE' && input.status !== 'REVOKED')
|| !Array.isArray(input.domainIds)
|| input.domainIds.length === 0
|| input.domainIds.some(domainId => !isIdentifier(domainId))
|| new Set(input.domainIds).size !== input.domainIds.length
|| !Array.isArray(input.nodeTypes)
|| input.nodeTypes.length === 0
|| input.nodeTypes.some(nodeType => nodeType !== 'local-terminal' && nodeType !== 'cloud-resident')
|| new Set(input.nodeTypes).size !== input.nodeTypes.length) invalidRegistry();
const endpoint = Object.freeze({
domainIds: Object.freeze([...input.domainIds] as string[]),
endpointId: input.endpointId,
nodeTypes: Object.freeze([...input.nodeTypes] as DomainNodeType[]),
status: input.status,
url: parseEndpointUrl(input.url),
});
registeredEndpoints.add(endpoint);
return endpoint;
}
function parseSource(
source: DomainRuntimeHandoffEndpointRegistrySource,
): Readonly<DomainRuntimeHandoffEndpointRegistrySource> {
if (!isRecord(source)
|| !hasExactKeys(source, ['repositoryId', 'sourceCommit', 'sourceUrl'])
|| source.repositoryId !== 'REPO-012'
|| typeof source.sourceCommit !== 'string'
|| !COMMIT_PATTERN.test(source.sourceCommit)) invalidRegistry();
const expected = `https://guanghulab.com/code/bingshuo/guanghu-ice-heart/raw/commit/${source.sourceCommit}/routing/domain-runtime-handoff-endpoints.json`;
if (source.sourceUrl !== expected) invalidRegistry();
return Object.freeze({ ...source });
}
export function parseDomainRuntimeHandoffEndpointRegistry(
input: unknown,
source: DomainRuntimeHandoffEndpointRegistrySource,
): DomainRuntimeHandoffEndpointRegistry {
if (!isRecord(input)
|| !hasExactKeys(input, ['endpoints', 'registryId', 'schema', 'state', 'version'])
|| input.registryId !== 'GH-AIOS-DOMAIN-RUNTIME-HANDOFF-ENDPOINTS-001'
|| input.schema !== REGISTRY_SCHEMA
|| input.state !== 'CURRENT'
|| typeof input.version !== 'string'
|| !VERSION_PATTERN.test(input.version)
|| !Array.isArray(input.endpoints)) invalidRegistry();
const endpoints = input.endpoints.map(parseEndpoint);
if (new Set(endpoints.map(endpoint => endpoint.endpointId)).size !== endpoints.length) invalidRegistry();
const activeScopes = new Set<string>();
for (const endpoint of endpoints.filter(candidate => candidate.status === 'ACTIVE')) {
for (const domainId of endpoint.domainIds) {
for (const nodeType of endpoint.nodeTypes) {
const scope = `${domainId}\0${nodeType}`;
if (activeScopes.has(scope)) invalidRegistry();
activeScopes.add(scope);
}
}
}
return Object.freeze({
endpoints: Object.freeze(endpoints),
registryId: input.registryId,
schema: input.schema,
source: parseSource(source),
state: input.state,
version: input.version,
});
}
export function resolveDomainRuntimeHandoffEndpoint(
registry: DomainRuntimeHandoffEndpointRegistry,
lookup: DomainRuntimeHandoffEndpointLookup,
): DomainRuntimeHandoffEndpoint | null {
const endpoint = registry.endpoints.find(candidate => candidate.status === 'ACTIVE'
&& candidate.domainIds.includes(lookup.domainId)
&& candidate.nodeTypes.includes(lookup.nodeType));
if (!endpoint) return null;
return assertRegisteredDomainRuntimeHandoffEndpoint(endpoint);
}
export function assertRegisteredDomainRuntimeHandoffEndpoint(
endpoint: DomainRuntimeHandoffEndpoint,
): DomainRuntimeHandoffEndpoint {
if (!registeredEndpoints.has(endpoint)) throw new Error('domain_runtime_handoff_endpoint_unregistered');
return endpoint;
}