ksf-account.mjs 2.9 KB

123456789101112131415161718192021222324252627282930
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. import crypto from 'node:crypto';
  4. import { fileURLToPath } from 'node:url';
  5. import { readAgentProfileByKey, readParticipantProfile } from './ksf-identity.mjs';
  6. import { createWorkshopCaller } from './ksf-workshop.mjs';
  7. const self=fileURLToPath(import.meta.url),sha=x=>crypto.createHash('sha256').update(x).digest('hex');
  8. const fail=code=>{throw Object.assign(Error(code),{code});};
  9. export async function runAccountBinding(command,task,{requestId,agentProfile,participantProfile,call}={}){
  10. if(!['account-prepare','account-status'].includes(command)||!task||typeof task!=='object'||Array.isArray(task)||Object.keys(task).some(k=>!['participant','username','displayName'].includes(k)))fail('INVALID_INPUT');
  11. if(!requestId||agentProfile?.role!=='workshop'||!participantProfile||task.participant?.profileKey!==participantProfile.key||Object.keys(task.participant).some(k=>k!=='profileKey'))fail('PARTICIPANT_PROFILE_REQUIRED');
  12. if(participantProfile.account?.platform!=='fmode')fail('ACCOUNT_PLATFORM_MISMATCH');
  13. const preparing=command==='account-prepare';
  14. if(!preparing&&(Object.hasOwn(task,'username')||Object.hasOwn(task,'displayName')))fail('INVALID_INPUT');
  15. if(preparing&&(task.username!==participantProfile.account.accountName||typeof task.displayName!=='string'||!task.displayName.trim()))fail('ACCOUNT_BINDING_CONFLICT');
  16. return call('ksfWorkshopAccount',{requestId,action:preparing?'prepare':'status',participantProfileKey:participantProfile.key,...(preparing?{username:task.username,displayName:task.displayName}:{})});
  17. }
  18. if(process.argv[1]&&path.resolve(process.argv[1])===self){
  19. const [command,...argv]=process.argv.slice(2),flags={};
  20. const base={skill:'ksf-workshop-runtime',command,entry:self,entrySha256:sha(fs.readFileSync(self)),invocationId:crypto.randomUUID(),at:new Date().toISOString()};
  21. try{
  22. for(let i=0;i<argv.length;i+=2){if(!['--input','--agent-profile','--request-id'].includes(argv[i])||!argv[i+1]||flags[argv[i]])fail('USAGE');flags[argv[i]]=argv[i+1];}
  23. if(!path.isAbsolute(flags['--input']||''))fail('INPUT_PATH_MUST_BE_ABSOLUTE');
  24. const task=JSON.parse(fs.readFileSync(flags['--input'],'utf8'));
  25. const result=await runAccountBinding(command,task,{requestId:flags['--request-id'],agentProfile:readAgentProfileByKey(flags['--agent-profile']),participantProfile:readParticipantProfile(task.participant?.profileKey),call:createWorkshopCaller({token:process.env.KSF_SERVICE_TOKEN})});
  26. const output=JSON.stringify({...base,requestId:flags['--request-id'],ok:true,readOnly:command==='account-status',result});
  27. if(process.env.KSF_SERVICE_TOKEN&&output.includes(process.env.KSF_SERVICE_TOKEN))fail('OUTPUT_CONTAINS_SECRET');
  28. console.log(output);
  29. }catch(e){console.error(JSON.stringify({...base,requestId:flags['--request-id'],ok:false,error:{code:/^[A-Z][A-Z0-9_]+$/.test(e.code||'')?e.code:'ACCOUNT_BINDING_FAILED',message:'账号绑定未完成;保留原档案,未输出凭据。'}}));process.exitCode=1;}
  30. }