@celljs/ta-host 4.15.2 → 4.17.0

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 (105) hide show
  1. package/esm/node/app-commands.d.ts +111 -0
  2. package/esm/node/app-commands.js +629 -0
  3. package/esm/node/delegate-extension.d.ts +11 -0
  4. package/esm/node/delegate-extension.js +33 -0
  5. package/esm/node/directory-relocator.d.ts +49 -0
  6. package/esm/node/directory-relocator.js +267 -0
  7. package/esm/node/exec-endpoint.d.ts +14 -0
  8. package/esm/node/exec-endpoint.js +48 -0
  9. package/esm/node/extension-providers.d.ts +16 -1
  10. package/esm/node/extension-providers.js +23 -3
  11. package/esm/node/gateway-models.d.ts +38 -0
  12. package/esm/node/gateway-models.js +124 -0
  13. package/esm/node/index.d.ts +8 -0
  14. package/esm/node/index.js +8 -0
  15. package/esm/node/keep-awake.d.ts +5 -1
  16. package/esm/node/keep-awake.js +18 -2
  17. package/esm/node/message-page.d.ts +6 -1
  18. package/esm/node/message-page.js +50 -2
  19. package/esm/node/package-hosts.d.ts +6 -0
  20. package/esm/node/package-hosts.js +35 -0
  21. package/esm/node/package-webhooks.d.ts +26 -0
  22. package/esm/node/package-webhooks.js +101 -0
  23. package/esm/node/page-book.d.ts +48 -0
  24. package/esm/node/page-book.js +117 -0
  25. package/esm/node/relay-bridge.d.ts +2 -0
  26. package/esm/node/relay-bridge.js +35 -1
  27. package/esm/node/remote/cloud-hosts.d.ts +40 -0
  28. package/esm/node/remote/cloud-hosts.js +99 -0
  29. package/esm/node/remote/index.d.ts +1 -0
  30. package/esm/node/remote/index.js +1 -0
  31. package/esm/node/remote/pi-directories.d.ts +2 -1
  32. package/esm/node/remote/pi-directories.js +7 -2
  33. package/esm/node/remote/remote-hosts.d.ts +5 -0
  34. package/esm/node/remote/remote-hosts.js +71 -2
  35. package/esm/node/remote/remote-protocol.d.ts +2 -0
  36. package/esm/node/remote/remote-server-impl.d.ts +1 -0
  37. package/esm/node/remote/remote-server-impl.js +3 -0
  38. package/esm/node/rooms-bridge.d.ts +3 -0
  39. package/esm/node/rooms-bridge.js +4 -0
  40. package/esm/node/session-info-util.d.ts +3 -1
  41. package/esm/node/session-info-util.js +32 -6
  42. package/esm/node/ta-server-impl.d.ts +5 -1
  43. package/esm/node/ta-server-impl.js +22 -0
  44. package/esm/node/ta-session-factory.d.ts +13 -1
  45. package/esm/node/ta-session-factory.js +39 -4
  46. package/esm/node/ta-session.d.ts +2 -0
  47. package/esm/node/ta-session.js +1 -0
  48. package/esm/node/team/team-calls.js +23 -0
  49. package/esm/node/voice-bridge.d.ts +68 -0
  50. package/esm/node/voice-bridge.js +273 -0
  51. package/esm/node/voices-file.d.ts +33 -0
  52. package/esm/node/voices-file.js +89 -0
  53. package/lib/node/app-commands.d.ts +112 -0
  54. package/lib/node/app-commands.js +633 -0
  55. package/lib/node/delegate-extension.d.ts +12 -0
  56. package/lib/node/delegate-extension.js +37 -0
  57. package/lib/node/directory-relocator.d.ts +50 -0
  58. package/lib/node/directory-relocator.js +270 -0
  59. package/lib/node/exec-endpoint.d.ts +15 -0
  60. package/lib/node/exec-endpoint.js +51 -0
  61. package/lib/node/extension-providers.d.ts +16 -1
  62. package/lib/node/extension-providers.js +23 -3
  63. package/lib/node/gateway-models.d.ts +39 -0
  64. package/lib/node/gateway-models.js +127 -0
  65. package/lib/node/index.d.ts +8 -0
  66. package/lib/node/index.js +8 -0
  67. package/lib/node/keep-awake.d.ts +5 -1
  68. package/lib/node/keep-awake.js +18 -2
  69. package/lib/node/message-page.d.ts +6 -1
  70. package/lib/node/message-page.js +50 -2
  71. package/lib/node/package-hosts.d.ts +6 -0
  72. package/lib/node/package-hosts.js +35 -0
  73. package/lib/node/package-webhooks.d.ts +27 -0
  74. package/lib/node/package-webhooks.js +104 -0
  75. package/lib/node/page-book.d.ts +49 -0
  76. package/lib/node/page-book.js +120 -0
  77. package/lib/node/relay-bridge.d.ts +2 -0
  78. package/lib/node/relay-bridge.js +34 -0
  79. package/lib/node/remote/cloud-hosts.d.ts +41 -0
  80. package/lib/node/remote/cloud-hosts.js +103 -0
  81. package/lib/node/remote/index.d.ts +1 -0
  82. package/lib/node/remote/index.js +1 -0
  83. package/lib/node/remote/pi-directories.d.ts +2 -1
  84. package/lib/node/remote/pi-directories.js +7 -2
  85. package/lib/node/remote/remote-hosts.d.ts +5 -0
  86. package/lib/node/remote/remote-hosts.js +71 -2
  87. package/lib/node/remote/remote-protocol.d.ts +2 -0
  88. package/lib/node/remote/remote-server-impl.d.ts +1 -0
  89. package/lib/node/remote/remote-server-impl.js +3 -0
  90. package/lib/node/rooms-bridge.d.ts +3 -0
  91. package/lib/node/rooms-bridge.js +4 -0
  92. package/lib/node/session-info-util.d.ts +3 -1
  93. package/lib/node/session-info-util.js +31 -5
  94. package/lib/node/ta-server-impl.d.ts +5 -1
  95. package/lib/node/ta-server-impl.js +22 -0
  96. package/lib/node/ta-session-factory.d.ts +13 -1
  97. package/lib/node/ta-session-factory.js +38 -3
  98. package/lib/node/ta-session.d.ts +2 -0
  99. package/lib/node/ta-session.js +1 -0
  100. package/lib/node/team/team-calls.js +23 -0
  101. package/lib/node/voice-bridge.d.ts +69 -0
  102. package/lib/node/voice-bridge.js +274 -0
  103. package/lib/node/voices-file.d.ts +34 -0
  104. package/lib/node/voices-file.js +93 -0
  105. package/package.json +10 -10
@@ -0,0 +1,49 @@
1
+ import { type RpcConnection } from '@celljs/rpc/lib/node';
2
+ import { type TaPageAskOptions, type TaPageInfo, TaPageServer } from '@celljs/ta-protocol';
3
+ import { TaHostTeamProvider, TaPrincipals } from './team/team-protocol';
4
+ export declare const TaPageBook: unique symbol;
5
+ /** The pages that answer requests (TaPageServer.attach), by connection, in memory only. */
6
+ export interface TaPageBook {
7
+ attach(connection: RpcConnection, info: TaPageInfo): void;
8
+ /** Asks the page of `principal` that answers `kind` (PageChoice.pick) and resolves to its answer. */
9
+ ask(principal: string, kind: string, payload: unknown, timeoutMs?: number): Promise<unknown>;
10
+ has(principal: string, kind: string): boolean;
11
+ /** Whom a package's request is for: who asked the run of the session file, or the principal given. */
12
+ principalOf(options: TaPageAskOptions): string;
13
+ }
14
+ /** A page as the book keeps it. */
15
+ export interface PageEntry {
16
+ principal: string;
17
+ info: TaPageInfo;
18
+ /** `Date.now()` time it was last said active, or attached. */
19
+ activeAt: number;
20
+ }
21
+ export declare namespace PageChoice {
22
+ /**
23
+ * The page to ask among those of one person that answer the kind: the one in front of them, the latest touched of
24
+ * those; else the window of Ta Desktop, kept while closed; else the latest touched.
25
+ */
26
+ function pick<T extends PageEntry>(entries: readonly T[]): T | undefined;
27
+ /** What a page said of itself, made safe: only strings of kinds, a device it knows. */
28
+ function clean(info: TaPageInfo | undefined): TaPageInfo;
29
+ }
30
+ export declare class DefaultTaPageBook implements TaPageBook {
31
+ protected readonly principals: TaPrincipals;
32
+ protected readonly teamProvider: TaHostTeamProvider;
33
+ protected readonly entries: Map<RpcConnection, PageEntry & {
34
+ connection: RpcConnection;
35
+ }>;
36
+ attach(connection: RpcConnection, info: TaPageInfo): void;
37
+ ask(principal: string, kind: string, payload: unknown, timeoutMs?: number): Promise<unknown>;
38
+ has(principal: string, kind: string): boolean;
39
+ principalOf(options: TaPageAskOptions): string;
40
+ protected of(principal: string, kind: string): (PageEntry & {
41
+ connection: RpcConnection;
42
+ })[];
43
+ }
44
+ /** A page says which requests it answers (TaPageServer). */
45
+ export declare class TaPageServerImpl implements TaPageServer {
46
+ protected readonly book: TaPageBook;
47
+ attach(info: TaPageInfo): Promise<void>;
48
+ }
49
+ //# sourceMappingURL=page-book.d.ts.map
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TaPageServerImpl = exports.DefaultTaPageBook = exports.PageChoice = exports.TaPageBook = void 0;
13
+ const core_1 = require("@celljs/core");
14
+ const rpc_1 = require("@celljs/rpc");
15
+ const node_1 = require("@celljs/rpc/lib/node");
16
+ const ta_protocol_1 = require("@celljs/ta-protocol");
17
+ const ta_errors_1 = require("./ta-errors");
18
+ const team_protocol_1 = require("./team/team-protocol");
19
+ exports.TaPageBook = Symbol('TaPageBook');
20
+ var PageChoice;
21
+ (function (PageChoice) {
22
+ /**
23
+ * The page to ask among those of one person that answer the kind: the one in front of them, the latest touched of
24
+ * those; else the window of Ta Desktop, kept while closed; else the latest touched.
25
+ */
26
+ function pick(entries) {
27
+ var _a, _b;
28
+ const latest = (list) => list.reduce((best, one) => (!best || one.activeAt > best.activeAt ? one : best), undefined);
29
+ return (_b = (_a = latest(entries.filter(entry => entry.info.visible))) !== null && _a !== void 0 ? _a : latest(entries.filter(entry => entry.info.desktop))) !== null && _b !== void 0 ? _b : latest(entries);
30
+ }
31
+ PageChoice.pick = pick;
32
+ /** What a page said of itself, made safe: only strings of kinds, a device it knows. */
33
+ function clean(info) {
34
+ const kinds = Array.isArray(info === null || info === void 0 ? void 0 : info.kinds) ? info.kinds.filter(kind => typeof kind === 'string' && kind.length <= 100).slice(0, 100) : [];
35
+ const device = (info === null || info === void 0 ? void 0 : info.device) === 'phone' || (info === null || info === void 0 ? void 0 : info.device) === 'tablet' ? info.device : 'computer';
36
+ return { kinds, device, ...((info === null || info === void 0 ? void 0 : info.desktop) === true ? { desktop: true } : {}), ...((info === null || info === void 0 ? void 0 : info.visible) === true ? { visible: true } : {}) };
37
+ }
38
+ PageChoice.clean = clean;
39
+ })(PageChoice || (exports.PageChoice = PageChoice = {}));
40
+ let DefaultTaPageBook = class DefaultTaPageBook {
41
+ constructor() {
42
+ this.entries = new Map();
43
+ }
44
+ attach(connection, info) {
45
+ if (connection.closed) {
46
+ return;
47
+ }
48
+ const before = this.entries.get(connection);
49
+ const clean = PageChoice.clean(info);
50
+ this.entries.set(connection, {
51
+ connection,
52
+ principal: this.principals.of(connection),
53
+ info: clean,
54
+ activeAt: (info === null || info === void 0 ? void 0 : info.active) === true || !before ? Date.now() : before.activeAt,
55
+ });
56
+ if (!before) {
57
+ connection.onClose(() => this.entries.delete(connection));
58
+ }
59
+ }
60
+ async ask(principal, kind, payload, timeoutMs = ta_protocol_1.TA_PAGE_ASK_MS) {
61
+ const page = PageChoice.pick(this.of(principal, kind));
62
+ if (!page) {
63
+ throw ta_errors_1.TaErrors.invalidArgument('No page of Ta is open for this.', 'The user opens Ta on a phone or a computer; meanwhile do it without the screen.');
64
+ }
65
+ let timer;
66
+ try {
67
+ return await Promise.race([
68
+ page.connection.client(ta_protocol_1.TaClient).onPageAsk(kind, payload),
69
+ new Promise((_resolve, reject) => {
70
+ timer = setTimeout(() => reject(ta_errors_1.TaErrors.invalidArgument('The page of Ta did not answer in time.', 'Try again in a moment.')), timeoutMs);
71
+ }),
72
+ ]);
73
+ }
74
+ finally {
75
+ clearTimeout(timer);
76
+ }
77
+ }
78
+ has(principal, kind) {
79
+ return this.of(principal, kind).length > 0;
80
+ }
81
+ principalOf(options) {
82
+ var _a;
83
+ if (options.principal) {
84
+ return options.principal;
85
+ }
86
+ const team = this.teamProvider.team;
87
+ const actors = options.sessionFile ? team.actorsOf(options.sessionFile) : [team.current()];
88
+ return (_a = actors.find(actor => actor !== ta_protocol_1.TA_OWNER)) !== null && _a !== void 0 ? _a : ta_protocol_1.TA_OWNER;
89
+ }
90
+ of(principal, kind) {
91
+ return [...this.entries.values()].filter(entry => !entry.connection.closed && entry.principal === principal && entry.info.kinds.includes(kind));
92
+ }
93
+ };
94
+ exports.DefaultTaPageBook = DefaultTaPageBook;
95
+ __decorate([
96
+ (0, core_1.Autowired)(team_protocol_1.TaPrincipals),
97
+ __metadata("design:type", Object)
98
+ ], DefaultTaPageBook.prototype, "principals", void 0);
99
+ __decorate([
100
+ (0, core_1.Autowired)({ id: team_protocol_1.TaHostTeamProvider, detached: true }),
101
+ __metadata("design:type", Object)
102
+ ], DefaultTaPageBook.prototype, "teamProvider", void 0);
103
+ exports.DefaultTaPageBook = DefaultTaPageBook = __decorate([
104
+ (0, core_1.Component)(exports.TaPageBook)
105
+ ], DefaultTaPageBook);
106
+ /** A page says which requests it answers (TaPageServer). */
107
+ let TaPageServerImpl = class TaPageServerImpl {
108
+ async attach(info) {
109
+ this.book.attach(node_1.RpcContext.requireConnection(), info);
110
+ }
111
+ };
112
+ exports.TaPageServerImpl = TaPageServerImpl;
113
+ __decorate([
114
+ (0, core_1.Autowired)(exports.TaPageBook),
115
+ __metadata("design:type", Object)
116
+ ], TaPageServerImpl.prototype, "book", void 0);
117
+ exports.TaPageServerImpl = TaPageServerImpl = __decorate([
118
+ (0, rpc_1.Rpc)(ta_protocol_1.TaPageServer)
119
+ ], TaPageServerImpl);
120
+ //# sourceMappingURL=page-book.js.map
@@ -80,6 +80,8 @@ export declare class TaRelayServerImpl implements TaRelayServer {
80
80
  addStore(type: string, options: Record<string, unknown>, name?: string): Promise<string>;
81
81
  removeRelay(id: string): Promise<void>;
82
82
  chooseRelay(feature: TaRelayFeature, id: string | null): Promise<TaRelays>;
83
+ joinDeployment(url: string): Promise<TaRelays>;
84
+ leaveDeployment(): Promise<TaRelays>;
83
85
  probeRelay(idOrUrl: string): Promise<TaRelayProbe>;
84
86
  /** A change of relays.json; what it refuses is said as the errors of Ta. */
85
87
  protected relaysCall<T>(change: () => T): T;
@@ -241,6 +241,7 @@ let RelayBridge = RelayBridge_1 = class RelayBridge {
241
241
  const url = node_1.RelayServiceUtil.isRelay(service) ? service.url : undefined;
242
242
  return [feature, service.id || ((_a = services.find(each => each.type === 'ta-relay' && each.url === url)) === null || _a === void 0 ? void 0 : _a.id) || ''];
243
243
  }));
244
+ const deployment = this.relays.deployment(config);
244
245
  const drives = node_1.RelayServiceUtil.drives().filter(drive => !services.some(service => service.type === 'folder' && service.path === drive.path));
245
246
  const types = [...this.types].map(([type, { title, hint, icon, options }]) => ({
246
247
  type,
@@ -256,6 +257,7 @@ let RelayBridge = RelayBridge_1 = class RelayBridge {
256
257
  ...(drives.length ? { drives } : {}),
257
258
  ...(types.length ? { types } : {}),
258
259
  ...(error ? { error } : {}),
260
+ ...(deployment ? { deployment } : {}),
259
261
  };
260
262
  }
261
263
  /** The store of a service of a type a package registered. */
@@ -427,6 +429,38 @@ let TaRelayServerImpl = class TaRelayServerImpl {
427
429
  return this.bridge.listRelays();
428
430
  });
429
431
  }
432
+ joinDeployment(url) {
433
+ return ta_errors_1.TaErrors.guard(async () => {
434
+ var _a, _b;
435
+ const address = ta_protocol_1.TaDeploymentUtil.url(url);
436
+ if (!address) {
437
+ throw ta_errors_1.TaErrors.invalidArgument(`${url} is not an address of http or https.`, 'Pass the address of the gateway or the relay of the deployment, https://….');
438
+ }
439
+ let deployment;
440
+ try {
441
+ deployment = await ta_protocol_1.TaDeploymentUtil.discover(address);
442
+ }
443
+ catch (error) {
444
+ throw ta_errors_1.TaErrors.invalidArgument(error instanceof Error ? error.message : String(error), 'Check the address: a service of a deployment answers /.well-known/ta. A relay alone is added as a relay.');
445
+ }
446
+ if (deployment.relay) {
447
+ const probe = await node_1.RelayInfoUtil.probe(deployment.relay);
448
+ if (!probe.online) {
449
+ throw ta_errors_1.TaErrors.invalidArgument(`The relay of ${(_a = deployment.name) !== null && _a !== void 0 ? _a : address}, ${deployment.relay}, does not answer: ${(_b = probe.error) !== null && _b !== void 0 ? _b : 'no answer'}.`, 'Ask whoever runs the deployment whether its relay runs.');
450
+ }
451
+ }
452
+ this.relaysCall(() => this.bridge.relays.join(address, deployment));
453
+ this.bridge.relaysChanged();
454
+ return this.bridge.listRelays();
455
+ });
456
+ }
457
+ leaveDeployment() {
458
+ return ta_errors_1.TaErrors.guard(async () => {
459
+ this.relaysCall(() => this.bridge.relays.leave());
460
+ this.bridge.relaysChanged();
461
+ return this.bridge.listRelays();
462
+ });
463
+ }
430
464
  async probeRelay(idOrUrl) {
431
465
  var _a, _b, _c;
432
466
  const text = String(idOrUrl !== null && idOrUrl !== void 0 ? idOrUrl : '');
@@ -0,0 +1,41 @@
1
+ import { ExecClient } from '@celljs/ta-exec';
2
+ /** The path of the exec server of ta serve (and, forwarded, of ta gateway): ExecEndpoint. */
3
+ export declare const TA_EXEC_PATH = "/api/exec";
4
+ export interface CloudHost {
5
+ name: string;
6
+ /** The http(s) address of the gateway or Ta service. */
7
+ url: string;
8
+ /** That of the account there: a sign-in of the gateway, or the access token of a Ta service. */
9
+ token: string;
10
+ addedAt: number;
11
+ }
12
+ export declare namespace CloudHostsUtil {
13
+ /** An address as one writes it: http(s), without a path, a query or credentials. */
14
+ function url(value: string): string;
15
+ /** The WebSocket of its exec server. */
16
+ function execUrl(url: string): string;
17
+ }
18
+ /**
19
+ * The cloud workspaces the user added (`<agent directory>/cloud-hosts.json`, readable by this user only, as their
20
+ * tokens are there), and a connection to each, made on its first call. Each connection fences the ones of
21
+ * before (its epoch is when it was made), as with `ta.exec.url`.
22
+ */
23
+ export declare class CloudHosts {
24
+ protected readonly dir: () => string;
25
+ protected readonly clients: Map<string, ExecClient>;
26
+ constructor(dir: () => string);
27
+ list(): CloudHost[];
28
+ get(name: string): CloudHost | undefined;
29
+ save(host: CloudHost): void;
30
+ remove(name: string): void;
31
+ /** A connection to its exec server, made once; undefined for a name not added. */
32
+ client(name: string): ExecClient | undefined;
33
+ /** What a connection to it would be, for a host not kept yet (checked before it is). */
34
+ static connect(host: Pick<CloudHost, 'url' | 'token'>): ExecClient;
35
+ connected(name: string): boolean;
36
+ /** Its connection ends; the next call connects again. */
37
+ drop(name: string): ExecClient | undefined;
38
+ protected file(): string;
39
+ protected write(hosts: CloudHost[]): void;
40
+ }
41
+ //# sourceMappingURL=cloud-hosts.d.ts.map
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudHosts = exports.CloudHostsUtil = exports.TA_EXEC_PATH = void 0;
4
+ const node_fs_1 = require("node:fs");
5
+ const node_path_1 = require("node:path");
6
+ const ta_exec_1 = require("@celljs/ta-exec");
7
+ const ta_protocol_1 = require("@celljs/ta-protocol");
8
+ /** The path of the exec server of ta serve (and, forwarded, of ta gateway): ExecEndpoint. */
9
+ exports.TA_EXEC_PATH = '/api/exec';
10
+ var CloudHostsUtil;
11
+ (function (CloudHostsUtil) {
12
+ /** An address as one writes it: http(s), without a path, a query or credentials. */
13
+ function url(value) {
14
+ let parsed;
15
+ try {
16
+ parsed = new URL(value.trim());
17
+ }
18
+ catch (_a) {
19
+ throw new Error(`${value} is not an address: write https://ta.example.com.`);
20
+ }
21
+ if ((parsed.protocol !== 'https:' && parsed.protocol !== 'http:') || parsed.username || parsed.password) {
22
+ throw new Error(`${value} is not an address of a Ta gateway: write https://ta.example.com.`);
23
+ }
24
+ return `${parsed.origin}${parsed.pathname.replace(/\/+$/, '')}`;
25
+ }
26
+ CloudHostsUtil.url = url;
27
+ /** The WebSocket of its exec server. */
28
+ function execUrl(url) {
29
+ return `${url.replace(/^http/, 'ws')}${exports.TA_EXEC_PATH}`;
30
+ }
31
+ CloudHostsUtil.execUrl = execUrl;
32
+ })(CloudHostsUtil || (exports.CloudHostsUtil = CloudHostsUtil = {}));
33
+ /**
34
+ * The cloud workspaces the user added (`<agent directory>/cloud-hosts.json`, readable by this user only, as their
35
+ * tokens are there), and a connection to each, made on its first call. Each connection fences the ones of
36
+ * before (its epoch is when it was made), as with `ta.exec.url`.
37
+ */
38
+ class CloudHosts {
39
+ constructor(dir) {
40
+ this.dir = dir;
41
+ this.clients = new Map();
42
+ }
43
+ list() {
44
+ try {
45
+ const value = JSON.parse((0, node_fs_1.readFileSync)(this.file(), 'utf8'));
46
+ return Array.isArray(value.hosts)
47
+ ? value.hosts.filter(host => ta_protocol_1.TaLocation.isCloudName(host === null || host === void 0 ? void 0 : host.name) && typeof host.url === 'string' && typeof host.token === 'string')
48
+ : [];
49
+ }
50
+ catch (_a) {
51
+ return [];
52
+ }
53
+ }
54
+ get(name) {
55
+ return this.list().find(host => host.name === name);
56
+ }
57
+ save(host) {
58
+ this.write([host, ...this.list().filter(other => other.name !== host.name)]);
59
+ }
60
+ remove(name) {
61
+ this.write(this.list().filter(other => other.name !== name));
62
+ }
63
+ /** A connection to its exec server, made once; undefined for a name not added. */
64
+ client(name) {
65
+ let client = this.clients.get(name);
66
+ if (!client) {
67
+ const host = this.get(name);
68
+ if (!host) {
69
+ return undefined;
70
+ }
71
+ client = CloudHosts.connect(host);
72
+ this.clients.set(name, client);
73
+ }
74
+ return client;
75
+ }
76
+ /** What a connection to it would be, for a host not kept yet (checked before it is). */
77
+ static connect(host) {
78
+ return new ta_exec_1.ExecClient({ connect: ta_exec_1.ExecClient.webSocket(CloudHostsUtil.execUrl(host.url), host.token), epoch: Date.now() });
79
+ }
80
+ connected(name) {
81
+ var _a;
82
+ var _b;
83
+ return (_b = (_a = this.clients.get(name)) === null || _a === void 0 ? void 0 : _a.connected) !== null && _b !== void 0 ? _b : false;
84
+ }
85
+ /** Its connection ends; the next call connects again. */
86
+ drop(name) {
87
+ const client = this.clients.get(name);
88
+ this.clients.delete(name);
89
+ return client;
90
+ }
91
+ file() {
92
+ return (0, node_path_1.join)(this.dir(), 'cloud-hosts.json');
93
+ }
94
+ write(hosts) {
95
+ const file = this.file();
96
+ const temporary = `${file}.${process.pid}.tmp`;
97
+ (0, node_fs_1.writeFileSync)(temporary, JSON.stringify({ hosts }, undefined, 2), { mode: 0o600 });
98
+ (0, node_fs_1.chmodSync)(temporary, 0o600);
99
+ (0, node_fs_1.renameSync)(temporary, file);
100
+ }
101
+ }
102
+ exports.CloudHosts = CloudHosts;
103
+ //# sourceMappingURL=cloud-hosts.js.map
@@ -1,3 +1,4 @@
1
+ export * from './cloud-hosts';
1
2
  export * from './pi-directories';
2
3
  export * from './port-forwards';
3
4
  export * from './remote-hosts';
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./cloud-hosts"), exports);
17
18
  __exportStar(require("./pi-directories"), exports);
18
19
  __exportStar(require("./port-forwards"), exports);
19
20
  __exportStar(require("./remote-hosts"), exports);
@@ -2,7 +2,8 @@ import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
2
2
  import { type TaExecOptions } from '../host-protocol';
3
3
  import { PiDirectories } from './remote-protocol';
4
4
  /**
5
- * `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path>`. With `ta.exec.url` every
5
+ * `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path>`, `remote/ta/<name>/<path>` for a cloud
6
+ * workspace, `ta://<name>/<path>`. With `ta.exec.url` every
6
7
  * directory is one of the machine of the exec server: `<agent directory>/remote/exec/<path>`.
7
8
  */
8
9
  export declare class DefaultPiDirectories implements PiDirectories {
@@ -19,14 +19,16 @@ const ta_protocol_1 = require("@celljs/ta-protocol");
19
19
  const host_protocol_1 = require("../host-protocol");
20
20
  const remote_protocol_1 = require("./remote-protocol");
21
21
  /**
22
- * `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path>`. With `ta.exec.url` every
22
+ * `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path>`, `remote/ta/<name>/<path>` for a cloud
23
+ * workspace, `ta://<name>/<path>`. With `ta.exec.url` every
23
24
  * directory is one of the machine of the exec server: `<agent directory>/remote/exec/<path>`.
24
25
  */
25
26
  let DefaultPiDirectories = class DefaultPiDirectories {
26
27
  toPi(location) {
27
28
  var _a;
28
29
  const remote = ta_protocol_1.TaLocation.parse(location);
29
- const prefix = remote ? (0, node_path_1.join)('ssh', remote.host) : ((_a = this.execOptions) === null || _a === void 0 ? void 0 : _a.url) ? 'exec' : undefined;
30
+ const cloud = remote && ta_protocol_1.TaLocation.cloudName(remote.host);
31
+ const prefix = cloud ? (0, node_path_1.join)('ta', cloud) : remote ? (0, node_path_1.join)('ssh', remote.host) : ((_a = this.execOptions) === null || _a === void 0 ? void 0 : _a.url) ? 'exec' : undefined;
30
32
  if (!prefix) {
31
33
  return location;
32
34
  }
@@ -44,6 +46,9 @@ let DefaultPiDirectories = class DefaultPiDirectories {
44
46
  if (kind === 'ssh' && rest.length > 0 && ta_protocol_1.TaLocation.isHost(rest[0])) {
45
47
  return ta_protocol_1.TaLocation.ssh(rest[0], `/${rest.slice(1).join('/')}`);
46
48
  }
49
+ if (kind === 'ta' && rest.length > 0 && ta_protocol_1.TaLocation.isCloudName(rest[0])) {
50
+ return ta_protocol_1.TaLocation.cloud(rest[0], `/${rest.slice(1).join('/')}`);
51
+ }
47
52
  return kind === 'exec' ? `/${rest.join('/')}` : cwd;
48
53
  }
49
54
  root() {
@@ -4,6 +4,7 @@ import type { RpcConnection } from '@celljs/rpc/lib/node';
4
4
  import { type ExecClient, SshHost, type SshQuestion } from '@celljs/ta-exec';
5
5
  import { type TaRemoteEvent, type TaRemoteHost, type TaRemotePrompt } from '@celljs/ta-protocol';
6
6
  import { TaWorkingDirectory } from '../working-directory';
7
+ import { CloudHosts } from './cloud-hosts';
7
8
  import { RemoteHosts, type TaRemoteOptions } from './remote-protocol';
8
9
  interface Waiting {
9
10
  prompt: TaRemotePrompt;
@@ -21,9 +22,13 @@ export declare class DefaultRemoteHosts implements RemoteHosts, ApplicationLifec
21
22
  protected readonly waiting: Map<string, Waiting>;
22
23
  protected readonly watchers: Set<RpcConnection>;
23
24
  protected readonly disconnectListeners: Set<(host: string) => void>;
25
+ protected readonly cloud: CloudHosts;
26
+ protected readonly watchedCloud: Set<string>;
24
27
  client(host: string): ExecClient;
25
28
  list(): TaRemoteHost[];
26
29
  add(host: string): void;
30
+ /** Checked by connecting and asking where its home is; kept only then. */
31
+ addCloud(name: string, url: string, token: string): Promise<TaRemoteHost>;
27
32
  remove(host: string): Promise<void>;
28
33
  disconnect(host: string): Promise<void>;
29
34
  onDisconnect(listener: (host: string) => void): () => void;
@@ -18,6 +18,7 @@ const ta_brand_1 = require("@celljs/ta-harness/lib/node/ta-brand");
18
18
  const ta_protocol_1 = require("@celljs/ta-protocol");
19
19
  const ta_errors_1 = require("../ta-errors");
20
20
  const working_directory_1 = require("../working-directory");
21
+ const cloud_hosts_1 = require("./cloud-hosts");
21
22
  const remote_protocol_1 = require("./remote-protocol");
22
23
  /** A prompt nobody answers is cancelled after this: ssh gives up. */
23
24
  const PROMPT_TIMEOUT_MS = 5 * 60 * 1000;
@@ -31,11 +32,25 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
31
32
  this.waiting = new Map();
32
33
  this.watchers = new Set();
33
34
  this.disconnectListeners = new Set();
35
+ this.cloud = new cloud_hosts_1.CloudHosts(() => { var _a; var _b; return (_b = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _b !== void 0 ? _b : ta_brand_1.TaBrand.agentDir(); });
36
+ this.watchedCloud = new Set();
34
37
  }
35
38
  client(host) {
36
39
  var _a, _b, _c, _d;
37
40
  var _e;
38
41
  this.check(host);
42
+ const name = ta_protocol_1.TaLocation.cloudName(host);
43
+ if (name) {
44
+ const client = this.cloud.client(name);
45
+ if (!client) {
46
+ throw ta_errors_1.TaErrors.invalidArgument(`There is no cloud workspace ${name}.`, 'Add it in Settings, Remote hosts, with the address of its gateway and a token.');
47
+ }
48
+ if (!this.watchedCloud.has(name)) {
49
+ this.watchedCloud.add(name);
50
+ client.onState((connected, error) => this.broadcast(error ? { type: 'connection', host, connected, error } : { type: 'connection', host, connected }));
51
+ }
52
+ return client;
53
+ }
39
54
  let ssh = this.hosts.get(host);
40
55
  if (!ssh) {
41
56
  ssh = new ta_exec_1.SshHost({
@@ -61,18 +76,68 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
61
76
  const recent = this.workingDirectory.recentHosts();
62
77
  const config = ta_exec_1.SshConfig.hosts().filter(host => ta_protocol_1.TaLocation.isHost(host) && !recent.includes(host));
63
78
  return [
79
+ ...this.cloud
80
+ .list()
81
+ .map(host => ({ host: ta_protocol_1.TaLocation.cloud(host.name), source: 'cloud', url: host.url, connected: this.cloud.connected(host.name) })),
64
82
  ...recent.map(host => ({ host, source: 'recent', connected: this.connected(host) })),
65
83
  ...config.map(host => ({ host, source: 'config', connected: this.connected(host) })),
66
84
  ];
67
85
  }
68
86
  add(host) {
87
+ if (ta_protocol_1.TaLocation.isCloud(host)) {
88
+ throw ta_errors_1.TaErrors.invalidArgument('A cloud workspace is added with its address and token.', 'Call addCloudHost.');
89
+ }
69
90
  this.workingDirectory.addRecentHost(this.check(host));
70
91
  }
92
+ /** Checked by connecting and asking where its home is; kept only then. */
93
+ async addCloud(name, url, token) {
94
+ if (typeof name !== 'string' || !ta_protocol_1.TaLocation.isCloudName(name)) {
95
+ throw ta_errors_1.TaErrors.invalidArgument(`${JSON.stringify(name)} cannot name a cloud workspace.`, 'Use lower-case letters, digits and -, 40 at most.');
96
+ }
97
+ let address;
98
+ try {
99
+ address = cloud_hosts_1.CloudHostsUtil.url(String(url !== null && url !== void 0 ? url : ''));
100
+ }
101
+ catch (error) {
102
+ throw ta_errors_1.TaErrors.invalidArgument(error.message, 'Write the address of the Ta gateway, as its page is opened.');
103
+ }
104
+ if (typeof token !== 'string' || !token.trim()) {
105
+ throw ta_errors_1.TaErrors.invalidArgument('A cloud workspace is reached with a token.', 'Make one on the gateway: Settings, Environment, Use it from another Ta.');
106
+ }
107
+ const probe = cloud_hosts_1.CloudHosts.connect({ url: address, token: token.trim() });
108
+ try {
109
+ await probe.info();
110
+ }
111
+ catch (error) {
112
+ throw ta_errors_1.TaErrors.invalidArgument(`${address} did not let Ta in: ${error.message}`, 'Check the address and the token; a token signed out on the gateway no longer works.');
113
+ }
114
+ finally {
115
+ probe.close();
116
+ }
117
+ await this.disconnect(ta_protocol_1.TaLocation.cloud(name));
118
+ this.cloud.save({ name, url: address, token: token.trim(), addedAt: Date.now() });
119
+ return { host: ta_protocol_1.TaLocation.cloud(name), source: 'cloud', url: address, connected: false };
120
+ }
71
121
  async remove(host) {
72
122
  await this.disconnect(host);
123
+ const name = ta_protocol_1.TaLocation.cloudName(host);
124
+ if (name) {
125
+ this.cloud.remove(name);
126
+ return;
127
+ }
73
128
  this.workingDirectory.removeRecentHost(host);
74
129
  }
75
130
  async disconnect(host) {
131
+ const name = ta_protocol_1.TaLocation.cloudName(host);
132
+ if (name) {
133
+ const client = this.cloud.drop(name);
134
+ this.watchedCloud.delete(name);
135
+ for (const listener of this.disconnectListeners) {
136
+ listener(host);
137
+ }
138
+ client === null || client === void 0 ? void 0 : client.close();
139
+ return;
140
+ }
76
141
  const ssh = this.hosts.get(host);
77
142
  this.hosts.delete(host);
78
143
  for (const listener of this.disconnectListeners) {
@@ -100,7 +165,7 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
100
165
  for (const waiting of [...this.waiting.values()]) {
101
166
  waiting.settle(undefined);
102
167
  }
103
- await Promise.all([...this.hosts.keys()].map(host => this.disconnect(host)));
168
+ await Promise.all([...this.hosts.keys(), ...[...this.watchedCloud].map(name => ta_protocol_1.TaLocation.cloud(name))].map(host => this.disconnect(host)));
104
169
  }
105
170
  ask(host, question) {
106
171
  const prompt = { promptId: (0, node_crypto_1.randomUUID)(), host, ...question };
@@ -129,13 +194,17 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
129
194
  }
130
195
  check(host) {
131
196
  if (typeof host !== 'string' || !ta_protocol_1.TaLocation.isHost(host)) {
132
- throw ta_errors_1.TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host.`, 'Name a Host of ~/.ssh/config, or [user@]host[:port].');
197
+ throw ta_errors_1.TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host.`, 'Name a Host of ~/.ssh/config, [user@]host[:port], or ta://<name> of a cloud workspace.');
133
198
  }
134
199
  return host;
135
200
  }
136
201
  connected(host) {
137
202
  var _a;
138
203
  var _b;
204
+ const name = ta_protocol_1.TaLocation.cloudName(host);
205
+ if (name) {
206
+ return this.cloud.connected(name);
207
+ }
139
208
  return (_b = (_a = this.hosts.get(host)) === null || _a === void 0 ? void 0 : _a.client.connected) !== null && _b !== void 0 ? _b : false;
140
209
  }
141
210
  };
@@ -21,6 +21,8 @@ export interface RemoteHosts {
21
21
  list(): TaRemoteHost[];
22
22
  /** Keeps a host in the list without connecting; throws for what is not a host. */
23
23
  add(host: string): void;
24
+ /** A cloud workspace, `ta://<name>`, kept once a connection to it worked. */
25
+ addCloud(name: string, url: string, token: string): Promise<TaRemoteHost>;
24
26
  /** Disconnects and drops it from the list of those opened before. */
25
27
  remove(host: string): Promise<void>;
26
28
  disconnect(host: string): Promise<void>;
@@ -8,6 +8,7 @@ export declare class TaRemoteServerImpl implements TaRemoteServer {
8
8
  protected readonly environments: ExecEnvironmentFactory;
9
9
  listHosts(): Promise<TaRemoteHost[]>;
10
10
  addHost(host: string): Promise<void>;
11
+ addCloudHost(name: string, url: string, token: string): Promise<TaRemoteHost>;
11
12
  removeHost(host: string): Promise<void>;
12
13
  resolveLocation(location: string): Promise<string>;
13
14
  forwardPort(port: number, cwd?: string): Promise<TaForwardedPort>;
@@ -25,6 +25,9 @@ let TaRemoteServerImpl = class TaRemoteServerImpl {
25
25
  addHost(host) {
26
26
  return ta_errors_1.TaErrors.guard(() => this.hosts.add(host));
27
27
  }
28
+ addCloudHost(name, url, token) {
29
+ return ta_errors_1.TaErrors.guard(() => this.hosts.addCloud(name, url, token));
30
+ }
28
31
  removeHost(host) {
29
32
  return ta_errors_1.TaErrors.guard(() => this.hosts.remove(host));
30
33
  }
@@ -53,6 +53,9 @@ export declare class TaRoomServerImpl implements TaRoomServer {
53
53
  listRequests(): Promise<TaRoomRequest[]>;
54
54
  answer(room: string, ask: string, accept: boolean): Promise<void>;
55
55
  removeMember(room: string, member: string): Promise<void>;
56
+ configureRoom(room: string, options: {
57
+ members?: number;
58
+ }): Promise<TaRoomInfo>;
56
59
  leave(room: string): Promise<void>;
57
60
  send(room: string, data: unknown, to?: string): Promise<void>;
58
61
  getState(room: string, prefix?: string): Promise<Record<string, TaRoomValue>>;
@@ -35,6 +35,7 @@ let RoomsBridge = RoomsBridge_1 = class RoomsBridge {
35
35
  listRooms: async () => this.hubs.list(),
36
36
  createRoom: input => this.hubs.create(input),
37
37
  invite: (room, options) => this.hubs.invite(room, options !== null && options !== void 0 ? options : {}),
38
+ configureRoom: async (room, options) => this.hubs.configure(room, options !== null && options !== void 0 ? options : {}),
38
39
  join: link => this.hubs.join(link),
39
40
  listRequests: () => this.hubs.requests(),
40
41
  answer: (room, ask, accept) => this.hubs.answer(room, ask, accept),
@@ -152,6 +153,9 @@ let TaRoomServerImpl = class TaRoomServerImpl {
152
153
  removeMember(room, member) {
153
154
  return ta_errors_1.TaErrors.guard(() => this.rooms.removeMember(room, member));
154
155
  }
156
+ configureRoom(room, options) {
157
+ return ta_errors_1.TaErrors.guard(() => this.rooms.configureRoom(room, { members: options === null || options === void 0 ? void 0 : options.members }));
158
+ }
155
159
  leave(room) {
156
160
  return ta_errors_1.TaErrors.guard(() => this.rooms.leave(room));
157
161
  }