@sleep2agi/agent-node 1.4.2 → 2.0.0
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/cli.js +2 -2
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{createRequire as wz}from"node:module";var l=wz(import.meta.url);import{readFileSync as S,existsSync as d,writeFileSync as m,chmodSync as Dz}from"fs";import{join as D}from"path";import{hostname as i,homedir as vz}from"os";import{mkdirSync as Kz,appendFileSync as Az}from"fs";var Zz=vz(),H=process.argv.slice(2),j={},$z=[],Hz="
|
|
2
|
+
import{createRequire as wz}from"node:module";var l=wz(import.meta.url);import{readFileSync as S,existsSync as d,writeFileSync as m,chmodSync as Dz}from"fs";import{join as D}from"path";import{hostname as i,homedir as vz}from"os";import{mkdirSync as Kz,appendFileSync as Az}from"fs";var Zz=vz(),H=process.argv.slice(2),j={},$z=[],Hz="2.0.0";for(let z=0;z<H.length;z++){if(H[z]==="--version"||H[z]==="-v")console.log(`agent-node v${Hz}`),process.exit(0);if(H[z]==="-h"||H[z]==="--help")console.log(`
|
|
3
3
|
@sleep2agi/agent-node — AI Agent 节点,一行命令加入 CommHub 网络
|
|
4
4
|
|
|
5
5
|
用法:
|
|
@@ -40,4 +40,4 @@ ${z}
|
|
|
40
40
|
${Q.map((X)=>`- 图片: ${X}`).join(`
|
|
41
41
|
`)}`;return{text:$.trim(),images:Q}}async function nz(z,Z){if(!mz(z.channel,Z))return;let $=Z.chat?.id,Q=Z.message_id,Y=`telegram:${Gz(Z)}`,{text:X,images:q}=await rz(z,Z);if(!$||!Q||!X)return;b(`[TG] processing: ${X.slice(0,80)}`);try{let W=await Bz(X,Y,q);await e(z,$,W,Q),B(`→ [${Y}] ${W.slice(0,100)}`)}catch(W){O(`telegram task failed: ${W.message}`),await e(z,$,`处理出错: ${W.message}`,Q).catch(()=>{})}}async function lz(z){let Z={channel:z,apiBase:`https://api.telegram.org/bot${z.token}`,fileBase:`https://api.telegram.org/file/bot${z.token}`,offset:0};try{let W=await u(Z,"getMe",{});B(`Telegram bot: @${W.username} (${W.first_name})`)}catch(W){O(`Telegram token 无效: ${W.message}`),process.exit(1)}let $=D(z.dir,"state.json");try{let W=JSON.parse(S($,"utf-8"));if(W.offset)Z.offset=W.offset,b(`Telegram offset restored: ${Z.offset}`)}catch{}let Q=()=>{try{m($,JSON.stringify({offset:Z.offset})+`
|
|
42
42
|
`)}catch{}},Y=!1,X=[];async function q(){if(Y)return;Y=!0;while(X.length){let{msg:W,updateId:K}=X.shift();try{await nz(Z,W),Z.offset=K+1,Q()}catch(G){O(`TG handle: ${G.message}`)}}Y=!1}B(`Telegram polling: ${z.dir}`);while(!0)try{let K=await(await fetch(`${Z.apiBase}/getUpdates?offset=${Z.offset}&timeout=30`)).json();if(!K.ok)throw Error(K.description||"getUpdates failed");for(let G of K.result||[])if(Z.offset=G.update_id+1,G.message){let J=G.message,v=Gz(J),f=J.text||J.caption||"";if(B(`← TG [${v}] ${f.slice(0,80)}${J.photo?" +img":""}${J.document?" +file":""}`),J.chat?.id&&J.message_id)u(Z,"setMessageReaction",{chat_id:J.chat.id,message_id:J.message_id,reaction:[{type:"emoji",emoji:X.length>0?"⏳":"\uD83D\uDC40"}]}).catch(()=>{});X.push({msg:J,updateId:G.update_id}),q()}}catch(W){L(`Telegram polling error: ${W.message}`),await new Promise((K)=>setTimeout(K,3000))}}async function iz(){let z=`${r}/events/${encodeURIComponent(F)}`,Z=3000;while(!0){b(`SSE connecting: ${z}`);try{let $={Accept:"text/event-stream","Cache-Control":"no-cache"};if(x)$.Authorization=`Bearer ${x}`;let Q=await fetch(z,{headers:$});if(!Q.ok||!Q.body){if(Q.status===401)O("SSE 401: token 无效或未配置。检查 ~/.anet/config.json 的 token 字段");else L(`SSE failed: ${Q.status}`);await new Promise((W)=>setTimeout(W,Z)),Z=Math.min(Z*1.5,60000);continue}Z=3000;let Y=Q.body.getReader(),X=new TextDecoder,q="";while(!0){let{done:W,value:K}=await Y.read();if(W)break;q+=X.decode(K,{stream:!0});let G=q.split(`
|
|
43
|
-
`);q=G.pop()||"";for(let J of G){if(!J.startsWith("data: "))continue;try{let v=JSON.parse(J.slice(6));if(v.type==="connected"){B("SSE connected");continue}if(["new_task","
|
|
43
|
+
`);q=G.pop()||"";for(let J of G){if(!J.startsWith("data: "))continue;try{let v=JSON.parse(J.slice(6));if(v.type==="connected"){B("SSE connected");continue}if(["new_task","broadcast"].includes(v.type))B(`← SSE ${v.type}`),await dz()}catch{}}}}catch($){L(`SSE error: ${$.message}`)}b(`SSE reconnecting (${Z/1000}s)...`),await new Promise(($)=>setTimeout($,Z)),Z=Math.min(Z*1.5,60000)}}B("启动");B(` runtime: ${yz}`);B(` model: ${y||(P==="codex"?"gpt-5.4":"claude-sonnet-4-6")} ${y?"":"(default)"}`);B(` hub: ${r}${x?" (auth)":" (no auth!)"}`);B(` tools: ${M.length?`[${M.join(",")}]`:"(none)"}`);B(` channels:${T.length?` telegram(${T.map((z)=>z.dir).join(",")})`:" (none)"}`);B(` session: ${N||"(new)"}`);B(` log-dir: ${n}`);await Ez();B("已注册到 CommHub");setInterval(()=>I("idle").catch(()=>{}),180000);var jz=async()=>{B("shutting down..."),await I("offline").catch(()=>{}),process.exit(0)};process.on("SIGINT",jz);process.on("SIGTERM",jz);for(let z of T)lz(z);iz();
|