@celljs/ta-gateway 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 (81) hide show
  1. package/README.en.md +5 -2
  2. package/README.md +5 -2
  3. package/esm/node/command-sandbox.d.ts +1 -0
  4. package/esm/node/command-sandbox.js +10 -1
  5. package/esm/node/docker-engine.d.ts +10 -1
  6. package/esm/node/docker-engine.js +18 -1
  7. package/esm/node/docker-sandbox.d.ts +13 -1
  8. package/esm/node/docker-sandbox.js +35 -2
  9. package/esm/node/egress-proxy.d.ts +98 -0
  10. package/esm/node/egress-proxy.js +428 -0
  11. package/esm/node/environments.d.ts +5 -0
  12. package/esm/node/environments.js +7 -3
  13. package/esm/node/gateway-cli.js +12 -5
  14. package/esm/node/gateway-exec.d.ts +19 -0
  15. package/esm/node/gateway-exec.js +65 -0
  16. package/esm/node/gateway-policy.d.ts +7 -1
  17. package/esm/node/gateway-policy.js +12 -2
  18. package/esm/node/gateway-quotas.d.ts +15 -2
  19. package/esm/node/gateway-quotas.js +51 -3
  20. package/esm/node/gateway-server.d.ts +43 -1
  21. package/esm/node/gateway-server.js +179 -10
  22. package/esm/node/gateway-service-util.d.ts +41 -0
  23. package/esm/node/gateway-service-util.js +178 -0
  24. package/esm/node/gateway-service.d.ts +13 -41
  25. package/esm/node/gateway-service.js +41 -176
  26. package/esm/node/gateway-socket.d.ts +1 -1
  27. package/esm/node/gateway-socket.js +1 -1
  28. package/esm/node/index.d.ts +4 -0
  29. package/esm/node/index.js +4 -0
  30. package/esm/node/kubernetes-api.d.ts +35 -0
  31. package/esm/node/kubernetes-api.js +115 -0
  32. package/esm/node/kubernetes-sandbox.d.ts +198 -0
  33. package/esm/node/kubernetes-sandbox.js +279 -0
  34. package/esm/node/local-sandbox.d.ts +1 -0
  35. package/esm/node/local-sandbox.js +2 -1
  36. package/esm/node/model-proxy.d.ts +105 -0
  37. package/esm/node/model-proxy.js +435 -0
  38. package/esm/node/sandbox-provider.d.ts +17 -0
  39. package/esm/node/sandbox-provider.js +2 -1
  40. package/esm/node/tenant-store.d.ts +22 -0
  41. package/esm/node/tenant-store.js +25 -0
  42. package/lib/node/command-sandbox.d.ts +1 -0
  43. package/lib/node/command-sandbox.js +10 -1
  44. package/lib/node/docker-engine.d.ts +10 -1
  45. package/lib/node/docker-engine.js +18 -1
  46. package/lib/node/docker-sandbox.d.ts +13 -1
  47. package/lib/node/docker-sandbox.js +35 -2
  48. package/lib/node/egress-proxy.d.ts +99 -0
  49. package/lib/node/egress-proxy.js +433 -0
  50. package/lib/node/environments.d.ts +5 -0
  51. package/lib/node/environments.js +7 -3
  52. package/lib/node/gateway-cli.js +12 -5
  53. package/lib/node/gateway-exec.d.ts +20 -0
  54. package/lib/node/gateway-exec.js +69 -0
  55. package/lib/node/gateway-policy.d.ts +7 -1
  56. package/lib/node/gateway-policy.js +12 -2
  57. package/lib/node/gateway-quotas.d.ts +15 -2
  58. package/lib/node/gateway-quotas.js +51 -3
  59. package/lib/node/gateway-server.d.ts +43 -1
  60. package/lib/node/gateway-server.js +177 -8
  61. package/lib/node/gateway-service-util.d.ts +42 -0
  62. package/lib/node/gateway-service-util.js +182 -0
  63. package/lib/node/gateway-service.d.ts +13 -41
  64. package/lib/node/gateway-service.js +63 -199
  65. package/lib/node/gateway-socket.d.ts +1 -1
  66. package/lib/node/gateway-socket.js +10 -10
  67. package/lib/node/index.d.ts +4 -0
  68. package/lib/node/index.js +4 -0
  69. package/lib/node/kubernetes-api.d.ts +36 -0
  70. package/lib/node/kubernetes-api.js +120 -0
  71. package/lib/node/kubernetes-sandbox.d.ts +199 -0
  72. package/lib/node/kubernetes-sandbox.js +283 -0
  73. package/lib/node/local-sandbox.d.ts +1 -0
  74. package/lib/node/local-sandbox.js +2 -1
  75. package/lib/node/model-proxy.d.ts +106 -0
  76. package/lib/node/model-proxy.js +440 -0
  77. package/lib/node/sandbox-provider.d.ts +17 -0
  78. package/lib/node/sandbox-provider.js +2 -1
  79. package/lib/node/tenant-store.d.ts +22 -0
  80. package/lib/node/tenant-store.js +25 -0
  81. package/package.json +4 -4
@@ -0,0 +1,178 @@
1
+ import { TA_RPC_ERROR_CODE, TaErrorCode } from '@celljs/ta-protocol';
2
+ import { GatewayPolicyUtil } from './gateway-policy.js';
3
+ import { TaSandboxUtil } from './ta-sandbox.js';
4
+ export class GatewayCallError extends Error {
5
+ constructor(message, code, hint) {
6
+ super(message);
7
+ this.code = code;
8
+ this.hint = hint;
9
+ }
10
+ }
11
+ export var GatewayServiceUtil;
12
+ (function (GatewayServiceUtil) {
13
+ GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
14
+ GatewayServiceUtil.MAX_LOG_LINES = 2000;
15
+ /**
16
+ * Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
17
+ * `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
18
+ * (GatewayService.manage).
19
+ */
20
+ GatewayServiceUtil.RULES = {
21
+ TaGatewayServer: {
22
+ getGateway: 'anyone',
23
+ setEnvironment: 'anyone',
24
+ restartEnvironment: 'anyone',
25
+ listTenants: 'admin',
26
+ getConsole: 'admin',
27
+ getTenantAgents: 'admin',
28
+ getAgentActivity: 'admin',
29
+ getTenantLogs: 'admin',
30
+ getUsageTrend: 'admin',
31
+ getPolicy: 'admin',
32
+ getAudit: 'admin',
33
+ startTenant: 'admin',
34
+ stopTenant: 'admin',
35
+ restartTenant: 'admin',
36
+ createSignInLink: 'admin',
37
+ setTenantLimits: 'admin',
38
+ grantMinutes: 'admin',
39
+ setSuspended: 'admin',
40
+ setDefaultLimits: 'owner',
41
+ setRole: 'owner',
42
+ setEgressHosts: 'owner',
43
+ createAccessToken: 'anyone',
44
+ },
45
+ TaAccessServer: {
46
+ getAccess: 'anyone',
47
+ signOut: 'anyone',
48
+ bindUrl: 'admin',
49
+ allow: 'admin',
50
+ dismiss: 'admin',
51
+ disallow: 'admin',
52
+ },
53
+ };
54
+ /** What an administrator did or looked at of others: kept in the audit. */
55
+ GatewayServiceUtil.AUDITED = new Set([
56
+ 'startTenant',
57
+ 'stopTenant',
58
+ 'restartTenant',
59
+ 'getTenantLogs',
60
+ 'getAgentActivity',
61
+ 'createSignInLink',
62
+ 'setTenantLimits',
63
+ 'grantMinutes',
64
+ 'setSuspended',
65
+ 'setDefaultLimits',
66
+ 'setRole',
67
+ 'setEgressHosts',
68
+ 'allow',
69
+ 'dismiss',
70
+ 'disallow',
71
+ ]);
72
+ /** By name, own keys only: a method such as `toString` is no rule. */
73
+ const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
74
+ function need(service, name) {
75
+ var _a;
76
+ return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
77
+ }
78
+ GatewayServiceUtil.need = need;
79
+ function allowed(role, need) {
80
+ return need === 'anyone' || GatewayPolicyUtil.rank(role) >= GatewayPolicyUtil.rank(need);
81
+ }
82
+ GatewayServiceUtil.allowed = allowed;
83
+ /** As many lines as asked, within bounds. */
84
+ function lines(value) {
85
+ const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
86
+ return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
87
+ }
88
+ GatewayServiceUtil.lines = lines;
89
+ /** Days of a trend, 1 to 31. */
90
+ function days(value) {
91
+ const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
92
+ return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
93
+ }
94
+ GatewayServiceUtil.days = days;
95
+ /** The services the gateway answers itself, not the Ta of the tenant. */
96
+ GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
97
+ function owns(method) {
98
+ return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
99
+ }
100
+ GatewayServiceUtil.owns = owns;
101
+ /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
102
+ function mayOwn(text) {
103
+ return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
104
+ }
105
+ GatewayServiceUtil.mayOwn = mayOwn;
106
+ /** The calls that send the agents a message, which the limit of messages a minute counts. */
107
+ GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
108
+ function mayPrompt(text) {
109
+ return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
110
+ }
111
+ GatewayServiceUtil.mayPrompt = mayPrompt;
112
+ /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
113
+ function error(id, message, code, hint) {
114
+ return JSON.stringify({ jsonrpc: '2.0', id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
115
+ }
116
+ GatewayServiceUtil.error = error;
117
+ function result(id, result) {
118
+ return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
119
+ }
120
+ GatewayServiceUtil.result = result;
121
+ function invalid(message, hint) {
122
+ return new GatewayCallError(message, TaErrorCode.INVALID_ARGUMENT, hint);
123
+ }
124
+ function forbidden(message, hint) {
125
+ return new GatewayCallError(message, TaErrorCode.FORBIDDEN, hint);
126
+ }
127
+ GatewayServiceUtil.forbidden = forbidden;
128
+ function choice(value, own, providers) {
129
+ var _a;
130
+ const given = (value !== null && value !== void 0 ? value : {});
131
+ if (typeof given.provider === 'string') {
132
+ if (!providers.includes(given.provider)) {
133
+ throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
134
+ }
135
+ return { provider: given.provider };
136
+ }
137
+ if (given.type === 'ta') {
138
+ if (!own) {
139
+ throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
140
+ }
141
+ let url;
142
+ try {
143
+ url = TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
144
+ }
145
+ catch (error) {
146
+ throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
147
+ }
148
+ if (typeof given.token !== 'string' || !given.token.trim()) {
149
+ throw invalid('A Ta service of your own is reached with its access token.', 'Copy it from ~/.ta/agent/serve.json there, or the link ta serve printed.');
150
+ }
151
+ return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
152
+ }
153
+ throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
154
+ }
155
+ GatewayServiceUtil.choice = choice;
156
+ /** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
157
+ function detail(method, params) {
158
+ const [, second] = params;
159
+ switch (method) {
160
+ case 'setTenantLimits':
161
+ case 'setDefaultLimits':
162
+ return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
163
+ case 'grantMinutes':
164
+ case 'getTenantLogs':
165
+ return String(second !== null && second !== void 0 ? second : '');
166
+ case 'setSuspended':
167
+ return second ? 'suspended' : 'let go';
168
+ case 'setRole':
169
+ return String(second);
170
+ case 'setEgressHosts':
171
+ return Array.isArray(params[0]) ? params[0].join(', ') || 'any public host' : undefined;
172
+ default:
173
+ return undefined;
174
+ }
175
+ }
176
+ GatewayServiceUtil.detail = detail;
177
+ })(GatewayServiceUtil || (GatewayServiceUtil = {}));
178
+ //# sourceMappingURL=gateway-service-util.js.map
@@ -1,10 +1,12 @@
1
1
  import type { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
2
- import { type TaAccessInfo, type TaAgentActivity, type TaEnvironmentChoice, TaErrorCode, type TaGatewayConsole, type TaGatewayInfo, type TaGatewayLimits, type TaGatewayPolicy, type TaGatewayRole, type TaGatewayTenant, type TaUsageTrend } from '@celljs/ta-protocol';
2
+ import { type TaAccessInfo, type TaAgentActivity, type TaGatewayConsole, type TaGatewayInfo, type TaGatewayLimits, type TaGatewayPolicy, type TaGatewayRole, type TaGatewayTenant, type TaUsageTrend } from '@celljs/ta-protocol';
3
+ import { type EgressProxy } from './egress-proxy.js';
3
4
  import type { Environments } from './environments.js';
4
5
  import type { GatewayAudit } from './gateway-audit.js';
5
6
  import { type GatewayPolicy } from './gateway-policy.js';
6
7
  import type { GatewayQuotas } from './gateway-quotas.js';
7
8
  import { type GatewayUsage } from './gateway-usage.js';
9
+ import { type ModelProxy } from './model-proxy.js';
8
10
  import type { SignInLinks } from './sign-in-links.js';
9
11
  import type { Tenant, TenantStore } from './tenant-store.js';
10
12
  /** Who calls: the browser signed in as `account`, with the token `token`, and what that account is on the gateway. */
@@ -16,11 +18,6 @@ export interface GatewayCaller {
16
18
  /** An owner or an admin. */
17
19
  admin: boolean;
18
20
  }
19
- export declare class GatewayCallError extends Error {
20
- readonly code: TaErrorCode;
21
- readonly hint: string;
22
- constructor(message: string, code: TaErrorCode, hint: string);
23
- }
24
21
  /** What the console says of the gateway itself. */
25
22
  export interface GatewayMeta {
26
23
  links: SignInLinks;
@@ -42,40 +39,8 @@ export interface GatewayParts {
42
39
  reset(tenantId: string): void;
43
40
  };
44
41
  meta: GatewayMeta;
45
- }
46
- type Need = TaGatewayRole | 'anyone';
47
- export declare namespace GatewayServiceUtil {
48
- const DEFAULT_LOG_LINES = 200;
49
- const MAX_LOG_LINES = 2000;
50
- /**
51
- * Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
52
- * `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
53
- * (GatewayService.manage).
54
- */
55
- const RULES: Record<string, Record<string, Need>>;
56
- /** What an administrator did or looked at of others: kept in the audit. */
57
- const AUDITED: Set<string>;
58
- function need(service: string, name: string): Need | undefined;
59
- function allowed(role: TaGatewayRole, need: Need): boolean;
60
- /** As many lines as asked, within bounds. */
61
- function lines(value: unknown): number;
62
- /** Days of a trend, 1 to 31. */
63
- function days(value: unknown): number;
64
- /** The services the gateway answers itself, not the Ta of the tenant. */
65
- const SERVICES: string[];
66
- function owns(method: unknown): method is string;
67
- /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
68
- function mayOwn(text: string): boolean;
69
- /** The calls that send the agents a message, which the limit of messages a minute counts. */
70
- const PROMPTS: string[];
71
- function mayPrompt(text: string): boolean;
72
- /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
73
- function error(id: unknown, message: string, code: TaErrorCode, hint: string): string;
74
- function result(id: unknown, result: unknown): string;
75
- function forbidden(message: string, hint: string): GatewayCallError;
76
- function choice(value: unknown, own: boolean, providers: string[]): TaEnvironmentChoice | undefined;
77
- /** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
78
- function detail(method: string, params: unknown[]): string | undefined;
42
+ models?: ModelProxy;
43
+ egress?: EgressProxy;
79
44
  }
80
45
  /**
81
46
  * The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
@@ -92,6 +57,8 @@ export declare class GatewayService {
92
57
  protected readonly audit: GatewayAudit;
93
58
  protected readonly hooks: GatewayParts['hooks'];
94
59
  protected readonly meta: GatewayMeta;
60
+ protected readonly models?: ModelProxy;
61
+ protected readonly egress?: EgressProxy;
95
62
  constructor(parts: GatewayParts);
96
63
  call(caller: GatewayCaller, method: string, params: unknown[]): Promise<unknown>;
97
64
  protected handle(caller: GatewayCaller, service: string, name: string, params: unknown[]): Promise<unknown>;
@@ -103,6 +70,7 @@ export declare class GatewayService {
103
70
  /** The tenants with what their sandboxes take and their agents do, read together: a slow one holds the console a second, not a second each. */
104
71
  protected console(caller: GatewayCaller): Promise<TaGatewayConsole>;
105
72
  protected policyView(): TaGatewayPolicy;
73
+ protected setEgressHosts(value: unknown): TaGatewayPolicy;
106
74
  protected tenant(id: unknown): Tenant;
107
75
  /** A tenant the caller may change: an owner any; an admin a member's only, not their own, nor another admin's or an owner's. */
108
76
  protected manage(caller: GatewayCaller, id: unknown): Tenant;
@@ -119,6 +87,11 @@ export declare class GatewayService {
119
87
  protected setSuspended(caller: GatewayCaller, id: unknown, suspended: unknown): Promise<void>;
120
88
  protected setDefaultLimits(value: unknown): TaGatewayPolicy;
121
89
  protected setRole(caller: GatewayCaller, account: unknown, role: unknown): TaGatewayPolicy;
90
+ /** A sign-in of the caller's own account, named for where it is used. */
91
+ protected accessToken(caller: GatewayCaller, label: unknown): {
92
+ token: string;
93
+ url?: string;
94
+ };
122
95
  protected access(caller: GatewayCaller): TaAccessInfo;
123
96
  protected allow(account: unknown, name: unknown): void;
124
97
  /** The browsers of the account are cut off, and what reaches its Ta otherwise (paired phones) goes with its sandbox. */
@@ -130,4 +103,3 @@ export declare class GatewayService {
130
103
  */
131
104
  protected signInLink(account: unknown, name: unknown): string;
132
105
  }
133
- export {};
@@ -1,179 +1,12 @@
1
1
  import { AccessUtil } from '@celljs/ta-host/lib/node/sign-in/access-store';
2
- import { TA_RPC_ERROR_CODE, TaErrorCode, } from '@celljs/ta-protocol';
2
+ import { TaErrorCode, } from '@celljs/ta-protocol';
3
3
  import { Redactor } from '@celljs/ta-protocol/lib/common/redactor';
4
+ import { EgressConfigError, EgressProxyUtil } from './egress-proxy.js';
4
5
  import { GatewayPolicyUtil, PolicyError } from './gateway-policy.js';
6
+ import { GatewayCallError, GatewayServiceUtil } from './gateway-service-util.js';
5
7
  import { GatewayUsageUtil } from './gateway-usage.js';
6
- import { TaSandboxUtil } from './ta-sandbox.js';
7
- export class GatewayCallError extends Error {
8
- constructor(message, code, hint) {
9
- super(message);
10
- this.code = code;
11
- this.hint = hint;
12
- }
13
- }
8
+ import { ModelProxyUtil } from './model-proxy.js';
14
9
  const DAY = 24 * 3600000;
15
- export var GatewayServiceUtil;
16
- (function (GatewayServiceUtil) {
17
- GatewayServiceUtil.DEFAULT_LOG_LINES = 200;
18
- GatewayServiceUtil.MAX_LOG_LINES = 2000;
19
- /**
20
- * Who may call each method: `anyone` signed in (about their own Ta, their own sign-ins), `admin` (owners too),
21
- * `owner`. A method not here is no method. What an admin may do to another account is checked again by its role
22
- * (GatewayService.manage).
23
- */
24
- GatewayServiceUtil.RULES = {
25
- TaGatewayServer: {
26
- getGateway: 'anyone',
27
- setEnvironment: 'anyone',
28
- restartEnvironment: 'anyone',
29
- listTenants: 'admin',
30
- getConsole: 'admin',
31
- getTenantAgents: 'admin',
32
- getAgentActivity: 'admin',
33
- getTenantLogs: 'admin',
34
- getUsageTrend: 'admin',
35
- getPolicy: 'admin',
36
- getAudit: 'admin',
37
- startTenant: 'admin',
38
- stopTenant: 'admin',
39
- restartTenant: 'admin',
40
- createSignInLink: 'admin',
41
- setTenantLimits: 'admin',
42
- grantMinutes: 'admin',
43
- setSuspended: 'admin',
44
- setDefaultLimits: 'owner',
45
- setRole: 'owner',
46
- },
47
- TaAccessServer: {
48
- getAccess: 'anyone',
49
- signOut: 'anyone',
50
- bindUrl: 'admin',
51
- allow: 'admin',
52
- dismiss: 'admin',
53
- disallow: 'admin',
54
- },
55
- };
56
- /** What an administrator did or looked at of others: kept in the audit. */
57
- GatewayServiceUtil.AUDITED = new Set([
58
- 'startTenant',
59
- 'stopTenant',
60
- 'restartTenant',
61
- 'getTenantLogs',
62
- 'getAgentActivity',
63
- 'createSignInLink',
64
- 'setTenantLimits',
65
- 'grantMinutes',
66
- 'setSuspended',
67
- 'setDefaultLimits',
68
- 'setRole',
69
- 'allow',
70
- 'dismiss',
71
- 'disallow',
72
- ]);
73
- /** By name, own keys only: a method such as `toString` is no rule. */
74
- const BY_SERVICE = new Map(Object.entries(GatewayServiceUtil.RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
75
- function need(service, name) {
76
- var _a;
77
- return (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(name);
78
- }
79
- GatewayServiceUtil.need = need;
80
- function allowed(role, need) {
81
- return need === 'anyone' || GatewayPolicyUtil.rank(role) >= GatewayPolicyUtil.rank(need);
82
- }
83
- GatewayServiceUtil.allowed = allowed;
84
- /** As many lines as asked, within bounds. */
85
- function lines(value) {
86
- const count = Math.floor(Number(value !== null && value !== void 0 ? value : GatewayServiceUtil.DEFAULT_LOG_LINES));
87
- return Number.isFinite(count) && count > 0 ? Math.min(count, GatewayServiceUtil.MAX_LOG_LINES) : GatewayServiceUtil.DEFAULT_LOG_LINES;
88
- }
89
- GatewayServiceUtil.lines = lines;
90
- /** Days of a trend, 1 to 31. */
91
- function days(value) {
92
- const count = Math.floor(Number(value !== null && value !== void 0 ? value : 7));
93
- return Number.isFinite(count) ? Math.min(31, Math.max(1, count)) : 7;
94
- }
95
- GatewayServiceUtil.days = days;
96
- /** The services the gateway answers itself, not the Ta of the tenant. */
97
- GatewayServiceUtil.SERVICES = Object.keys(GatewayServiceUtil.RULES);
98
- function owns(method) {
99
- return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
100
- }
101
- GatewayServiceUtil.owns = owns;
102
- /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
103
- function mayOwn(text) {
104
- return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
105
- }
106
- GatewayServiceUtil.mayOwn = mayOwn;
107
- /** The calls that send the agents a message, which the limit of messages a minute counts. */
108
- GatewayServiceUtil.PROMPTS = ['TaServer/prompt', 'TaServer/steer', 'TaServer/followUp'];
109
- function mayPrompt(text) {
110
- return GatewayServiceUtil.PROMPTS.some(method => text.includes(`"${method}"`));
111
- }
112
- GatewayServiceUtil.mayPrompt = mayPrompt;
113
- /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
114
- function error(id, message, code, hint) {
115
- return JSON.stringify({ jsonrpc: '2.0', id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
116
- }
117
- GatewayServiceUtil.error = error;
118
- function result(id, result) {
119
- return JSON.stringify({ jsonrpc: '2.0', id, result: result !== null && result !== void 0 ? result : null });
120
- }
121
- GatewayServiceUtil.result = result;
122
- function invalid(message, hint) {
123
- return new GatewayCallError(message, TaErrorCode.INVALID_ARGUMENT, hint);
124
- }
125
- function forbidden(message, hint) {
126
- return new GatewayCallError(message, TaErrorCode.FORBIDDEN, hint);
127
- }
128
- GatewayServiceUtil.forbidden = forbidden;
129
- function choice(value, own, providers) {
130
- var _a;
131
- const given = (value !== null && value !== void 0 ? value : {});
132
- if (typeof given.provider === 'string') {
133
- if (!providers.includes(given.provider)) {
134
- throw invalid(`The gateway has no provider ${given.provider} for you.`, 'Pick one of the options of getGateway.');
135
- }
136
- return { provider: given.provider };
137
- }
138
- if (given.type === 'ta') {
139
- if (!own) {
140
- throw invalid('This gateway runs your Ta in its own sandboxes only.', 'Pick one of the options of getGateway.');
141
- }
142
- let url;
143
- try {
144
- url = TaSandboxUtil.url(String((_a = given.url) !== null && _a !== void 0 ? _a : ''));
145
- }
146
- catch (error) {
147
- throw invalid(error.message, 'Write the address its Ta service is reached at, such as https://ta.example.com.');
148
- }
149
- if (typeof given.token !== 'string' || !given.token.trim()) {
150
- throw invalid('A Ta service of your own is reached with its access token.', 'Copy it from ~/.ta/agent/serve.json there, or the link ta serve printed.');
151
- }
152
- return { type: 'ta', url, token: given.token.trim(), name: typeof given.name === 'string' && given.name.trim() ? given.name.trim() : undefined };
153
- }
154
- throw invalid('No environment was named.', 'Pass { provider } or { type: "ta", url, token }.');
155
- }
156
- GatewayServiceUtil.choice = choice;
157
- /** What the audit says of the arguments of a call: its numbers, flags and roles, never more. */
158
- function detail(method, params) {
159
- const [, second] = params;
160
- switch (method) {
161
- case 'setTenantLimits':
162
- case 'setDefaultLimits':
163
- return JSON.stringify(method === 'setDefaultLimits' ? params[0] : second);
164
- case 'grantMinutes':
165
- case 'getTenantLogs':
166
- return String(second !== null && second !== void 0 ? second : '');
167
- case 'setSuspended':
168
- return second ? 'suspended' : 'let go';
169
- case 'setRole':
170
- return String(second);
171
- default:
172
- return undefined;
173
- }
174
- }
175
- GatewayServiceUtil.detail = detail;
176
- })(GatewayServiceUtil || (GatewayServiceUtil = {}));
177
10
  /**
178
11
  * The calls the gateway answers on the WebSocket of a browser: TaGatewayServer, and TaAccessServer, whose
179
12
  * accounts are those that may have a tenant here. Each method needs a role (GatewayServiceUtil.RULES); what an
@@ -181,6 +14,8 @@ export var GatewayServiceUtil;
181
14
  */
182
15
  export class GatewayService {
183
16
  constructor(parts) {
17
+ this.models = parts.models;
18
+ this.egress = parts.egress;
184
19
  this.flow = parts.flow;
185
20
  this.tenants = parts.tenants;
186
21
  this.environments = parts.environments;
@@ -232,6 +67,8 @@ export class GatewayService {
232
67
  setSuspended: () => this.setSuspended(caller, first, second),
233
68
  setDefaultLimits: () => this.setDefaultLimits(first),
234
69
  setRole: () => this.setRole(caller, first, second),
70
+ setEgressHosts: () => this.setEgressHosts(first),
71
+ createAccessToken: () => this.accessToken(caller, first),
235
72
  }
236
73
  : {
237
74
  getAccess: () => this.access(caller),
@@ -306,8 +143,8 @@ export class GatewayService {
306
143
  }
307
144
  /** The tenants with what their sandboxes take and their agents do, read together: a slow one holds the console a second, not a second each. */
308
145
  async console(caller) {
309
- var _a;
310
- var _b, _c;
146
+ var _a, _b;
147
+ var _c, _d, _e;
311
148
  const tenants = this.tenants.list();
312
149
  const [usages, agents] = await Promise.all([
313
150
  Promise.all(tenants.map(tenant => this.environments.usage(tenant).catch(() => undefined))),
@@ -316,9 +153,9 @@ export class GatewayService {
316
153
  const counts = new Map();
317
154
  for (const tenant of tenants) {
318
155
  const { provider } = this.environments.info(tenant);
319
- counts.set(provider, ((_b = counts.get(provider)) !== null && _b !== void 0 ? _b : 0) + 1);
156
+ counts.set(provider, ((_c = counts.get(provider)) !== null && _c !== void 0 ? _c : 0) + 1);
320
157
  }
321
- const fallback = (_c = this.environments.settings.default) !== null && _c !== void 0 ? _c : (_a = this.environments.providers[0]) === null || _a === void 0 ? void 0 : _a.id;
158
+ const fallback = (_d = this.environments.settings.default) !== null && _d !== void 0 ? _d : (_a = this.environments.providers[0]) === null || _a === void 0 ? void 0 : _a.id;
322
159
  return {
323
160
  version: this.meta.version,
324
161
  startedAt: this.meta.startedAt,
@@ -339,10 +176,29 @@ export class GatewayService {
339
176
  requests: this.flow.store.info(caller.token).requests.length,
340
177
  role: caller.role,
341
178
  policy: this.policyView(),
179
+ models: ModelProxyUtil.catalog((_e = (_b = this.models) === null || _b === void 0 ? void 0 : _b.config) !== null && _e !== void 0 ? _e : {}).providers.map(provider => ({
180
+ id: provider.id,
181
+ name: provider.name,
182
+ api: provider.api,
183
+ models: provider.models.length,
184
+ })),
342
185
  };
343
186
  }
344
187
  policyView() {
345
- return this.policy.view(this.environments.settings.idleMinutes);
188
+ var _a;
189
+ return this.policy.view(this.environments.settings.idleMinutes, (_a = this.egress) === null || _a === void 0 ? void 0 : _a.settings);
190
+ }
191
+ setEgressHosts(value) {
192
+ try {
193
+ this.policy.setEgressHosts(EgressProxyUtil.hosts(value));
194
+ }
195
+ catch (error) {
196
+ if (error instanceof EgressConfigError) {
197
+ throw new GatewayCallError(error.message, TaErrorCode.INVALID_ARGUMENT, 'Send hosts such as registry.npmjs.org, *.github.com or example.com:8443.');
198
+ }
199
+ throw error;
200
+ }
201
+ return this.policyView();
346
202
  }
347
203
  tenant(id) {
348
204
  const tenant = typeof id === 'string' ? this.tenants.get(id) : undefined;
@@ -448,6 +304,15 @@ export class GatewayService {
448
304
  this.policy.setRole(id, role);
449
305
  return this.policyView();
450
306
  }
307
+ /** A sign-in of the caller's own account, named for where it is used. */
308
+ accessToken(caller, label) {
309
+ var _a;
310
+ const tenant = (_a = this.tenants.get(caller.tenant.id)) !== null && _a !== void 0 ? _a : caller.tenant;
311
+ const device = typeof label === 'string' && label.trim() ? label.trim().slice(0, 60) : 'Another Ta';
312
+ const token = this.flow.store.signIn({ id: caller.account, name: tenant.name, avatar: tenant.avatar }, device);
313
+ console.log(`ta gateway: ${caller.account} made a token for ${device}`);
314
+ return { token, url: this.meta.publicUrl };
315
+ }
451
316
  access(caller) {
452
317
  const providers = this.flow.providers.map(({ id, name }) => ({ id, name }));
453
318
  const info = this.flow.store.info(caller.token);
@@ -1,7 +1,7 @@
1
1
  import type { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
2
2
  import type { Environments } from './environments.js';
3
3
  import type { GatewayQuotas } from './gateway-quotas.js';
4
- import { type GatewayCaller, type GatewayService } from './gateway-service.js';
4
+ import type { GatewayCaller, GatewayService } from './gateway-service.js';
5
5
  /**
6
6
  * One browser's WebSocket: the calls of the gateway's services answered here, everything else forwarded to
7
7
  * the Ta of its tenant, whose token only the gateway has, and back. Until that Ta answers, what the browser
@@ -1,5 +1,5 @@
1
1
  import { TA_WEBSOCKET_PATH, TaErrorCode } from '@celljs/ta-protocol';
2
- import { GatewayCallError, GatewayServiceUtil } from './gateway-service.js';
2
+ import { GatewayCallError, GatewayServiceUtil } from './gateway-service-util.js';
3
3
  /** What a browser sends before the Ta of its tenant answers, at most: then it is cut off, to connect again. */
4
4
  const MAX_QUEUED = 256;
5
5
  /** And as many bytes at most: a message may be 32 MB, and 256 of them would hold the gateway's memory for one browser. */
@@ -1,11 +1,15 @@
1
1
  export * from './command-sandbox.js';
2
2
  export * from './docker-engine.js';
3
3
  export * from './docker-sandbox.js';
4
+ export * from './egress-proxy.js';
4
5
  export * from './environments.js';
5
6
  export * from './gateway-cli.js';
6
7
  export * from './gateway-server.js';
7
8
  export * from './gateway-service.js';
9
+ export * from './gateway-service-util.js';
8
10
  export * from './gateway-socket.js';
11
+ export * from './kubernetes-api.js';
12
+ export * from './kubernetes-sandbox.js';
9
13
  export * from './local-sandbox.js';
10
14
  export * from './sandbox-provider.js';
11
15
  export * from './sign-in-links.js';
package/esm/node/index.js CHANGED
@@ -1,11 +1,15 @@
1
1
  export * from './command-sandbox.js';
2
2
  export * from './docker-engine.js';
3
3
  export * from './docker-sandbox.js';
4
+ export * from './egress-proxy.js';
4
5
  export * from './environments.js';
5
6
  export * from './gateway-cli.js';
6
7
  export * from './gateway-server.js';
7
8
  export * from './gateway-service.js';
9
+ export * from './gateway-service-util.js';
8
10
  export * from './gateway-socket.js';
11
+ export * from './kubernetes-api.js';
12
+ export * from './kubernetes-sandbox.js';
9
13
  export * from './local-sandbox.js';
10
14
  export * from './sandbox-provider.js';
11
15
  export * from './sign-in-links.js';
@@ -0,0 +1,35 @@
1
+ export declare class KubernetesError extends Error {
2
+ readonly status: number;
3
+ constructor(status: number, message: string);
4
+ }
5
+ /** The calls a provider makes: a JSON body or text back; KubernetesError for an answer not 2xx. */
6
+ export interface KubernetesCalls {
7
+ call<T = unknown>(method: string, path: string, body?: unknown, type?: string): Promise<T>;
8
+ text(path: string): Promise<string>;
9
+ }
10
+ export interface KubernetesApiOptions {
11
+ /** https://<host>:<port> of the API server. */
12
+ server: string;
13
+ token?: string;
14
+ /** The CA of the server, PEM. */
15
+ ca?: string;
16
+ }
17
+ /**
18
+ * The API of a cluster over HTTPS, without a client library: what a provider of sandboxes needs is a few
19
+ * resources by their paths.
20
+ */
21
+ export declare class KubernetesApi implements KubernetesCalls {
22
+ protected readonly options: KubernetesApiOptions;
23
+ /** In a pod of the cluster: its service account, and the namespace it runs in. */
24
+ static inCluster(env?: Record<string, string | undefined>, read?: (path: string) => string): KubernetesApiOptions & {
25
+ namespace: string;
26
+ };
27
+ constructor(options: KubernetesApiOptions);
28
+ call<T = unknown>(method: string, path: string, body?: unknown, type?: string): Promise<T>;
29
+ text(path: string): Promise<string>;
30
+ protected send(method: string, path: string, body?: string, type?: string): Promise<string>;
31
+ }
32
+ export declare namespace KubernetesUtil {
33
+ /** A quantity of Kubernetes (`4Gi`, `500M`, `250m`, `123456n`) as a number of its unit (bytes, CPUs). */
34
+ function quantity(value: string | number | undefined): number | undefined;
35
+ }