@agimon-ai/po-native 0.0.1-alpha.58 → 0.0.1-alpha.59

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agimon-ai/po-native",
3
- "version": "0.0.1-alpha.58",
3
+ "version": "0.0.1-alpha.59",
4
4
  "description": "Po for iOS and Android",
5
5
  "homepage": "https://agimon.ai",
6
6
  "bugs": {
@@ -29,17 +29,17 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@agimon-ai/po-core": "0.0.0-alpha.178",
33
- "@agimon-ai/po-expo-signing": "0.0.1-alpha.58",
34
- "@agimon-ai/po-identity": "0.0.2-alpha.125",
35
- "@agimon-ai/po-native-components": "0.0.0-alpha.178",
36
- "@agimon-ai/po-native-dashboard": "0.0.0-alpha.50",
37
- "@agimon-ai/po-template-advanced": "0.0.0-alpha.178",
38
- "@agimon-ai/po-template-elegant": "0.0.0-alpha.178",
39
- "@agimon-ai/po-voice": "0.0.0-alpha.178",
40
- "@agimon-ai/po-web-components": "0.0.0-alpha.178",
41
- "@agimon-ai/po-web-dashboard": "0.0.0-alpha.178",
42
- "@agimon-ai/po-web-security": "0.0.0-alpha.178",
32
+ "@agimon-ai/po-core": "0.0.0-alpha.179",
33
+ "@agimon-ai/po-expo-signing": "0.0.1-alpha.59",
34
+ "@agimon-ai/po-identity": "0.0.2-alpha.126",
35
+ "@agimon-ai/po-native-components": "0.0.0-alpha.179",
36
+ "@agimon-ai/po-native-dashboard": "0.0.0-alpha.51",
37
+ "@agimon-ai/po-template-advanced": "0.0.0-alpha.179",
38
+ "@agimon-ai/po-template-elegant": "0.0.0-alpha.179",
39
+ "@agimon-ai/po-voice": "0.0.0-alpha.179",
40
+ "@agimon-ai/po-web-components": "0.0.0-alpha.179",
41
+ "@agimon-ai/po-web-dashboard": "0.0.0-alpha.179",
42
+ "@agimon-ai/po-web-security": "0.0.0-alpha.179",
43
43
  "@better-auth/expo": "1.7.12",
44
44
  "@config-plugins/react-native-webrtc": "15.0.2",
45
45
  "@earendil-works/chord": "1.5.2",
@@ -47,6 +47,8 @@ import { activateNativeTemplateHost, resetNativeTemplateChoice } from './nativeT
47
47
  const PUSH_DEREGISTER_DEADLINE_MS = 2_000;
48
48
  /** A Cloud-linked computer's protocol socket dropped: try again after this, so an offline computer is not hammered. */
49
49
  const CLOUD_COMPUTER_RECONNECT_MS = 3_000;
50
+ /** A paired computer running through Po Cloud tries its own connection again this often. */
51
+ const PAIRED_RETRY_MS = 30_000;
50
52
 
51
53
  async function withinDeadline(operation: Promise<unknown>, deferred: string): Promise<void> {
52
54
  let timer: ReturnType<typeof setTimeout> | undefined;
@@ -71,12 +73,16 @@ export interface HostRuntimeState {
71
73
  linkError?: string;
72
74
  /** Incompatible plugins do not change this host's connection status. */
73
75
  pluginCompatibility?: readonly PluginCompatibility[];
76
+ /** A paired computer its own connection cannot reach, running through Po Cloud for now. */
77
+ via?: 'cloud';
74
78
  }
75
79
 
76
80
  interface HostRuntimeEntry extends HostRuntimeState {
77
81
  controller: AbortController;
78
82
  push?: ReturnType<typeof startNativePush>;
79
83
  lanTransport?: TrustedSession['lanTransport'];
84
+ /** A Cloud computer that is also paired here: hidden, with no connection of its own. */
85
+ standby?: true;
80
86
  }
81
87
 
82
88
  function sameHostState(left: HostRuntimeState, right: HostRuntimeState): boolean {
@@ -86,7 +92,8 @@ function sameHostState(left: HostRuntimeState, right: HostRuntimeState): boolean
86
92
  left.runtime === right.runtime &&
87
93
  left.error === right.error &&
88
94
  left.linkError === right.linkError &&
89
- left.pluginCompatibility === right.pluginCompatibility
95
+ left.pluginCompatibility === right.pluginCompatibility &&
96
+ left.via === right.via
90
97
  );
91
98
  }
92
99
 
@@ -104,7 +111,9 @@ export function createHostRuntimes(
104
111
  const repairs = new Map<string, { left: HostRuntimeEntry; right: HostRuntimeEntry; controller: AbortController }>();
105
112
  const repairLinks = () => {
106
113
  // Host links join paired computers; a cloud host has no pairing keys to offer hints with.
107
- const connected = [...entries.values()].filter((entry) => entry.status === 'connected' && !isCloudHost(entry.host));
114
+ const connected = [...entries.values()].filter(
115
+ (entry) => entry.status === 'connected' && !isCloudHost(entry.host) && entry.via === undefined,
116
+ );
108
117
  for (let i = 0; i < connected.length; i++)
109
118
  for (const right of connected.slice(i + 1)) {
110
119
  const left = connected[i]!;
@@ -135,11 +144,13 @@ export function createHostRuntimes(
135
144
  // or a repeated status would otherwise re-render every screen of the app.
136
145
  let reported: readonly HostRuntimeState[] = [];
137
146
  const snapshot = (): readonly HostRuntimeState[] => {
138
- const next = [...entries.values()].map(({ host, status, runtime, error, linkError, pluginCompatibility }) => {
139
- const state = { host, status, runtime, error, linkError, pluginCompatibility };
140
- const previous = reported.find((candidate) => candidate.host.id === host.id);
141
- return previous && sameHostState(previous, state) ? previous : state;
142
- });
147
+ const next = [...entries.values()]
148
+ .filter((entry) => !entry.standby)
149
+ .map(({ host, status, runtime, error, linkError, pluginCompatibility, via }) => {
150
+ const state = { host, status, runtime, error, linkError, pluginCompatibility, ...(via ? { via } : {}) };
151
+ const previous = reported.find((candidate) => candidate.host.id === host.id);
152
+ return previous && sameHostState(previous, state) ? previous : state;
153
+ });
143
154
  return next.length === reported.length && next.every((state, index) => state === reported[index]) ? reported : next;
144
155
  };
145
156
  const changed = () => {
@@ -164,6 +175,29 @@ export function createHostRuntimes(
164
175
  entry.lanTransport = undefined;
165
176
  };
166
177
 
178
+ // One computer, two entries: a Cloud listing and a pairing that carry the same bundle signer.
179
+ const pairedTwin = (publicKey: string | undefined) =>
180
+ publicKey === undefined
181
+ ? undefined
182
+ : [...entries.values()].find(
183
+ (entry): entry is HostRuntimeEntry & { host: HostRecord } =>
184
+ !isCloudHost(entry.host) && entry.host.signer.publicKey === publicKey && !forgetting.has(entry.host.id),
185
+ );
186
+ const cloudTwin = (publicKey: string) =>
187
+ [...entries.values()].find(
188
+ (entry): entry is HostRuntimeEntry & { host: CloudHostRecord } =>
189
+ isCloudHost(entry.host) && entry.host.computer?.publicKey === publicKey,
190
+ );
191
+ /** The paired entry shows the computer; its Cloud listing keeps only the record for a fallback. */
192
+ const standDown = (entry: HostRuntimeEntry) => {
193
+ stopEntry(entry);
194
+ entry.runtime = undefined;
195
+ entry.push = undefined;
196
+ entry.standby = true;
197
+ applySessionsSnapshot({ sessions: [] }, entry.host.id);
198
+ applyWorkspacesSnapshot({ workspaces: [] }, entry.host.id);
199
+ };
200
+
167
201
  const dropCloudEntry = (entry: HostRuntimeEntry) => {
168
202
  const { id } = entry.host;
169
203
  stopEntry(entry);
@@ -171,15 +205,110 @@ export function createHostRuntimes(
171
205
  applySessionsSnapshot({ sessions: [] }, id);
172
206
  applyWorkspacesSnapshot({ workspaces: [] }, id);
173
207
  if (focused !== null && splitHostId(focused)?.hostId === id) focused = null;
208
+ // A paired computer running through this listing goes back to its own connection.
209
+ const key = isCloudHost(entry.host) ? entry.host.computer?.publicKey : undefined;
210
+ const twin = pairedTwin(key);
211
+ if (twin?.via === 'cloud') void connect(twin.host).catch(() => undefined);
174
212
  changed();
175
213
  };
176
214
 
215
+ /** How a paired computer is reached: its relay route, its pinned private network, or plain HTTPS. */
216
+ const pairedTransport = async (host: HostRecord) =>
217
+ !host.tlsCertificateSha256 && !host.relayRoute
218
+ ? undefined
219
+ : host.relayRoute
220
+ ? createRelayTransport({
221
+ origin: host.origin,
222
+ route: host.relayRoute,
223
+ capability: await loadRelayCapability(host.id),
224
+ signer: host.signer.publicKey,
225
+ })
226
+ : createPinnedLanTransport(host.origin, host.tlsCertificateSha256!);
227
+
228
+ /**
229
+ * Runs a paired computer through its Cloud listing, under the paired id so its sessions keep their ids, and
230
+ * keeps trying its own connection, which takes over once it answers.
231
+ */
232
+ const fallBackToCloud = (host: HostRecord, cloud: CloudHostRecord): LiveRuntime => {
233
+ const previous = entries.get(host.id);
234
+ if (previous) stopEntry(previous, true);
235
+ const entry: HostRuntimeEntry = { host, status: 'connecting', controller: new AbortController(), via: 'cloud' };
236
+ entries.set(host.id, entry);
237
+ const active = () =>
238
+ !closed && entries.get(host.id) === entry && !entry.controller.signal.aborted && !forgetting.has(host.id);
239
+ const send = cloudApiSend(cloud);
240
+ const runtime = startLiveRuntime(
241
+ { hostId: host.id, origin: cloud.origin, sealed: send, openLink: cloudComputerLink(cloud) },
242
+ host,
243
+ () => {
244
+ if (active())
245
+ setTimeout(() => {
246
+ if (active()) fallBackToCloud(host, cloud);
247
+ }, CLOUD_COMPUTER_RECONNECT_MS);
248
+ },
249
+ (owner, state) => {
250
+ if (active()) options.activity?.(host.id, owner, state);
251
+ },
252
+ {
253
+ onSummariesLoaded() {
254
+ if (!active() || entry.status === 'connected') return;
255
+ entry.status = 'connected';
256
+ changed();
257
+ },
258
+ onPluginCompatibility(issues) {
259
+ if (!active()) return;
260
+ entry.pluginCompatibility = issues;
261
+ changed();
262
+ },
263
+ onUpdateRequired() {
264
+ if (!active()) return;
265
+ entry.status = 'update_required';
266
+ changed();
267
+ },
268
+ },
269
+ );
270
+ entry.runtime = runtime;
271
+ // Plugin and Settings calls for this computer go through Cloud too, until its own connection returns.
272
+ entry.controller.signal.addEventListener('abort', bindCloudApi(host.id, send), { once: true });
273
+ const retry = setInterval(() => {
274
+ if (!active()) return clearInterval(retry);
275
+ void (async () => {
276
+ const transport = await pairedTransport(host);
277
+ try {
278
+ const trusted = await reconnect(host.origin, transport?.fetch ?? nativeFetch, {
279
+ hostId: host.id,
280
+ signal: entry.controller.signal,
281
+ });
282
+ if (!active()) throw new Error('The host connection was replaced.');
283
+ if (transport) trusted.lanTransport = transport;
284
+ clearInterval(retry);
285
+ await connect(host, trusted);
286
+ } catch {
287
+ // Still unreachable on its own; Cloud keeps carrying it until the next try.
288
+ transport?.close();
289
+ }
290
+ })();
291
+ }, PAIRED_RETRY_MS);
292
+ entry.controller.signal.addEventListener('abort', () => clearInterval(retry), { once: true });
293
+ runtime.open(focused !== null && splitHostId(focused)?.hostId === host.id ? focused : null);
294
+ changed();
295
+ return runtime;
296
+ };
297
+
177
298
  /** Cloud hosts poll and focus over cookie admission. Only the Cloud service enrolls push. */
178
- const connectCloud = (host: CloudHostRecord, requestPermission: boolean): LiveRuntime => {
299
+ const connectCloud = (host: CloudHostRecord, requestPermission: boolean): LiveRuntime | undefined => {
179
300
  const previous = entries.get(host.id);
180
301
  if (previous) stopEntry(previous);
181
302
  const entry: HostRuntimeEntry = { host, status: 'connecting', controller: new AbortController() };
182
303
  entries.set(host.id, entry);
304
+ // Paired here too: the paired entry shows it, and runs through this listing only while it cannot connect.
305
+ const twin = pairedTwin(host.computer?.publicKey);
306
+ if (twin) {
307
+ standDown(entry);
308
+ const runtime = twin.status === 'disconnected' ? fallBackToCloud(twin.host, host) : twin.runtime;
309
+ changed();
310
+ return runtime;
311
+ }
183
312
  const active = () => !closed && entries.get(host.id) === entry && !forgetting.has(host.id);
184
313
  let discovering = false;
185
314
  const discoverComputers = async () => {
@@ -285,11 +414,18 @@ export function createHostRuntimes(
285
414
  return runtime;
286
415
  };
287
416
 
288
- const connect = async (
417
+ // A paired computer always gets a runtime; a Cloud listing hidden behind its pairing has none of its own.
418
+ async function connect(host: HostRecord, session?: TrustedSession, requestPermission?: boolean): Promise<LiveRuntime>;
419
+ async function connect(
420
+ host: HostRecord | CloudHostRecord,
421
+ session?: TrustedSession,
422
+ requestPermission?: boolean,
423
+ ): Promise<LiveRuntime | undefined>;
424
+ async function connect(
289
425
  host: HostRecord | CloudHostRecord,
290
426
  session?: TrustedSession,
291
427
  requestPermission = false,
292
- ): Promise<LiveRuntime> => {
428
+ ): Promise<LiveRuntime | undefined> {
293
429
  if (closed) throw new Error('The host runtimes are closed.');
294
430
  if (forgetting.has(host.id)) throw new Error('This host is being forgotten.');
295
431
  if (isCloudHost(host)) return connectCloud(host, requestPermission);
@@ -307,6 +443,8 @@ export function createHostRuntimes(
307
443
  stopEntry(previous, !isCloudHost(previous.host) && previous.host.signer.publicKey === host.signer.publicKey);
308
444
  const entry: HostRuntimeEntry = { host, status: 'connecting', controller: new AbortController() };
309
445
  entries.set(host.id, entry);
446
+ const twin = cloudTwin(host.signer.publicKey);
447
+ if (twin && !twin.standby) standDown(twin);
310
448
  const saved = hostSnapshots.open(host).read();
311
449
  if (saved) {
312
450
  applySessionsSnapshot({ sessions: saved.sessions }, host.id);
@@ -321,17 +459,9 @@ export function createHostRuntimes(
321
459
  scan?.hostId !== host.id;
322
460
  try {
323
461
  // A relay host never touches its Cloud origin directly, so its transport is chosen before any request.
462
+ // Plain HTTPS starts without waiting, so cached plugins start before authentication.
324
463
  const transport =
325
- session || (!host.tlsCertificateSha256 && !host.relayRoute)
326
- ? undefined
327
- : host.relayRoute
328
- ? createRelayTransport({
329
- origin: host.origin,
330
- route: host.relayRoute,
331
- capability: await loadRelayCapability(host.id),
332
- signer: host.signer.publicKey,
333
- })
334
- : createPinnedLanTransport(host.origin, host.tlsCertificateSha256!);
464
+ session || (!host.tlsCertificateSha256 && !host.relayRoute) ? undefined : await pairedTransport(host);
335
465
  entry.lanTransport = session?.lanTransport ?? transport;
336
466
  const trustedSession =
337
467
  session ??
@@ -401,13 +531,16 @@ export function createHostRuntimes(
401
531
  entry.lanTransport?.close();
402
532
  entry.lanTransport = undefined;
403
533
  if (active()) {
534
+ // Its own connection is down; the same computer's Cloud listing carries it meanwhile.
535
+ const listed = cloudTwin(host.signer.publicKey);
536
+ if (listed) return fallBackToCloud(host, listed.host);
404
537
  entry.status = 'disconnected';
405
538
  entry.error = error instanceof Error ? error.message : 'Could not connect to this host.';
406
539
  changed();
407
540
  }
408
541
  throw error;
409
542
  }
410
- };
543
+ }
411
544
 
412
545
  return {
413
546
  connect,
@@ -551,6 +684,10 @@ export function createHostRuntimes(
551
684
  if (focused !== null && splitHostId(focused)?.hostId === hostId) focused = null;
552
685
  changed();
553
686
  forgetHostModules(hostId);
687
+ // Forgetting the pairing leaves the computer reachable through its Cloud listing.
688
+ const listed =
689
+ forgotten && !isCloudHost(forgotten.host) ? cloudTwin(forgotten.host.signer.publicKey) : undefined;
690
+ if (listed?.standby && !closed) connectCloud(listed.host, false);
554
691
  purgeNativeBundleUpdates(hostId);
555
692
  await bundleCacheStorage(hostId).clear();
556
693
  } finally {
@@ -106,15 +106,18 @@ export function bindCloudApi(hostId: string, send: Send): () => void {
106
106
  }
107
107
 
108
108
  /**
109
- * One host's exchange: its paired sealed channel, or Po Cloud when the host is one. With no host named
110
- * and no computer paired, the only signed-in Po Cloud host answers.
109
+ * One host's exchange: its paired sealed channel, or Po Cloud when the host is one or a paired computer runs
110
+ * through it for now. With no host named and no computer paired, the only signed-in Po Cloud host answers.
111
111
  */
112
112
  function sendTo(hostId?: string): Send {
113
113
  return (request, signal) => {
114
- const candidate =
115
- hostId === undefined ? (cloudApis.size === 1 ? [...cloudApis.values()][0] : undefined) : cloudApis.get(hostId);
116
- // A paired computer keeps its own sealed channel.
117
- const cloud = candidate !== undefined && !currentSession(hostId) ? candidate : undefined;
114
+ // A named host bound to Cloud is reached through Cloud, even with a paired channel left from before.
115
+ const cloud =
116
+ hostId !== undefined
117
+ ? cloudApis.get(hostId)
118
+ : cloudApis.size === 1 && !currentSession()
119
+ ? [...cloudApis.values()][0]
120
+ : undefined;
118
121
  if (cloud) return cloud(request, signal);
119
122
  if (hostId !== undefined) return fetchWithStepUp(request, undefined, signal, hostId);
120
123
  return signal === undefined ? fetchWithStepUp(request) : fetchWithStepUp(request, undefined, signal);