@whyvix/remote 0.3.2 → 0.3.4
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/package.json
CHANGED
|
@@ -8,7 +8,7 @@ import { RemoteExecutionManager } from "./remote-execution-manager.js";
|
|
|
8
8
|
import { DeploymentManager } from "./deployment-manager.js";
|
|
9
9
|
import { createToolExecutor } from "./tools.js";
|
|
10
10
|
|
|
11
|
-
export function createAgent({gatewayUrl="ws://127.0.0.1:8787",deviceId=randomUUID(),deviceName=os.hostname(),token="",workspaces=[process.cwd()],remoteTargets=[],deploymentProfiles=[],agentVersion="unknown",capabilities=[],reconnectMs=1000,reconnect=true,audit=()=>{},log=console.log,errorLog=console.error,onRegistered=()=>{}}={}) {
|
|
11
|
+
export function createAgent({gatewayUrl="ws://127.0.0.1:8787",deviceId=randomUUID(),deviceName=os.hostname(),token="",workspaces=[process.cwd()],remoteTargets=[],deploymentProfiles=[],agentVersion="unknown",capabilities=[],reconnectMs=1000,reconnect=true,heartbeatMs=30000,audit=()=>{},log=console.log,errorLog=console.error,onRegistered=()=>{}}={}) {
|
|
12
12
|
let ws=null,reconnectTimer=null,stopped=false,generation=0;
|
|
13
13
|
const security=createSecurity({token,workspaces,audit});
|
|
14
14
|
const processManager=new ProcessManager({audit});
|
|
@@ -25,10 +25,21 @@ export function createAgent({gatewayUrl="ws://127.0.0.1:8787",deviceId=randomUUI
|
|
|
25
25
|
clearTimeout(reconnectTimer); reconnectTimer=null;
|
|
26
26
|
const current=++generation;
|
|
27
27
|
const socket=new WebSocket(gatewayUrl); ws=socket;
|
|
28
|
+
let alive=true,heartbeatTimer=null;
|
|
29
|
+
const stopHeartbeat=()=>{if(heartbeatTimer){clearInterval(heartbeatTimer);heartbeatTimer=null;}};
|
|
30
|
+
if(heartbeatMs>0){
|
|
31
|
+
socket.on("pong",()=>{alive=true;});
|
|
32
|
+
heartbeatTimer=setInterval(()=>{
|
|
33
|
+
if(ws!==socket||socket.readyState!==WebSocket.OPEN)return;
|
|
34
|
+
if(!alive){errorLog("[agent:heartbeat-timeout]","generation="+current);socket.terminate();return;}
|
|
35
|
+
alive=false;socket.ping();
|
|
36
|
+
},heartbeatMs);
|
|
37
|
+
heartbeatTimer.unref?.();
|
|
38
|
+
}
|
|
28
39
|
log("[agent:connecting]",gatewayUrl,"generation="+current);
|
|
29
40
|
socket.on("open",()=>{if(ws!==socket)return;log("[agent:connected]",gatewayUrl);socket.send(JSON.stringify(envelope(TYPES.REGISTER,{auth:{token},device:{id:deviceId,name:deviceName,platform:process.platform,agentVersion,capabilities:[...new Set(capabilities.map(String))].sort()}})));});
|
|
30
41
|
socket.on("message",async(raw)=>{if(ws!==socket)return;try{const m=parseMessage(raw);if(m.type===TYPES.REGISTERED){log("[agent:registered]",m.deviceId);onRegistered({deviceId:m.deviceId,authorizedAccount:m.authorizedAccount||""});return;}if(m.type!==TYPES.REQUEST)return;log("[tool:request]",m.tool,{path:m.arguments?.path,cwd:m.arguments?.cwd});try{const result=await executeTool(m.tool,m.arguments);if(m.tool==="device.ping")Object.assign(result,{deviceId,deviceName});log("[tool:success]",m.tool);if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(envelope(TYPES.RESPONSE,{requestId:m.requestId,result})));}catch(error){errorLog("[tool:rejected]",m.tool,error.code||"TOOL_ERROR",error.message);if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(envelope(TYPES.ERROR,{requestId:m.requestId,error:{code:error.code||"TOOL_ERROR",message:error.message}})));}}catch(error){errorLog("[agent:protocol-error]",error.message);}});
|
|
31
|
-
socket.on("close",(code)=>{log("[agent:disconnected]","generation="+current,"code="+code);if(ws===socket)ws=null;if(code===1008){errorLog("[agent:authorization-required] device credential rejected");return;}if(code===4001){errorLog("[agent:replaced] newer WHYVIX Remote connection is active");return;}scheduleReconnect("close");});
|
|
42
|
+
socket.on("close",(code)=>{stopHeartbeat();log("[agent:disconnected]","generation="+current,"code="+code);if(ws===socket)ws=null;if(code===1008){errorLog("[agent:authorization-required] device credential rejected");return;}if(code===4001){errorLog("[agent:replaced] newer WHYVIX Remote connection is active");return;}scheduleReconnect("close");});
|
|
32
43
|
socket.on("error",(e)=>{errorLog("[agent:socket-error]",e.message);scheduleReconnect("error");});
|
|
33
44
|
}
|
|
34
45
|
async function stop(){stopped=true;clearTimeout(reconnectTimer);reconnectTimer=null;const socket=ws;ws=null;if(socket&&socket.readyState<WebSocket.CLOSING)await new Promise(r=>{socket.once("close",r);socket.close();});}
|
|
@@ -3,12 +3,13 @@ import path from "node:path";
|
|
|
3
3
|
|
|
4
4
|
function fail(code,message){throw Object.assign(new Error(message),{code});}
|
|
5
5
|
|
|
6
|
-
export async function loadDeploymentProfiles(filePath){
|
|
6
|
+
export async function loadDeploymentProfiles(filePath,{optional=false}={}){
|
|
7
7
|
const requested=String(filePath||"").trim();
|
|
8
8
|
if(!requested)return[];
|
|
9
9
|
const resolved=path.resolve(requested);
|
|
10
10
|
let parsed;
|
|
11
11
|
try{parsed=JSON.parse(await fs.readFile(resolved,"utf8"));}catch(error){
|
|
12
|
+
if(optional&&error?.code==="ENOENT")return[];
|
|
12
13
|
if(error instanceof SyntaxError)fail("DEPLOY_PROFILE_DENIED","Deployment profile file is not valid JSON");
|
|
13
14
|
fail("DEPLOY_PROFILE_DENIED","Deployment profile file cannot be read");
|
|
14
15
|
}
|
package/packages/cli/index.js
CHANGED
|
@@ -16,7 +16,9 @@ const gatewayUrl=process.env.WHYVIX_GATEWAY_URL||"wss://remote.whyvix.com";
|
|
|
16
16
|
const authUrl=(process.env.WHYVIX_AUTH_URL||"https://auth.whyvix.com").replace(/\/$/,"");
|
|
17
17
|
const workspaces=(process.env.WHYVIX_WORKSPACES||process.cwd()).split(";").filter(Boolean);
|
|
18
18
|
const remoteTargets=(process.env.WHYVIX_REMOTE_TARGETS||"").split(";").map(alias=>alias.trim()).filter(Boolean).map(alias=>({alias}));
|
|
19
|
-
const
|
|
19
|
+
const configuredDeploymentProfiles=String(process.env.WHYVIX_DEPLOY_PROFILES_FILE||"").trim();
|
|
20
|
+
const defaultDeploymentProfiles=join(os.homedir(),".whyvix-remote","deployment-profiles.json");
|
|
21
|
+
const deploymentProfiles=await loadDeploymentProfiles(configuredDeploymentProfiles||defaultDeploymentProfiles,{optional:!configuredDeploymentProfiles});
|
|
20
22
|
const capabilities=["filesystem.crud","process.bounded","git.read","git.mutate",...(remoteTargets.length?["remote.bounded"]:[]),...(deploymentProfiles.length?["deployment.profiled"]:[])];
|
|
21
23
|
const deviceId=process.env.WHYVIX_DEVICE_ID||crypto.createHash("sha256").update(os.hostname()+"|"+os.userInfo().username).digest("hex").slice(0,24);
|
|
22
24
|
const credentialDir=join(os.homedir(),".whyvix-remote");
|