@proagentstore/cli 0.2.9 → 0.2.10

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.
Files changed (2) hide show
  1. package/dist/index.js +31 -15
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -932,20 +932,16 @@ async function connectViaRelay(instanceIds, localUrl, runnerToken, opts) {
932
932
  writeLine(` Agents: ${instanceIds.length} instance${instanceIds.length === 1 ? "" : "s"}`);
933
933
  writeLine(" No cloudflared needed. Ctrl+C to disconnect.");
934
934
  writeLine("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");
935
- let stopped = false;
936
- const heartbeatLoop = async () => {
937
- while (!stopped) {
938
- await new Promise((r) => setTimeout(r, 3e4));
939
- if (stopped) break;
935
+ const heartbeat = () => {
936
+ const timer = setTimeout(async () => {
940
937
  for (const id of instanceIds) {
941
938
  await requestPags("POST", `/v1/instances/${apiPathSegment(id)}/runtime/heartbeat`, opts, {}).catch(() => void 0);
942
939
  }
943
- }
940
+ heartbeat();
941
+ }, 3e4);
942
+ timer.unref();
944
943
  };
945
- void heartbeatLoop();
946
- process.once("beforeExit", () => {
947
- stopped = true;
948
- });
944
+ heartbeat();
949
945
  }
950
946
  function openRelaySocket(instanceId, wsBase, pagsToken, localUrl, runnerToken) {
951
947
  let backoffMs = 1e3;
@@ -959,6 +955,13 @@ function openRelaySocket(instanceId, wsBase, pagsToken, localUrl, runnerToken) {
959
955
  };
960
956
  ws.onmessage = async (event) => {
961
957
  const text = typeof event.data === "string" ? event.data : String(event.data);
958
+ if (text === "ping") {
959
+ try {
960
+ ws.send("pong");
961
+ } catch {
962
+ }
963
+ return;
964
+ }
962
965
  let cmd;
963
966
  try {
964
967
  cmd = JSON.parse(text);
@@ -978,16 +981,29 @@ function openRelaySocket(instanceId, wsBase, pagsToken, localUrl, runnerToken) {
978
981
  headers,
979
982
  body: hasBody ? JSON.stringify(cmd.body) : void 0
980
983
  });
981
- const result = await res.json().catch(() => ({}));
982
- ws.send(JSON.stringify({ id: cmd.id, status: res.status, result }));
984
+ const text2 = await res.text().catch(() => "");
985
+ let result;
986
+ try {
987
+ result = text2 ? JSON.parse(text2) : {};
988
+ } catch {
989
+ result = { raw: text2.slice(0, 500) };
990
+ }
991
+ try {
992
+ ws.send(JSON.stringify({ id: cmd.id, status: res.status, result }));
993
+ } catch {
994
+ }
983
995
  } catch (err) {
984
- ws.send(JSON.stringify({ id: cmd.id, status: 500, error: err instanceof Error ? err.message : String(err) }));
996
+ try {
997
+ ws.send(JSON.stringify({ id: cmd.id, status: 500, error: err instanceof Error ? err.message : String(err) }));
998
+ } catch {
999
+ }
985
1000
  }
986
1001
  };
987
- ws.onclose = () => {
1002
+ ws.onclose = (ev) => {
988
1003
  if (reconnecting) return;
989
1004
  reconnecting = true;
990
- writeLine(`Relay disconnected: ${instanceId.slice(0, 8)}\u2026 \u2014 reconnecting in ${Math.round(backoffMs / 1e3)}s`);
1005
+ const reason = ev.code === 4401 || ev.code === 1008 ? " (token expired \u2014 run `pags login` then `pags up`)" : "";
1006
+ writeLine(`Relay disconnected: ${instanceId.slice(0, 8)}\u2026${reason} \u2014 reconnecting in ${Math.round(backoffMs / 1e3)}s`);
991
1007
  setTimeout(() => {
992
1008
  reconnecting = false;
993
1009
  connect();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proagentstore/cli",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "description": "CLI for creating, publishing, and running ProAgentStore agents",
5
5
  "license": "MIT",
6
6
  "type": "module",