| 123456789101112131415161718192021222324252627282930 |
- import fs from 'node:fs';
- import path from 'node:path';
- import crypto from 'node:crypto';
- import { fileURLToPath } from 'node:url';
- import { readAgentProfileByKey, readParticipantProfile } from './ksf-identity.mjs';
- import { createWorkshopCaller } from './ksf-workshop.mjs';
- const self=fileURLToPath(import.meta.url),sha=x=>crypto.createHash('sha256').update(x).digest('hex');
- const fail=code=>{throw Object.assign(Error(code),{code});};
- export async function runAccountBinding(command,task,{requestId,agentProfile,participantProfile,call}={}){
- 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');
- if(!requestId||agentProfile?.role!=='workshop'||!participantProfile||task.participant?.profileKey!==participantProfile.key||Object.keys(task.participant).some(k=>k!=='profileKey'))fail('PARTICIPANT_PROFILE_REQUIRED');
- if(participantProfile.account?.platform!=='fmode')fail('ACCOUNT_PLATFORM_MISMATCH');
- const preparing=command==='account-prepare';
- if(!preparing&&(Object.hasOwn(task,'username')||Object.hasOwn(task,'displayName')))fail('INVALID_INPUT');
- if(preparing&&(task.username!==participantProfile.account.accountName||typeof task.displayName!=='string'||!task.displayName.trim()))fail('ACCOUNT_BINDING_CONFLICT');
- return call('ksfWorkshopAccount',{requestId,action:preparing?'prepare':'status',participantProfileKey:participantProfile.key,...(preparing?{username:task.username,displayName:task.displayName}:{})});
- }
- if(process.argv[1]&&path.resolve(process.argv[1])===self){
- const [command,...argv]=process.argv.slice(2),flags={};
- const base={skill:'ksf-workshop-runtime',command,entry:self,entrySha256:sha(fs.readFileSync(self)),invocationId:crypto.randomUUID(),at:new Date().toISOString()};
- try{
- 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];}
- if(!path.isAbsolute(flags['--input']||''))fail('INPUT_PATH_MUST_BE_ABSOLUTE');
- const task=JSON.parse(fs.readFileSync(flags['--input'],'utf8'));
- 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})});
- const output=JSON.stringify({...base,requestId:flags['--request-id'],ok:true,readOnly:command==='account-status',result});
- if(process.env.KSF_SERVICE_TOKEN&&output.includes(process.env.KSF_SERVICE_TOKEN))fail('OUTPUT_CONTAINS_SECRET');
- console.log(output);
- }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;}
- }
|