@sleep2agi/agent-network 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/dist/bin/cli.js +48 -44
  2. package/package.json +1 -1
package/dist/bin/cli.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
- var LB=Object.defineProperty;var VB=(Q)=>Q;function _B(Q,B){this[Q]=VB.bind(null,B)}var qB=(Q,B)=>{for(var W in B)LB(Q,W,{get:B[W],enumerable:!0,configurable:!0,set:_B.bind(B,W)})};var EB=(Q,B)=>()=>(Q&&(B=Q(Q=0)),B);var a={};qB(a,{default:()=>zB,CommHub:()=>j});import{EventEmitter as HB}from"events";import{hostname as l}from"os";var j,zB;var r=EB(()=>{j=class j extends HB{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,B){let W={"Content-Type":"application/json",Accept:"application/json, text/event-stream"};if(this.token)W.Authorization=`Bearer ${this.token}`;let $=await(await fetch(`${this.url}/mcp`,{method:"POST",headers:W,body:JSON.stringify({jsonrpc:"2.0",id:Date.now(),method:"tools/call",params:{name:Q,arguments:B}})})).text(),Y=$.match(/data: (.+)/),U=Y?JSON.parse(Y[1]):JSON.parse($),L=U?.result?.content?.[0]?.text;return L?JSON.parse(L):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,B,W="normal"){return this.call("send_task",{alias:Q,task:B,priority:W,from_session:this.alias})}async message(Q,B){return this.call("send_message",{alias:Q,message:B,from_session:this.alias})}async reply(Q,B,W="completed"){return this.call("reply",{task_id:Q,text:B,status:W})}async status(Q,B){return this.call("report_status",{resume_id:this.resumeId,alias:this.alias,status:Q,server:l(),hostname:l(),agent:this.agent,project_dir:process.cwd(),...B})}async getAllStatus(){return this.call("get_all_status",{})}async broadcast(Q,B){return this.call("broadcast",{message:Q,filter_server:B?.server,filter_status:B?.status})}async connectSSE(){let Q=encodeURIComponent(this.alias),B=`${this.url}/events/${Q}`,W=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 $=await fetch(B,{headers:X,signal:this.sseAbort.signal});if(!$.ok||!$.body){this.log(`SSE failed: ${$.status}`),await this.sleep(W),W=Math.min(W*1.5,60000);continue}W=this.reconnectDelay;let Y=$.body.getReader(),U=new TextDecoder,L="";while(this.running){let{done:Z,value:V}=await Y.read();if(Z)break;L+=U.decode(V,{stream:!0});let _=L.split(`
3
- `);L=_.pop()||"";for(let z of _){if(!z.startsWith("data: "))continue;try{let E=JSON.parse(z.slice(6));if(E.type==="connected"){this.log("SSE connected"),this.emit("connected");continue}if(E.type==="new_task"||E.type==="new_message"||E.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 ${W/1000}s...`),await this.sleep(W),W=Math.min(W*1.5,60000)}}async processInbox(){try{let B=(await this.call("get_inbox",{alias:this.alias,limit:10}))?.messages||[];for(let W of B)await this.call("ack_inbox",{alias:this.alias,message_id:W.id}),this.log(`← ${W.from_session}: ${W.content.slice(0,60)}`),this.emit("task",W),this.emit("message",W)}catch(Q){this.log(`inbox error: ${Q.message}`)}}sleep(Q){return new Promise((B)=>setTimeout(B,Q))}};zB=j});import{chmodSync as TB,readFileSync as J,writeFileSync as T,existsSync as H,mkdirSync as M,readdirSync as v,statSync as P}from"fs";import{join as K}from"path";import{spawn as h,execSync as f}from"child_process";import{createInterface as FB}from"readline";var q=process.argv.slice(2),y=q[0],G=process.env.HOME||process.env.USERPROFILE||"~";function e(){return K(G,".anet","config.json")}function S(){return K(G,".anet","server","config.json")}function O(){return K(process.cwd(),".anet","nodes")}function BB(){return N().token||process.env.COMMHUB_TOKEN||I().token||""}function u(Q){let B=Q||BB();return B?{Authorization:`Bearer ${B}`}:{}}function I(){let Q=e();if(H(Q))try{return JSON.parse(J(Q,"utf-8"))}catch{}return{}}function QB(Q){let B=K(G,".anet");M(B,{recursive:!0}),T(K(B,"config.json"),JSON.stringify(Q,null,2)+`
4
- `)}function t(){let Q=S();if(H(Q))try{return JSON.parse(J(Q,"utf-8"))}catch{}return{}}function n(Q){let B=K(G,".anet","server");M(B,{recursive:!0}),T(K(B,"config.json"),JSON.stringify(Q,null,2)+`
5
- `)}function F(Q){if(typeof Q==="string"){if(Q==="codex"||Q==="codex-sdk")return"codex-sdk";if(Q==="claude"||Q==="claude-sdk"||Q==="claude-agent-sdk")return"claude-agent-sdk";if(Q==="agent-sdk")return"claude-agent-sdk";return"claude-code-cli"}let B=Q;if(!B)return"claude-code-cli";if(B.runtime==="agent-sdk")return B.codexRuntime==="codex"?"codex-sdk":"claude-agent-sdk";return F(B.runtime||"claude-code-cli")}function JB(Q,B){return B?.name||B?.alias||Q}function k(Q){return Q.session||Q.resume||""}function MB(Q){return Q.normalize("NFC")}function m(Q){if(Q!==MB(Q))console.error(`Error: node-name must be Unicode NFC normalized: ${Q}`),process.exit(1);if(!/^[^\s\/\\:*?"<>|.][^\s\/\\:*?"<>|.]*$/.test(Q))console.error(`Error: invalid node-name "${Q}"`),console.error(`Allowed: Chinese/letters/numbers/-/_ ; forbidden: whitespace, '.', / \\ : * ? " < > |`),process.exit(1)}function x(Q){let B=K(O(),Q,"config.json");if(!H(B))return null;try{let W=JSON.parse(J(B,"utf-8")),X=I();return{...W,name:W.name||W.alias||Q,alias:W.alias||W.name||Q,session:W.session||W.resume||"",hub:W.hub||X.hub||"",token:W.token||X.token||"",channels:Array.isArray(W.channels)?W.channels:[],env:{...W.env},flags:{...W.flags}}}catch{return null}}function WB(Q){let B=K(O(),Q,"config.json");if(!H(B))return null;try{let W=JSON.parse(J(B,"utf-8"));return{...W,channels:Array.isArray(W.channels)?W.channels:[],env:{...W.env},flags:{...W.flags}}}catch{return null}}function w(Q,B){let W=K(O(),Q);M(W,{recursive:!0}),T(K(W,"config.json"),JSON.stringify(B,null,2)+`
6
- `)}function c(){let Q=O();if(!H(Q))return[];return v(Q).filter((B)=>H(K(Q,B,"config.json")))}function N(){let Q={_channels:[],_envs:[]};for(let B=0;B<q.length;B++){if(q[B]==="--channel"&&q[B+1]){Q._channels.push(q[++B]);continue}if(q[B]==="--env"&&q[B+1]){Q._envs.push(q[++B]);continue}if(q[B].startsWith("--")&&q[B+1]&&!q[B+1].startsWith("--"))Q[q[B].slice(2)]=q[++B];else if(q[B].startsWith("--"))Q[q[B].slice(2)]="true"}return Q}function i(Q){try{return f(`command -v ${JSON.stringify(Q)}`,{stdio:"ignore",shell:"/bin/bash"}),!0}catch{return!1}}function XB(){console.log("[anet] claude-code-cli requires:"),console.log(" - Claude Pro / Team / Enterprise subscription"),console.log(' - Run "claude auth login" first'),console.log(" - Uses Anthropic Claude only"),console.log(" - For other models, use --runtime codex-sdk or claude-agent-sdk")}function YB(Q,B){if(Q==="claude-code-cli"){if(!i("claude"))console.warn("[anet] Warning: claude CLI not found in PATH."),console.warn("[anet] Install: npm install -g @anthropic-ai/claude-code");if(B==="start")XB();return}if(!i("npx"))console.warn("[anet] Warning: npx not found in PATH; cannot launch @sleep2agi/agent-node."),console.warn("[anet] Install Node.js/npm first, or install agent-node globally: npm install -g @sleep2agi/agent-node")}function s(){console.log(`
2
+ var EB=Object.defineProperty;var HB=(B)=>B;function LB(B,Q){this[B]=HB.bind(null,Q)}var JB=(B,Q)=>{for(var X in Q)EB(B,X,{get:Q[X],enumerable:!0,configurable:!0,set:LB.bind(Q,X)})};var MB=(B,Q)=>()=>(B&&(Q=B(B=0)),Q);var s={};JB(s,{default:()=>GB,CommHub:()=>y});import{EventEmitter as zB}from"events";import{hostname as n}from"os";var y,GB;var o=MB(()=>{y=class y extends zB{url;alias;token;agent;resumeId;heartbeatInterval;reconnectDelay;heartbeatTimer;sseAbort;running=!1;constructor(B){super();if(this.url=B.url.replace(/\/$/,""),this.alias=B.alias,this.token=B.token,this.agent=B.agent||"sdk",this.resumeId=`sdk-${B.alias}-${Date.now().toString(36)}`,this.heartbeatInterval=B.heartbeatInterval??180000,this.reconnectDelay=B.reconnectDelay??3000,B.autoConnect!==!1)this.connect()}log(B){console.log(`[${new Date().toTimeString().slice(0,8)}] [commhub:${this.alias}] ${B}`)}async call(B,Q){let X={"Content-Type":"application/json",Accept:"application/json, text/event-stream"};if(this.token)X.Authorization=`Bearer ${this.token}`;let U=await(await fetch(`${this.url}/mcp`,{method:"POST",headers:X,body:JSON.stringify({jsonrpc:"2.0",id:Date.now(),method:"tools/call",params:{name:B,arguments:Q}})})).text(),Z=U.match(/data: (.+)/),V=Z?JSON.parse(Z[1]):JSON.parse(U),W=V?.result?.content?.[0]?.text;return W?JSON.parse(W):V}async connect(){if(this.running)return;this.running=!0,await this.status("idle"),this.log("registered"),this.heartbeatTimer=setInterval(()=>{this.status("idle").catch((B)=>this.log(`heartbeat failed: ${B.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(B,Q,X="normal"){return this.call("send_task",{alias:B,task:Q,priority:X,from_session:this.alias})}async message(B,Q){return this.call("send_message",{alias:B,message:Q,from_session:this.alias})}async reply(B,Q,X="completed"){return this.call("reply",{task_id:B,text:Q,status:X})}async status(B,Q){return this.call("report_status",{resume_id:this.resumeId,alias:this.alias,status:B,server:n(),hostname:n(),agent:this.agent,project_dir:process.cwd(),...Q})}async getAllStatus(){return this.call("get_all_status",{})}async broadcast(B,Q){return this.call("broadcast",{message:B,filter_server:Q?.server,filter_status:Q?.status})}async connectSSE(){let B=encodeURIComponent(this.alias),Q=`${this.url}/events/${B}`,X=this.reconnectDelay;while(this.running){try{this.sseAbort=new AbortController;let Y={Accept:"text/event-stream"};if(this.token)Y.Authorization=`Bearer ${this.token}`;let U=await fetch(Q,{headers:Y,signal:this.sseAbort.signal});if(!U.ok||!U.body){this.log(`SSE failed: ${U.status}`),await this.sleep(X),X=Math.min(X*1.5,60000);continue}X=this.reconnectDelay;let Z=U.body.getReader(),V=new TextDecoder,W="";while(this.running){let{done:$,value:_}=await Z.read();if($)break;W+=V.decode(_,{stream:!0});let q=W.split(`
3
+ `);W=q.pop()||"";for(let M of q){if(!M.startsWith("data: "))continue;try{let H=JSON.parse(M.slice(6));if(H.type==="connected"){this.log("SSE connected"),this.emit("connected");continue}if(H.type==="new_task"||H.type==="new_message"||H.type==="broadcast")await this.processInbox()}catch{}}}}catch(Y){if(Y.name==="AbortError")break;this.emit("error",Y),this.log(`SSE error: ${Y.message}`)}if(this.running)this.emit("disconnected"),this.log(`SSE reconnecting in ${X/1000}s...`),await this.sleep(X),X=Math.min(X*1.5,60000)}}async processInbox(){try{let Q=(await this.call("get_inbox",{alias:this.alias,limit:10}))?.messages||[];for(let X of Q)await this.call("ack_inbox",{alias:this.alias,message_id:X.id}),this.log(`← ${X.from_session}: ${X.content.slice(0,60)}`),this.emit("task",X),this.emit("message",X)}catch(B){this.log(`inbox error: ${B.message}`)}}sleep(B){return new Promise((Q)=>setTimeout(Q,B))}};GB=y});import{chmodSync as YB,readFileSync as G,writeFileSync as J,existsSync as L,mkdirSync as A,readdirSync as v,statSync as b}from"fs";import{join as K}from"path";import{spawn as g,execSync as p}from"child_process";import{createInterface as DB}from"readline";var E=process.argv.slice(2),S=E[0],F=process.env.HOME||process.env.USERPROFILE||"~";function ZB(){return K(F,".anet","config.json")}function h(){return K(F,".anet","server","config.json")}function I(){return K(process.cwd(),".anet","nodes")}function $B(){return N().token||process.env.COMMHUB_TOKEN||x().token||""}function f(B){let Q=B||$B();return Q?{Authorization:`Bearer ${Q}`}:{}}function x(){let B=ZB();if(L(B))try{return JSON.parse(G(B,"utf-8"))}catch{}return{}}function UB(B){let Q=K(F,".anet");A(Q,{recursive:!0}),J(K(Q,"config.json"),JSON.stringify(B,null,2)+`
4
+ `)}function i(){let B=h();if(L(B))try{return JSON.parse(G(B,"utf-8"))}catch{}return{}}function e(B){let Q=K(F,".anet","server");A(Q,{recursive:!0}),J(K(Q,"config.json"),JSON.stringify(B,null,2)+`
5
+ `)}function T(B){if(typeof B==="string"){if(B==="codex"||B==="codex-sdk")return"codex-sdk";if(B==="claude"||B==="claude-sdk"||B==="claude-agent-sdk")return"claude-agent-sdk";if(B==="agent-sdk")return"claude-agent-sdk";return"claude-code-cli"}let Q=B;if(!Q)return"claude-code-cli";if(Q.runtime==="agent-sdk")return Q.codexRuntime==="codex"?"codex-sdk":"claude-agent-sdk";return T(Q.runtime||"claude-code-cli")}function TB(B,Q){return Q?.name||Q?.alias||B}function j(B){return B.session||B.resume||""}function AB(B){return B.normalize("NFC")}function k(B){if(B!==AB(B))console.error(`Error: node-name must be Unicode NFC normalized: ${B}`),process.exit(1);if(!/^[^\s\/\\:*?"<>|.][^\s\/\\:*?"<>|.]*$/.test(B))console.error(`Error: invalid node-name "${B}"`),console.error(`Allowed: Chinese/letters/numbers/-/_ ; forbidden: whitespace, '.', / \\ : * ? " < > |`),process.exit(1)}function D(B){let Q=K(I(),B,"config.json");if(!L(Q))return null;try{let X=JSON.parse(G(Q,"utf-8")),Y=x();return{...X,name:X.name||X.alias||B,alias:X.alias||X.name||B,session:X.session||X.resume||"",hub:X.hub||Y.hub||"",token:X.token||Y.token||"",channels:Array.isArray(X.channels)?X.channels:[],env:{...X.env},flags:{...X.flags}}}catch{return null}}function WB(B){let Q=K(I(),B,"config.json");if(!L(Q))return null;try{let X=JSON.parse(G(Q,"utf-8"));return{...X,channels:Array.isArray(X.channels)?X.channels:[],env:{...X.env},flags:{...X.flags}}}catch{return null}}function C(B,Q){let X=K(I(),B);A(X,{recursive:!0}),J(K(X,"config.json"),JSON.stringify(Q,null,2)+`
6
+ `)}function c(){let B=I();if(!L(B))return[];return v(B).filter((Q)=>L(K(B,Q,"config.json")))}function N(){let B={_channels:[],_envs:[]};for(let Q=0;Q<E.length;Q++){if(E[Q]==="--channel"&&E[Q+1]){B._channels.push(E[++Q]);continue}if(E[Q]==="--env"&&E[Q+1]){B._envs.push(E[++Q]);continue}if(E[Q].startsWith("--")&&E[Q+1]&&!E[Q+1].startsWith("--"))B[E[Q].slice(2)]=E[++Q];else if(E[Q].startsWith("--"))B[E[Q].slice(2)]="true"}return B}function BB(B){try{return p(`command -v ${JSON.stringify(B)}`,{stdio:"ignore",shell:"/bin/bash"}),!0}catch{return!1}}function d(){console.log("[anet] claude-code-cli requires:"),console.log(" - Claude Pro / Team / Enterprise subscription"),console.log(' - Run "claude auth login" first'),console.log(" - Uses Anthropic Claude only"),console.log(" - For other models, use --runtime codex-sdk or claude-agent-sdk")}function l(B,Q){if(B==="claude-code-cli"){if(!BB("claude"))console.warn("[anet] Warning: claude CLI not found in PATH."),console.warn("[anet] Install: npm install -g @anthropic-ai/claude-code");if(Q==="start")d();return}if(!BB("npx"))console.warn("[anet] Warning: npx not found in PATH; cannot launch @sleep2agi/agent-node."),console.warn("[anet] Install Node.js/npm first, or install agent-node globally: npm install -g @sleep2agi/agent-node")}function QB(){console.log(`
7
7
  anet — AI Agent Network CLI
8
8
 
9
9
  anet init Configure hub URL (global, once)
@@ -24,12 +24,12 @@ Quick start:
24
24
  anet init --hub http://IP:9200
25
25
  anet create 指挥室
26
26
  anet start 指挥室
27
- `)}async function GB(){let Q=N(),B=Q.hub;if(!B)B=await C("CommHub URL (e.g. http://YOUR_IP:9200)");if(!B)b(),console.error("Error: hub URL required"),process.exit(1);B=B.replace(/\/+$/,"");let W=Q.token||"";if(!W)W=await C("Auth token (empty to skip)");b();try{let Y=await(await fetch(`${B}/health`,{headers:W?{Authorization:`Bearer ${W}`}:{}})).json();console.log(`✅ CommHub v${Y.version} — ${Y.sessions} sessions, ${Y.sse_connections} SSE`)}catch($){console.error(`❌ Cannot reach ${B}: ${$.message}`),process.exit(1)}let X=I();if(X.hub=B,W)X.token=W;else if(!X.token)delete X.token;QB(X),console.log(`
28
- Saved to ${e()}`),console.log("Next: anet init project")}async function NB(){let Q=I(),B=Q.hub;if(!B)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let W=K(process.cwd(),".anet");M(W,{recursive:!0});let X=K(W,"node-server.ts");if(!H(X)){let z=[K(new URL(".",import.meta.url).pathname,"..","..","src","node-server.ts"),K(new URL(".",import.meta.url).pathname,"..","src","node-server.ts"),K(process.argv[1],"..","..","src","node-server.ts")],E=!1;for(let A of z)if(H(A)){T(X,J(A,"utf-8")),console.log(" ✅ .anet/node-server.ts"),E=!0;break}if(!E)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 $=K(W,"package.json");if(!H($)){T($,JSON.stringify({private:!0,dependencies:{"@modelcontextprotocol/sdk":"^1.12.0"}},null,2)+`
29
- `);try{f("bun install",{cwd:W,stdio:"pipe"}),console.log(" ✅ Dependencies installed")}catch{console.log(" ⚠️ Run: cd .anet && bun install")}}let Y=K(W,".env"),U=Q.token||"",L=`COMMHUB_URL=${B}
30
- `;if(U)L+=`COMMHUB_TOKEN=${U}
31
- `;T(Y,L),console.log(`CommHub URL: ${B}${U?" (with token)":""}`);let Z=K(process.cwd(),".mcp.json"),V={};if(H(Z))try{V=JSON.parse(J(Z,"utf-8"))}catch{}if(!V.mcpServers?.commhub)V.mcpServers=V.mcpServers||{},V.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},T(Z,JSON.stringify(V,null,2)+`
32
- `),console.log(".mcp.json: commhub → .anet/node-server.ts");else console.log(".mcp.json: commhub already set");let _=K(process.cwd(),"CLAUDE.md");if(!H(_))T(_,`# Agent Network (CommHub)
27
+ `)}async function NB(){let B=N(),Q=B.hub;if(!Q)Q=await z("CommHub URL (e.g. http://YOUR_IP:9200)");if(!Q)R(),console.error("Error: hub URL required"),process.exit(1);Q=Q.replace(/\/+$/,"");let X=B.token||"";if(!X)X=await z("Auth token (empty to skip)");R();try{let Z=await(await fetch(`${Q}/health`,{headers:X?{Authorization:`Bearer ${X}`}:{}})).json();console.log(`✅ CommHub v${Z.version} — ${Z.sessions} sessions, ${Z.sse_connections} SSE`)}catch(U){console.error(`❌ Cannot reach ${Q}: ${U.message}`),process.exit(1)}let Y=x();if(Y.hub=Q,X)Y.token=X;else if(!Y.token)delete Y.token;UB(Y),console.log(`
28
+ Saved to ${ZB()}`),console.log("Next: anet init project")}async function FB(){let B=x(),Q=B.hub;if(!Q)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let X=K(process.cwd(),".anet");A(X,{recursive:!0});let Y=K(X,"node-server.ts");if(!L(Y)){let M=[K(new URL(".",import.meta.url).pathname,"..","..","src","node-server.ts"),K(new URL(".",import.meta.url).pathname,"..","src","node-server.ts"),K(process.argv[1],"..","..","src","node-server.ts")],H=!1;for(let O of M)if(L(O)){J(Y,G(O,"utf-8")),console.log(" ✅ .anet/node-server.ts"),H=!0;break}if(!H)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 U=K(X,"package.json");if(!L(U)){J(U,JSON.stringify({private:!0,dependencies:{"@modelcontextprotocol/sdk":"^1.12.0"}},null,2)+`
29
+ `);try{p("bun install",{cwd:X,stdio:"pipe"}),console.log(" ✅ Dependencies installed")}catch{console.log(" ⚠️ Run: cd .anet && bun install")}}let Z=K(X,".env"),V=B.token||"",W=`COMMHUB_URL=${Q}
30
+ `;if(V)W+=`COMMHUB_TOKEN=${V}
31
+ `;J(Z,W),console.log(`CommHub URL: ${Q}${V?" (with token)":""}`);let $=K(process.cwd(),".mcp.json"),_={};if(L($))try{_=JSON.parse(G($,"utf-8"))}catch{}if(!_.mcpServers?.commhub)_.mcpServers=_.mcpServers||{},_.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},J($,JSON.stringify(_,null,2)+`
32
+ `),console.log(".mcp.json: commhub → .anet/node-server.ts");else console.log(".mcp.json: commhub already set");let q=K(process.cwd(),"CLAUDE.md");if(!L(q))J(q,`# Agent Network (CommHub)
33
33
 
34
34
  ## 通信方式
35
35
 
@@ -68,28 +68,32 @@ commhub_get_all_status()
68
68
  - 回复指挥室用 commhub_send_task(不是 commhub_reply,reply 不推送)
69
69
  - 不要猜 alias,用 get_all_status 查
70
70
  `),console.log("CLAUDE.md: created");else console.log("CLAUDE.md: already exists");console.log(`
71
- ✅ Project ready. Next: anet create <node-name>`)}function AB(){console.warn("[deprecated] anet init profile is now anet create."),console.warn(` Run: anet create <node-name> [--runtime ...]
72
- `),$B(q[2])}function ZB(Q,B){let W=I(),X=B.hub||W.hub;if(!X)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let $={};for(let Z of B._envs){let V=Z.indexOf("=");if(V>0)$[Z.slice(0,V)]=Z.slice(V+1)}let Y=F(B.runtime||"claude-code-cli"),U=Y==="codex-sdk"?"gpt-5.4":void 0;return{anet_version:"0.1.0",name:Q,runtime:Y,...B.hub?{hub:X}:{},...B.model||U?{model:B.model||U}:{},...B.tools?{tools:B.tools.split(",").map((Z)=>Z.trim())}:{},channels:B._channels.length>0?B._channels:["server:commhub"],env:$,flags:{dangerouslySkipPermissions:!0,...Y==="claude-code-cli"?{teammateMode:B["teammate-mode"]||"in-process"}:{},...B["max-turns"]?{maxTurns:parseInt(B["max-turns"])}:{}},...B.session?{session:B.session}:{}}}function $B(Q){let B=Q||q[1];if(!B)console.error("Usage: anet create <node-name> [--runtime claude-code-cli|codex-sdk|claude-agent-sdk] [--model ...] [--tools ...]"),process.exit(1);if(m(B),x(B))console.error(`Node "${B}" already exists: .anet/nodes/${B}/config.json`),process.exit(1);let W=N(),X=ZB(B,W),$=K(G,".claude","channels","commhub"),Y=process.cwd().replace(/\//g,"-"),U=K($,Y);if(M(U,{recursive:!0}),T(K(U,".env"),`COMMHUB_ALIAS=${B}
73
- `),w(B,X),YB(F(X),"create"),console.log(`
74
- [anet] Created node "${B}" (${F(X)})`),F(X)==="claude-code-cli")XB();console.log("[anet] ⚠ dangerouslySkipPermissions and teammateMode enabled by default."),console.log(`[anet] To disable: edit .anet/nodes/${B}/config.json → flags`),console.log(`
75
- Start: anet start ${B}`)}var R=null;function IB(){if(!R)R=FB({input:process.stdin,output:process.stdout});return R}function b(){if(R)R.close(),R=null}function C(Q,B){let W=B?` [${B}]`:"";return new Promise((X)=>{IB().question(`${Q}${W}: `,($)=>{X($.trim()||B||"")})})}function OB(Q){if(F(Q)!=="claude-code-cli")return;if(!Q.channels?.some((z)=>z.includes("commhub")))return;let B=K(process.cwd(),".mcp.json"),W={};if(H(B))try{W=JSON.parse(J(B,"utf-8"))}catch{}let X=K(process.cwd(),".anet"),$=K(X,"node-server.ts"),Y=[K(new URL(".",import.meta.url).pathname,"..","..","src","node-server.ts"),K(new URL(".",import.meta.url).pathname,"..","src","node-server.ts"),K(process.argv[1],"..","..","src","node-server.ts")];for(let z of Y)if(H(z)){M(X,{recursive:!0});let E=J(z,"utf-8"),A=H($)?J($,"utf-8"):"";if(E!==A)T($,E),console.log("[anet] Updated .anet/node-server.ts");break}let U=K(X,"package.json");if(!H(U)){M(X,{recursive:!0}),T(U,JSON.stringify({private:!0,dependencies:{"@modelcontextprotocol/sdk":"^1.12.0"}},null,2)+`
76
- `);try{f("bun install",{cwd:X,stdio:"pipe"})}catch{}}if(W.mcpServers=W.mcpServers||{},!Object.keys(W.mcpServers).some((z)=>z.includes("commhub")))W.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},T(B,JSON.stringify(W,null,2)+`
77
- `),console.log("[anet] .mcp.json: added commhub");let Z=K(X,".env"),V=Q.token||"",_=`COMMHUB_URL=${Q.hub||"http://127.0.0.1:9200"}
78
- `;if(V)_+=`COMMHUB_TOKEN=${V}
79
- `;T(Z,_),W.mcpServers=W.mcpServers||{},W.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},T(B,JSON.stringify(W,null,2)+`
80
- `),console.log("[anet] .mcp.json: added commhub channel server")}async function g(Q,B=!1){let W=x(Q);if(!W)console.error(`Node "${Q}" not found. Create it first: anet create ${Q}`),process.exit(1);let X=F(W),$=JB(Q,W),Y=k(W),U=!!Y&&!B,L=U?`Resuming session ${Y.slice(0,8)}...`:"Starting new session";console.log(`[anet] ${L} for "${Q}" [${X}]...
81
- `),YB(X,"start"),OB(W);let Z=W.token||"";if(X==="codex-sdk"||X==="claude-agent-sdk"){let V=["@sleep2agi/agent-node","--config",K(O(),Q,"config.json"),"--alias",$];if(B)V.push("--new-session","true");let _={...process.env,...Z?{COMMHUB_TOKEN:Z}:{}};for(let[E,A]of Object.entries(W.env))_[E]=A.replace(/^~/,G);h("npx",V,{env:_,stdio:"inherit",shell:!0}).on("exit",(E)=>process.exit(E||0))}else{let V={...process.env,COMMHUB_ALIAS:W.alias,...Z?{COMMHUB_TOKEN:Z}:{}};for(let[E,A]of Object.entries(W.env))V[E]=A.replace(/^~/,G);if(W.channels.includes("telegram"))V.TELEGRAM_STATE_DIR=K(O(),Q,"channels","telegram");let _=[];if(W.flags.dangerouslySkipPermissions)_.push("--dangerously-skip-permissions");for(let E of W.channels)if(E.startsWith("server:"))_.push("--dangerously-load-development-channels",E);else if(E==="telegram")_.push("--channels","plugin:telegram@claude-plugins-official");else _.push("--channels",E);if(W.flags.teammateMode)_.push("--teammate-mode",W.flags.teammateMode);if(U)_.push("--resume",Y);_.push("-n",$),h("claude",_,{env:V,stdio:"inherit",shell:!0}).on("exit",(E)=>{if(!U||B){if(console.log(`
82
- [anet] Tip: bind this Claude Code session with:`),console.log("[anet] anet session ls"),console.log(`[anet] anet resume ${Q} --session <session-id>`),B&&Y)console.log(`[anet] Next "anet start ${Q}" will still resume ${Y.slice(0,8)}... until you rebind.`)}process.exit(E||0)})}}async function o(){let Q=q[1];if(!Q){RB("start");return}await g(Q,!!N()["new-session"])}async function xB(){let Q=q[1];if(!Q){console.error("Usage: anet resume <node-name> --session <session-id>"),console.error("Daily start/resume: anet start <node-name>");return}let B=x(Q),W=N(),X=W.session;if(!X){console.warn("[deprecated] anet resume <node-name> without --session is now anet start <node-name>."),await g(Q,!1);return}if(m(Q),!B){let $={...W,session:X,runtime:W.runtime||"claude-code-cli"};B=ZB(Q,$),w(Q,B),console.log(`[anet] Created node "${Q}"`)}else{let $=k(B);if($&&$!==X&&W.yes!=="true"){let U=await C(`[anet] ${Q} already has session ${$.slice(0,8)}..., overwrite? (y/n)`,"n");if(b(),!/^y(es)?$/i.test(U)){console.log("[anet] Session unchanged.");return}}let Y=WB(Q)||B;Y.session=X,delete Y.resume,delete Y.resumeAlias,w(Q,Y)}console.log(`[anet] Saved session ${X.slice(0,8)}... to .anet/nodes/${Q}/config.json
83
- `),await g(Q,!1)}function RB(Q){let B=c();if(B.length===0){console.log("No nodes. Run: anet create <node-name>");return}console.log(`
71
+ ✅ Project ready. Next: anet create <node-name>`)}async function OB(){console.warn("[deprecated] anet init profile is now anet create."),console.warn(` Run: anet create <node-name> [--runtime ...]
72
+ `),await VB(E[2])}function a(B,Q){let X=x(),Y=Q.hub||X.hub;if(!Y)console.error("Run 'anet init' first to configure hub URL"),process.exit(1);let U={};for(let $ of Q._envs){let _=$.indexOf("=");if(_>0)U[$.slice(0,_)]=$.slice(_+1)}let Z=T(Q.runtime||"claude-code-cli"),V=Z==="codex-sdk"?"gpt-5.4":void 0;return{anet_version:"0.1.0",name:B,runtime:Z,...Q.hub?{hub:Y}:{},...Q.model||V?{model:Q.model||V}:{},...Q.tools?{tools:Q.tools.split(",").map(($)=>$.trim())}:{},channels:Q._channels.length>0?Q._channels:["server:commhub"],env:U,flags:{dangerouslySkipPermissions:!0,...Z==="claude-code-cli"?{teammateMode:Q["teammate-mode"]||"in-process"}:{},...Q["max-turns"]?{maxTurns:parseInt(Q["max-turns"])}:{}},...Q.session?{session:Q.session}:{}}}function KB(B,Q){let X=K(F,".claude","channels","commhub"),Y=process.cwd().replace(/\//g,"-"),U=K(X,Y);A(U,{recursive:!0}),J(K(U,".env"),`COMMHUB_ALIAS=${B}
73
+ `),C(B,Q)}function IB(B,Q){if(B.channels=B.channels||[],!B.channels.includes(Q))B.channels.push(Q)}function xB(B,Q,X){let Y=K(I(),B,"channels","telegram");A(Y,{recursive:!0}),A(K(Y,"inbox"),{recursive:!0});let U=K(Y,".env");J(U,`TELEGRAM_BOT_TOKEN=${Q}
74
+ `);try{YB(U,384)}catch{}return J(K(Y,"access.json"),JSON.stringify({dmPolicy:"allowlist",allowFrom:[X],groups:{},pending:{}},null,2)+`
75
+ `),Y}async function u(B,Q){console.log(B),Q.forEach((X,Y)=>{let U=X.description?` ${X.description}`:"";console.log(` ${Y+1}) ${X.label}${U}`)});while(!0){let X=await z("Select","1"),Y=Number.parseInt(X,10)-1;if(Y>=0&&Y<Q.length)return Q[Y].value;console.log(`Please enter 1-${Q.length}.`)}}async function RB(){let B=await z("Node name");if(!B)R(),console.error("Error: node-name required"),process.exit(1);if(k(B),D(B))R(),console.error(`Node "${B}" already exists: .anet/nodes/${B}/config.json`),process.exit(1);let Q=await u("Select runtime:",[{label:"claude-code-cli",value:"claude-code-cli",description:"Claude Code CLI(需要 Pro 订阅)"},{label:"codex-sdk",value:"codex-sdk",description:"Codex SDK(GPT-5.4)"},{label:"claude-agent-sdk",value:"claude-agent-sdk",description:"Claude Agent SDK(MiniMax/书生等)"}]),X=N();if(X.runtime=Q,Q==="codex-sdk"){let W=await u("Select model:",[{label:"gpt-5.4",value:"gpt-5.4"},{label:"o3",value:"o3"},{label:"custom",value:"__custom__"}]);X.model=W==="__custom__"?await z("Model"):W}else if(Q==="claude-agent-sdk"){let W=await u("Select model:",[{label:"MiniMax-M2.7",value:"MiniMax-M2.7"},{label:"intern-s1-pro",value:"intern-s1-pro"},{label:"claude-sonnet-4-6",value:"claude-sonnet-4-6"},{label:"custom",value:"__custom__"}]);if(X.model=W==="__custom__"?await z("Model"):W,X.model==="MiniMax-M2.7")X._envs.push("ANTHROPIC_BASE_URL=https://api.minimaxi.com/anthropic");else if(X.model==="intern-s1-pro")X._envs.push("ANTHROPIC_BASE_URL=https://chat.intern-ai.org.cn");let $=await z("ANTHROPIC_AUTH_TOKEN");if($)X._envs.push(`ANTHROPIC_AUTH_TOKEN=${$}`)}let Y=a(B,X),U=await z("Add Telegram channel? (y/n)","n"),Z=null;if(/^y(es)?$/i.test(U)){let W=await z("Telegram Bot Token"),$=await z("Allow User ID","7612221352");if(!W)R(),console.error("Error: Telegram Bot Token required"),process.exit(1);Z={botToken:W,allowId:$},IB(Y,"telegram")}let V=await z("Add WeChat channel? (y/n)","n");if(/^y(es)?$/i.test(V))console.log("[anet] WeChat channel is not implemented yet; skipped.");if(R(),KB(B,Y),Z)xB(B,Z.botToken,Z.allowId);if(l(T(Y),"create"),console.log(`
76
+ [anet] Created node "${B}" (${T(Y)})`),Z)console.log("[anet] ✅ Telegram channel added");if(T(Y)==="claude-code-cli")d();console.log("[anet] ⚠ dangerouslySkipPermissions and teammateMode enabled by default."),console.log(`[anet] To disable: edit .anet/nodes/${B}/config.json → flags`),console.log(`
77
+ Start: anet start ${B}`)}async function VB(B){let Q=B||E[1];if(!Q)return RB();if(Q.startsWith("--"))console.error("Usage: anet create <node-name> [--runtime claude-code-cli|codex-sdk|claude-agent-sdk] [--model ...] [--tools ...]"),console.error("Or run fully interactive: anet create"),process.exit(1);if(k(Q),D(Q))console.error(`Node "${Q}" already exists: .anet/nodes/${Q}/config.json`),process.exit(1);let X=N(),Y=a(Q,X);if(KB(Q,Y),l(T(Y),"create"),console.log(`
78
+ [anet] Created node "${Q}" (${T(Y)})`),T(Y)==="claude-code-cli")d();console.log("[anet] ⚠ dangerouslySkipPermissions and teammateMode enabled by default."),console.log(`[anet] To disable: edit .anet/nodes/${Q}/config.json → flags`),console.log(`
79
+ Start: anet start ${Q}`)}var P=null;function PB(){if(!P)P=DB({input:process.stdin,output:process.stdout});return P}function R(){if(P)P.close(),P=null}function z(B,Q){let X=Q?` [${Q}]`:"";return new Promise((Y)=>{PB().question(`${B}${X}: `,(U)=>{Y(U.trim()||Q||"")})})}function wB(B){if(T(B)!=="claude-code-cli")return;if(!B.channels?.some((M)=>M.includes("commhub")))return;let Q=K(process.cwd(),".mcp.json"),X={};if(L(Q))try{X=JSON.parse(G(Q,"utf-8"))}catch{}let Y=K(process.cwd(),".anet"),U=K(Y,"node-server.ts"),Z=[K(new URL(".",import.meta.url).pathname,"..","..","src","node-server.ts"),K(new URL(".",import.meta.url).pathname,"..","src","node-server.ts"),K(process.argv[1],"..","..","src","node-server.ts")];for(let M of Z)if(L(M)){A(Y,{recursive:!0});let H=G(M,"utf-8"),O=L(U)?G(U,"utf-8"):"";if(H!==O)J(U,H),console.log("[anet] Updated .anet/node-server.ts");break}let V=K(Y,"package.json");if(!L(V)){A(Y,{recursive:!0}),J(V,JSON.stringify({private:!0,dependencies:{"@modelcontextprotocol/sdk":"^1.12.0"}},null,2)+`
80
+ `);try{p("bun install",{cwd:Y,stdio:"pipe"})}catch{}}if(X.mcpServers=X.mcpServers||{},!Object.keys(X.mcpServers).some((M)=>M.includes("commhub")))X.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},J(Q,JSON.stringify(X,null,2)+`
81
+ `),console.log("[anet] .mcp.json: added commhub");let $=K(Y,".env"),_=B.token||"",q=`COMMHUB_URL=${B.hub||"http://127.0.0.1:9200"}
82
+ `;if(_)q+=`COMMHUB_TOKEN=${_}
83
+ `;J($,q),X.mcpServers=X.mcpServers||{},X.mcpServers.commhub={type:"stdio",command:"bun",args:[".anet/node-server.ts"]},J(Q,JSON.stringify(X,null,2)+`
84
+ `),console.log("[anet] .mcp.json: added commhub channel server")}async function m(B,Q=!1){let X=D(B);if(!X)console.error(`Node "${B}" not found. Create it first: anet create ${B}`),process.exit(1);let Y=T(X),U=TB(B,X),Z=j(X),V=!!Z&&!Q,W=V?`Resuming session ${Z.slice(0,8)}...`:"Starting new session";console.log(`[anet] ${W} for "${B}" [${Y}]...
85
+ `),l(Y,"start"),wB(X);let $=X.token||"";if(Y==="codex-sdk"||Y==="claude-agent-sdk"){let _=["@sleep2agi/agent-node","--config",K(I(),B,"config.json"),"--alias",U];if(Q)_.push("--new-session","true");let q={...process.env,...$?{COMMHUB_TOKEN:$}:{}};for(let[H,O]of Object.entries(X.env))q[H]=O.replace(/^~/,F);g("npx",_,{env:q,stdio:"inherit",shell:!0}).on("exit",(H)=>process.exit(H||0))}else{let _={...process.env,COMMHUB_ALIAS:X.alias,...$?{COMMHUB_TOKEN:$}:{}};for(let[H,O]of Object.entries(X.env))_[H]=O.replace(/^~/,F);if(X.channels.includes("telegram"))_.TELEGRAM_STATE_DIR=K(I(),B,"channels","telegram");let q=[];if(X.flags.dangerouslySkipPermissions)q.push("--dangerously-skip-permissions");for(let H of X.channels)if(H.startsWith("server:"))q.push("--dangerously-load-development-channels",H);else if(H==="telegram")q.push("--channels","plugin:telegram@claude-plugins-official");else q.push("--channels",H);if(X.flags.teammateMode)q.push("--teammate-mode",X.flags.teammateMode);if(V)q.push("--resume",Z);q.push("-n",U),g("claude",q,{env:_,stdio:"inherit",shell:!0}).on("exit",(H)=>{if(!V||Q){if(console.log(`
86
+ [anet] Tip: bind this Claude Code session with:`),console.log("[anet] anet session ls"),console.log(`[anet] anet resume ${B} --session <session-id>`),Q&&Z)console.log(`[anet] Next "anet start ${B}" will still resume ${Z.slice(0,8)}... until you rebind.`)}process.exit(H||0)})}}async function XB(){let B=E[1];if(!B){CB("start");return}await m(B,!!N()["new-session"])}async function bB(){let B=E[1];if(!B){console.error("Usage: anet resume <node-name> --session <session-id>"),console.error("Daily start/resume: anet start <node-name>");return}let Q=D(B),X=N(),Y=X.session;if(!Y){console.warn("[deprecated] anet resume <node-name> without --session is now anet start <node-name>."),await m(B,!1);return}if(k(B),!Q){let U={...X,session:Y,runtime:X.runtime||"claude-code-cli"};Q=a(B,U),C(B,Q),console.log(`[anet] Created node "${B}"`)}else{let U=j(Q);if(U&&U!==Y&&X.yes!=="true"){let V=await z(`[anet] ${B} already has session ${U.slice(0,8)}..., overwrite? (y/n)`,"n");if(R(),!/^y(es)?$/i.test(V)){console.log("[anet] Session unchanged.");return}}let Z=WB(B)||Q;Z.session=Y,delete Z.resume,delete Z.resumeAlias,C(B,Z)}console.log(`[anet] Saved session ${Y.slice(0,8)}... to .anet/nodes/${B}/config.json
87
+ `),await m(B,!1)}function CB(B){let Q=c();if(Q.length===0){console.log("No nodes. Run: anet create <node-name>");return}console.log(`
84
88
  Nodes:
85
- `);for(let W of B){let X=x(W);console.log(` ${W} [${F(X||void 0)}] session=${X?k(X).slice(0,8)||"-":"-"} channels=[${X?.channels.join(", ")}]`)}console.log(`
86
- anet ${Q} <node-name>
87
- `)}async function CB(){let Q=c();if(Q.length>0){console.log(`
89
+ `);for(let X of Q){let Y=D(X);console.log(` ${X} [${T(Y||void 0)}] session=${Y?j(Y).slice(0,8)||"-":"-"} channels=[${Y?.channels.join(", ")}]`)}console.log(`
90
+ anet ${B} <node-name>
91
+ `)}async function vB(){let B=c();if(B.length>0){console.log(`
88
92
  Nodes:
89
- `);for(let L of Q){let Z=x(L),V=Z?k(Z).slice(0,8)||"-":"-";console.log(` ${L} [${F(Z||void 0)}] session=${V} channels=[${Z?.channels.join(", ")}]`)}console.log()}let B=process.cwd(),W=K(G,".claude","sessions"),X=[];if(H(W))for(let L of v(W).filter((Z)=>Z.endsWith(".json")))try{let Z=JSON.parse(J(K(W,L),"utf-8"));if(Z.cwd===B)X.push(Z)}catch{}if(X.length===0&&Q.length===0){console.log("No sessions or nodes in this directory."),console.log(`Get started: anet init
90
- `);return}let $=I(),Y=[],U={};if($.hub)try{let[L,Z]=await Promise.all([fetch(`${$.hub}/api/status`,{headers:u()}).then((V)=>V.json()),fetch(`${$.hub}/health`,{headers:u()}).then((V)=>V.json())]);Y=L.sessions||[],U=Z.sse_sessions||{}}catch{}if(X.length>0){console.log(`Sessions (${B}):
91
- `),console.log(" SESSION PID NETWORK"),console.log(" ──────────────────── ─────── ─────────────────────");for(let L of X){let Z=L.sessionId.slice(0,18),V=!1;try{process.kill(L.pid,0),V=!0}catch{}let _="(not in network)",z=B.replace(/\//g,"-"),E=K(G,".claude","channels","commhub",z,".env");if(H(E)){let p=J(E,"utf-8").match(/COMMHUB_ALIAS=(.+)/);if(p){let D=p[1].trim(),d=Y.find((KB)=>KB.alias===D),UB=U[D]?"●":"○";_=d?`${D} ${d.status} ${UB}`:`${D} (not registered)`}}console.log(` ${Z} ${(V?`${L.pid}`:`${L.pid}✕`).padEnd(7)} ${_}`)}console.log()}}async function DB(){let Q=I(),B=N(),W=process.env.COMMHUB_URL||B.hub||Q.hub||"http://127.0.0.1:9200",X=process.env.COMMHUB_ALIAS||B.alias;if(!X)console.error("Error: --alias required"),process.exit(1);let{CommHub:$}=await Promise.resolve().then(() => (r(),a)),Y=new $({url:W,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 ${W}`)}async function PB(){let Q=q[1];if(Q==="start"){let B=N(),W=t(),X=B.port||W.port||"9200",$=B.host||W.host||"0.0.0.0",Y=B.token||W.token||BB();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.
92
- `);n({port:X,host:$,token:Y});let U=I();if(!U.token)U.token=Y,QB(U);console.log(`[anet] Starting CommHub Server on ${$}:${X}${Y?" (auth enabled)":""}...`);let L={...process.env,PORT:X,HOST:$};if(Y)L.COMMHUB_AUTH_TOKEN=Y;h("bunx",["@sleep2agi/commhub-server"],{env:L,stdio:"inherit",shell:!0}).on("exit",(V)=>process.exit(V||0))}else if(Q==="config"){let B=N(),W=t();if(B.port)W.port=B.port;if(B.host)W.host=B.host;if(B.token)W.token=B.token;if(B.port||B.host||B.token)n(W),console.log(`Server config saved: ${S()}`);console.log(JSON.stringify(W,null,2))}else console.log(`
93
+ `);for(let W of B){let $=D(W),_=$?j($).slice(0,8)||"-":"-";console.log(` ${W} [${T($||void 0)}] session=${_} channels=[${$?.channels.join(", ")}]`)}console.log()}let Q=process.cwd(),X=K(F,".claude","sessions"),Y=[];if(L(X))for(let W of v(X).filter(($)=>$.endsWith(".json")))try{let $=JSON.parse(G(K(X,W),"utf-8"));if($.cwd===Q)Y.push($)}catch{}if(Y.length===0&&B.length===0){console.log("No sessions or nodes in this directory."),console.log(`Get started: anet init
94
+ `);return}let U=x(),Z=[],V={};if(U.hub)try{let[W,$]=await Promise.all([fetch(`${U.hub}/api/status`,{headers:f()}).then((_)=>_.json()),fetch(`${U.hub}/health`,{headers:f()}).then((_)=>_.json())]);Z=W.sessions||[],V=$.sse_sessions||{}}catch{}if(Y.length>0){console.log(`Sessions (${Q}):
95
+ `),console.log(" SESSION PID NETWORK"),console.log(" ──────────────────── ─────── ─────────────────────");for(let W of Y){let $=W.sessionId.slice(0,18),_=!1;try{process.kill(W.pid,0),_=!0}catch{}let q="(not in network)",M=Q.replace(/\//g,"-"),H=K(F,".claude","channels","commhub",M,".env");if(L(H)){let r=G(H,"utf-8").match(/COMMHUB_ALIAS=(.+)/);if(r){let w=r[1].trim(),t=Z.find((qB)=>qB.alias===w),_B=V[w]?"●":"○";q=t?`${w} ${t.status} ${_B}`:`${w} (not registered)`}}console.log(` ${$} ${(_?`${W.pid}`:`${W.pid}✕`).padEnd(7)} ${q}`)}console.log()}}async function jB(){let B=x(),Q=N(),X=process.env.COMMHUB_URL||Q.hub||B.hub||"http://127.0.0.1:9200",Y=process.env.COMMHUB_ALIAS||Q.alias;if(!Y)console.error("Error: --alias required"),process.exit(1);let{CommHub:U}=await Promise.resolve().then(() => (o(),s)),Z=new U({url:X,alias:Y});Z.on("task",async(V)=>{console.log(`[${Y}] ← ${V.from_session}: ${V.content.slice(0,100)}`),await Z.send(V.from_session,`[${Y}] 收到: ${V.content.slice(0,200)}`)}),Z.on("connected",()=>console.log(`[${Y}] Connected`)),Z.on("disconnected",()=>console.log(`[${Y}] Reconnecting...`)),process.on("SIGINT",()=>Z.disconnect().then(()=>process.exit(0))),console.log(`[${Y}] Listening on ${X}`)}async function kB(){let B=E[1];if(B==="start"){let Q=N(),X=i(),Y=Q.port||X.port||"9200",U=Q.host||X.host||"0.0.0.0",Z=Q.token||X.token||$B();if(!Z)Z=crypto.randomUUID().replace(/-/g,""),console.log(`[anet] Generated auth token: ${Z}`),console.log(`[anet] Save this token — agents need it to connect.
96
+ `);e({port:Y,host:U,token:Z});let V=x();if(!V.token)V.token=Z,UB(V);console.log(`[anet] Starting CommHub Server on ${U}:${Y}${Z?" (auth enabled)":""}...`);let W={...process.env,PORT:Y,HOST:U};if(Z)W.COMMHUB_AUTH_TOKEN=Z;g("bunx",["@sleep2agi/commhub-server"],{env:W,stdio:"inherit",shell:!0}).on("exit",(_)=>process.exit(_||0))}else if(B==="config"){let Q=N(),X=i();if(Q.port)X.port=Q.port;if(Q.host)X.host=Q.host;if(Q.token)X.token=Q.token;if(Q.port||Q.host||Q.token)e(X),console.log(`Server config saved: ${h()}`);console.log(JSON.stringify(X,null,2))}else console.log(`
93
97
  anet server <command>
94
98
 
95
99
  start [options] Start CommHub Server
@@ -100,22 +104,22 @@ Options:
100
104
  --host <host> Bind address (default: 0.0.0.0)
101
105
  --token <token> Auth token
102
106
 
103
- Config: ${S()}
107
+ Config: ${h()}
104
108
  First 'anet server start' saves config, after that just 'anet server start'.
105
109
 
106
110
  Example:
107
111
  anet server start --port 9200 --token my-secret # 首次,保存配置
108
112
  anet server start # 之后直接启动
109
113
  anet server config # 查看配置
110
- `)}async function wB(){let Q=I(),W=N().hub||Q.hub;if(!W)console.error("Run 'anet init' first"),process.exit(1);let X=[];try{X=(await(await fetch(`${W}/api/status`,{headers:u()})).json()).sessions||[]}catch(Z){console.error(`Cannot reach ${W}: ${Z.message}`),process.exit(1)}if(X.length===0){console.log("No sessions in CommHub.");return}let $=X.filter((Z)=>Z.agent==="claude-code"&&Z.project_dir);if($.length===0){console.log("No claude-code sessions found.");return}let Y=q[1],U=Y?$.filter((Z)=>Z.alias===Y):$;if(U.length===0){console.log(`No session found for "${Y}".`);return}let L=0;for(let Z of U){let V=Z.project_dir,_=K(V,".anet","nodes",Z.alias),z=K(_,"config.json");if(H(z)){console.log(` ⏭ ${Z.alias} — already exists (${V})`);continue}if(!H(V)){console.log(` ⚠ ${Z.alias} — project_dir not found: ${V}`);continue}let E={anet_version:"0.1.0",name:Z.alias,runtime:"claude-code-cli",channels:["server:commhub"],env:{},flags:{dangerouslySkipPermissions:!0,teammateMode:"in-process"},session:Z.resume_id};M(_,{recursive:!0}),T(z,JSON.stringify(E,null,2)+`
111
- `),console.log(` ✅ ${Z.alias} → ${V}/.anet/nodes/${Z.alias}/config.json`),L++}console.log(`
112
- Imported ${L} session(s). Use: cd <project> && anet resume <alias>`)}function bB(){let Q=q[1];if(Q==="ls"||Q==="list"||!Q){let B=process.cwd(),W=B.replace(/\//g,"-"),X=K(G,".claude","projects",W);if(!H(X)){console.log(`No sessions for ${B}`);return}let $=v(X).filter((Y)=>Y.endsWith(".jsonl")).sort((Y,U)=>{let L=P(K(X,Y));return P(K(X,U)).mtimeMs-L.mtimeMs});if($.length===0){console.log("No sessions.");return}console.log(`
113
- Sessions in ${B} (${$.length} total):
114
- `),console.log(" SESSION ID SIZE MODIFIED"),console.log(" ────────────────────────────────────── ──────── ────────────────");for(let Y of $){let U=Y.replace(".jsonl",""),L=P(K(X,Y)),Z=L.size<1024?`${L.size}B`:L.size<1048576?`${(L.size/1024).toFixed(0)}KB`:`${(L.size/1024/1024).toFixed(1)}MB`,V=L.mtime.toISOString().replace("T"," ").slice(0,16);console.log(` ${U} ${Z.padStart(8)} ${V}`)}console.log()}else console.log(`
114
+ `)}async function yB(){let B=x(),X=N().hub||B.hub;if(!X)console.error("Run 'anet init' first"),process.exit(1);let Y=[];try{Y=(await(await fetch(`${X}/api/status`,{headers:f()})).json()).sessions||[]}catch($){console.error(`Cannot reach ${X}: ${$.message}`),process.exit(1)}if(Y.length===0){console.log("No sessions in CommHub.");return}let U=Y.filter(($)=>$.agent==="claude-code"&&$.project_dir);if(U.length===0){console.log("No claude-code sessions found.");return}let Z=E[1],V=Z?U.filter(($)=>$.alias===Z):U;if(V.length===0){console.log(`No session found for "${Z}".`);return}let W=0;for(let $ of V){let _=$.project_dir,q=K(_,".anet","nodes",$.alias),M=K(q,"config.json");if(L(M)){console.log(` ⏭ ${$.alias} — already exists (${_})`);continue}if(!L(_)){console.log(` ⚠ ${$.alias} — project_dir not found: ${_}`);continue}let H={anet_version:"0.1.0",name:$.alias,runtime:"claude-code-cli",channels:["server:commhub"],env:{},flags:{dangerouslySkipPermissions:!0,teammateMode:"in-process"},session:$.resume_id};A(q,{recursive:!0}),J(M,JSON.stringify(H,null,2)+`
115
+ `),console.log(` ✅ ${$.alias} → ${_}/.anet/nodes/${$.alias}/config.json`),W++}console.log(`
116
+ Imported ${W} session(s). Use: cd <project> && anet resume <alias>`)}function SB(){let B=E[1];if(B==="ls"||B==="list"||!B){let Q=process.cwd(),X=Q.replace(/\//g,"-"),Y=K(F,".claude","projects",X);if(!L(Y)){console.log(`No sessions for ${Q}`);return}let U=v(Y).filter((Z)=>Z.endsWith(".jsonl")).sort((Z,V)=>{let W=b(K(Y,Z));return b(K(Y,V)).mtimeMs-W.mtimeMs});if(U.length===0){console.log("No sessions.");return}console.log(`
117
+ Sessions in ${Q} (${U.length} total):
118
+ `),console.log(" SESSION ID SIZE MODIFIED"),console.log(" ────────────────────────────────────── ──────── ────────────────");for(let Z of U){let V=Z.replace(".jsonl",""),W=b(K(Y,Z)),$=W.size<1024?`${W.size}B`:W.size<1048576?`${(W.size/1024).toFixed(0)}KB`:`${(W.size/1024/1024).toFixed(1)}MB`,_=W.mtime.toISOString().replace("T"," ").slice(0,16);console.log(` ${V} ${$.padStart(8)} ${_}`)}console.log()}else console.log(`
115
119
  anet session <command>
116
120
 
117
121
  ls List Claude Code sessions in current project
118
- `)}async function vB(){let Q=q[1],B=N();if(Q==="add"){let W=q[2],X=q[3];if(!W||!X){console.log(`
122
+ `)}async function uB(){let B=E[1],Q=N();if(B==="add"){let X=E[2],Y=E[3];if(!X||!Y){console.log(`
119
123
  anet channel add <type> <node-id> [options]
120
124
 
121
125
  Types: telegram
@@ -127,16 +131,16 @@ Options:
127
131
  Example:
128
132
  anet channel add telegram 指挥室 --bot-token 123:ABC --allow 7612221352
129
133
  anet channel add telegram 指挥室 # 交互式
130
- `);return}if(W!=="telegram")console.error(`P0 only supports telegram channels. Unsupported type: ${W}`),process.exit(1);m(X);let $=x(X),Y=WB(X);if(!$)console.error(`Node "${X}" not found. Create it first: anet create ${X} --runtime codex-sdk`),process.exit(1);let U=B["bot-token"],L=B.allow;if(!U)U=await C(`${W} Bot Token`);if(!L)L=await C("Allow User ID (发 @userinfobot 获取数字ID)","7612221352");if(b(),!U||!L)console.error("Error: bot-token and allow required"),process.exit(1);let Z=K(O(),X,"channels",W);M(Z,{recursive:!0}),M(K(Z,"inbox"),{recursive:!0});let V="TELEGRAM_BOT_TOKEN",_=K(Z,".env");T(_,`${V}=${U}
131
- `);try{TB(_,384)}catch{}if(T(K(Z,"access.json"),JSON.stringify({dmPolicy:"allowlist",allowFrom:[L],groups:{},pending:{}},null,2)+`
132
- `),!Y)console.error(`Node "${X}" not found. Create it first: anet create ${X} --runtime codex-sdk`),process.exit(1);if(Y.channels=Y.channels||[],!Y.channels.includes("telegram"))Y.channels.push("telegram");w(X,Y),console.log(`
133
- ✅ ${W} channel added to "${X}"`),console.log(` ${Z}/`),console.log(" config.json updated")}else if(Q==="ls"){let W=q[2],X=W?[W]:c(),$=!1;for(let Y of X){let U=K(O(),Y,"channels");if(!H(U))continue;let L=v(U).filter((Z)=>{try{return P(K(U,Z)).isDirectory()}catch{return!1}});if(L.length===0)continue;if(!$)console.log(`
134
+ `);return}if(X!=="telegram")console.error(`P0 only supports telegram channels. Unsupported type: ${X}`),process.exit(1);k(Y);let U=D(Y),Z=WB(Y);if(!U)console.error(`Node "${Y}" not found. Create it first: anet create ${Y} --runtime codex-sdk`),process.exit(1);let V=Q["bot-token"],W=Q.allow;if(!V)V=await z(`${X} Bot Token`);if(!W)W=await z("Allow User ID (发 @userinfobot 获取数字ID)","7612221352");if(R(),!V||!W)console.error("Error: bot-token and allow required"),process.exit(1);let $=K(I(),Y,"channels",X);A($,{recursive:!0}),A(K($,"inbox"),{recursive:!0});let _="TELEGRAM_BOT_TOKEN",q=K($,".env");J(q,`${_}=${V}
135
+ `);try{YB(q,384)}catch{}if(J(K($,"access.json"),JSON.stringify({dmPolicy:"allowlist",allowFrom:[W],groups:{},pending:{}},null,2)+`
136
+ `),!Z)console.error(`Node "${Y}" not found. Create it first: anet create ${Y} --runtime codex-sdk`),process.exit(1);if(Z.channels=Z.channels||[],!Z.channels.includes("telegram"))Z.channels.push("telegram");C(Y,Z),console.log(`
137
+ ✅ ${X} channel added to "${Y}"`),console.log(` ${$}/`),console.log(" config.json updated")}else if(B==="ls"){let X=E[2],Y=X?[X]:c(),U=!1;for(let Z of Y){let V=K(I(),Z,"channels");if(!L(V))continue;let W=v(V).filter(($)=>{try{return b(K(V,$)).isDirectory()}catch{return!1}});if(W.length===0)continue;if(!U)console.log(`
134
138
  Node Channels:
135
- `),$=!0;for(let Z of L){let V=K(U,Z,"access.json"),_="";if(H(V))try{_=JSON.parse(J(V,"utf-8")).allowFrom?.join(", ")||""}catch{}console.log(` ${Y.padEnd(20)} ${Z.padEnd(12)} allow: ${_||"(none)"}`)}}if(!$)console.log("No channels. Add one: anet channel add telegram <node-id>");console.log()}else console.log(`
139
+ `),U=!0;for(let $ of W){let _=K(V,$,"access.json"),q="";if(L(_))try{q=JSON.parse(G(_,"utf-8")).allowFrom?.join(", ")||""}catch{}console.log(` ${Z.padEnd(20)} ${$.padEnd(12)} allow: ${q||"(none)"}`)}}if(!U)console.log("No channels. Add one: anet channel add telegram <node-id>");console.log()}else console.log(`
136
140
  anet channel <command>
137
141
 
138
142
  add <type> <node-id> Add channel to a node
139
143
  ls [node-id] List channels
140
144
 
141
145
  Data: .anet/nodes/<node-id>/channels/<type>/
142
- `)}switch(y){case"init":if(q[1]==="project")NB();else if(q[1]==="profile")AB();else GB();break;case"create":$B();break;case"server":PB();break;case"start":o();break;case"resume":xB();break;case"import":wB();break;case"channel":vB();break;case"session":bB();break;case"ls":case"list":CB();break;case"run":DB();break;case"-v":case"--version":case"version":{let Q=JSON.parse(J(K(new URL(".",import.meta.url).pathname,"..","..","package.json"),"utf-8"));console.log(`anet v${Q.version}`);break}case"--help":case"-h":case void 0:s();break;default:if(x(y))q.unshift("start"),o();else console.error(`Unknown: ${y}`),s(),process.exit(1)}
146
+ `)}switch(S){case"init":if(E[1]==="project")FB();else if(E[1]==="profile")await OB();else await NB();break;case"create":await VB();break;case"server":kB();break;case"start":XB();break;case"resume":bB();break;case"import":yB();break;case"channel":uB();break;case"session":SB();break;case"ls":case"list":vB();break;case"run":jB();break;case"-v":case"--version":case"version":{let B=JSON.parse(G(K(new URL(".",import.meta.url).pathname,"..","..","package.json"),"utf-8"));console.log(`anet v${B.version}`);break}case"--help":case"-h":case void 0:QB();break;default:if(D(S))E.unshift("start"),XB();else console.error(`Unknown: ${S}`),QB(),process.exit(1)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/agent-network",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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",