@whyvix/remote 0.1.0 → 0.1.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/package.json +1 -1
- package/packages/agent/src/agent.js +7 -7
- package/packages/cli/index.js +27 -7
package/package.json
CHANGED
|
@@ -6,14 +6,14 @@ import { createSecurity } from "./security.js";
|
|
|
6
6
|
import { ProcessManager } from "./process-manager.js";
|
|
7
7
|
import { createToolExecutor } from "./tools.js";
|
|
8
8
|
|
|
9
|
-
export function createAgent({gatewayUrl="ws://127.0.0.1:8787",deviceId=randomUUID(),deviceName=os.hostname(),token="",workspaces=[process.cwd()],reconnectMs=1000,reconnect=true,audit=()=>{}}={}) {
|
|
9
|
+
export function createAgent({gatewayUrl="ws://127.0.0.1:8787",deviceId=randomUUID(),deviceName=os.hostname(),token="",workspaces=[process.cwd()],reconnectMs=1000,reconnect=true,audit=()=>{},log=console.log,errorLog=console.error,onRegistered=()=>{}}={}) {
|
|
10
10
|
let ws=null,reconnectTimer=null,stopped=false,generation=0;
|
|
11
11
|
const security=createSecurity({token,workspaces,audit});
|
|
12
12
|
const processManager=new ProcessManager({audit});
|
|
13
13
|
const executeTool=createToolExecutor({security,processManager});
|
|
14
14
|
function scheduleReconnect(reason){
|
|
15
15
|
if(stopped||!reconnect||reconnectTimer)return;
|
|
16
|
-
|
|
16
|
+
log("[agent:reconnect-scheduled]",reason,reconnectMs);
|
|
17
17
|
reconnectTimer=setTimeout(()=>{reconnectTimer=null;connect();},reconnectMs);
|
|
18
18
|
}
|
|
19
19
|
function connect(){
|
|
@@ -21,11 +21,11 @@ export function createAgent({gatewayUrl="ws://127.0.0.1:8787",deviceId=randomUUI
|
|
|
21
21
|
clearTimeout(reconnectTimer); reconnectTimer=null;
|
|
22
22
|
const current=++generation;
|
|
23
23
|
const socket=new WebSocket(gatewayUrl); ws=socket;
|
|
24
|
-
|
|
25
|
-
socket.on("open",()=>{if(ws!==socket)return;
|
|
26
|
-
socket.on("message",async(raw)=>{if(ws!==socket)return;try{const m=parseMessage(raw);if(m.type===TYPES.REGISTERED){
|
|
27
|
-
socket.on("close",(code)=>{
|
|
28
|
-
socket.on("error",(e)=>{
|
|
24
|
+
log("[agent:connecting]",gatewayUrl,"generation="+current);
|
|
25
|
+
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:"0.3.0"}})));});
|
|
26
|
+
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;try{const result=await executeTool(m.tool,m.arguments);if(m.tool==="device.ping")Object.assign(result,{deviceId,deviceName});if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(envelope(TYPES.RESPONSE,{requestId:m.requestId,result})));}catch(error){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);}});
|
|
27
|
+
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");});
|
|
28
|
+
socket.on("error",(e)=>{errorLog("[agent:socket-error]",e.message);scheduleReconnect("error");});
|
|
29
29
|
}
|
|
30
30
|
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();});}
|
|
31
31
|
connect(); return{deviceId,deviceName,stop};
|
package/packages/cli/index.js
CHANGED
|
@@ -45,10 +45,30 @@ if(!credential){
|
|
|
45
45
|
console.log("Device authorized.");
|
|
46
46
|
}
|
|
47
47
|
console.log("WHYVIX Remote");
|
|
48
|
-
console.log("
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
console.log("");
|
|
49
|
+
const agent=createAgent({
|
|
50
|
+
gatewayUrl,
|
|
51
|
+
deviceId,
|
|
52
|
+
deviceName:os.hostname(),
|
|
53
|
+
token:credential,
|
|
54
|
+
workspaces,
|
|
55
|
+
audit:()=>{},
|
|
56
|
+
log:()=>{},
|
|
57
|
+
onRegistered:({authorizedAccount})=>{
|
|
58
|
+
console.log("Device :",os.hostname());
|
|
59
|
+
console.log("Status : Connected");
|
|
60
|
+
console.log("Authorized :",authorizedAccount||"-");
|
|
61
|
+
console.log("");
|
|
62
|
+
console.log("Keep this terminal open while using WHYVIX Remote.");
|
|
63
|
+
console.log("Press Ctrl+C to disconnect.");
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
let shuttingDown=false;
|
|
67
|
+
async function shutdown(){
|
|
68
|
+
if(shuttingDown)return;
|
|
69
|
+
shuttingDown=true;
|
|
70
|
+
await agent.stop();
|
|
71
|
+
process.exit(0);
|
|
72
|
+
}
|
|
73
|
+
process.once("SIGINT",shutdown);
|
|
74
|
+
process.once("SIGTERM",shutdown);
|