@messenger-agent/messenger-agent 0.27.0 → 0.29.0-alpha.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.
@@ -1 +1 @@
1
- import{createRequire as p}from"node:module";import u from"ws";import{logger as l}from"@messenger-agent/shared/logger";import{createAgentFileRoutes as h}from"@messenger-agent/shared/agent-files";import{createPrefixedRouteRequest as f,createWorkspaceRouteRequest as d,isAgentFileHttpTunnelRequest as k,isWorkspaceHttpTunnelRequest as g,sendHttpTunnelResponse as w}from"@messenger-agent/shared/tunnel-http";import{CODING_AGENT_TUNNEL_DUPLICATE_CLOSE_CODE as y,CODING_AGENT_TUNNEL_PROTOCOL_VERSION as m}from"@messenger-agent/shared/tunnel-protocol";import{createAgentWorkspaceRoutes as R}from"@messenger-agent/shared/workspace-routes";import{appConfig as T}from"./config.js";const v=p(import.meta.url)("../package.json").version,b=c=>new Promise(e=>setTimeout(e,c));function q(c){try{const e=JSON.parse(c.toString());return e&&typeof e=="object"&&"type"in e?e:void 0}catch{return}}class x{config;stopped=!1;socket;running=new Map;workspaceRoutes;fileRoutes=h();constructor(e=T){this.config=e,this.workspaceRoutes=R({config:e,textMaxBytes:e.textMaxBytes,allowManagement:!0,onWorkspacesChanged:()=>{process.send?.({type:"client.workspaces.changed"})}})}async start(){if(!this.config.tunnel.enabled)return;if(!this.config.tunnel.serverUrl||!this.config.tunnel.tunnelId||!this.config.tunnel.token)throw new Error("Workspace tunnel requires server_url, tunnel_id, and token when enabled");let e=this.config.tunnel.reconnectInitialMs;for(;!this.stopped;){try{await this.connectOnce(),e=this.config.tunnel.reconnectInitialMs}catch(o){this.stopped||l.warn("[WorkspaceTunnel] connection failed:",o)}this.stopped||(await b(e),e=Math.min(e*2,this.config.tunnel.reconnectMaxMs))}}stop(){this.stopped=!0;for(const e of this.running.values())e.abort();this.running.clear(),this.socket?.close()}connectOnce(){return new Promise((e,o)=>{const r=new u(this.config.tunnel.serverUrl,{headers:{Authorization:`Bearer ${this.config.tunnel.token}`}});this.socket=r;let s,i=!1;const a=()=>{s&&clearInterval(s),this.socket===r&&(this.socket=void 0);for(const t of this.running.values())t.abort(new Error("Workspace tunnel closed"));this.running.clear()};r.on("open",()=>{this.send({type:"hello",protocolVersion:m,tunnelId:this.config.tunnel.tunnelId,agentType:"workspace",capabilities:["workspace.files","workspace.git","workspace.tasks"],clientVersion:v,workspaces:[...this.config.workspaces.values()].map(({id:t,name:n})=>({id:t,name:n})).sort((t,n)=>t.id.localeCompare(n.id))}),s=setInterval(()=>this.send({type:"heartbeat"}),this.config.tunnel.heartbeatIntervalMs),l.info(`[WorkspaceTunnel] connected to ${this.config.tunnel.serverUrl} as ${this.config.tunnel.tunnelId}`)}),r.on("message",t=>{const n=q(t);!n||n.type==="heartbeat"||(n.type==="cancel"?this.running.get(n.id)?.abort():n.type==="request"&&this.handleRequest(n))}),r.once("error",t=>{i||(i=!0,a(),o(t))}),r.once("close",(t,n)=>{a(),(t===y||t===1008)&&(this.stopped=!0,l.warn(`[WorkspaceTunnel] connection rejected with code ${t}; reconnect disabled until service restart: ${n.toString()}`)),i||(i=!0,e())})})}send(e){this.socket?.readyState===u.OPEN&&this.socket.send(JSON.stringify(e))}async handleRequest(e){const o=k(e);if(!o&&!g(e)){this.send({type:"response.error",id:e.id,code:"unsupported_request",errorText:`Unsupported workspace tunnel request: ${e.method} ${e.path}`});return}const r=new AbortController;this.running.set(e.id,r);try{const s=o?f(e,"/v1/files",r.signal):d(e,r.signal),i=await(o?this.fileRoutes:this.workspaceRoutes).fetch(s);await w(e.id,i,a=>this.send(a))}catch(s){this.send({type:"response.error",id:e.id,code:"request_failed",errorText:s instanceof Error?s.message:"Workspace tunnel request failed"})}finally{this.running.delete(e.id)}}}export{x as WorkspaceTunnelClient};
1
+ import{createRequire as p}from"node:module";import u from"ws";import{logger as l}from"@messenger-agent/shared/logger";import{reconnectDelayWithJitter as h}from"@messenger-agent/shared/reconnect-backoff";import{createAgentFileRoutes as f}from"@messenger-agent/shared/agent-files";import{createPrefixedRouteRequest as d,createWorkspaceRouteRequest as k,isAgentFileHttpTunnelRequest as g,isWorkspaceHttpTunnelRequest as w,sendHttpTunnelResponse as y}from"@messenger-agent/shared/tunnel-http";import{CODING_AGENT_TUNNEL_DUPLICATE_CLOSE_CODE as m,CODING_AGENT_TUNNEL_PROTOCOL_VERSION as R}from"@messenger-agent/shared/tunnel-protocol";import{createAgentWorkspaceRoutes as T}from"@messenger-agent/shared/workspace-routes";import{appConfig as v}from"./config.js";const b=p(import.meta.url)("../package.json").version,q=c=>new Promise(e=>setTimeout(e,c));function I(c){try{const e=JSON.parse(c.toString());return e&&typeof e=="object"&&"type"in e?e:void 0}catch{return}}class A{config;stopped=!1;socket;running=new Map;workspaceRoutes;fileRoutes=f();constructor(e=v){this.config=e,this.workspaceRoutes=T({config:e,textMaxBytes:e.textMaxBytes,allowManagement:!0,onWorkspacesChanged:()=>{process.send?.({type:"client.workspaces.changed"})}})}async start(){if(!this.config.tunnel.enabled)return;if(!this.config.tunnel.serverUrl||!this.config.tunnel.tunnelId||!this.config.tunnel.token)throw new Error("Workspace tunnel requires server_url, tunnel_id, and token when enabled");let e=this.config.tunnel.reconnectInitialMs;for(;!this.stopped;){try{await this.connectOnce(),e=this.config.tunnel.reconnectInitialMs}catch(o){this.stopped||l.warn("[WorkspaceTunnel] connection failed:",o)}this.stopped||(await q(h(e)),e=Math.min(e*2,this.config.tunnel.reconnectMaxMs))}}stop(){this.stopped=!0;for(const e of this.running.values())e.abort();this.running.clear(),this.socket?.close()}connectOnce(){return new Promise((e,o)=>{const r=new u(this.config.tunnel.serverUrl,{headers:{Authorization:`Bearer ${this.config.tunnel.token}`}});this.socket=r;let s,i=!1;const a=()=>{s&&clearInterval(s),this.socket===r&&(this.socket=void 0);for(const t of this.running.values())t.abort(new Error("Workspace tunnel closed"));this.running.clear()};r.on("open",()=>{this.send({type:"hello",protocolVersion:R,tunnelId:this.config.tunnel.tunnelId,agentType:"workspace",capabilities:["workspace.files","workspace.git","workspace.tasks"],clientVersion:b,workspaces:[...this.config.workspaces.values()].map(({id:t,name:n})=>({id:t,name:n})).sort((t,n)=>t.id.localeCompare(n.id))}),s=setInterval(()=>this.send({type:"heartbeat"}),this.config.tunnel.heartbeatIntervalMs),l.info(`[WorkspaceTunnel] connected to ${this.config.tunnel.serverUrl} as ${this.config.tunnel.tunnelId}`)}),r.on("message",t=>{const n=I(t);!n||n.type==="heartbeat"||(n.type==="cancel"?this.running.get(n.id)?.abort():n.type==="request"&&this.handleRequest(n))}),r.once("error",t=>{i||(i=!0,a(),o(t))}),r.once("close",(t,n)=>{a(),(t===m||t===1008)&&(this.stopped=!0,l.warn(`[WorkspaceTunnel] connection rejected with code ${t}; reconnect disabled until service restart: ${n.toString()}`)),i||(i=!0,e())})})}send(e){this.socket?.readyState===u.OPEN&&this.socket.send(JSON.stringify(e))}async handleRequest(e){const o=g(e);if(!o&&!w(e)){this.send({type:"response.error",id:e.id,code:"unsupported_request",errorText:`Unsupported workspace tunnel request: ${e.method} ${e.path}`});return}const r=new AbortController;this.running.set(e.id,r);try{const s=o?d(e,"/v1/files",r.signal):k(e,r.signal),i=await(o?this.fileRoutes:this.workspaceRoutes).fetch(s);await y(e.id,i,a=>this.send(a))}catch(s){this.send({type:"response.error",id:e.id,code:"request_failed",errorText:s instanceof Error?s.message:"Workspace tunnel request failed"})}finally{this.running.delete(e.id)}}}export{A as WorkspaceTunnelClient};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@messenger-agent/messenger-agent",
3
- "version": "0.27.0",
3
+ "version": "0.29.0-alpha.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "ws": "^8.18.3",
21
21
  "yaml": "^2.9.0",
22
22
  "zod": "^4.4.3",
23
- "@messenger-agent/shared": "0.27.0"
23
+ "@messenger-agent/shared": "0.29.0-alpha.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/ws": "^8.18.1"