@sleep2agi/agent-network 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/cli.js +49 -215
- package/dist/src/client.js +2 -2
- package/package.json +2 -2
package/dist/src/client.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
2
|
-
`);
|
|
1
|
+
import{EventEmitter as W}from"events";import{hostname as P}from"os";class Q extends W{url;alias;token;agent;resumeId;heartbeatInterval;reconnectDelay;heartbeatTimer;sseAbort;running=!1;constructor(j){super();if(this.url=j.url.replace(/\/$/,""),this.alias=j.alias,this.token=j.token,this.agent=j.agent||"sdk",this.resumeId=`sdk-${j.alias}-${Date.now().toString(36)}`,this.heartbeatInterval=j.heartbeatInterval??180000,this.reconnectDelay=j.reconnectDelay??3000,j.autoConnect!==!1)this.connect()}log(j){console.log(`[${new Date().toTimeString().slice(0,8)}] [commhub:${this.alias}] ${j}`)}async call(j,w){let q={"Content-Type":"application/json"};if(this.token)q.Authorization=`Bearer ${this.token}`;let B=await(await fetch(`${this.url}/mcp`,{method:"POST",headers:q,body:JSON.stringify({jsonrpc:"2.0",id:Date.now(),method:"tools/call",params:{name:j,arguments:w}})})).json(),D=B?.result?.content?.[0]?.text;return D?JSON.parse(D):B}async connect(){if(this.running)return;this.running=!0,await this.status("idle"),this.log("registered"),this.heartbeatTimer=setInterval(()=>{this.status("idle").catch((j)=>this.log(`heartbeat failed: ${j.message}`))},this.heartbeatInterval),this.connectSSE()}async disconnect(){if(this.running=!1,this.sseAbort?.abort(),this.heartbeatTimer)clearInterval(this.heartbeatTimer);await this.status("offline").catch(()=>{}),this.log("disconnected")}async send(j,w,q="normal"){return this.call("send_task",{alias:j,task:w,priority:q,from_session:this.alias})}async message(j,w){return this.call("send_message",{alias:j,message:w,from_session:this.alias})}async reply(j,w,q="completed"){return this.call("reply",{task_id:j,text:w,status:q})}async status(j,w){return this.call("report_status",{resume_id:this.resumeId,alias:this.alias,status:j,server:P(),hostname:P(),agent:this.agent,project_dir:process.cwd(),...w})}async getAllStatus(){return this.call("get_all_status",{})}async broadcast(j,w){return this.call("broadcast",{message:j,filter_server:w?.server,filter_status:w?.status})}async connectSSE(){let j=encodeURIComponent(this.alias),w=`${this.url}/events/${j}`,q=this.reconnectDelay;while(this.running){try{this.sseAbort=new AbortController;let z={Accept:"text/event-stream"};if(this.token)z.Authorization=`Bearer ${this.token}`;let B=await fetch(w,{headers:z,signal:this.sseAbort.signal});if(!B.ok||!B.body){this.log(`SSE failed: ${B.status}`),await this.sleep(q),q=Math.min(q*1.5,60000);continue}q=this.reconnectDelay;let D=B.body.getReader(),R=new TextDecoder,G="";while(this.running){let{done:T,value:V}=await D.read();if(T)break;G+=R.decode(V,{stream:!0});let L=G.split(`
|
|
2
|
+
`);G=L.pop()||"";for(let M of L){if(!M.startsWith("data: "))continue;try{let F=JSON.parse(M.slice(6));if(F.type==="connected"){this.log("SSE connected"),this.emit("connected");continue}if(F.type==="new_task"||F.type==="new_message"||F.type==="broadcast")await this.processInbox()}catch{}}}}catch(z){if(z.name==="AbortError")break;this.emit("error",z),this.log(`SSE error: ${z.message}`)}if(this.running)this.emit("disconnected"),this.log(`SSE reconnecting in ${q/1000}s...`),await this.sleep(q),q=Math.min(q*1.5,60000)}}async processInbox(){try{let w=(await this.call("get_inbox",{alias:this.alias,limit:10}))?.messages||[];for(let q of w)await this.call("ack_inbox",{alias:this.alias,message_id:q.id}),this.log(`← ${q.from_session}: ${q.content.slice(0,60)}`),this.emit("task",q),this.emit("message",q)}catch(j){this.log(`inbox error: ${j.message}`)}}sleep(j){return new Promise((w)=>setTimeout(w,j))}}var _=Q;export{_ as default,Q as CommHub};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleep2agi/agent-network",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "AI Agent Network — Server + Client + Setup in one package. SSE real-time communication for multi-agent orchestration.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/src/client.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"src/server.ts"
|
|
23
23
|
],
|
|
24
24
|
"scripts": {
|
|
25
|
-
"build": "bun build src/client.ts bin/cli.ts --outdir dist --target node --minify && tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
25
|
+
"build": "bun build src/client.ts --outdir dist/src --target node --minify && bun build bin/cli.ts --outdir dist/bin --target node --minify --external @sleep2agi/commhub-server --external bun:sqlite --external '../../server/*' && tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
26
26
|
"prepublishOnly": "npm run build"
|
|
27
27
|
},
|
|
28
28
|
"keywords": [
|