#!/usr/bin/env node import { readFileSync } from 'node:fs'; import { homedir } from 'node:os'; import { join } from 'node:path'; const F='https://server.fmode.cn/api/functions', T='agent-node-lj-test'; const sleep=(ms)=>new Promise(r=>setTimeout(r,ms)); const t=JSON.parse(readFileSync(join(homedir(),'.fmode/config/user.json'),'utf8'))?.sessionToken; async function prov(id){const r=await fetch(F,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:'RP5Ht55NeX',token:t,params:{agentId:id}})});const j=await r.json();const d=j.result??j;if(!d?.accessToken)throw new Error('provision 失败');return d;} const QS=JSON.parse(process.argv[2]||'[]'); const p1=await prov(T), agent=await prov(T); const mx=await import('matrix-js-sdk'); const c=mx.createClient({baseUrl:p1.homeserver,accessToken:p1.accessToken,userId:p1.userMxId}); const room=(await c.createRoom({invite:[agent.agentMxId],name:`seq-${Date.now()}`,preset:mx.Preset.TrustedPrivateChat})).room_id; const hs=p1.homeserver.replace(/\/$/,''), auth={Authorization:`Bearer ${p1.accessToken}`}; for(let i=0;i<30;i++){await sleep(3000);const j=await(await fetch(`${hs}/_matrix/client/v3/rooms/${encodeURIComponent(room)}/joined_members`,{headers:auth})).json().catch(()=>({}));if((j.joined??{})[agent.agentMxId])break;} const seen=new Set(); for(const q of QS){ console.log(`\n===== 问 =====\n${q}\n`); const s=await c.sendEvent(room,'m.room.message',{msgtype:'m.text',body:q,'m.mentions':{user_ids:[agent.agentMxId]}}); seen.add(s.event_id); let buf='',lastNew=Date.now();const stop=Date.now()+420_000; while(Date.now()30000)break;} console.log(buf.trim()||'(无回复)'); } console.log('\n房间:',room);c.stopClient();