@celljs/ta-host 4.12.0 → 4.13.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 (129) hide show
  1. package/README.en.md +3 -2
  2. package/README.md +3 -2
  3. package/esm/node/extension-data-notifier.d.ts +3 -0
  4. package/esm/node/extension-data-notifier.js +8 -2
  5. package/esm/node/extension-providers.js +1 -1
  6. package/esm/node/host-protocol.d.ts +20 -1
  7. package/esm/node/host-protocol.js +1 -0
  8. package/esm/node/index.d.ts +3 -0
  9. package/esm/node/index.js +3 -0
  10. package/esm/node/keep-awake.d.ts +44 -0
  11. package/esm/node/keep-awake.js +112 -0
  12. package/esm/node/package-host-extension.js +2 -0
  13. package/esm/node/package-hosts.d.ts +23 -1
  14. package/esm/node/package-hosts.js +61 -3
  15. package/esm/node/relay-bridge.d.ts +37 -3
  16. package/esm/node/relay-bridge.js +271 -10
  17. package/esm/node/rooms-bridge.d.ts +13 -2
  18. package/esm/node/rooms-bridge.js +34 -4
  19. package/esm/node/session-catalog.d.ts +1 -1
  20. package/esm/node/session-catalog.js +5 -0
  21. package/esm/node/session-list-feed.d.ts +11 -1
  22. package/esm/node/session-list-feed.js +17 -3
  23. package/esm/node/session-resume.d.ts +52 -0
  24. package/esm/node/session-resume.js +174 -0
  25. package/esm/node/settings-store.d.ts +3 -0
  26. package/esm/node/settings-store.js +22 -0
  27. package/esm/node/ta-errors.d.ts +1 -0
  28. package/esm/node/ta-errors.js +4 -0
  29. package/esm/node/ta-server-impl.d.ts +9 -0
  30. package/esm/node/ta-server-impl.js +52 -7
  31. package/esm/node/ta-session-factory.js +5 -2
  32. package/esm/node/ta-session-registry.d.ts +12 -1
  33. package/esm/node/ta-session-registry.js +30 -1
  34. package/esm/node/ta-session.d.ts +3 -0
  35. package/esm/node/ta-session.js +16 -0
  36. package/esm/node/team/authority.d.ts +19 -0
  37. package/esm/node/team/authority.js +79 -0
  38. package/esm/node/team/call-util.d.ts +23 -0
  39. package/esm/node/team/call-util.js +88 -0
  40. package/esm/node/team/connections.d.ts +15 -0
  41. package/esm/node/team/connections.js +81 -0
  42. package/esm/node/team/host-team.d.ts +13 -0
  43. package/esm/node/team/host-team.js +69 -0
  44. package/esm/node/team/index.d.ts +13 -0
  45. package/esm/node/team/index.js +14 -0
  46. package/esm/node/team/member-devices.d.ts +11 -0
  47. package/esm/node/team/member-devices.js +56 -0
  48. package/esm/node/team/package-permissions.d.ts +23 -0
  49. package/esm/node/team/package-permissions.js +75 -0
  50. package/esm/node/team/principals.d.ts +22 -0
  51. package/esm/node/team/principals.js +72 -0
  52. package/esm/node/team/run-actors.d.ts +21 -0
  53. package/esm/node/team/run-actors.js +100 -0
  54. package/esm/node/team/shared-data.d.ts +18 -0
  55. package/esm/node/team/shared-data.js +55 -0
  56. package/esm/node/team/team-calls.d.ts +33 -0
  57. package/esm/node/team/team-calls.js +266 -0
  58. package/esm/node/team/team-gate.d.ts +31 -0
  59. package/esm/node/team/team-gate.js +177 -0
  60. package/esm/node/team/team-protocol.d.ts +89 -0
  61. package/esm/node/team/team-protocol.js +27 -0
  62. package/esm/node/team/visibility.d.ts +13 -0
  63. package/esm/node/team/visibility.js +50 -0
  64. package/esm/node/terminal-service.d.ts +5 -0
  65. package/esm/node/terminal-service.js +11 -2
  66. package/lib/node/extension-data-notifier.d.ts +3 -0
  67. package/lib/node/extension-data-notifier.js +8 -2
  68. package/lib/node/extension-providers.js +1 -1
  69. package/lib/node/host-protocol.d.ts +20 -1
  70. package/lib/node/host-protocol.js +2 -1
  71. package/lib/node/index.d.ts +3 -0
  72. package/lib/node/index.js +3 -0
  73. package/lib/node/keep-awake.d.ts +45 -0
  74. package/lib/node/keep-awake.js +115 -0
  75. package/lib/node/package-host-extension.js +2 -0
  76. package/lib/node/package-hosts.d.ts +23 -1
  77. package/lib/node/package-hosts.js +60 -2
  78. package/lib/node/relay-bridge.d.ts +37 -3
  79. package/lib/node/relay-bridge.js +269 -8
  80. package/lib/node/rooms-bridge.d.ts +13 -2
  81. package/lib/node/rooms-bridge.js +34 -4
  82. package/lib/node/session-catalog.d.ts +1 -1
  83. package/lib/node/session-catalog.js +5 -0
  84. package/lib/node/session-list-feed.d.ts +11 -1
  85. package/lib/node/session-list-feed.js +17 -3
  86. package/lib/node/session-resume.d.ts +53 -0
  87. package/lib/node/session-resume.js +177 -0
  88. package/lib/node/settings-store.d.ts +3 -0
  89. package/lib/node/settings-store.js +22 -0
  90. package/lib/node/ta-errors.d.ts +1 -0
  91. package/lib/node/ta-errors.js +4 -0
  92. package/lib/node/ta-server-impl.d.ts +9 -0
  93. package/lib/node/ta-server-impl.js +52 -7
  94. package/lib/node/ta-session-factory.js +4 -1
  95. package/lib/node/ta-session-registry.d.ts +12 -1
  96. package/lib/node/ta-session-registry.js +30 -1
  97. package/lib/node/ta-session.d.ts +3 -0
  98. package/lib/node/ta-session.js +16 -0
  99. package/lib/node/team/authority.d.ts +20 -0
  100. package/lib/node/team/authority.js +82 -0
  101. package/lib/node/team/call-util.d.ts +24 -0
  102. package/lib/node/team/call-util.js +91 -0
  103. package/lib/node/team/connections.d.ts +16 -0
  104. package/lib/node/team/connections.js +84 -0
  105. package/lib/node/team/host-team.d.ts +14 -0
  106. package/lib/node/team/host-team.js +72 -0
  107. package/lib/node/team/index.d.ts +14 -0
  108. package/lib/node/team/index.js +30 -0
  109. package/lib/node/team/member-devices.d.ts +12 -0
  110. package/lib/node/team/member-devices.js +59 -0
  111. package/lib/node/team/package-permissions.d.ts +24 -0
  112. package/lib/node/team/package-permissions.js +78 -0
  113. package/lib/node/team/principals.d.ts +23 -0
  114. package/lib/node/team/principals.js +75 -0
  115. package/lib/node/team/run-actors.d.ts +22 -0
  116. package/lib/node/team/run-actors.js +103 -0
  117. package/lib/node/team/shared-data.d.ts +19 -0
  118. package/lib/node/team/shared-data.js +58 -0
  119. package/lib/node/team/team-calls.d.ts +34 -0
  120. package/lib/node/team/team-calls.js +270 -0
  121. package/lib/node/team/team-gate.d.ts +32 -0
  122. package/lib/node/team/team-gate.js +180 -0
  123. package/lib/node/team/team-protocol.d.ts +90 -0
  124. package/lib/node/team/team-protocol.js +30 -0
  125. package/lib/node/team/visibility.d.ts +14 -0
  126. package/lib/node/team/visibility.js +53 -0
  127. package/lib/node/terminal-service.d.ts +5 -0
  128. package/lib/node/terminal-service.js +11 -2
  129. package/package.json +14 -10
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaCallPermissions = void 0;
4
+ exports.plainPath = plainPath;
5
+ const node_path_1 = require("node:path");
6
+ const anyone = () => 'anyone';
7
+ const fixed = (action) => () => ({ action });
8
+ /** The conversation of the session of the first parameter. */
9
+ const session = (action) => (params, lookup) => {
10
+ var _a, _b;
11
+ const id = String((_a = params[0]) !== null && _a !== void 0 ? _a : '');
12
+ const found = lookup.session(id);
13
+ return { action, resource: { kind: 'session', id: (_b = found === null || found === void 0 ? void 0 : found.sessionFile) !== null && _b !== void 0 ? _b : `session:${id}`, ...(found ? { cwd: found.cwd } : {}) } };
14
+ };
15
+ const text = (value) => (typeof value === 'string' && value ? value : undefined);
16
+ const cwdOf = (value) => text(value === null || value === void 0 ? void 0 : value.cwd);
17
+ /**
18
+ * A path made plain: `..` and `.` gone, the same for a path of this machine and an `ssh://host/path` location, so
19
+ * that `/shared/../etc` is `/etc` before anything compares it with a folder.
20
+ */
21
+ function plainPath(path) {
22
+ var _a;
23
+ const [, scheme = '', rest = ''] = (_a = /^([a-z][a-z0-9+.-]*:\/\/[^/]*)?(.*)$/i.exec(path)) !== null && _a !== void 0 ? _a : [];
24
+ const parts = [];
25
+ for (const part of rest.split('/')) {
26
+ if (part === '..') {
27
+ parts.pop();
28
+ }
29
+ else if (part && part !== '.') {
30
+ parts.push(part);
31
+ }
32
+ }
33
+ return `${scheme}/${parts.join('/')}`;
34
+ }
35
+ const located = (value) => (0, node_path_1.isAbsolute)(value) || /^[a-z][a-z0-9+.-]*:\/\//i.test(value);
36
+ /** A path of the parameters, against the directory of another (or of the server). */
37
+ const path = (action, pathAt, cwd, alsoBase = false) => async (params, lookup) => {
38
+ var _a;
39
+ const base = plainPath((_a = cwd(params)) !== null && _a !== void 0 ? _a : (await lookup.defaultCwd()));
40
+ const given = pathAt === undefined ? undefined : text(params[pathAt]);
41
+ // Joined as text: path.join would mangle the // of an ssh:// location.
42
+ const at = plainPath(given === undefined ? base : located(given) ? given : `${base}/${given}`);
43
+ const one = { action, resource: { kind: 'dir', id: at } };
44
+ // A URL of a file opens the directory it is in (WorkspaceFiles serves the base): that too must be theirs.
45
+ return alsoBase && at !== base ? [one, { action, resource: { kind: 'dir', id: base } }] : one;
46
+ };
47
+ /** A conversation is opened in a directory: one let in only starts one where they may read (its AGENTS.md is read in). */
48
+ const inDirectory = (action, cwd) => async (params, lookup) => {
49
+ var _a;
50
+ const at = plainPath((_a = cwd(params)) !== null && _a !== void 0 ? _a : (await lookup.defaultCwd()));
51
+ return [{ action }, { action: 'files.read', resource: { kind: 'dir', id: at } }];
52
+ };
53
+ /**
54
+ * The action each call of the services of ta serve needs of one let in (docs/design/ta-team.md 4). A call of no rule
55
+ * is refused: a service or a method added later is for the owner alone until it is given one here (the spec checks
56
+ * that each method of the protocol has one).
57
+ */
58
+ const RULES = {
59
+ TaServer: {
60
+ getServerInfo: anyone,
61
+ setWorkingDirectory: fixed('settings.manage'),
62
+ openSession: (params, lookup) => {
63
+ var _a;
64
+ const options = ((_a = params[0]) !== null && _a !== void 0 ? _a : {});
65
+ const file = text(options.sessionFile);
66
+ if (file) {
67
+ return { action: 'chat.view', resource: { kind: 'session', id: file, ...(text(options.cwd) ? { cwd: text(options.cwd) } : {}) } };
68
+ }
69
+ return inDirectory('chat.create', () => text(options.cwd))(params, lookup);
70
+ },
71
+ closeSession: session('chat.manage'),
72
+ listSessions: anyone,
73
+ watchSessions: anyone,
74
+ listSavedSessions: anyone,
75
+ attach: session('chat.view'),
76
+ getEarlierMessages: session('chat.view'),
77
+ getMessageImage: session('chat.view'),
78
+ detach: anyone,
79
+ respondUi: session('chat.send'),
80
+ claimClientTool: session('chat.send'),
81
+ respondClientTool: session('chat.send'),
82
+ prompt: session('chat.send'),
83
+ steer: session('chat.send'),
84
+ followUp: session('chat.send'),
85
+ abort: session('chat.stop'),
86
+ clearQueue: session('chat.send'),
87
+ removeQueued: session('chat.send'),
88
+ newSession: session('chat.create'),
89
+ getState: session('chat.view'),
90
+ setModel: session('chat.send'),
91
+ cycleModel: session('chat.send'),
92
+ getAvailableModels: session('chat.view'),
93
+ setThinkingLevel: session('chat.send'),
94
+ cycleThinkingLevel: session('chat.send'),
95
+ getAvailableThinkingLevels: session('chat.view'),
96
+ setSteeringMode: session('chat.send'),
97
+ setFollowUpMode: session('chat.send'),
98
+ compact: session('chat.send'),
99
+ setAutoCompaction: session('chat.send'),
100
+ setAutoRetry: session('chat.send'),
101
+ abortRetry: session('chat.stop'),
102
+ bash: session('commands.run'),
103
+ abortBash: session('chat.stop'),
104
+ getSessionStats: session('chat.view'),
105
+ exportHtml: session('chat.share'),
106
+ switchSession: session('chat.manage'),
107
+ fork: session('chat.create'),
108
+ forkToFile: session('chat.create'),
109
+ clone: session('chat.create'),
110
+ getForkMessages: session('chat.view'),
111
+ getEntries: session('chat.view'),
112
+ getTree: session('chat.view'),
113
+ navigateTree: session('chat.send'),
114
+ getLastAssistantText: session('chat.view'),
115
+ setSessionName: session('chat.manage'),
116
+ getMessages: session('chat.view'),
117
+ getCommands: session('chat.view'),
118
+ getDirectoryCommands: path('files.read', undefined, params => text(params[0])),
119
+ reload: fixed('settings.manage'),
120
+ },
121
+ TaPackageServer: {
122
+ listWebExtensions: anyone,
123
+ readWebExtension: anyone,
124
+ setPackageEnabled: fixed('extensions.install'),
125
+ listPackages: anyone,
126
+ installPackage: fixed('extensions.install'),
127
+ removePackage: fixed('extensions.install'),
128
+ // What the host of a package needs is the package's to say (pi.permissions), on the agent or channel a request names.
129
+ callPackageHost: (params, lookup) => {
130
+ var _a, _b;
131
+ const first = Array.isArray(params[2]) ? params[2][0] : undefined;
132
+ const kind = typeof (first === null || first === void 0 ? void 0 : first.kind) === 'string' ? first.kind : undefined;
133
+ const action = lookup.packageAction(String((_a = params[0]) !== null && _a !== void 0 ? _a : ''), String((_b = params[1]) !== null && _b !== void 0 ? _b : ''), kind);
134
+ if (action === 'anyone' || action === 'invite') {
135
+ return action;
136
+ }
137
+ // A request about a channel (one of its members too) is the channel's.
138
+ const target = typeof (first === null || first === void 0 ? void 0 : first.channel) === 'string' && first.channel
139
+ ? { kind: 'channel', id: first.channel }
140
+ : typeof (first === null || first === void 0 ? void 0 : first.agent) === 'string' && first.agent
141
+ ? { kind: 'agent', id: first.agent }
142
+ : undefined;
143
+ return { action, ...(target ? { resource: target } : {}) };
144
+ },
145
+ // Kept apart for each one let in (MemberDataUtil.name): a page of theirs keeps its own state.
146
+ getExtensionData: anyone,
147
+ setExtensionData: anyone,
148
+ openTemporaryPackage: fixed('extensions.install'),
149
+ updateTemporaryPackage: fixed('extensions.install'),
150
+ installTemporaryPackage: fixed('extensions.install'),
151
+ getTemporaryRetention: anyone,
152
+ setTemporaryRetention: fixed('extensions.install'),
153
+ },
154
+ TaWorkspaceServer: {
155
+ listDirectory: path('files.read', 0, params => text(params[1])),
156
+ readFile: path('files.read', 0, params => text(params[1])),
157
+ writeFile: path('files.write', 0, params => text(params[2])),
158
+ uploadFile: path('files.write', 0, params => cwdOf(params[2])),
159
+ fileUrl: path('files.read', 0, params => text(params[1]), true),
160
+ findFiles: path('files.read', undefined, params => text(params[1])),
161
+ runCommand: path('commands.run', undefined, params => cwdOf(params[2])),
162
+ openTerminal: path('commands.terminal', undefined, params => cwdOf(params[0])),
163
+ writeTerminal: fixed('commands.terminal'),
164
+ resizeTerminal: fixed('commands.terminal'),
165
+ closeTerminal: fixed('commands.terminal'),
166
+ listTerminals: fixed('commands.terminal'),
167
+ },
168
+ TaTerminalServer: {
169
+ openTerminal: path('commands.terminal', undefined, params => cwdOf(params[0])),
170
+ writeTerminal: fixed('commands.terminal'),
171
+ resizeTerminal: fixed('commands.terminal'),
172
+ closeTerminal: fixed('commands.terminal'),
173
+ listTerminals: fixed('commands.terminal'),
174
+ },
175
+ TaRelayServer: {
176
+ getRelay: fixed('settings.view'),
177
+ pair: fixed('settings.manage'),
178
+ unpair: fixed('settings.manage'),
179
+ setKeepAwake: fixed('settings.manage'),
180
+ listRelays: fixed('settings.view'),
181
+ addRelay: fixed('settings.manage'),
182
+ addFolder: fixed('settings.manage'),
183
+ addStore: fixed('settings.manage'),
184
+ removeRelay: fixed('settings.manage'),
185
+ chooseRelay: fixed('settings.manage'),
186
+ probeRelay: fixed('settings.view'),
187
+ sharePackage: fixed('extensions.install'),
188
+ unsharePackage: fixed('extensions.install'),
189
+ create: fixed('chat.share'),
190
+ list: fixed('chat.share'),
191
+ remove: fixed('chat.share'),
192
+ open: fixed('chat.share'),
193
+ },
194
+ // Rooms are the user's own with other Ta: their identity and friends.
195
+ TaRoomServer: Object.fromEntries([
196
+ 'getIdentity',
197
+ 'setName',
198
+ 'listRooms',
199
+ 'createRoom',
200
+ 'invite',
201
+ 'join',
202
+ 'listRequests',
203
+ 'answer',
204
+ 'removeMember',
205
+ 'leave',
206
+ 'send',
207
+ 'getState',
208
+ 'setState',
209
+ 'listFriends',
210
+ 'friendLink',
211
+ 'addFriend',
212
+ 'offerFriend',
213
+ 'listFriendRequests',
214
+ 'answerFriend',
215
+ 'removeFriend',
216
+ 'friendRoom',
217
+ 'watch',
218
+ 'unwatch',
219
+ ].map(method => [method, fixed('settings.manage')])),
220
+ TaSettingsServer: {
221
+ getSettings: fixed('settings.view'),
222
+ updateSettings: fixed('settings.manage'),
223
+ setLanguage: fixed('settings.manage'),
224
+ },
225
+ TaAuthServer: {
226
+ listProviders: fixed('models.use'),
227
+ login: fixed('models.manage'),
228
+ answer: fixed('models.manage'),
229
+ cancel: fixed('models.manage'),
230
+ logout: fixed('models.manage'),
231
+ },
232
+ TaAccessServer: {
233
+ getAccess: fixed('members.view'),
234
+ bindUrl: fixed('members.roles'),
235
+ allow: fixed('members.roles'),
236
+ dismiss: fixed('members.roles'),
237
+ disallow: fixed('members.roles'),
238
+ signOut: fixed('members.roles'),
239
+ },
240
+ TaRemoteServer: {
241
+ listHosts: fixed('hosts.use'),
242
+ addHost: fixed('settings.manage'),
243
+ removeHost: fixed('settings.manage'),
244
+ resolveLocation: fixed('hosts.use'),
245
+ forwardPort: fixed('hosts.use'),
246
+ listForwardedPorts: fixed('hosts.use'),
247
+ closeForwardedPort: fixed('hosts.use'),
248
+ watch: fixed('hosts.use'),
249
+ answer: fixed('hosts.use'),
250
+ disconnect: fixed('hosts.use'),
251
+ },
252
+ };
253
+ /** By name, own keys only: a method such as `toString` is no rule. */
254
+ const BY_SERVICE = new Map(Object.entries(RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
255
+ var TaCallPermissions;
256
+ (function (TaCallPermissions) {
257
+ /** What the call needs; undefined for a call of no rule, which only the owner makes. */
258
+ async function need(service, method, params, lookup) {
259
+ var _a;
260
+ const rule = (_a = BY_SERVICE.get(service)) === null || _a === void 0 ? void 0 : _a.get(method);
261
+ return rule ? rule(Array.isArray(params) ? params : [], lookup) : undefined;
262
+ }
263
+ TaCallPermissions.need = need;
264
+ /** The methods with a rule, by service: for the spec. */
265
+ function methods() {
266
+ return Object.fromEntries(Object.entries(RULES).map(([service, rules]) => [service, Object.keys(rules)]));
267
+ }
268
+ TaCallPermissions.methods = methods;
269
+ })(TaCallPermissions || (exports.TaCallPermissions = TaCallPermissions = {}));
270
+ //# sourceMappingURL=team-calls.js.map
@@ -0,0 +1,32 @@
1
+ import { JsonRpc } from '@celljs/rpc';
2
+ import { ExecEnvironmentFactory } from '@celljs/ta-exec';
3
+ import { type TaDenial } from '@celljs/ta-protocol';
4
+ import { type Context, Middleware } from '@celljs/web/lib/node';
5
+ import { ExtensionDataStore } from '../extension-data';
6
+ import { ExtensionDataNotifier } from '../extension-data-notifier';
7
+ import { TaSessionRegistry } from '../host-protocol';
8
+ import { type TaCall } from './call-util';
9
+ import { TaAuthority, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry } from './team-protocol';
10
+ /**
11
+ * The one place where ta serve checks a call of one let in (docs/design/ta-team.md 4): what the call needs (TaCallPermissions,
12
+ * what packages declare), whether they may (TaAuthority), and a line of the record either way. The owner's calls go
13
+ * through untouched. Data of packages a page of theirs reads or writes is theirs (MemberDataUtil) or the Ta's, as shared.
14
+ */
15
+ export declare class TaTeamGate implements Middleware {
16
+ /** Right after TaAccess, which lets the connection in. */
17
+ readonly priority: number;
18
+ protected readonly principals: TaPrincipals;
19
+ protected readonly authority: TaAuthority;
20
+ protected readonly runs: TaRunActors;
21
+ protected readonly permissions: TaPackagePermissions;
22
+ protected readonly shared: TaSharedDataRegistry;
23
+ protected readonly sessions: TaSessionRegistry;
24
+ protected readonly environments: ExecEnvironmentFactory;
25
+ protected readonly dataStore: ExtensionDataStore;
26
+ protected readonly dataNotifier: ExtensionDataNotifier;
27
+ handle(ctx: Context, next: () => Promise<void>): Promise<void>;
28
+ /** A page of one let in reads data of the Ta's: the owner's value, as they may see it; never written by them. */
29
+ protected answerShared(ctx: Context, call: TaCall, principal: string): Promise<boolean>;
30
+ protected refuse(ctx: Context, id: JsonRpc.Id | undefined, denial: TaDenial): void;
31
+ }
32
+ //# sourceMappingURL=team-gate.d.ts.map
@@ -0,0 +1,180 @@
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.TaTeamGate = 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_exec_1 = require("@celljs/ta-exec");
17
+ const ta_protocol_1 = require("@celljs/ta-protocol");
18
+ const node_2 = require("@celljs/web/lib/node");
19
+ const extension_data_1 = require("../extension-data");
20
+ const extension_data_notifier_1 = require("../extension-data-notifier");
21
+ const host_protocol_1 = require("../host-protocol");
22
+ const ta_access_1 = require("../ta-access");
23
+ const call_util_1 = require("./call-util");
24
+ const shared_data_1 = require("./shared-data");
25
+ const team_calls_1 = require("./team-calls");
26
+ const team_protocol_1 = require("./team-protocol");
27
+ /**
28
+ * The one place where ta serve checks a call of one let in (docs/design/ta-team.md 4): what the call needs (TaCallPermissions,
29
+ * what packages declare), whether they may (TaAuthority), and a line of the record either way. The owner's calls go
30
+ * through untouched. Data of packages a page of theirs reads or writes is theirs (MemberDataUtil) or the Ta's, as shared.
31
+ */
32
+ let TaTeamGate = class TaTeamGate {
33
+ constructor() {
34
+ /** Right after TaAccess, which lets the connection in. */
35
+ this.priority = ta_access_1.ACCESS_MIDDLEWARE_PRIORITY - 1;
36
+ }
37
+ async handle(ctx, next) {
38
+ var _a, _b, _c;
39
+ var _d;
40
+ const connection = node_1.RpcContext.connection();
41
+ const principal = connection ? this.principals.of(connection) : ta_protocol_1.TA_OWNER;
42
+ if (principal === ta_protocol_1.TA_OWNER) {
43
+ await next();
44
+ return;
45
+ }
46
+ const request = ctx.request;
47
+ const call = call_util_1.TaCallUtil.of(request.url, request.body);
48
+ if (!call) {
49
+ this.refuse(ctx, undefined, { action: 'call', askable: false });
50
+ return;
51
+ }
52
+ const name = `${call.service}.${call.method}`;
53
+ const summary = call_util_1.TaCallUtil.summary(call.method, call.params);
54
+ const refused = (denial, one) => {
55
+ var _a;
56
+ this.refuse(ctx, call.id, denial);
57
+ this.authority.record({
58
+ principal,
59
+ kind: 'call',
60
+ name,
61
+ action: denial.action,
62
+ resource: (_a = denial.resource) !== null && _a !== void 0 ? _a : one === null || one === void 0 ? void 0 : one.resource,
63
+ allowed: false,
64
+ summary,
65
+ denial,
66
+ });
67
+ };
68
+ const need = await team_calls_1.TaCallPermissions.need(call.service, call.method, call.params, {
69
+ session: id => this.sessions.locate(id),
70
+ defaultCwd: () => this.environments.defaultCwd(),
71
+ packageAction: (pkg, method, kind) => this.permissions.host(pkg, method, kind),
72
+ });
73
+ // A call of no rule is the owner's; one holding a link of invitation only asks to come in.
74
+ if (!need || (principal.startsWith(ta_protocol_1.TA_INVITE_PREFIX) && need !== 'invite')) {
75
+ refused({ action: name, askable: false });
76
+ return;
77
+ }
78
+ const needs = need === 'anyone' || need === 'invite' ? [] : Array.isArray(need) ? need : [need];
79
+ for (const one of needs) {
80
+ const answer = this.authority.check(principal, one.action, one.resource);
81
+ if (answer !== true) {
82
+ refused(answer, one);
83
+ return;
84
+ }
85
+ }
86
+ // A question of a run is answered by who asked the run: one let in does not say yes to what a run of others asks.
87
+ if (call_util_1.TaCallUtil.ANSWERS.has(name)) {
88
+ const actors = this.runs.actorsOfSession(String((_d = call.params[0]) !== null && _d !== void 0 ? _d : ''));
89
+ if (!(actors === null || actors === void 0 ? void 0 : actors.has(principal)) || [...actors].some(actor => actor !== principal)) {
90
+ refused({ action: 'chat.manage', ...(((_a = needs[0]) === null || _a === void 0 ? void 0 : _a.resource) ? { resource: needs[0].resource } : {}), askable: false });
91
+ return;
92
+ }
93
+ }
94
+ if (!call_util_1.TaCallUtil.QUIET.has(name)) {
95
+ this.authority.record({ principal, kind: 'call', name, action: (_b = needs[0]) === null || _b === void 0 ? void 0 : _b.action, resource: (_c = needs[0]) === null || _c === void 0 ? void 0 : _c.resource, allowed: true, summary });
96
+ }
97
+ if (call.service === 'TaPackageServer' && (call.method === 'getExtensionData' || call.method === 'setExtensionData')) {
98
+ if (await this.answerShared(ctx, call, principal)) {
99
+ return;
100
+ }
101
+ // A page of one let in keeps its own state, apart: in data of its own (its own 4 MB), never the user's.
102
+ request.body.params = call.params.map((param, index) => (index === 0 ? shared_data_1.MemberDataUtil.name(principal, param) : param));
103
+ }
104
+ await next();
105
+ }
106
+ /** A page of one let in reads data of the Ta's: the owner's value, as they may see it; never written by them. */
107
+ async answerShared(ctx, call, principal) {
108
+ var _a;
109
+ const [name, key, cwdOf] = call.params;
110
+ const data = this.shared.of(String(name), key);
111
+ if (!data) {
112
+ return false;
113
+ }
114
+ if (call.method === 'setExtensionData') {
115
+ this.refuse(ctx, call.id, { action: 'settings.manage', askable: false });
116
+ return true;
117
+ }
118
+ const cwd = typeof cwdOf === 'string' ? cwdOf : undefined;
119
+ const connection = node_1.RpcContext.connection();
120
+ if (connection) {
121
+ this.dataNotifier.watch(connection, String(name), cwd);
122
+ }
123
+ let value;
124
+ try {
125
+ value = data.view(principal, String(key), await this.dataStore.get(String(name), String(key), cwd));
126
+ }
127
+ catch (error) {
128
+ console.error(`The view of ${String(name)} failed: ${error instanceof Error ? error.message : error}`);
129
+ value = undefined;
130
+ }
131
+ ctx.response.statusCode = 200;
132
+ ctx.response.end(JSON.stringify(rpc_1.JsonRpc.result((_a = call.id) !== null && _a !== void 0 ? _a : null, value !== null && value !== void 0 ? value : null)));
133
+ return true;
134
+ }
135
+ refuse(ctx, id, denial) {
136
+ ctx.response.statusCode = 200;
137
+ ctx.response.end(JSON.stringify(call_util_1.TaCallUtil.forbidden(id, denial)));
138
+ }
139
+ };
140
+ exports.TaTeamGate = TaTeamGate;
141
+ __decorate([
142
+ (0, core_1.Autowired)(team_protocol_1.TaPrincipals),
143
+ __metadata("design:type", Object)
144
+ ], TaTeamGate.prototype, "principals", void 0);
145
+ __decorate([
146
+ (0, core_1.Autowired)(team_protocol_1.TaAuthority),
147
+ __metadata("design:type", Object)
148
+ ], TaTeamGate.prototype, "authority", void 0);
149
+ __decorate([
150
+ (0, core_1.Autowired)(team_protocol_1.TaRunActors),
151
+ __metadata("design:type", Object)
152
+ ], TaTeamGate.prototype, "runs", void 0);
153
+ __decorate([
154
+ (0, core_1.Autowired)(team_protocol_1.TaPackagePermissions),
155
+ __metadata("design:type", Object)
156
+ ], TaTeamGate.prototype, "permissions", void 0);
157
+ __decorate([
158
+ (0, core_1.Autowired)(team_protocol_1.TaSharedDataRegistry),
159
+ __metadata("design:type", Object)
160
+ ], TaTeamGate.prototype, "shared", void 0);
161
+ __decorate([
162
+ (0, core_1.Autowired)({ id: host_protocol_1.TaSessionRegistry, detached: true }),
163
+ __metadata("design:type", Object)
164
+ ], TaTeamGate.prototype, "sessions", void 0);
165
+ __decorate([
166
+ (0, core_1.Autowired)(ta_exec_1.ExecEnvironmentFactory),
167
+ __metadata("design:type", Object)
168
+ ], TaTeamGate.prototype, "environments", void 0);
169
+ __decorate([
170
+ (0, core_1.Autowired)(extension_data_1.ExtensionDataStore),
171
+ __metadata("design:type", Object)
172
+ ], TaTeamGate.prototype, "dataStore", void 0);
173
+ __decorate([
174
+ (0, core_1.Autowired)({ id: extension_data_notifier_1.ExtensionDataNotifier, detached: true }),
175
+ __metadata("design:type", Object)
176
+ ], TaTeamGate.prototype, "dataNotifier", void 0);
177
+ exports.TaTeamGate = TaTeamGate = __decorate([
178
+ (0, core_1.Component)(node_2.Middleware)
179
+ ], TaTeamGate);
180
+ //# sourceMappingURL=team-gate.js.map
@@ -0,0 +1,90 @@
1
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
2
+ import type { TaAuditEvent, TaAuthorizer, TaDenial, TaHostTeam, TaMemberInfo, TaResource, TaSessionInfo, TaSharedData, TaSharedRef, TaTeamDevice, TaTeamPairing } from '@celljs/ta-protocol';
3
+ /**
4
+ * The parts by which ta serve lets people in besides its user (docs/design/ta-team.md 4, 14). Each has one concern, and
5
+ * the rest of ta serve depends on these interfaces only: the sessions, the data of packages and the terminals ask
6
+ * TaVisibility and TaPrincipals what they need, nothing reaches into them.
7
+ */
8
+ /** Who makes a call: TA_OWNER, a principal let in (`m:<id>`), one holding an invitation (`invite:<id>`), or no one (`device:<id>`). */
9
+ export declare const TaPrincipals: unique symbol;
10
+ export interface TaPrincipals {
11
+ of(connection: RpcConnection): string;
12
+ /** The principal of the call being served, or the one a package acts as; TA_OWNER outside of both. */
13
+ current(): string;
14
+ actAs<T>(principal: string, run: () => Promise<T>): Promise<T>;
15
+ /** Whether a package acts as someone now (TaHostTeam.actAs). */
16
+ acting(): boolean;
17
+ }
18
+ /** The authorizer a package gave (TaHostTeam.authorize), behind which every answer about one let in is asked. */
19
+ export declare const TaAuthority: unique symbol;
20
+ export interface TaAuthority {
21
+ authorize(authorizer: TaAuthorizer): () => void;
22
+ /** The owner may do anything; another what the authorizer says; anyone but the owner nothing without one. */
23
+ check(principal: string, action: string, resource?: TaResource): true | TaDenial;
24
+ describe(principal: string): TaMemberInfo | undefined;
25
+ record(event: TaAuditEvent): void;
26
+ created(principal: string, resource: TaResource): void;
27
+ admit(principal: string, theirs: number, everyone: number): boolean;
28
+ mayRun(principal: string, running: number): boolean;
29
+ /** The agent or channel a conversation is of (TaHostTeam.link). */
30
+ link(sessionFile: string, ref: TaSharedRef | undefined): void;
31
+ }
32
+ /** Who asked the run going on in each session: its tools do what all of them may. */
33
+ export declare const TaRunActors: unique symbol;
34
+ export interface TaRunActors {
35
+ /** A message of the principal of this call goes to a session: within the owner's limit of runs, it is one who asks the run. */
36
+ prompted(sessionId: string): void;
37
+ /** The principal of this call made a conversation, by a call of its own: its author. */
38
+ created(sessionFile: string | undefined, cwd: string): void;
39
+ actorsOf(sessionFile: string): string[];
40
+ /** By the id of the session. */
41
+ actorsOfSession(sessionId: string): ReadonlySet<string> | undefined;
42
+ /** Stops the runs a principal asked. */
43
+ stopRunsOf(principal: string): void;
44
+ }
45
+ /** Data of packages that is the Ta's (TaHostTeam.shareData). */
46
+ export declare const TaSharedDataRegistry: unique symbol;
47
+ export interface TaSharedDataRegistry {
48
+ add(name: string, data: TaSharedData): () => void;
49
+ of(name: string, key: unknown): TaSharedData | undefined;
50
+ }
51
+ /** What the built-in packages declare their hosts and tools need (TaPermissionManifest, `pi.permissions`). */
52
+ export declare const TaPackagePermissions: unique symbol;
53
+ export interface TaPackagePermissions {
54
+ host(name: string, method: string, kind?: string): string;
55
+ tool(name: string): string | undefined;
56
+ }
57
+ /** The devices of those let in, through the relay. */
58
+ export declare const TaMemberDevices: unique symbol;
59
+ export interface TaMemberDevices {
60
+ pair(principal: string, name: string, expiresAt?: number): Promise<TaTeamPairing>;
61
+ unpair(deviceId: string): void;
62
+ list(): TaTeamDevice[];
63
+ }
64
+ /** The connections open, by principal. */
65
+ export declare const TaConnections: unique symbol;
66
+ export interface TaConnections {
67
+ /** Those open of principals other than the owner, how many each. */
68
+ counted(): Record<string, number>;
69
+ /** Closes those of a principal; how many. */
70
+ close(principal: string): number;
71
+ }
72
+ /**
73
+ * What a connection sees of what the sessions and the data of packages tell: the parts of ta serve that push to pages
74
+ * ask it, so that they do not know who is let in or how.
75
+ */
76
+ export declare const TaVisibility: unique symbol;
77
+ export interface TaVisibility {
78
+ /** The conversations of a list a connection may see. */
79
+ sessions<T extends Pick<TaSessionInfo, 'cwd'> & {
80
+ sessionFile?: string;
81
+ }>(connection: RpcConnection | undefined, list: T[]): T[];
82
+ /** The package a connection is told a key of the data of `name` changed of; undefined: not told. */
83
+ dataName(connection: RpcConnection, name: string, key: string): string | undefined;
84
+ }
85
+ /** The face of all this the server side of packages gets (TaExtensionHost.team). */
86
+ export declare const TaHostTeamProvider: unique symbol;
87
+ export interface TaHostTeamProvider {
88
+ readonly team: TaHostTeam;
89
+ }
90
+ //# sourceMappingURL=team-protocol.d.ts.map
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaHostTeamProvider = exports.TaVisibility = exports.TaConnections = exports.TaMemberDevices = exports.TaPackagePermissions = exports.TaSharedDataRegistry = exports.TaRunActors = exports.TaAuthority = exports.TaPrincipals = void 0;
4
+ /**
5
+ * The parts by which ta serve lets people in besides its user (docs/design/ta-team.md 4, 14). Each has one concern, and
6
+ * the rest of ta serve depends on these interfaces only: the sessions, the data of packages and the terminals ask
7
+ * TaVisibility and TaPrincipals what they need, nothing reaches into them.
8
+ */
9
+ /** Who makes a call: TA_OWNER, a principal let in (`m:<id>`), one holding an invitation (`invite:<id>`), or no one (`device:<id>`). */
10
+ exports.TaPrincipals = Symbol('TaPrincipals');
11
+ /** The authorizer a package gave (TaHostTeam.authorize), behind which every answer about one let in is asked. */
12
+ exports.TaAuthority = Symbol('TaAuthority');
13
+ /** Who asked the run going on in each session: its tools do what all of them may. */
14
+ exports.TaRunActors = Symbol('TaRunActors');
15
+ /** Data of packages that is the Ta's (TaHostTeam.shareData). */
16
+ exports.TaSharedDataRegistry = Symbol('TaSharedDataRegistry');
17
+ /** What the built-in packages declare their hosts and tools need (TaPermissionManifest, `pi.permissions`). */
18
+ exports.TaPackagePermissions = Symbol('TaPackagePermissions');
19
+ /** The devices of those let in, through the relay. */
20
+ exports.TaMemberDevices = Symbol('TaMemberDevices');
21
+ /** The connections open, by principal. */
22
+ exports.TaConnections = Symbol('TaConnections');
23
+ /**
24
+ * What a connection sees of what the sessions and the data of packages tell: the parts of ta serve that push to pages
25
+ * ask it, so that they do not know who is let in or how.
26
+ */
27
+ exports.TaVisibility = Symbol('TaVisibility');
28
+ /** The face of all this the server side of packages gets (TaExtensionHost.team). */
29
+ exports.TaHostTeamProvider = Symbol('TaHostTeamProvider');
30
+ //# sourceMappingURL=team-protocol.js.map
@@ -0,0 +1,14 @@
1
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
2
+ import { TaAuthority, TaPrincipals, TaSharedDataRegistry, TaVisibility } from './team-protocol';
3
+ /** What each connection sees of what is pushed: conversations it may view; data of its own, or of the Ta's it may read. */
4
+ export declare class DefaultTaVisibility implements TaVisibility {
5
+ protected readonly principals: TaPrincipals;
6
+ protected readonly authority: TaAuthority;
7
+ protected readonly shared: TaSharedDataRegistry;
8
+ sessions<T extends {
9
+ cwd: string;
10
+ sessionFile?: string;
11
+ }>(connection: RpcConnection | undefined, list: T[]): T[];
12
+ dataName(connection: RpcConnection, name: string, key: string): string | undefined;
13
+ }
14
+ //# sourceMappingURL=visibility.d.ts.map