tg-agent 1.2.0 → 1.2.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.
- package/dist/index.js +14 -14
- package/dist/mcp.js +5 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
`)}function
|
|
3
|
-
`)}function
|
|
4
|
-
`),g)}}const
|
|
1
|
+
import U from"node:path";import X from"node:fs/promises";import $e from"node-telegram-bot-api";import{discoverAuthStorage as ve,discoverModels as he}from"@mariozechner/pi-coding-agent";import{getOAuthProviders as Me}from"@mariozechner/pi-ai";import{config as m,assertConfig as Se}from"./config.js";import{resolveApiKeyForProvider as _e,resolveProxyInfo as ke}from"./auth.js";import{applyFetchProxy as Ae}from"./proxy.js";import{runPiAgentPrompt as xe}from"./piAgentRunner.js";import{readCodexOAuth as Pe}from"./codexAuth.js";import{formatMcpTarget as Ce,freezeMcpCatalog as J,getMcpCatalog as Z,loadMcpServers as be,loadMcpServersSync as Ee,probeMcpServer as Re,refreshMcpCatalog as De}from"./mcp.js";import{appendMessage as Y,closeSession as ze,createSession as F,deleteSessionFile as j,getActiveSession as z,listSessions as T,loadChatState as I,pruneExpiredSessions as ee,resetSession as Le,saveChatState as $,setActiveSession as Ne,setWorkspaceDir as Ue}from"./sessionStore.js";import{chunkText as je,createQueueMap as We,createSemaphore as Oe,ensureDir as K,expandHome as Be,nowMs as C,shortId as te}from"./utils.js";Se();const v=new $e(m.telegramToken,{polling:!0}),ne=We(),Fe=Oe(m.maxConcurrent),G=new Map;function Ke(e,t){G.set(e,t)}function Ge(e){G.delete(e)}function Qe(e){const t=e.trim();if(t){if(t==="Markdown"||t==="MarkdownV2"||t==="HTML")return t;console.warn(`[tg-agent] invalid TELEGRAM_PARSE_MODE=${t}, ignoring`)}}const Ve="image/jpeg";function qe(){return U.join(m.agentDir,"uploads")}function He(e){return e.length===0?null:e.reduce((t,n)=>{const o=t.file_size??t.width*t.height;return(n.file_size??n.width*n.height)>o?n:t})}function Xe(e){return!!e?.startsWith("image/")}function Je(e){switch(U.extname(e).toLowerCase()){case".png":return"image/png";case".jpg":case".jpeg":return"image/jpeg";case".webp":return"image/webp";case".gif":return"image/gif";default:return}}function Ze(e){if(!Number.isFinite(e)||e<=0)return"0 B";const t=["B","KB","MB","GB"];let n=e,o=0;for(;n>=1024&&o<t.length-1;)n/=1024,o+=1;return`${n.toFixed(n>=10||o===0?0:1)} ${t[o]}`}function Ye(e){return e.length===0?"":e.map(t=>{const n=[];t.mimeType&&n.push(t.mimeType),t.bytes>0&&n.push(Ze(t.bytes));const o=n.length>0?` (${n.join(", ")})`:"";return`Attachment saved: ${t.path}${o}`}).join(`
|
|
2
|
+
`)}function Te(e){const t=[];if(e.photo&&e.photo.length>0){const n=He(e.photo);n&&t.push({kind:"photo",fileId:n.file_id,fileSize:n.file_size})}if(e.document&&t.push({kind:"document",fileId:e.document.file_id,fileName:e.document.file_name,mimeType:e.document.mime_type,fileSize:e.document.file_size}),e.audio){const n=e.audio.mime_type?.split("/")[1]??"mp3",o=e.audio.title??e.audio.performer??"audio";t.push({kind:"document",fileId:e.audio.file_id,fileName:`${o}.${n}`,mimeType:e.audio.mime_type,fileSize:e.audio.file_size})}if(e.voice&&t.push({kind:"document",fileId:e.voice.file_id,fileName:`voice.${e.voice.mime_type?.split("/")[1]??"ogg"}`,mimeType:e.voice.mime_type,fileSize:e.voice.file_size}),e.video){const n=e.video.mime_type?.split("/")[1]??"mp4";t.push({kind:"document",fileId:e.video.file_id,fileName:`video.${n}`,mimeType:e.video.mime_type,fileSize:e.video.file_size})}return e.video_note&&t.push({kind:"document",fileId:e.video_note.file_id,fileName:"video_note.mp4",mimeType:"video/mp4",fileSize:e.video_note.file_size}),t}async function Ie(e){if(e.length===0)return{resolved:[],images:[]};const t=qe();await K(t);const n=[],o=[];for(const i of e)try{const c=await v.downloadFile(i.fileId,t),p=await X.stat(c),a=i.fileName??U.basename(c),s=i.mimeType??Je(c),d=i.kind==="photo"||Xe(s),r={path:c,name:a,mimeType:s,bytes:p.size,isImage:d};if(n.push(r),d){const u=await X.readFile(c);o.push({type:"image",data:u.toString("base64"),mimeType:s??Ve})}}catch(c){console.warn("[tg-agent] attachment download failed",c)}return{resolved:n,images:o}}const et=new Set(["_","*","[","]","(",")","~","`",">","#","+","-","=","|","{","}",".","!"]);function L(e,t){let n=0;for(let o=0;o<e.length;o+=1){const i=e[o];if(i==="\\"){o+=1;continue}i===t&&(n+=1)}return n}function tt(e){if(L(e,"*")%2!==0||L(e,"_")%2!==0||L(e,"`")%2!==0)return!1;const i=L(e,"["),c=L(e,"]");return i===c}function nt(e){for(let t=0;t<e.length;t+=1){const n=e[t];if(n==="\\"){t+=1;continue}if(et.has(n))return!1}return!0}function oe(e){const t=Qe(m.telegramParseMode);return t?[t]:nt(e)?["MarkdownV2","Markdown"]:tt(e)?["Markdown"]:[]}const ot=600*1e3,A=new Map,Q=new Set,it=new Map([["codex","openai-codex"],["antigravity","google-antigravity"],["gemini","google-gemini-cli"],["gemini-cli","google-gemini-cli"]]),rt={"codex-mini-latest":"gpt-5.1-codex-mini","codex-max-latest":"gpt-5.1-codex-max","codex-latest":"gpt-5.2-codex"};function E(e){const t=e.trim().toLowerCase();return it.get(t)??t}function st(e,t){return e!=="openai-codex"?t:rt[t]??t}async function R(){await K(m.agentDir);const e=ve(m.agentDir),t=Pe();t&&e.setRuntimeApiKey("openai-codex",t.accessToken);const n=he(e,m.agentDir);return{authStorage:e,modelRegistry:n}}function ie(e){const t=A.get(e);t&&(clearTimeout(t.timeoutId),A.delete(e))}function at(e,t){const n=A.get(e);n&&(clearTimeout(n.timeoutId),A.delete(e),n.reject(new Error(t)))}async function re(e,t,n,o){if(A.has(e))throw new Error("Login is already awaiting input.");const i=n.placeholder?`${n.message} (${n.placeholder})`:n.message;return await l(t,i,o),await new Promise((c,p)=>{const a=setTimeout(()=>{A.delete(e),p(new Error("Login prompt timed out."))},ot);A.set(e,{resolve:c,reject:p,timeoutId:a,chatId:t})})}async function se(e){const t=String(e),n=G.get(t);if(!n){await l(e,"No active request to stop.");return}n.cancelRequested=!0,n.abortRequested=!0,n.status&&n.status.update("Cancelled by user.",!0),n.abort&&n.abort(),(!n.status||n.chatId!==e)&&await l(e,"Stopping current request...")}function ct(){console.log(`[tg-agent] modelProvider=${m.modelProvider} modelRef=${m.modelRef} sessionDir=${m.sessionDir} maxConcurrent=${m.maxConcurrent}`),console.log(`[tg-agent] agentDir=${m.agentDir} workspaceDir=${m.workspaceDir}`);const e=Ee(m.agentDir),t=e.length>0?`on (${e.length})`:"off";console.log(`[tg-agent] tools fetchMaxBytes=${m.fetchMaxBytes} fetchTimeoutMs=${m.fetchTimeoutMs} mcp=${t}`),console.log(`[tg-agent] proxy url=${m.proxyUrl||"(none)"} fetchProxy=${m.fetchProxyUrl||"(none)"}`),console.log(`[tg-agent] tls extra_ca=${m.tlsExtraCaCerts||"(empty)"} reject_unauthorized=${m.tlsRejectUnauthorized===null?"(unset)":String(m.tlsRejectUnauthorized)}`);try{const{source:o}=_e(m.modelProvider);console.log(`[tg-agent] authSource=${o}`)}catch(o){const i=o instanceof Error?o.message:String(o);console.warn(`[tg-agent] authSource=missing (${i})`)}const n=ke();console.log(n?`[tg-agent] proxyUrl=${n.url} kind=${n.kind} source=${n.source}`:"[tg-agent] proxyUrl=(none)")}ct(),K(m.agentDir).catch(e=>{console.warn(`[tg-agent] ensure agentDir failed: ${e instanceof Error?e.message:String(e)}`)}),(async()=>{try{await Z(m.agentDir,{timeoutMs:3e3,maxBytes:m.fetchMaxBytes,maxChars:2e3}),J()}catch(e){const t=e instanceof Error?e.message:String(e);console.warn(`[tg-agent] mcp catalog warmup failed: ${t}`)}})();const W=Ae();console.log(W?`[tg-agent] fetchProxy=${W.url} kind=${W.kind} source=${W.source}`:"[tg-agent] fetchProxy=(none)");function V(e){if(!e.startsWith("/"))return null;const t=e.trim(),[n,...o]=t.split(" ");return{command:n.split("@")[0].slice(1).toLowerCase(),args:o.join(" ").trim()}}async function ae(e,t,n){let o=z(e);return o||(o=F(e,""),await $(e),await l(t,`Created session ${o.id}.`,n)),o}function lt(e,t){const n=e.getAll(),o=new Map;for(const i of n){const c=o.get(i.provider)??{count:0,auth:t.hasAuth(i.provider)};c.count+=1,o.set(i.provider,c)}return Array.from(o.entries()).sort((i,c)=>i[0].localeCompare(c[0])).map(([i,c])=>({label:`${i} (${c.count}, ${c.auth?"ok":"no auth"})`,command:`/provider ${i}`}))}function ut(e,t,n=[]){const o=e.getAll().filter(a=>a.provider===t).sort((a,s)=>a.id.localeCompare(s.id));if(n.length===0)return o.map(a=>({label:a.id,command:`/model ${t}/${a.id}`}));const i=new Map(o.map(a=>[a.id,a])),c=new Set,p=[];for(const a of n){const s=i.get(a);!s||c.has(s.id)||(c.add(s.id),p.push(s))}for(const a of o)c.has(a.id)||p.push(a);return p.map(a=>({label:a.id,command:`/model ${t}/${a.id}`}))}function ft(e,t,n=3){const o=new Set,i=[],c=T(e);for(const p of c){if(p.modelProvider!==t)continue;const a=p.modelId;if(!(!a||o.has(a))&&(o.add(a),i.push(a),i.length>=n))break}return i}function dt(e){const t=e.trim();if(!t)return null;if(t.includes("/")){const[n,o]=t.split("/",2);return!n||!o?null:{provider:E(n),modelId:o.trim()}}return{modelId:t}}const mt=6e4,ce=new Map;function le(e,t){const n=Date.now(),o=ce.get(e)??0;n-o<mt||(ce.set(e,n),console.warn(`[tg-agent] unauthorized user=${e} chat=${t}`))}function ue(e){const t=m.telegramAllowedUsers;return!t||t.size===0?!0:t.has(e)}function pt(e){return e.length===0?"No sessions found.":["Sessions:",...e.map(n=>{const o=new Date(n.updatedAt).toISOString();return`- ${n.id} | ${n.title} | updated ${o}`})].join(`
|
|
3
|
+
`)}function O(e,t,n){const o=String(e);return t!==void 0&&!n?null:t!==void 0&&n?`${o}_${t}`:o}function q(e,t){if(t.workspaceDir)return t.workspaceDir;const n=m.workspaceMappings.get(e);return n||m.workspaceDir}async function fe(e,t,n,o){const i={};return n&&(i.parse_mode=n),o!==void 0&&(i.message_thread_id=o),await v.sendMessage(e,t,i)}async function de(e,t,n,o,i){const c={chat_id:e,message_id:t};o&&(c.parse_mode=o),i!==void 0&&(c.message_thread_id=i),await v.editMessageText(n,c)}async function me(e,t){let n;for(const o of t)try{return await e(o)}catch(i){n=i;const c=i instanceof Error?i.message:String(i);console.warn(`[tg-agent] parse_mode ${o} failed, trying fallback: ${c}`)}try{return await e(void 0)}catch(o){if(n){const i=o instanceof Error?o.message:String(o);console.warn(`[tg-agent] parse_mode fallback to plain failed: ${i}`)}throw o}}async function gt(e,t,n){return await me(o=>fe(e,t,o,n),oe(t))}async function wt(e,t,n,o){await me(i=>de(e,t,n,i,o),oe(n))}async function l(e,t,n){const o=je(t,3900);for(const i of o)await gt(e,i,n)}const pe=1800*1e3,ge=2e3,yt=60,S=new Map;function $t(){const e=C()-pe;for(const[o,i]of S)i.createdAt<e&&S.delete(o);if(S.size<=ge)return;const t=Array.from(S.entries()).sort((o,i)=>o[1].createdAt-i[1].createdAt),n=S.size-ge;for(let o=0;o<n;o+=1)S.delete(t[o][0])}function vt(e){$t();let t=te();for(;S.has(t);)t=te();return S.set(t,{command:e,createdAt:C()}),t}function ht(e){if(!e.startsWith("cmd:"))return null;const t=e.slice(4),n=S.get(t);return n?C()-n.createdAt>pe?(S.delete(t),null):n.command:null}function Mt(e,t){if(e.length<=t)return[e];const n=[];for(let o=0;o<e.length;o+=t)n.push(e.slice(o,o+t));return n}function St(e,t){const n=[];let o=[];for(const i of e){const c=vt(i.command);o.push({text:i.label,callback_data:`cmd:${c}`}),o.length>=t&&(n.push(o),o=[])}return o.length>0&&n.push(o),n}async function _t(e,t,n,o){const i={reply_markup:{inline_keyboard:n}};return o!==void 0&&(i.message_thread_id=o),await v.sendMessage(e,t,i)}async function we(e,t,n,o){const i=o?.perRow??1,c=o?.pageSize??yt,p=o?.footer,a=o?.messageThreadId,s=Mt(n,c);for(let d=0;d<s.length;d+=1){const r=s[d],u=s.length>1?` (page ${d+1}/${s.length})`:"",f=[`${t}${u}`];p&&f.push(p);const g=St(r,i);await _t(e,f.join(`
|
|
4
|
+
`),g,a)}}const kt=3900,At=1200;function H(e,t=kt){return e.length<=t?{text:e,truncated:!1}:{text:`${e.slice(0,Math.max(0,t-15))}...
|
|
5
5
|
|
|
6
|
-
[truncated]`,truncated:!0}}async function
|
|
7
|
-
args: ${
|
|
8
|
-
`)}async function
|
|
9
|
-
`);await l(e,r);return}case"new":{try{const
|
|
6
|
+
[truncated]`,truncated:!0}}async function xt(e,t){const n="Working...",i=(await fe(e,n,void 0,t)).message_id;let c=n,p=0,a=Promise.resolve();const s=(r,u=!1)=>{const f=Date.now();!u&&f-p<At||r!==c&&(c=r,p=f,a=a.then(async()=>{await de(e,i,r,void 0,t)}).catch(g=>{console.warn("[tg-agent] status edit failed",g)}))},d=r=>{c=r,p=Date.now(),a=a.then(async()=>{await wt(e,i,r,t)}).catch(u=>{console.warn("[tg-agent] status edit failed",u)})};return{update:(r,u=!1)=>{const f=H(r).text;s(f,u)},finalize:async r=>{const u=H(r).text;d(u),await a},fail:async r=>{const u=H(r).text;d(u),await a}}}function Pt(e){switch(e.type){case"agent_start":return"Working...";case"tool_start":return`Running tool: ${e.name}
|
|
7
|
+
args: ${Ct(e.args)}`;case"tool_end":return e.ok?`Tool finished: ${e.name} (${e.durationMs}ms)`:`Tool failed: ${e.name} (${e.durationMs}ms)`;case"message_start":return e.role==="assistant"?"Generating reply...":null;case"message_end":return e.role==="assistant"?"Finalizing reply...":null;case"heartbeat":return`Working... ${Math.max(1,Math.round(e.elapsedMs/1e3))}s`;default:return null}}function Ct(e){try{const t=new WeakSet,n=200,o=12,i=12,c=3,p=(d,r)=>{if(d==null)return d;if(typeof d=="string")return d.length>n?`${d.slice(0,n)}...`:d;if(typeof d=="number"||typeof d=="boolean")return d;if(typeof d=="bigint")return d.toString();if(typeof d=="function")return"[function]";if(typeof d!="object")return String(d);if(t.has(d))return"[circular]";if(r>=c)return"[truncated]";if(t.add(d),Array.isArray(d)){const g=d.slice(0,i).map(w=>p(w,r+1));return d.length>i&&g.push("[truncated]"),g}const u=Object.entries(d),f={};for(const[g,w]of u.slice(0,o))f[g]=p(w,r+1);return u.length>o&&(f._truncated=!0),f},a=p(e,0),s=JSON.stringify(a);return s?s.length>700?`${s.slice(0,700)}...`:s:"null"}catch{return"[unavailable]"}}function B(e){const t=e instanceof Error?e.message:String(e),n=e?.cause;return n instanceof Error?`${t} (${n.message})`:n?`${t} (${String(n)})`:t}function bt(e,t=140){return e.length<=t?e:`${e.slice(0,t-3)}...`}async function Et(){const e=await be(m.agentDir);if(e.length===0)return"No MCP servers configured. Add [mcp_servers.*] to ~/.tg-agent/config.toml.";const t=await Promise.all(e.map(o=>Re(o))),n=["MCP servers:"];for(let o=0;o<e.length;o+=1){const i=e[o],c=t[o],p=c.ok?"ok":`error: ${bt(c.error??"unknown")}`;n.push(`- ${i.name} (${i.type}) ${Ce(i)} status=${p} (${c.durationMs}ms)`)}return n.join(`
|
|
8
|
+
`)}async function ye(e,t,n,o,i){const c=i,a=O(e,c,c!==void 0);if(!a)return;const s=await I(a),d=ee(s);switch(d.length>0&&(await $(s),await Promise.all(d.map(r=>j(a,r).catch(u=>{console.warn(`[tg-agent] cleanup session file failed id=${r}`,u)})))),n){case"start":case"help":{const r=["Commands:","/new [title] - create a new session","/list - list sessions","/use <id> - switch active session","/close [id] - close a session (default: active)","/reset - clear active session history","/workspace [path] - view or set workspace directory","/providers - list available providers","/models [provider] - list models for provider","/provider <name> - set provider for current session","/model <provider>/<model> - set model for current session","/mcp - list configured MCP servers","/mcp refresh - reload MCP catalog","/status - show session and workspace settings","/login <provider> - login to OAuth provider","/logout <provider> - logout from provider","/stop - stop the current running request","","Tips:","Send images or files to attach them to the prompt."].join(`
|
|
9
|
+
`);await l(e,r,i);return}case"new":{try{const u=F(s,o||"");await $(s),await l(e,`Created session ${u.id} (${u.title}).`,i)}catch(r){await l(e,r.message,i)}return}case"list":{await $(s),await l(e,pt(T(s)),i);return}case"use":{if(!o){await l(e,"Usage: /use <id>",i);return}if(!Ne(s,o)){await l(e,`Session not found: ${o}`,i);return}await $(s),await l(e,`Active session set to ${o}.`,i);return}case"close":{const r=o||s.activeSessionId;if(!r){await l(e,"No active session to close.",i);return}if(!ze(s,r)){await l(e,`Session not found: ${r}`,i);return}await $(s),await j(a,r).catch(f=>{console.warn(`[tg-agent] delete session file failed id=${r}`,f)}),await l(e,`Closed session ${r}.`,i);return}case"reset":{const r=z(s);if(!r){await l(e,"No active session.",i);return}Le(r),await $(s),await j(a,r.id).catch(u=>{console.warn(`[tg-agent] reset session file failed id=${r.id}`,u)}),await l(e,`Reset session ${r.id}.`,i);return}case"workspace":{if(!o){const u=q(a,s),f=s.workspaceDir?"chat":m.workspaceMappings.has(a)?"config":"default";await l(e,`Workspace: ${u} (source: ${f})`,i);return}const r=U.resolve(Be(o.trim()));Ue(s,r),await $(s),await l(e,`Workspace set to: ${r}`,i);return}case"providers":{const{authStorage:r,modelRegistry:u}=await R(),f=u.getError(),g=lt(u,r);if(g.length===0){const y=f?`Warning: ${f}
|
|
10
10
|
|
|
11
|
-
No providers found.`:"No providers found.";await l(e,
|
|
12
|
-
`),
|
|
13
|
-
`);await l(e,
|
|
14
|
-
`));return}const
|
|
15
|
-
`))},onPrompt:
|
|
11
|
+
No providers found.`:"No providers found.";await l(e,y,i);return}const w=[];f&&w.push(`Warning: ${f}`),w.push("Providers:"),await we(e,w.join(`
|
|
12
|
+
`),g,{perRow:1,footer:"Tap a provider to set it. Use /models <provider> to list models.",messageThreadId:i});return}case"models":{const{authStorage:r,modelRegistry:u}=await R();let f=o?E(o):"";if(!f){const x=z(s);if(!x?.modelProvider){await l(e,"Usage: /models <provider>",i);return}f=x.modelProvider}const g=r.hasAuth(f),w=ft(s,f),y=ut(u,f,w);if(y.length===0){await l(e,`No models found for provider ${f}.`,i);return}await we(e,`Models for ${f} (auth: ${g?"ok":"missing"}):`,y,{perRow:1,footer:"Tap a model to set it.",messageThreadId:i});return}case"provider":{if(!o){await l(e,"Usage: /provider <name>",i);return}const r=E(o),{modelRegistry:u}=await R();if(!u.getAll().some(w=>w.provider===r)){await l(e,`Unknown provider: ${r}`,i);return}const g=await ae(s,e,i);g.modelProvider=r,g.modelId=void 0,g.updatedAt=C(),await $(s),await l(e,`Session ${g.id} provider set to ${r}.`,i);return}case"model":{const r=dt(o);if(!r){await l(e,"Usage: /model <provider>/<model>",i);return}const u=await ae(s,e,i),f=E(r.provider??u.modelProvider??"");if(!f){await l(e,"Usage: /model <provider>/<model>",i);return}const{modelRegistry:g}=await R(),w=st(f,r.modelId);if(!g.find(f,w)){await l(e,`Model not found: ${f}/${w}`,i);return}u.modelProvider=f,u.modelId=w,u.updatedAt=C(),await $(s),await l(e,`Session ${u.id} model set to ${f}/${w}.`,i);return}case"status":{const r=z(s);if(!r){await l(e,"No active session.",i);return}const u=m.modelRef.includes("/")?m.modelRef:`${m.modelProvider}/${m.modelRef}`,f=r.modelProvider||r.modelId?`Model: ${r.modelProvider??"?"}/${r.modelId??"?"}`:`Model: ${u} (default)`,g=q(a,s),w=s.workspaceDir?"chat":m.workspaceMappings.has(a)?"config":"default",y=[`Session: ${r.id} (${r.title})`,f,`Workspace: ${g} (${w})`].join(`
|
|
13
|
+
`);await l(e,y,i);return}case"mcp":{if(o.trim().toLowerCase()==="refresh"){De(),await Z(m.agentDir,{timeoutMs:5e3,maxBytes:m.fetchMaxBytes,maxChars:3e3}),J(),await l(e,"MCP catalog refreshed.",i);return}const r=await Et();await l(e,r,i);return}case"login":{const r=Me();if(!o){const g=r.map(w=>`- ${w.id} (${w.name})`);await l(e,["OAuth providers:",...g].join(`
|
|
14
|
+
`),i);return}const u=E(o);if(Q.has(t)){await l(e,"Login already in progress.",i);return}const f=r.find(g=>g.id===u);if(!f){await l(e,`Unknown OAuth provider: ${u}`,i);return}Q.add(t);try{const{authStorage:g}=await R();await l(e,`Starting login for ${f.id}...`,i),await g.login(f.id,{onAuth:({url:w,instructions:y})=>{const x=["Open this URL in your browser:",w];y&&x.push("",y),l(e,x.join(`
|
|
15
|
+
`),i)},onPrompt:w=>re(t,e,w,i),onProgress:w=>{l(e,w,i)},onManualCodeInput:()=>re(t,e,{message:"Paste the authorization code:"},i)}),await l(e,`Login completed for ${f.id}.`,i)}catch(g){const w=B(g);await l(e,`Login failed: ${w}`,i)}finally{Q.delete(t),ie(t)}return}case"logout":{if(!o){await l(e,"Usage: /logout <provider>",i);return}const r=E(o),{authStorage:u}=await R();u.logout(r),await l(e,`Logged out from ${r}.`,i);return}default:await l(e,`Unknown command: ${n}`,i)}}async function Rt(e,t,n,o=[],i){const c=V(n);if(c){await ye(e,t,c.command,c.args,i);return}const a=O(e,i,i!==void 0);if(!a)return;const s=await I(a),d=ee(s);d.length>0&&(await $(s),await Promise.all(d.map(h=>j(a,h).catch(b=>{console.warn(`[tg-agent] cleanup session file failed id=${h}`,b)}))));let r=z(s);if(!r)try{r=F(s,""),await $(s),await l(e,`Created session ${r.id}.`,i)}catch(h){await l(e,h.message,i);return}const{resolved:u,images:f}=await Ie(o);if(o.length>0&&u.length===0&&!n.trim()){await l(e,"Failed to download attachments.",i);return}const g=Ye(u),w=[];n.trim()&&w.push(n.trim()),g&&w.push(g);const y=w.join(`
|
|
16
16
|
|
|
17
|
-
`)||"Attachment received.",
|
|
17
|
+
`)||"Attachment received.",x={role:"user",content:y,ts:C()};Y(r,x,m.maxHistoryMessages),await $(s),console.log(`[tg-agent] request user=${t} chat=${a} session=${r.id} messages=${r.messages.length} textLen=${y.length} attachments=${u.length} provider=${r.modelProvider??"-"} model=${r.modelId??"-"}`);let M=null;try{M=await xt(e,i)}catch(h){console.warn("[tg-agent] status message failed",h)}const D={sessionId:r.id,chatId:e,messageThreadId:i,status:M,abortRequested:!1,cancelRequested:!1};Ke(a,D);try{const h=await Fe(async()=>{const N=await xe({chatId:a,sessionId:r.id,prompt:y,images:f,systemPrompt:m.systemPrompt,modelProvider:r.modelProvider,modelId:r.modelId,workspaceDir:q(a,s),telegram:{chatId:e,sendPhoto:async(_,k)=>{const P={};k&&(P.caption=k),i!==void 0&&(P.message_thread_id=i),await v.sendPhoto(e,_,P)},sendDocument:async(_,k)=>{const P={};k&&(P.caption=k),i!==void 0&&(P.message_thread_id=i),await v.sendDocument(e,_,P)}},onAbortReady:_=>{D.abort=_,D.abortRequested&&_()},onStatus:M?_=>{const k=Pt(_);k&&M?.update(k)}:void 0});return console.log(`[tg-agent] response session=${r.id} model=${N.modelProvider}/${N.modelId} file=${N.sessionFile}`),N.text});if(D.cancelRequested){M?await M.finalize("Cancelled by user."):await l(e,"Cancelled by user.",i);return}const b={role:"assistant",content:h,ts:C()};Y(r,b,m.maxHistoryMessages),await $(s),M?await M.finalize(h):await l(e,h,i)}catch(h){if(console.error("[tg-agent] runModel error",h),D.cancelRequested){M?await M.fail("Cancelled by user."):await l(e,"Cancelled by user.",i);return}const b=B(h);M?await M.fail(`Error: ${b}`):await l(e,`Error: ${b}`,i)}finally{Ge(a)}}v.on("callback_query",e=>{const t=e.data??"",n=e.message?.chat.id;if(!e.from?.id||!n){v.answerCallbackQuery(e.id);return}const o=String(e.from.id);if(!ue(o)){le(o,n),v.answerCallbackQuery(e.id);return}if(!t.startsWith("cmd:")){v.answerCallbackQuery(e.id);return}const i=ht(t);if(!i){v.answerCallbackQuery(e.id,{text:"Command expired."});return}v.answerCallbackQuery(e.id);const c=e.message?.message_thread_id,p=e.message?.is_topic_message??!1,a=O(n,c,p);a&&ne(a,async()=>{const s=V(i);if(!s){await l(n,"Invalid command.",c);return}if(s.command==="stop"){await se(n);return}await ye(n,o,s.command,s.args,c)}).catch(async s=>{console.error("[tg-agent] runModel error",s);const d=B(s);await l(n,`Error: ${d}`,c)})}),v.on("message",e=>{if(!e.from?.id)return;const t=String(e.from.id),n=e.chat.id;if(!ue(t)){le(t,n);return}const o=e.message_thread_id,i=e.is_topic_message??!1,c=O(n,o,i);if(!c)return;const p=(e.text??e.caption??"").trim(),a=Te(e);if(!p&&a.length===0)return;const s=A.get(t);if(s){if(p==="/stop"||p==="/cancel"){at(t,"Login cancelled by user."),l(n,"Login cancelled.",o);return}if(!p){l(n,"Login expects a text response.",o);return}ie(t),s.resolve(p);return}if(V(p)?.command==="stop"){se(n);return}ne(c,()=>Rt(n,t,p,a,o)).catch(async r=>{console.error("[tg-agent] runModel error",r);const u=B(r);await l(n,`Error: ${u}`,o)})}),v.on("polling_error",e=>{console.error("Polling error",e)}),console.log("tg-agent started");
|
package/dist/mcp.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
`);return c.length>
|
|
3
|
-
(truncated)`),w=c,c}function
|
|
4
|
-
`)})},
|
|
5
|
-
`);const
|
|
1
|
+
import J from"node:fs";import R from"node:fs/promises";import W from"node:path";import q from"node:readline";import{spawn as H}from"node:child_process";import*as U from"@iarna/toml";const G=6e4,A=2e5,Q=40,X=3500;let w=null,_=!1;const m=new Map,V=1500;function I(t){return W.join(t,"config.toml")}function lt(t){const e=I(t);try{const r=J.readFileSync(e,"utf8");return O(r)}catch{return[]}}async function Y(t){const e=I(t);try{const r=await R.readFile(e,"utf8");return O(r)}catch{return[]}}async function ft(t,e={}){if(w)return w;const r=await Y(t);if(r.length===0)return w="","";const o=e.maxTools??Q,a=e.maxChars??X,i=e.timeoutMs??4e3,n=e.maxBytes??A,s=["MCP catalog (auto-discovered):","Use tool 'mcp' with server=<name> method=<tool> params=<object>."];for(const u of r){const l=await B(u,{timeoutMs:i,maxBytes:n});if(s.push(`${u.name} (${u.type})`),l.length===0){s.push(" - tools unavailable");continue}const d=l.slice(0,o);for(const p of d){const g=p.description?`: ${p.description}`:"";s.push(` - ${p.name}${g}`)}l.length>d.length&&s.push(` - ...and ${l.length-d.length} more`)}let c=s.join(`
|
|
2
|
+
`);return c.length>a&&(c=`${c.slice(0,a-20)}...
|
|
3
|
+
(truncated)`),w=c,c}function dt(){w&&(_=!0)}function pt(){w=null,_=!1,m.clear()}function ht(t){if(t.type==="http")return t.url?`url=${t.url}`:"url=(missing)";const e=t.args&&t.args.length>0?` ${t.args.join(" ")}`:"";return t.command?`command=${t.command}${e}`:"command=(missing)"}async function mt(t,e=5e3){const r=Date.now();try{return await P(t,"tools/list",{},{timeoutMs:e}),{ok:!0,durationMs:Date.now()-r}}catch(o){const a=o instanceof Error?o.message:String(o);return{ok:!1,durationMs:Date.now()-r,error:a}}}async function P(t,e,r,o={}){const a=k(o.timeoutMs??G,1e3,12e4),i=k(o.maxBytes??A,1024,5e6),n=new AbortController,s=()=>n.abort();o.signal?.addEventListener("abort",s,{once:!0});const c=setTimeout(()=>n.abort(),a);try{return t.type==="http"?(e!=="initialize"&&e!=="notifications/initialized"&&await v(t,i,n.signal),await Z(t,e,r,i,n.signal)):await nt(t,e,r,i,n.signal)}finally{clearTimeout(c),o.signal?.removeEventListener("abort",s)}}function k(t,e,r){return Number.isNaN(t)?e:Math.min(r,Math.max(e,t))}async function B(t,e){try{const r=await P(t,"tools/list",{},e);return K(r.output)}catch(r){const o=r instanceof Error?r.message:String(r);return console.warn(`[tg-agent] mcp tools/list failed server=${t.name} error=${o}`),[]}}function K(t){let e;try{e=JSON.parse(t)}catch{return[]}if(!e||typeof e!="object")return[];const r=e,a=(r.result??r).tools??r.tools;if(!Array.isArray(a))return[];const i=[];for(const n of a){if(!n||typeof n!="object")continue;const s=n,c=typeof s.name=="string"?s.name:"";if(!c)continue;const u=typeof s.description=="string"?s.description:void 0,l=s.inputSchema;i.push({name:c,description:u,inputSchema:l})}return i}async function Z(t,e,r,o,a){if(!t.url)throw new Error("MCP server url is missing.");const i=Date.now(),n=await x(t,e,r,o,a),s=j(n.parsed);if(s)throw new Error(s);let c=n.bodyText;return n.parsed&&(c=JSON.stringify(n.parsed,null,2)),{ok:n.statusCode>=200&&n.statusCode<300,output:c,durationMs:Date.now()-i,bytes:n.bytes,truncated:n.truncated,statusCode:n.statusCode,statusText:n.statusText,contentType:n.contentType}}async function x(t,e,r,o,a){if(!t.url)throw new Error("MCP server url is missing.");const i=m.get(t.name),n={jsonrpc:"2.0",id:`tg-agent-${Date.now()}`,method:e,params:r??{}},s={"content-type":"application/json"};t.auth&&(s.authorization=t.auth.startsWith("Bearer ")?t.auth:`Bearer ${t.auth}`),i?.cookie&&(s.cookie=i.cookie),i?.sessionId&&(s["mcp-session-id"]=i.sessionId);const c=await fetch(t.url,{method:"POST",headers:s,body:JSON.stringify(n),signal:a});tt(t.name,c);const u=await ot(c,o,a);let l;try{l=JSON.parse(u.text)}catch{l=void 0}return et(t.name,l),{statusCode:c.status,statusText:c.statusText,contentType:c.headers.get("content-type"),bodyText:u.text,parsed:l,bytes:u.bytes,truncated:u.truncated}}function j(t){if(!t||typeof t!="object")return null;const e=t;if(e.error&&typeof e.error=="object"){const r=e.error;if(typeof r.message=="string")return r.message}return typeof e.error_description=="string"?e.error_description:typeof e.error=="string"?e.error:null}async function v(t,e,r){const o=m.get(t.name);if(o?.initializedAt)return;if(o?.inFlight){await o.inFlight;return}const a=(async()=>{const i=await x(t,"initialize",{protocolVersion:"2024-11-05",clientInfo:{name:"tg-agent",version:process.env.npm_package_version??"dev"},capabilities:{}},e,r),n=j(i.parsed);if(n){const u=n.toLowerCase();if(u.includes("method")&&u.includes("not"))return;throw new Error(n)}const s=new AbortController,c=setTimeout(()=>s.abort(),V);try{await x(t,"notifications/initialized",{},e,s.signal)}catch{}finally{clearTimeout(c)}})();m.set(t.name,{initializedAt:Date.now(),inFlight:a});try{await a;const i=m.get(t.name);m.set(t.name,{initializedAt:Date.now(),cookie:i?.cookie,sessionId:i?.sessionId})}catch(i){throw m.delete(t.name),i}}function tt(t,e){const o=m.get(t)??{initializedAt:0},a=e.headers.get("set-cookie"),i=e.headers.getSetCookie?.(),n=i&&i.length>0?i.join("; "):a;n&&m.set(t,{...o,cookie:n});const s=e.headers.get("mcp-session-id")??e.headers.get("x-mcp-session-id");s&&m.set(t,{...o,sessionId:s})}function et(t,e){if(!e||typeof e!="object")return;const o=e.result;if(!o)return;const a=typeof o.sessionId=="string"&&o.sessionId||typeof o.session_id=="string"&&o.session_id;if(!a)return;const i=m.get(t)??{initializedAt:0};m.set(t,{...i,sessionId:a})}async function nt(t,e,r,o,a){if(!t.command)throw new Error("MCP server command is missing.");const i=Date.now(),n=H(t.command,t.args??[],{stdio:["pipe","pipe","pipe"]}),s=()=>{n.killed||n.kill("SIGTERM")};if(a.aborted)throw s(),new Error("MCP request aborted.");const c=new Map;let u=!1,l="";const d=new Error("MCP request aborted."),p=q.createInterface({input:n.stdout,terminal:!1});p.on("line",f=>{if(f.length>o){u=!0;return}let h=null;try{h=JSON.parse(f)}catch{return}const y=typeof h.id=="string"?h.id:typeof h.id=="number"?String(h.id):"";if(!y)return;const C=c.get(y);C&&(c.delete(y),C.resolve(h))}),n.stderr?.on("data",f=>{const h=f.toString();if(l.length+h.length>o){u=!0;return}l+=h});const g=()=>{for(const f of c.values())f.reject(d);c.clear(),s()};a.addEventListener("abort",g,{once:!0});const b=f=>{const h=f===0?"MCP stdio exited.":l.trim()||"MCP stdio exited with error.";for(const y of c.values())y.reject(new Error(h));c.clear()};n.once("exit",f=>b(f));const E=async(f,h)=>{const y=`tg-agent-${Date.now()}-${Math.random().toString(16).slice(2,8)}`,C={jsonrpc:"2.0",id:y,method:f,params:h??{}};return await new Promise((F,N)=>{c.set(y,{resolve:F,reject:N}),n.stdin?.write(`${JSON.stringify(C)}
|
|
4
|
+
`)})};fetch("http://127.0.0.1:7243/ingest/9e452bb4-cc67-4519-89fa-8fb51d810231",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({location:"mcp.ts:callStdioMcp:beforeInit",message:"sending initialize request",data:{elapsed:Date.now()-i},timestamp:Date.now(),sessionId:"debug-session",hypothesisId:"B,C"})}).catch(()=>{});const T=await E("initialize",{protocolVersion:"2024-11-05",clientInfo:{name:"tg-agent",version:process.env.npm_package_version??"dev"},capabilities:{}});if(fetch("http://127.0.0.1:7243/ingest/9e452bb4-cc67-4519-89fa-8fb51d810231",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({location:"mcp.ts:callStdioMcp:afterInit",message:"initialize response received",data:{elapsed:Date.now()-i,hasError:!!T.error},timestamp:Date.now(),sessionId:"debug-session",hypothesisId:"B,C"})}).catch(()=>{}),T.error&&typeof T.error=="object"){const f=T.error.message??"MCP initialize failed.";throw s(),new Error(String(f))}const D={jsonrpc:"2.0",method:"notifications/initialized"};n.stdin?.write(`${JSON.stringify(D)}
|
|
5
|
+
`),fetch("http://127.0.0.1:7243/ingest/9e452bb4-cc67-4519-89fa-8fb51d810231",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({location:"mcp.ts:callStdioMcp:beforeMethod",message:"sending method request",data:{method:e,elapsed:Date.now()-i},timestamp:Date.now(),sessionId:"debug-session",hypothesisId:"C"})}).catch(()=>{});const S=await E(e,r);a.removeEventListener("abort",g),p.close(),n.stdin?.end(),s();const $=new Promise(f=>{n.once("exit",()=>f())});if(await Promise.race([$,new Promise(f=>setTimeout(f,500))]),S.error&&typeof S.error=="object"){const f=S.error.message??"MCP request failed.";throw new Error(String(f))}const M=JSON.stringify(S,null,2),L=M.length>o?M.slice(0,o):M;return M.length>o&&(u=!0),{ok:!0,output:L,durationMs:Date.now()-i,bytes:Math.min(M.length,o),truncated:u}}async function ot(t,e,r){const o=t.body?.getReader?.();if(!o){const d=await t.text(),p=Math.min(d.length,e);return{text:d.slice(0,e),bytes:p,truncated:d.length>e}}const a=new TextDecoder("utf-8"),i=[];let n=0,s=!1;for(;;){if(r.aborted){try{await o.cancel()}catch{}throw new Error("MCP request aborted.")}const{done:d,value:p}=await o.read();if(d)break;if(!p)continue;const g=n+p.byteLength;if(g>e){const b=Math.max(0,e-n);b>0&&(i.push(p.slice(0,b)),n+=b),s=!0;try{await o.cancel()}catch{}break}i.push(p),n=g}const c=new Uint8Array(n);let u=0;for(const d of i)c.set(d,u),u+=d.byteLength;return{text:a.decode(c),bytes:n,truncated:s}}function O(t){const e=[];let r;try{r=U.parse(t)}catch{return e}const o=r.mcp_servers;if(!o||typeof o!="object")return e;for(const[a,i]of Object.entries(o)){if(!i||typeof i!="object")continue;const n=i,s=typeof n.type=="string"?n.type.toLowerCase():void 0,c=typeof n.url=="string"?n.url:void 0,u=typeof n.command=="string"?n.command:void 0,l=s==="http"||s==="stdio"?s:c?"http":u?"stdio":void 0;if(!l||l==="http"&&!c||l==="stdio"&&!u)continue;let d;Array.isArray(n.args)&&(d=n.args.filter(g=>typeof g=="string").map(g=>g.trim()));const p=typeof n.auth=="string"?n.auth:typeof n.authorization=="string"?n.authorization:typeof n.token=="string"?n.token:void 0;e.push({name:a,type:l,url:c,command:u,args:d,auth:p})}return e}function z(t){return t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'")?t.slice(1,-1):t}function gt(t){const e=t.trim();return e?e.startsWith('"')&&e.endsWith('"')?e.slice(1,-1).replace(/\\\\/g,"\\").replace(/\\"/g,'"'):e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e:""}function yt(t){const e=t.trim();if(!e.startsWith("[")||!e.endsWith("]"))return[];const r=e.slice(1,-1).trim();if(!r)return[];const o=[];let a="",i=!1,n="";for(let c=0;c<r.length;c+=1){const u=r[c];if(!i&&(u==='"'||u==="'")){i=!0,n=u;continue}if(i&&u===n){i=!1;continue}if(!i&&u===","){const l=a.trim();l&&o.push(z(l)),a="";continue}a+=u}const s=a.trim();return s&&o.push(z(s)),o}function wt(t){let e=!1,r="";for(let o=0;o<t.length;o+=1){const a=t[o];if(!e&&(a==='"'||a==="'")){e=!0,r=a;continue}if(e&&a===r){e=!1;continue}if(!e&&(a==="#"||a===";"))return t.slice(0,o)}return t}export{P as callMcpServer,ht as formatMcpTarget,dt as freezeMcpCatalog,ft as getMcpCatalog,I as getMcpConfigPath,Y as loadMcpServers,lt as loadMcpServersSync,mt as probeMcpServer,pt as refreshMcpCatalog};
|