@agimon-ai/po-remote 0.0.0-alpha.177 → 0.0.0-alpha.178

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.cjs CHANGED
@@ -3276,6 +3276,8 @@ const LAN_NATIVE_ROUTES = [
3276
3276
  path: _agimon_ai_po_core_remoteContract.REMOTE_HTTP_ROUTE
3277
3277
  }
3278
3278
  ];
3279
+ /** The one socket path the private-network listener upgrades. */
3280
+ const LAN_SOCKET_PATH = "/api/ws";
3279
3281
  /**
3280
3282
  * The relay's live protocol door: one exact POST whose request and response bodies are length-prefixed frames, in
3281
3283
  * place of the WebSocket upgrade the relay cannot carry. Only the private relay answers it. The private network and
@@ -3291,43 +3293,26 @@ function isPrivateRelayRoute(method, path) {
3291
3293
  const wanted = method.toUpperCase();
3292
3294
  return PRIVATE_RELAY_ROUTES.some((route) => route.method === wanted && route.path === path);
3293
3295
  }
3294
- const CLOUD_WORKSPACE = "[A-Za-z0-9_-]{1,128}";
3295
- const CLOUD_ROUTES = [
3296
- {
3297
- method: METHOD_GET,
3298
- pattern: /^\/api\/workspaces$/u
3299
- },
3300
- {
3301
- method: METHOD_GET,
3302
- pattern: new RegExp(`^/api/workspaces/${CLOUD_WORKSPACE}$`, "u")
3303
- },
3304
- {
3305
- method: METHOD_GET,
3306
- pattern: new RegExp(`^/api/workspaces/${CLOUD_WORKSPACE}/sessions$`, "u")
3307
- },
3308
- {
3309
- method: METHOD_POST,
3310
- pattern: new RegExp(`^/api/workspaces/${CLOUD_WORKSPACE}/sessions$`, "u")
3311
- },
3312
- {
3313
- method: METHOD_GET,
3314
- pattern: new RegExp(`^/api/workspaces/${CLOUD_WORKSPACE}/sessions/(${CLOUD_WORKSPACE})$`, "u")
3315
- }
3316
- ];
3296
+ const CLOUD_METHODS = /* @__PURE__ */ new Set([
3297
+ "GET",
3298
+ "HEAD",
3299
+ "POST",
3300
+ "PUT",
3301
+ "PATCH",
3302
+ "DELETE"
3303
+ ]);
3304
+ /** This computer's own pairing, devices and links between computers: a paired device's or the owner's at the desk. */
3305
+ const CLOUD_DENIED = /^\/api\/(?:remote|network|networks|auth|account|owner)(?:\/|$)/u;
3317
3306
  /**
3318
- * What a Cloud-account stream may reach: list and create, and read one session. Nothing else is named, so a new
3319
- * route stays closed. `session` is the session a route addresses and `create` marks a session start. Matched by
3320
- * string equality on the path exactly as spelled, with no decoding.
3307
+ * Whether a Cloud-account stream may make this call: every Po API route a paired device may, except this computer's
3308
+ * remote access, device and network administration. The caller also refuses step-up actions, which no Cloud
3309
+ * account can confirm. Matched on the path exactly as spelled: an encoded slash or dot, a dot segment or an empty
3310
+ * segment is refused rather than decoded.
3321
3311
  */
3322
- function cloudRelayRoute(method, path) {
3323
- const wanted = method.toUpperCase();
3324
- for (const route of CLOUD_ROUTES) {
3325
- if (route.method !== wanted) continue;
3326
- const match = route.pattern.exec(path);
3327
- if (!match) continue;
3328
- if (match[1] !== void 0) return { session: match[1] };
3329
- return wanted === METHOD_POST ? { create: true } : {};
3330
- }
3312
+ function cloudRelayAllowed(method, path) {
3313
+ if (!CLOUD_METHODS.has(method.toUpperCase()) || !path.startsWith("/api/")) return false;
3314
+ if (/%2[ef]|%5c|\\|\/\/|\/\.\.?(?:\/|$)/iu.test(path)) return false;
3315
+ return !CLOUD_DENIED.test(path);
3331
3316
  }
3332
3317
  function isLanNativeRoute(method, path) {
3333
3318
  const wanted = method.toUpperCase();
@@ -5746,8 +5731,15 @@ async function readBody(request) {
5746
5731
  return Buffer.concat(chunks);
5747
5732
  }
5748
5733
  /** Headers a relayed client may set. Everything else, such as an address claim or a caller stamp, is dropped. */
5749
- /** The one socket path a Cloud chat stream may name: a session's own, spelled as `cloudRelayRoute` spells ids. */
5734
+ /** The socket paths a Cloud stream may name: the host protocol a paired device uses, or one session's own chat. */
5750
5735
  const CLOUD_CHAT_SOCKET = /^\/api\/workspaces\/[A-Za-z0-9_-]{1,128}\/sessions\/[A-Za-z0-9_-]{1,128}\/ws$/u;
5736
+ /** Request headers a Cloud stream may pass on. Everything else, any caller stamp included, is dropped. */
5737
+ const CLOUD_FORWARDED_HEADERS = [
5738
+ "accept",
5739
+ "content-type",
5740
+ "range",
5741
+ "x-po-client-metadata"
5742
+ ];
5751
5743
  const RELAY_FORWARDED_HEADERS = [
5752
5744
  "content-type",
5753
5745
  "cookie",
@@ -5986,8 +5978,14 @@ function createRemoteRuntime(options) {
5986
5978
  ...state.closesAt === void 0 ? {} : { closesAt: Date.parse(state.closesAt) }
5987
5979
  };
5988
5980
  };
5981
+ let lastRelayOn = false;
5989
5982
  const publishIntent = () => {
5990
5983
  if (closing) return;
5984
+ const relayOn = dual && remote.instance("relay").state().status === "on";
5985
+ if (relayOn !== lastRelayOn) {
5986
+ lastRelayOn = relayOn;
5987
+ options.onRelayIntentChanged?.(relayOn);
5988
+ }
5991
5989
  const intent = tunnelIntent();
5992
5990
  if (intent?.hostname === lastIntent?.hostname && intent?.closesAt === lastIntent?.closesAt) return;
5993
5991
  lastIntent = intent;
@@ -6896,17 +6894,46 @@ function createRemoteRuntime(options) {
6896
6894
  if (stream !== void 0 && !stream.locked) stream.cancel().catch(() => void 0);
6897
6895
  return response;
6898
6896
  };
6897
+ const cloudCallerHeaders = (origin, caller, request) => {
6898
+ const headers = new Headers({
6899
+ host: new URL(origin).host,
6900
+ origin
6901
+ });
6902
+ for (const name of CLOUD_FORWARDED_HEADERS) {
6903
+ const value = request?.headers.get(name);
6904
+ if (value !== null && value !== void 0) headers.set(name, value);
6905
+ }
6906
+ headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_LOCALITY_HEADER, "remote");
6907
+ headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_DEVICE_ID_HEADER, `cloud-account:${caller.subject}`);
6908
+ headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_STEP_UP_HEADER, "not-required");
6909
+ headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_ACCESS_HEADER, "owner");
6910
+ headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_FEDERATION_HEADER, JSON.stringify({
6911
+ principal: {
6912
+ issuer: caller.issuer,
6913
+ subject: caller.subject
6914
+ },
6915
+ org: caller.org,
6916
+ device: { keyId: `cloud:${caller.issuer}` }
6917
+ }));
6918
+ return headers;
6919
+ };
6899
6920
  /**
6900
- * A Cloud stream's chat: the streamed protocol door, limited to one exact session's socket path. The broker-tagged
6921
+ * A Cloud stream's protocol: the host socket a paired device uses, or one exact session's chat. The broker-tagged
6901
6922
  * sealed exchange is the transport's authentication, so no paired device or channel stands behind it.
6902
6923
  */
6903
- const serveCloudChat = (request, caller) => {
6924
+ const serveCloudChat = async (request, caller, origin) => {
6904
6925
  const refuse = (status, error) => Response.json({ error }, { status });
6905
6926
  const selected = request.headers.get(RELAY_PROTOCOL_HEADER);
6906
- const mount = selected === null ? void 0 : CLOUD_CHAT_SOCKET.exec(selected);
6907
- if (selected === null || mount === null || mount === void 0 || (0, _agimon_ai_po_core_packageApi.parsePoSocketPath)(selected)?.scope !== "session") return refuse(400, "That protocol is not served to a Cloud account.");
6927
+ const host = selected === LAN_SOCKET_PATH;
6928
+ if (selected === null || !host && (!CLOUD_CHAT_SOCKET.test(selected) || (0, _agimon_ai_po_core_packageApi.parsePoSocketPath)(selected)?.scope !== "session")) return refuse(400, "That protocol is not served to a Cloud account.");
6908
6929
  if (options.acceptCloudChat === void 0) return refuse(503, "Cloud chat is not available on this computer.");
6909
6930
  if (request.body === null) return refuse(400, "The protocol stream needs a request body.");
6931
+ const revived = host ? void 0 : await options.forward(new Request(new URL(selected.replace(/\/ws$/u, "/revive"), origin), {
6932
+ method: "POST",
6933
+ headers: cloudCallerHeaders(origin, caller),
6934
+ signal: request.signal
6935
+ }));
6936
+ if (revived && !revived.ok && revived.status !== 404) return revived;
6910
6937
  const { wire, response } = framedWire({
6911
6938
  body: request.body,
6912
6939
  signal: request.signal,
@@ -6935,35 +6962,16 @@ function createRemoteRuntime(options) {
6935
6962
  if (relay === void 0 || remote.tunnelPolicy() === void 0) return refuse(503, "The private relay is not open.");
6936
6963
  const url = new URL(request.url);
6937
6964
  if (url.origin !== relay.origin || url.username || url.password || url.hash) return refuse(404, "Not found.");
6938
- if (request.method === "POST" && url.pathname === "/api/remote/protocol") return serveCloudChat(request, caller);
6939
- if (cloudRelayRoute(request.method, url.pathname) === void 0) return refuse(403, "A Cloud account cannot do that on this computer.");
6965
+ if (request.method === "POST" && url.pathname === "/api/remote/protocol") return serveCloudChat(request, caller, relay.origin);
6966
+ const action = stepUpActionFor(request.method, url.pathname);
6967
+ if (!cloudRelayAllowed(request.method, url.pathname) || action !== void 0 && action !== "session.create" || (0, _agimon_ai_po_core_sessionMcpContract.isSessionMcpHostRoute)(url.pathname) && !(0, _agimon_ai_po_core_sessionMcpContract.isSessionMcpPhoneRoute)(request.method, url.pathname)) return refuse(403, "A Cloud account cannot do that on this computer.");
6940
6968
  const read = await readWebBody(request.body ?? new Blob([]).stream(), MAX_REQUEST_BYTES);
6941
6969
  if (read === "too-large") return refuse(413, "The relay request body is too large.");
6942
- const headers = new Headers({
6943
- host: new URL(relay.origin).host,
6944
- origin: relay.origin
6945
- });
6946
- for (const name of ["content-type", "x-po-client-metadata"]) {
6947
- const value = request.headers.get(name);
6948
- if (value !== null) headers.set(name, value);
6949
- }
6950
- headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_LOCALITY_HEADER, "remote");
6951
- headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_DEVICE_ID_HEADER, `cloud-account:${caller.subject}`);
6952
- headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_STEP_UP_HEADER, "not-required");
6953
- headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_ACCESS_HEADER, "owner");
6954
- headers.set(_agimon_ai_po_core_packageApi.PO_API_CALLER_FEDERATION_HEADER, JSON.stringify({
6955
- principal: {
6956
- issuer: caller.issuer,
6957
- subject: caller.subject
6958
- },
6959
- org: caller.org,
6960
- device: { keyId: `cloud:${caller.issuer}` }
6961
- }));
6962
6970
  return options.forward(new Request(url, {
6963
6971
  method: request.method,
6964
- headers,
6972
+ headers: cloudCallerHeaders(relay.origin, caller, request),
6965
6973
  signal: request.signal,
6966
- ...request.method === "GET" || read.byteLength === 0 ? {} : { body: read }
6974
+ ...request.method === "GET" || request.method === "HEAD" || read.byteLength === 0 ? {} : { body: read }
6967
6975
  }));
6968
6976
  };
6969
6977
  return {
@@ -7000,6 +7008,13 @@ function createRemoteRuntime(options) {
7000
7008
  remote.updateSettings(parsed.settings);
7001
7009
  return remote.enable(intent.closesAt);
7002
7010
  },
7011
+ restoreRelay: async () => {
7012
+ if (closing || !dual || relaySetup?.state().linked !== true) return {
7013
+ ok: false,
7014
+ error: "This computer is no longer linked to Po Cloud."
7015
+ };
7016
+ return remote.within("relay", () => remote.enable(void 0, "relay"));
7017
+ },
7003
7018
  close: async () => {
7004
7019
  closing = true;
7005
7020
  await remote.close();