@sleep2agi/agent-network 0.0.40 → 0.0.42
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 +29 -29
- package/package.json +1 -1
- package/src/node-server.ts +1 -1
package/dist/bin/cli.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var QB=Object.defineProperty;var WB=(Q)=>Q;function XB(Q,W){this[Q]=WB.bind(null,W)}var YB=(Q,W)=>{for(var B in W)QB(Q,B,{get:W[B],enumerable:!0,configurable:!0,set:XB.bind(W,B)})};var ZB=(Q,W)=>()=>(Q&&(W=Q(Q=0)),W);var g={};YB(g,{default:()=>zB,CommHub:()=>D});import{EventEmitter as $B}from"events";import{hostname as h}from"os";var D,zB;var f=ZB(()=>{D=class D extends $B{url;alias;token;agent;resumeId;heartbeatInterval;reconnectDelay;heartbeatTimer;sseAbort;running=!1;constructor(Q){super();if(this.url=Q.url.replace(/\/$/,""),this.alias=Q.alias,this.token=Q.token,this.agent=Q.agent||"sdk",this.resumeId=`sdk-${Q.alias}-${Date.now().toString(36)}`,this.heartbeatInterval=Q.heartbeatInterval??180000,this.reconnectDelay=Q.reconnectDelay??3000,Q.autoConnect!==!1)this.connect()}log(Q){console.log(`[${new Date().toTimeString().slice(0,8)}] [commhub:${this.alias}] ${Q}`)}async call(Q,W){let B={"Content-Type":"application/json",Accept:"application/json, text/event-stream"};if(this.token)B.Authorization=`Bearer ${this.token}`;let
|
|
3
|
-
`);Z=K.pop()||"";for(let
|
|
4
|
-
`)}function m(){let Q=k();if(
|
|
5
|
-
`)}function A(Q){let W=L(j(),Q,"config.json");if(!
|
|
6
|
-
`)}function t(){let Q=j();if(!
|
|
2
|
+
var QB=Object.defineProperty;var WB=(Q)=>Q;function XB(Q,W){this[Q]=WB.bind(null,W)}var YB=(Q,W)=>{for(var B in W)QB(Q,B,{get:W[B],enumerable:!0,configurable:!0,set:XB.bind(W,B)})};var ZB=(Q,W)=>()=>(Q&&(W=Q(Q=0)),W);var g={};YB(g,{default:()=>zB,CommHub:()=>D});import{EventEmitter as $B}from"events";import{hostname as h}from"os";var D,zB;var f=ZB(()=>{D=class D extends $B{url;alias;token;agent;resumeId;heartbeatInterval;reconnectDelay;heartbeatTimer;sseAbort;running=!1;constructor(Q){super();if(this.url=Q.url.replace(/\/$/,""),this.alias=Q.alias,this.token=Q.token,this.agent=Q.agent||"sdk",this.resumeId=`sdk-${Q.alias}-${Date.now().toString(36)}`,this.heartbeatInterval=Q.heartbeatInterval??180000,this.reconnectDelay=Q.reconnectDelay??3000,Q.autoConnect!==!1)this.connect()}log(Q){console.log(`[${new Date().toTimeString().slice(0,8)}] [commhub:${this.alias}] ${Q}`)}async call(Q,W){let B={"Content-Type":"application/json",Accept:"application/json, text/event-stream"};if(this.token)B.Authorization=`Bearer ${this.token}`;let z=await(await fetch(`${this.url}/mcp`,{method:"POST",headers:B,body:JSON.stringify({jsonrpc:"2.0",id:Date.now(),method:"tools/call",params:{name:Q,arguments:W}})})).text(),Y=z.match(/data: (.+)/),U=Y?JSON.parse(Y[1]):JSON.parse(z),Z=U?.result?.content?.[0]?.text;return Z?JSON.parse(Z):U}async connect(){if(this.running)return;this.running=!0,await this.status("idle"),this.log("registered"),this.heartbeatTimer=setInterval(()=>{this.status("idle").catch((Q)=>this.log(`heartbeat failed: ${Q.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(Q,W,B="normal"){return this.call("send_task",{alias:Q,task:W,priority:B,from_session:this.alias})}async message(Q,W){return this.call("send_message",{alias:Q,message:W,from_session:this.alias})}async reply(Q,W,B="completed"){return this.call("reply",{task_id:Q,text:W,status:B})}async status(Q,W){return this.call("report_status",{resume_id:this.resumeId,alias:this.alias,status:Q,server:h(),hostname:h(),agent:this.agent,project_dir:process.cwd(),...W})}async getAllStatus(){return this.call("get_all_status",{})}async broadcast(Q,W){return this.call("broadcast",{message:Q,filter_server:W?.server,filter_status:W?.status})}async connectSSE(){let Q=encodeURIComponent(this.alias),W=`${this.url}/events/${Q}`,B=this.reconnectDelay;while(this.running){try{this.sseAbort=new AbortController;let X={Accept:"text/event-stream"};if(this.token)X.Authorization=`Bearer ${this.token}`;let z=await fetch(W,{headers:X,signal:this.sseAbort.signal});if(!z.ok||!z.body){this.log(`SSE failed: ${z.status}`),await this.sleep(B),B=Math.min(B*1.5,60000);continue}B=this.reconnectDelay;let Y=z.body.getReader(),U=new TextDecoder,Z="";while(this.running){let{done:$,value:N}=await Y.read();if($)break;Z+=U.decode(N,{stream:!0});let K=Z.split(`
|
|
3
|
+
`);Z=K.pop()||"";for(let T of K){if(!T.startsWith("data: "))continue;try{let R=JSON.parse(T.slice(6));if(R.type==="connected"){this.log("SSE connected"),this.emit("connected");continue}if(R.type==="new_task"||R.type==="new_message"||R.type==="broadcast")await this.processInbox()}catch{}}}}catch(X){if(X.name==="AbortError")break;this.emit("error",X),this.log(`SSE error: ${X.message}`)}if(this.running)this.emit("disconnected"),this.log(`SSE reconnecting in ${B/1000}s...`),await this.sleep(B),B=Math.min(B*1.5,60000)}}async processInbox(){try{let W=(await this.call("get_inbox",{alias:this.alias,limit:10}))?.messages||[];for(let B of W)await this.call("ack_inbox",{alias:this.alias,message_id:B.id}),this.log(`← ${B.from_session}: ${B.content.slice(0,60)}`),this.emit("task",B),this.emit("message",B)}catch(Q){this.log(`inbox error: ${Q.message}`)}}sleep(Q){return new Promise((W)=>setTimeout(W,Q))}};zB=D});import{readFileSync as O,writeFileSync as _,existsSync as V,mkdirSync as M,readdirSync as v,statSync as w}from"fs";import{join as L}from"path";import{spawn as C,execSync as l}from"child_process";import{createInterface as KB}from"readline";var E=process.argv.slice(2),P=E[0],I=process.env.HOME||process.env.USERPROFILE||"~";function n(){return L(I,".anet","config.json")}function k(){return L(I,".anet","server","config.json")}function j(){return L(process.cwd(),".anet","nodes")}function a(){return G().token||process.env.COMMHUB_TOKEN||H().token||""}function b(Q){let W=Q||a();return W?{Authorization:`Bearer ${W}`}:{}}function H(){let Q=n();if(V(Q))try{return JSON.parse(O(Q,"utf-8"))}catch{}return{}}function r(Q){let W=L(I,".anet");M(W,{recursive:!0}),_(L(W,"config.json"),JSON.stringify(Q,null,2)+`
|
|
4
|
+
`)}function m(){let Q=k();if(V(Q))try{return JSON.parse(O(Q,"utf-8"))}catch{}return{}}function p(Q){let W=L(I,".anet","server");M(W,{recursive:!0}),_(L(W,"config.json"),JSON.stringify(Q,null,2)+`
|
|
5
|
+
`)}function A(Q){let W=L(j(),Q,"config.json");if(!V(W))return null;try{let B=JSON.parse(O(W,"utf-8")),X=H();return{...B,hub:B.hub||X.hub||"",token:B.token||X.token||"",env:{...B.env},flags:{...B.flags}}}catch{return null}}function y(Q,W){let B=L(j(),Q);M(B,{recursive:!0}),_(L(B,"config.json"),JSON.stringify(W,null,2)+`
|
|
6
|
+
`)}function t(){let Q=j();if(!V(Q))return[];return v(Q).filter((W)=>V(L(Q,W,"config.json")))}function G(){let Q={_channels:[],_envs:[]};for(let W=0;W<E.length;W++){if(E[W]==="--channel"&&E[W+1]){Q._channels.push(E[++W]);continue}if(E[W]==="--env"&&E[W+1]){Q._envs.push(E[++W]);continue}if(E[W].startsWith("--")&&E[W+1]&&!E[W+1].startsWith("--"))Q[E[W].slice(2)]=E[++W]}return Q}function c(){console.log(`
|
|
7
7
|
anet — AI Agent Network CLI
|
|
8
8
|
|
|
9
9
|
anet init Configure hub URL (global, once)
|
|
@@ -23,12 +23,12 @@ Quick start:
|
|
|
23
23
|
anet init --hub http://IP:9200
|
|
24
24
|
anet start 指挥室 # Claude Code Agent
|
|
25
25
|
anet start 小明 # MiniMax Agent (runtime: agent-sdk)
|
|
26
|
-
`)}async function NB(){let Q=G(),W=Q.hub;if(!W)W=await J("CommHub URL (e.g. http://YOUR_IP:9200)"),i();if(!W)console.error("Error: hub URL required"),process.exit(1);W=W.replace(/\/+$/,"");let B=Q.token||"";try{let Y=await(await fetch(`${W}/health`,{headers:B?{Authorization:`Bearer ${B}`}:{}})).json();console.log(`✅ CommHub v${Y.version} — ${Y.sessions} sessions, ${Y.sse_connections} SSE`)}catch(
|
|
27
|
-
Saved to ${n()}`),console.log("Next: anet init project")}async function UB(){let Q=H(),W=Q.hub;if(!W)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let B=L(process.cwd(),".anet");M(B,{recursive:!0});let X=L(B,"node-server.ts");if(!
|
|
26
|
+
`)}async function NB(){let Q=G(),W=Q.hub;if(!W)W=await J("CommHub URL (e.g. http://YOUR_IP:9200)"),i();if(!W)console.error("Error: hub URL required"),process.exit(1);W=W.replace(/\/+$/,"");let B=Q.token||"";try{let Y=await(await fetch(`${W}/health`,{headers:B?{Authorization:`Bearer ${B}`}:{}})).json();console.log(`✅ CommHub v${Y.version} — ${Y.sessions} sessions, ${Y.sse_connections} SSE`)}catch(z){console.error(`❌ Cannot reach ${W}: ${z.message}`),process.exit(1)}let X=H();if(X.hub=W,B)X.token=B;r(X),console.log(`
|
|
27
|
+
Saved to ${n()}`),console.log("Next: anet init project")}async function UB(){let Q=H(),W=Q.hub;if(!W)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let B=L(process.cwd(),".anet");M(B,{recursive:!0});let X=L(B,"node-server.ts");if(!V(X)){let T=[L(new URL(".",import.meta.url).pathname,"..","..","src","node-server.ts"),L(new URL(".",import.meta.url).pathname,"..","src","node-server.ts"),L(process.argv[1],"..","..","src","node-server.ts")],R=!1;for(let q of T)if(V(q)){_(X,O(q,"utf-8")),console.log(" ✅ .anet/node-server.ts"),R=!0;break}if(!R)console.log(" ❌ Cannot find node-server.ts"),console.log(" Fix: cp $(npm root -g)/@sleep2agi/agent-network/src/node-server.ts .anet/node-server.ts")}else console.log(" Channel plugin: exists");let z=L(B,"package.json");if(!V(z)){_(z,JSON.stringify({private:!0,dependencies:{"@modelcontextprotocol/sdk":"^1.12.0"}},null,2)+`
|
|
28
28
|
`);try{l("bun install",{cwd:B,stdio:"pipe"}),console.log(" ✅ Dependencies installed")}catch{console.log(" ⚠️ Run: cd .anet && bun install")}}let Y=L(B,".env"),U=Q.token||"",Z=`COMMHUB_URL=${W}
|
|
29
29
|
`;if(U)Z+=`COMMHUB_TOKEN=${U}
|
|
30
|
-
`;_(Y,Z),console.log(`CommHub URL: ${W}${U?" (with token)":""}`);let $=L(process.cwd(),".mcp.json"),
|
|
31
|
-
`),console.log(".mcp.json: commhub → .anet/node-server.ts");else console.log(".mcp.json: commhub already set");let K=L(process.cwd(),"CLAUDE.md");if(!
|
|
30
|
+
`;_(Y,Z),console.log(`CommHub URL: ${W}${U?" (with token)":""}`);let $=L(process.cwd(),".mcp.json"),N={};if(V($))try{N=JSON.parse(O($,"utf-8"))}catch{}if(!N.mcpServers?.commhub)N.mcpServers=N.mcpServers||{},N.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},_($,JSON.stringify(N,null,2)+`
|
|
31
|
+
`),console.log(".mcp.json: commhub → .anet/node-server.ts");else console.log(".mcp.json: commhub already set");let K=L(process.cwd(),"CLAUDE.md");if(!V(K))_(K,`# Agent Network (CommHub)
|
|
32
32
|
|
|
33
33
|
## 通信方式
|
|
34
34
|
|
|
@@ -67,31 +67,31 @@ commhub_get_all_status()
|
|
|
67
67
|
- 回复指挥室用 commhub_send_task(不是 commhub_reply,reply 不推送)
|
|
68
68
|
- 不要猜 alias,用 get_all_status 查
|
|
69
69
|
`),console.log("CLAUDE.md: created");else console.log("CLAUDE.md: already exists");console.log(`
|
|
70
|
-
✅ Project ready. Next: anet init profile <id> --alias <名字> --channel server:commhub`)}function LB(){let Q=E[2];if(!Q)console.error("Usage: anet init profile <id> --alias <名字> [--channel ...] [--env ...]"),process.exit(1);let W=H(),B=G(),X=B.alias||Q,
|
|
70
|
+
✅ Project ready. Next: anet init profile <id> --alias <名字> --channel server:commhub`)}function LB(){let Q=E[2];if(!Q)console.error("Usage: anet init profile <id> --alias <名字> [--channel ...] [--env ...]"),process.exit(1);let W=H(),B=G(),X=B.alias||Q,z=B.hub||W.hub;if(!z)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let Y={};for(let T of B._envs){let R=T.indexOf("=");if(R>0)Y[T.slice(0,R)]=T.slice(R+1)}let U=B.runtime||"claude-code",Z={anet_version:"0.0.24",...B.name?{name:B.name}:{},runtime:U,alias:X,hub:z,...B.model?{model:B.model}:{},...B.tools?{tools:B.tools.split(",").map((T)=>T.trim())}:{},channels:B._channels.length>0?B._channels:U==="claude-code"?["server:commhub"]:[],env:Y,flags:{dangerouslySkipPermissions:!0,...U==="claude-code"?{teammateMode:B["teammate-mode"]||"in-process"}:{},...B["max-turns"]?{maxTurns:parseInt(B["max-turns"])}:{}},...B.resume?{resume:B.resume}:{},...B["resume-alias"]?{resumeAlias:B["resume-alias"]}:{}},$=L(I,".claude","channels","commhub"),N=process.cwd().replace(/\//g,"-"),K=L($,N);if(M(K,{recursive:!0}),_(L(K,".env"),`COMMHUB_ALIAS=${X}
|
|
71
71
|
`),y(Q,Z),console.log(`
|
|
72
72
|
✅ Profile "${Q}" saved`),console.log(` alias: ${X}`),console.log(` channels: ${Z.channels.join(", ")}`),Object.keys(Y).length)console.log(` env: ${Object.keys(Y).join(", ")}`);console.log(`
|
|
73
|
-
Start: anet start ${Q}`)}var F=null;function RB(){if(!F)F=KB({input:process.stdin,output:process.stdout});return F}function i(){if(F)F.close(),F=null}function J(Q,W){let B=W?` [${W}]`:"";return new Promise((X)=>{RB().question(`${Q}${B}: `,(
|
|
73
|
+
Start: anet start ${Q}`)}var F=null;function RB(){if(!F)F=KB({input:process.stdin,output:process.stdout});return F}function i(){if(F)F.close(),F=null}function J(Q,W){let B=W?` [${W}]`:"";return new Promise((X)=>{RB().question(`${Q}${B}: `,(z)=>{X(z.trim()||W||"")})})}async function EB(Q){let W=H();console.log(`
|
|
74
74
|
Profile "${Q}" not found. Let's create it:
|
|
75
|
-
`);let B=await J("Runtime (claude-code / agent-sdk)","claude-code"),X=await J("Alias",Q),
|
|
76
|
-
Run 'anet init' first to configure hub URL`),process.exit(1);let
|
|
75
|
+
`);let B=await J("Runtime (claude-code / agent-sdk)","claude-code"),X=await J("Alias",Q),z,Y=[],U=[],Z="";if(B==="agent-sdk")z=await J("Model","MiniMax-M2.7"),Y=(await J("Tools (comma-separated)","Read,Bash,Grep")).split(",").map((q)=>q.trim()).filter(Boolean);else U=(await J("Channels (comma-separated)","server:commhub")).split(",").map((q)=>q.trim()).filter(Boolean),Z=await J("Teammate mode","in-process");let $=await J("Extra env (K=V, comma-separated, empty to skip)"),N={};if($)for(let R of $.split(",")){let q=R.trim().indexOf("=");if(q>0)N[R.trim().slice(0,q)]=R.trim().slice(q+1)}let K=W.hub;if(!K)console.error(`
|
|
76
|
+
Run 'anet init' first to configure hub URL`),process.exit(1);let T={anet_version:"0.0.23",alias:X,hub:K,runtime:B,...z?{model:z}:{},...Y.length?{tools:Y}:{},channels:U,env:N,flags:{dangerouslySkipPermissions:!0,...Z?{teammateMode:Z}:{}}};return y(Q,T),i(),console.log(`
|
|
77
77
|
✅ Profile "${Q}" saved
|
|
78
|
-
`),
|
|
79
|
-
`);try{l("bun install",{cwd:X,stdio:"pipe"})}catch{}}let
|
|
80
|
-
`;if(
|
|
81
|
-
`;_(
|
|
78
|
+
`),T}function TB(Q){if((Q.runtime||"claude-code")!=="claude-code")return;if(!Q.channels?.some((K)=>K.includes("commhub")))return;let W=L(process.cwd(),".mcp.json"),B={};if(V(W))try{B=JSON.parse(O(W,"utf-8"))}catch{}let X=L(process.cwd(),".anet"),z=L(X,"node-server.ts"),Y=[L(new URL(".",import.meta.url).pathname,"..","..","src","node-server.ts"),L(new URL(".",import.meta.url).pathname,"..","src","node-server.ts"),L(process.argv[1],"..","..","src","node-server.ts")];for(let K of Y)if(V(K)){M(X,{recursive:!0});let T=O(K,"utf-8"),R=V(z)?O(z,"utf-8"):"";if(T!==R)_(z,T),console.log("[anet] Updated .anet/node-server.ts");break}if(B.mcpServers?.commhub)return;let U=L(X,"package.json");if(!V(U)){M(X,{recursive:!0}),_(U,JSON.stringify({private:!0,dependencies:{"@modelcontextprotocol/sdk":"^1.12.0"}},null,2)+`
|
|
79
|
+
`);try{l("bun install",{cwd:X,stdio:"pipe"})}catch{}}let Z=L(X,".env"),$=Q.token||"",N=`COMMHUB_URL=${Q.hub||"http://127.0.0.1:9200"}
|
|
80
|
+
`;if($)N+=`COMMHUB_TOKEN=${$}
|
|
81
|
+
`;_(Z,N),B.mcpServers=B.mcpServers||{},B.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},_(W,JSON.stringify(B,null,2)+`
|
|
82
82
|
`),console.log("[anet] .mcp.json: added commhub channel server")}async function o(Q,W){let B=A(Q);if(!B)B=await EB(Q);let X=B.runtime||"claude-code";console.log(`[anet] ${W==="start"?"Starting new":"Resuming"} "${Q}" (${B.alias}) [${X}]...
|
|
83
|
-
`),TB(B);let Y=B.token||"";if(X==="agent-sdk"){let U=["@sleep2agi/agent-node","--alias",B.alias,"--hub",B.hub];if(B.model)U.push("--model",B.model);if(B.tools?.length)U.push("--tools",B.tools.join(","));if(B.flags?.maxTurns)U.push("--max-turns",String(B.flags.maxTurns));let Z={...process.env,...Y?{COMMHUB_TOKEN:Y}:{}};for(let[
|
|
84
|
-
`),console.log(`Quick setup: anet resume ${Q} --session <session-id>`),console.log(`Or create: anet init profile ${Q} --alias ${Q} --resume <session-id>`),process.exit(1);if(!
|
|
83
|
+
`),TB(B);let Y=B.token||"";if(X==="agent-sdk"){let U=["@sleep2agi/agent-node","--alias",B.alias,"--hub",B.hub];if(B.model)U.push("--model",B.model);if(B.tools?.length)U.push("--tools",B.tools.join(","));if(B.flags?.maxTurns)U.push("--max-turns",String(B.flags.maxTurns));let Z={...process.env,...Y?{COMMHUB_TOKEN:Y}:{}};for(let[N,K]of Object.entries(B.env))Z[N]=K.replace(/^~/,I);C("npx",U,{env:Z,stdio:"inherit",shell:!0}).on("exit",(N)=>process.exit(N||0))}else{let U={...process.env,COMMHUB_ALIAS:B.alias,...Y?{COMMHUB_TOKEN:Y}:{}};for(let[N,K]of Object.entries(B.env))U[N]=K.replace(/^~/,I);let Z=[];if(B.flags.dangerouslySkipPermissions)Z.push("--dangerously-skip-permissions");for(let N of B.channels)if(N.startsWith("server:"))Z.push("--dangerously-load-development-channels",N);else Z.push("--channels",N);if(B.flags.teammateMode)Z.push("--teammate-mode",B.flags.teammateMode);if(W==="resume"){let N=B.resume||B.resumeAlias||B.name||B.alias;Z.push("--resume",N)}Z.push("-n",B.name||B.alias),C("claude",Z,{env:U,stdio:"inherit",shell:!0}).on("exit",(N)=>process.exit(N||0))}}async function d(){let Q=E[1];if(!Q){s("start");return}await o(Q,"start")}async function VB(){let Q=E[1];if(!Q){s("resume");return}let W=A(Q);if(!W){let B=G(),X=H(),z=B.hub||X.hub,Y=B.session;if(!Y)console.log(`Profile "${Q}" not found.
|
|
84
|
+
`),console.log(`Quick setup: anet resume ${Q} --session <session-id>`),console.log(`Or create: anet init profile ${Q} --alias ${Q} --resume <session-id>`),process.exit(1);if(!z)console.error("Run 'anet init' first"),process.exit(1);W={runtime:"claude-code",alias:B.alias||Q,hub:z,channels:["server:commhub"],env:{},flags:{dangerouslySkipPermissions:!0,teammateMode:"in-process"},resume:Y},y(Q,W),console.log(`[anet] Created .anet/nodes/${Q}/config.json (resume: ${Y.slice(0,8)}...)
|
|
85
85
|
`)}await o(Q,"resume")}function s(Q){let W=t();if(W.length===0){console.log("No profiles. Run: anet init profile <id> --alias <名字>");return}console.log(`
|
|
86
86
|
Profiles:
|
|
87
87
|
`);for(let B of W){let X=A(B);console.log(` ${B}${X?.name?` (${X.name})`:""} → ${X?.alias} [${X?.channels.join(", ")}]`)}console.log(`
|
|
88
88
|
anet ${Q} <id>
|
|
89
89
|
`)}async function _B(){let Q=t();if(Q.length>0){console.log(`
|
|
90
90
|
Profiles:
|
|
91
|
-
`);for(let Z of Q){let $=A(Z);console.log(` ${Z}${$?.name?` (${$.name})`:""} → ${$?.alias} [${$?.channels.join(", ")}]`)}console.log()}let W=process.cwd(),B=L(
|
|
92
|
-
`);return}let
|
|
93
|
-
`),console.log(" SESSION PID NETWORK"),console.log(" ──────────────────── ─────── ─────────────────────");for(let Z of X){let $=Z.sessionId.slice(0,18),
|
|
94
|
-
`);
|
|
91
|
+
`);for(let Z of Q){let $=A(Z);console.log(` ${Z}${$?.name?` (${$.name})`:""} → ${$?.alias} [${$?.channels.join(", ")}]`)}console.log()}let W=process.cwd(),B=L(I,".claude","sessions"),X=[];if(V(B))for(let Z of v(B).filter(($)=>$.endsWith(".json")))try{let $=JSON.parse(O(L(B,Z),"utf-8"));if($.cwd===W)X.push($)}catch{}if(X.length===0&&Q.length===0){console.log("No sessions or profiles in this directory."),console.log(`Get started: anet init
|
|
92
|
+
`);return}let z=H(),Y=[],U={};if(z.hub)try{let[Z,$]=await Promise.all([fetch(`${z.hub}/api/status`,{headers:b()}).then((N)=>N.json()),fetch(`${z.hub}/health`,{headers:b()}).then((N)=>N.json())]);Y=Z.sessions||[],U=$.sse_sessions||{}}catch{}if(X.length>0){console.log(`Sessions (${W}):
|
|
93
|
+
`),console.log(" SESSION PID NETWORK"),console.log(" ──────────────────── ─────── ─────────────────────");for(let Z of X){let $=Z.sessionId.slice(0,18),N=!1;try{process.kill(Z.pid,0),N=!0}catch{}let K="(not in network)",T=W.replace(/\//g,"-"),R=L(I,".claude","channels","commhub",T,".env");if(V(R)){let S=O(R,"utf-8").match(/COMMHUB_ALIAS=(.+)/);if(S){let x=S[1].trim(),u=Y.find((BB)=>BB.alias===x),e=U[x]?"●":"○";K=u?`${x} ${u.status} ${e}`:`${x} (not registered)`}}console.log(` ${$} ${(N?`${Z.pid}`:`${Z.pid}✕`).padEnd(7)} ${K}`)}console.log()}}async function qB(){let Q=H(),W=G(),B=process.env.COMMHUB_URL||W.hub||Q.hub||"http://127.0.0.1:9200",X=process.env.COMMHUB_ALIAS||W.alias;if(!X)console.error("Error: --alias required"),process.exit(1);let{CommHub:z}=await Promise.resolve().then(() => (f(),g)),Y=new z({url:B,alias:X});Y.on("task",async(U)=>{console.log(`[${X}] ← ${U.from_session}: ${U.content.slice(0,100)}`),await Y.send(U.from_session,`[${X}] 收到: ${U.content.slice(0,200)}`)}),Y.on("connected",()=>console.log(`[${X}] Connected`)),Y.on("disconnected",()=>console.log(`[${X}] Reconnecting...`)),process.on("SIGINT",()=>Y.disconnect().then(()=>process.exit(0))),console.log(`[${X}] Listening on ${B}`)}async function HB(){let Q=E[1];if(Q==="start"){let W=G(),B=m(),X=W.port||B.port||"9200",z=W.host||B.host||"0.0.0.0",Y=W.token||B.token||a();if(!Y)Y=crypto.randomUUID().replace(/-/g,""),console.log(`[anet] Generated auth token: ${Y}`),console.log(`[anet] Save this token — agents need it to connect.
|
|
94
|
+
`);p({port:X,host:z,token:Y});let U=H();if(!U.token)U.token=Y,r(U);console.log(`[anet] Starting CommHub Server on ${z}:${X}${Y?" (auth enabled)":""}...`);let Z={...process.env,PORT:X,HOST:z};if(Y)Z.COMMHUB_AUTH_TOKEN=Y;C("bunx",["@sleep2agi/commhub-server"],{env:Z,stdio:"inherit",shell:!0}).on("exit",(N)=>process.exit(N||0))}else if(Q==="config"){let W=G(),B=m();if(W.port)B.port=W.port;if(W.host)B.host=W.host;if(W.token)B.token=W.token;if(W.port||W.host||W.token)p(B),console.log(`Server config saved: ${k()}`);console.log(JSON.stringify(B,null,2))}else console.log(`
|
|
95
95
|
anet server <command>
|
|
96
96
|
|
|
97
97
|
start [options] Start CommHub Server
|
|
@@ -109,12 +109,12 @@ Example:
|
|
|
109
109
|
anet server start --port 9200 --token my-secret # 首次,保存配置
|
|
110
110
|
anet server start # 之后直接启动
|
|
111
111
|
anet server config # 查看配置
|
|
112
|
-
`)}async function OB(){let Q=H(),B=G().hub||Q.hub;if(!B)console.error("Run 'anet init' first"),process.exit(1);let X=[];try{X=(await(await fetch(`${B}/api/status`,{headers:
|
|
113
|
-
`),console.log(` ✅ ${$.alias} → ${
|
|
114
|
-
Imported ${Z} session(s). Use: cd <project> && anet resume <alias>`)}function IB(){let Q=E[1];if(Q==="ls"||Q==="list"||!Q){let W=process.cwd(),B=W.replace(/\//g,"-"),X=L(
|
|
115
|
-
Sessions in ${W} (${
|
|
116
|
-
`),console.log(" SESSION ID SIZE MODIFIED"),console.log(" ────────────────────────────────────── ──────── ────────────────");for(let Y of
|
|
112
|
+
`)}async function OB(){let Q=H(),B=G().hub||Q.hub;if(!B)console.error("Run 'anet init' first"),process.exit(1);let X=[];try{X=(await(await fetch(`${B}/api/status`,{headers:b()})).json()).sessions||[]}catch($){console.error(`Cannot reach ${B}: ${$.message}`),process.exit(1)}if(X.length===0){console.log("No sessions in CommHub.");return}let z=X.filter(($)=>$.agent==="claude-code"&&$.project_dir);if(z.length===0){console.log("No claude-code sessions found.");return}let Y=E[1],U=Y?z.filter(($)=>$.alias===Y):z;if(U.length===0){console.log(`No session found for "${Y}".`);return}let Z=0;for(let $ of U){let N=$.project_dir,K=L(N,".anet","nodes",$.alias),T=L(K,"config.json");if(V(T)){console.log(` ⏭ ${$.alias} — already exists (${N})`);continue}if(!V(N)){console.log(` ⚠ ${$.alias} — project_dir not found: ${N}`);continue}let R={runtime:"claude-code",alias:$.alias,hub:B,channels:["server:commhub"],env:{},flags:{dangerouslySkipPermissions:!0,teammateMode:"in-process"},resume:$.resume_id};M(K,{recursive:!0}),_(T,JSON.stringify(R,null,2)+`
|
|
113
|
+
`),console.log(` ✅ ${$.alias} → ${N}/.anet/nodes/${$.alias}/config.json`),Z++}console.log(`
|
|
114
|
+
Imported ${Z} session(s). Use: cd <project> && anet resume <alias>`)}function IB(){let Q=E[1];if(Q==="ls"||Q==="list"||!Q){let W=process.cwd(),B=W.replace(/\//g,"-"),X=L(I,".claude","projects",B);if(!V(X)){console.log(`No sessions for ${W}`);return}let z=v(X).filter((Y)=>Y.endsWith(".jsonl")).sort((Y,U)=>{let Z=w(L(X,Y));return w(L(X,U)).mtimeMs-Z.mtimeMs});if(z.length===0){console.log("No sessions.");return}console.log(`
|
|
115
|
+
Sessions in ${W} (${z.length} total):
|
|
116
|
+
`),console.log(" SESSION ID SIZE MODIFIED"),console.log(" ────────────────────────────────────── ──────── ────────────────");for(let Y of z){let U=Y.replace(".jsonl",""),Z=w(L(X,Y)),$=Z.size<1024?`${Z.size}B`:Z.size<1048576?`${(Z.size/1024).toFixed(0)}KB`:`${(Z.size/1024/1024).toFixed(1)}MB`,N=Z.mtime.toISOString().replace("T"," ").slice(0,16);console.log(` ${U} ${$.padStart(8)} ${N}`)}console.log()}else console.log(`
|
|
117
117
|
anet session <command>
|
|
118
118
|
|
|
119
119
|
ls List Claude Code sessions in current project
|
|
120
|
-
`)}switch(P){case"init":if(E[1]==="project")UB();else if(E[1]==="profile")LB();else NB();break;case"server":HB();break;case"start":d();break;case"resume":VB();break;case"import":OB();break;case"session":IB();break;case"ls":case"list":_B();break;case"run":qB();break;case"--help":case"-h":case void 0:
|
|
120
|
+
`)}switch(P){case"init":if(E[1]==="project")UB();else if(E[1]==="profile")LB();else NB();break;case"server":HB();break;case"start":d();break;case"resume":VB();break;case"import":OB();break;case"session":IB();break;case"ls":case"list":_B();break;case"run":qB();break;case"--help":case"-h":case void 0:c();break;default:if(A(P))E.unshift("start"),d();else console.error(`Unknown: ${P}`),c(),process.exit(1)}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleep2agi/agent-network",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.42",
|
|
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",
|
package/src/node-server.ts
CHANGED
|
@@ -88,7 +88,7 @@ log(`ENV: URL=${COMMHUB_URL} ALIAS=${ALIAS} RESUME_ID=${RESUME_ID.slice(0, 8)}..
|
|
|
88
88
|
// ── MCP Server with Channel capability ──────────────
|
|
89
89
|
const mcp = new Server(
|
|
90
90
|
{
|
|
91
|
-
name:
|
|
91
|
+
name: `commhub · ${ALIAS}`,
|
|
92
92
|
version: "0.3.0",
|
|
93
93
|
},
|
|
94
94
|
{
|