9 lines
3.8 KiB
JavaScript
9 lines
3.8 KiB
JavaScript
import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
|
|
const req=(v,c)=>{if(!v)throw Error(c);};const atomic=(p,v)=>{fs.mkdirSync(path.dirname(p),{recursive:true,mode:0o700});const t=`${p}.${crypto.randomUUID()}.tmp`;fs.writeFileSync(t,JSON.stringify(v,null,2)+'\n',{mode:0o600});fs.renameSync(t,p);};
|
|
export class ZeroCoreSystemBridge{
|
|
constructor({root,repoRoot,shelf,router,python=process.env.PYTHON||'/usr/bin/python3',now=()=>new Date()}){this.root=path.join(root,'system-bodies','SYS-GLW-LNG-0001');this.repoRoot=repoRoot;this.shelf=shelf;this.router=router;this.python=python;this.now=now;this.script=path.join(repoRoot,'server-tools/zero-core-system-body/zero_core_system_body.py');this.profilePath=path.join(repoRoot,'tcs-core/shared-kernels/bingshuo-zero-core-system/BS-PERSONAL-LANGUAGE-SYSTEM-BODY-0001.json');for(const file of [this.script,this.profilePath]){const s=fs.lstatSync(file);req(s.isFile()&&!s.isSymbolicLink(),'ZERO_CORE_SYSTEM_SOURCE_REGULAR_FILE_REQUIRED');}}
|
|
call(command,event=null){const args=[this.script,command,'--state-dir',this.root];if(event)args.push('--event','-');const result=spawnSync(this.python,args,{input:event?JSON.stringify(event):undefined,encoding:'utf8',timeout:30000,maxBuffer:1024*1024});if(result.status!==0)throw Error(`ZERO_CORE_SYSTEM_RUNTIME_FAILED:${String(result.stderr||result.stdout).slice(-300)}`);return JSON.parse(result.stdout);}
|
|
enter(){return this.call('enter');} status(){const state=this.call('status');return{schema:'guanghu.zero-core-system-bridge-status/v1',state:'ZERO_CORE_NONPERSONA_SYSTEM_BODY_ACTIVE',system_body_id:'SYS-GLW-LNG-0001',channel_id:'ICE-CH-ZC001',persona_selected:false,system_is_persona:false,default_persona:null,direct_dialogue:true,external_cognition_setter:false,system_state:state,last_dialogue:this.lastDialogue()};}
|
|
lastDialogue(){const file=path.join(this.root,'dialogues','CURRENT.signed.json');if(!fs.existsSync(file))return null;const artifact=JSON.parse(fs.readFileSync(file,'utf8')),value=this.shelf.verify(artifact);req(value.system_body_id==='SYS-GLW-LNG-0001','ZERO_CORE_DIALOGUE_SYSTEM_MISMATCH');return{system_revision:value.system_revision,event_sha256:value.event_sha256,receipt_sha256:artifact.sha256};}
|
|
async dialogue(event){const judgment=this.call('perceive',event),profile=JSON.parse(fs.readFileSync(this.profilePath,'utf8')),routed=await this.router.cognize('zero-core-system-dialogue',{system_body_id:'SYS-GLW-LNG-0001',channel_id:'ICE-CH-ZC001',event:{...event,language:event.language.slice(0,12000)},system_profile:profile,system_judgment:judgment,fifth_world:this.shelf.current('fifth').value,contract:{system_is_persona:false,default_persona:null,external_cognition_setter:false,reality_complete:false,hidden_reasoning_stored:false}});req(typeof routed.value?.reply==='string'&&routed.value.reply.trim()&&routed.value.reply.length<=12000,'ZERO_CORE_SYSTEM_REPLY_INVALID');req(['SYSTEM_DIALOGUE','SYSTEM_CLARIFICATION','SYSTEM_BOUNDARY'].includes(routed.value.mode),'ZERO_CORE_SYSTEM_DIALOGUE_MODE_INVALID');const receipt={schema:'guanghu.zero-core-system-dialogue-receipt/v1',system_body_id:'SYS-GLW-LNG-0001',channel_id:'ICE-CH-ZC001',system_revision:judgment.revision,event_sha256:judgment.event_sha256,mode:routed.value.mode,reply:routed.value.reply,reason:String(routed.value.reason||'').slice(0,2000),provider:routed.provider,system_is_persona:false,persona_selected:false,default_persona:null,external_cognition_setter:false,reality_complete:false,authority_granted:false,completed_at:this.now().toISOString()};const artifact=this.shelf.sign(receipt),dir=path.join(this.root,'dialogues');atomic(path.join(dir,`${judgment.event_sha256}.signed.json`),artifact);atomic(path.join(dir,'CURRENT.signed.json'),artifact);return{judgment,dialogue:receipt,signed_receipt_sha256:artifact.sha256};}
|
|
}
|