| 12345678910111213141516171819202122232425262728293031 |
- #!/usr/bin/env node
- /** 直读 Matrix 事件,确认 content 里真的有 fmode.kind */
- 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 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:`kind-${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;}
- console.log('房间:', room);
- await c.sendEvent(room,'m.room.message',{msgtype:'m.text',body:'跑一条命令看看当前目录,然后告诉我结果。','m.mentions':{user_ids:[agent.agentMxId]}});
- console.log('已提问,等待 100 秒收集事件…');
- await sleep(100_000);
- const j=await(await fetch(`${hs}/_matrix/client/v3/rooms/${encodeURIComponent(room)}/messages?dir=b&limit=60`,{headers:auth})).json();
- let found=0, total=0;
- for(const ev of (j.chunk??[]).reverse()){
- if(ev.type!=='m.room.message'||ev.sender===p1.userMxId) continue;
- total++;
- const k=ev.content?.['fmode.kind'];
- if(k) found++;
- const body=String(ev.content?.body??'').slice(0,60).replace(/\n/g,' ');
- console.log(` [${k ?? '∅'}] ${body}`);
- }
- console.log(`\n汇总:容器消息 ${total} 条,其中带 fmode.kind 的 ${found} 条`);
- c.stopClient();
|