205 lines
6.6 KiB
TypeScript
205 lines
6.6 KiB
TypeScript
|
|
export const GUANGHU_LIVING_SYSTEM_VERSION = 1 as const
|
||
|
|
|
||
|
|
export const GUANGHU_CHANNEL_ROUTES = [
|
||
|
|
'world',
|
||
|
|
'world-login',
|
||
|
|
'zero-core',
|
||
|
|
'fifth-domain',
|
||
|
|
'pufferfish',
|
||
|
|
'eternal-lake-heart',
|
||
|
|
'light-lake',
|
||
|
|
'heartbeat-core',
|
||
|
|
'love-core',
|
||
|
|
'servers',
|
||
|
|
] as const
|
||
|
|
|
||
|
|
export type GuanghuChannelRoute = typeof GUANGHU_CHANNEL_ROUTES[number]
|
||
|
|
export type LivingSceneDepth = 'overview' | 'focused' | 'immersive'
|
||
|
|
export type LivingSceneMotion = 'quiet' | 'responsive' | 'active'
|
||
|
|
export type LivingSceneStarDensity = 'sparse' | 'balanced' | 'rich'
|
||
|
|
export type LivingSceneConnectionEmphasis = 'contextual' | 'active-route' | 'network'
|
||
|
|
export const GUANGHU_LIVING_INTENTS = [
|
||
|
|
'navigate',
|
||
|
|
'open-knowledge',
|
||
|
|
'open-agent-workspace',
|
||
|
|
'open-local-workspace',
|
||
|
|
'apply-theme',
|
||
|
|
] as const
|
||
|
|
export type GuanghuLivingIntent = typeof GUANGHU_LIVING_INTENTS[number]
|
||
|
|
|
||
|
|
export type GuanghuLivingScene = {
|
||
|
|
depth: LivingSceneDepth
|
||
|
|
motion: LivingSceneMotion
|
||
|
|
starDensity: LivingSceneStarDensity
|
||
|
|
connectionEmphasis: LivingSceneConnectionEmphasis
|
||
|
|
}
|
||
|
|
|
||
|
|
export type GuanghuLivingSystemEvent = {
|
||
|
|
eventId: string
|
||
|
|
intent: GuanghuLivingIntent
|
||
|
|
currentRoute: GuanghuChannelRoute
|
||
|
|
requestedRoute: GuanghuChannelRoute
|
||
|
|
appearanceTheme?: string
|
||
|
|
worldOpen: boolean
|
||
|
|
receiptIds: string[]
|
||
|
|
occurredAt: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export type GuanghuLivingSystemPlan = {
|
||
|
|
version: typeof GUANGHU_LIVING_SYSTEM_VERSION
|
||
|
|
eventId: string
|
||
|
|
intent: GuanghuLivingIntent
|
||
|
|
planId: string
|
||
|
|
route: GuanghuChannelRoute
|
||
|
|
requiredTruth: string[]
|
||
|
|
scene: GuanghuLivingScene
|
||
|
|
}
|
||
|
|
|
||
|
|
export type GuanghuLivingSystemExecutionReceipt = {
|
||
|
|
version: typeof GUANGHU_LIVING_SYSTEM_VERSION
|
||
|
|
receiptId: string
|
||
|
|
eventId: string
|
||
|
|
intent: GuanghuLivingIntent
|
||
|
|
planId: string
|
||
|
|
route: GuanghuChannelRoute
|
||
|
|
source: 'server' | 'model' | 'fallback'
|
||
|
|
outcome: 'executed'
|
||
|
|
executedAt: number
|
||
|
|
}
|
||
|
|
|
||
|
|
type CreateLivingSystemEventInput = Omit<GuanghuLivingSystemEvent, 'occurredAt'> & {
|
||
|
|
now?: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export type LivingSystemPlanValidation =
|
||
|
|
| { accepted: true; plan: GuanghuLivingSystemPlan }
|
||
|
|
| { accepted: false; reason: 'event_mismatch' | 'intent_mismatch' | 'route_mismatch' | 'unverified_truth' | 'world_closed' }
|
||
|
|
|
||
|
|
const PROTECTED_ROUTES = new Set<GuanghuChannelRoute>([
|
||
|
|
'fifth-domain',
|
||
|
|
'pufferfish',
|
||
|
|
'eternal-lake-heart',
|
||
|
|
'light-lake',
|
||
|
|
'heartbeat-core',
|
||
|
|
'love-core',
|
||
|
|
'servers',
|
||
|
|
])
|
||
|
|
|
||
|
|
const SCENE_DEPTHS = new Set<LivingSceneDepth>(['overview', 'focused', 'immersive'])
|
||
|
|
const SCENE_MOTIONS = new Set<LivingSceneMotion>(['quiet', 'responsive', 'active'])
|
||
|
|
const STAR_DENSITIES = new Set<LivingSceneStarDensity>(['sparse', 'balanced', 'rich'])
|
||
|
|
const CONNECTION_EMPHASES = new Set<LivingSceneConnectionEmphasis>(['contextual', 'active-route', 'network'])
|
||
|
|
const LIVING_INTENTS = new Set<GuanghuLivingIntent>(GUANGHU_LIVING_INTENTS)
|
||
|
|
|
||
|
|
function isStringArray(value: unknown): value is string[] {
|
||
|
|
return Array.isArray(value) && value.every(item => typeof item === 'string')
|
||
|
|
}
|
||
|
|
|
||
|
|
function isRoute(value: unknown): value is GuanghuChannelRoute {
|
||
|
|
return typeof value === 'string' && (GUANGHU_CHANNEL_ROUTES as readonly string[]).includes(value)
|
||
|
|
}
|
||
|
|
|
||
|
|
function isLivingSystemPlan(value: unknown): value is GuanghuLivingSystemPlan {
|
||
|
|
if (!value || typeof value !== 'object') return false
|
||
|
|
const candidate = value as Partial<GuanghuLivingSystemPlan>
|
||
|
|
const scene = candidate.scene
|
||
|
|
return candidate.version === GUANGHU_LIVING_SYSTEM_VERSION
|
||
|
|
&& typeof candidate.eventId === 'string'
|
||
|
|
&& candidate.eventId.length > 0
|
||
|
|
&& LIVING_INTENTS.has(candidate.intent!)
|
||
|
|
&& typeof candidate.planId === 'string'
|
||
|
|
&& candidate.planId.length > 0
|
||
|
|
&& isRoute(candidate.route)
|
||
|
|
&& isStringArray(candidate.requiredTruth)
|
||
|
|
&& Boolean(scene)
|
||
|
|
&& SCENE_DEPTHS.has(scene!.depth)
|
||
|
|
&& SCENE_MOTIONS.has(scene!.motion)
|
||
|
|
&& STAR_DENSITIES.has(scene!.starDensity)
|
||
|
|
&& CONNECTION_EMPHASES.has(scene!.connectionEmphasis)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createLivingSystemEvent(input: CreateLivingSystemEventInput): GuanghuLivingSystemEvent {
|
||
|
|
return {
|
||
|
|
eventId: input.eventId,
|
||
|
|
intent: input.intent,
|
||
|
|
currentRoute: input.currentRoute,
|
||
|
|
requestedRoute: input.requestedRoute,
|
||
|
|
...(input.appearanceTheme ? { appearanceTheme: input.appearanceTheme } : {}),
|
||
|
|
worldOpen: input.worldOpen,
|
||
|
|
receiptIds: [...input.receiptIds],
|
||
|
|
occurredAt: input.now ?? Date.now(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function parseLivingSystemPlan(output: string): GuanghuLivingSystemPlan | null {
|
||
|
|
try {
|
||
|
|
const parsed: unknown = JSON.parse(output.trim())
|
||
|
|
return isLivingSystemPlan(parsed) ? parsed : null
|
||
|
|
} catch {
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function validateLivingSystemPlan(
|
||
|
|
event: GuanghuLivingSystemEvent,
|
||
|
|
plan: GuanghuLivingSystemPlan | null,
|
||
|
|
): LivingSystemPlanValidation {
|
||
|
|
if (!plan || plan.eventId !== event.eventId) return { accepted: false, reason: 'event_mismatch' }
|
||
|
|
if (plan.intent !== event.intent) return { accepted: false, reason: 'intent_mismatch' }
|
||
|
|
if (plan.route !== event.requestedRoute && plan.route !== 'world-login') {
|
||
|
|
return { accepted: false, reason: 'route_mismatch' }
|
||
|
|
}
|
||
|
|
const suppliedTruth = new Set(event.receiptIds)
|
||
|
|
if (plan.requiredTruth.some(receiptId => !suppliedTruth.has(receiptId))) {
|
||
|
|
return { accepted: false, reason: 'unverified_truth' }
|
||
|
|
}
|
||
|
|
if (!event.worldOpen && PROTECTED_ROUTES.has(plan.route)) {
|
||
|
|
return { accepted: false, reason: 'world_closed' }
|
||
|
|
}
|
||
|
|
return { accepted: true, plan }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createDeterministicLivingSystemPlan(
|
||
|
|
event: GuanghuLivingSystemEvent,
|
||
|
|
): GuanghuLivingSystemPlan {
|
||
|
|
const route = !event.worldOpen && PROTECTED_ROUTES.has(event.requestedRoute)
|
||
|
|
? 'world-login'
|
||
|
|
: event.requestedRoute
|
||
|
|
const immersive = event.worldOpen && route !== 'world' && route !== 'world-login'
|
||
|
|
return {
|
||
|
|
version: GUANGHU_LIVING_SYSTEM_VERSION,
|
||
|
|
eventId: event.eventId,
|
||
|
|
intent: event.intent,
|
||
|
|
planId: `fallback-${event.eventId}`,
|
||
|
|
route,
|
||
|
|
requiredTruth: [],
|
||
|
|
scene: {
|
||
|
|
depth: immersive ? 'immersive' : route === 'world' ? 'overview' : 'focused',
|
||
|
|
motion: route === 'world' ? 'quiet' : 'responsive',
|
||
|
|
starDensity: immersive ? 'rich' : 'balanced',
|
||
|
|
connectionEmphasis: immersive ? 'active-route' : 'contextual',
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createLivingSystemExecutionReceipt({
|
||
|
|
plan,
|
||
|
|
source,
|
||
|
|
now = Date.now(),
|
||
|
|
}: {
|
||
|
|
plan: GuanghuLivingSystemPlan
|
||
|
|
source: GuanghuLivingSystemExecutionReceipt['source']
|
||
|
|
now?: number
|
||
|
|
}): GuanghuLivingSystemExecutionReceipt {
|
||
|
|
return {
|
||
|
|
version: GUANGHU_LIVING_SYSTEM_VERSION,
|
||
|
|
receiptId: `local-execution:${plan.planId}`,
|
||
|
|
eventId: plan.eventId,
|
||
|
|
intent: plan.intent,
|
||
|
|
planId: plan.planId,
|
||
|
|
route: plan.route,
|
||
|
|
source,
|
||
|
|
outcome: 'executed',
|
||
|
|
executedAt: now,
|
||
|
|
}
|
||
|
|
}
|