@celljs/ta-host 4.13.0 → 4.14.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 (79) hide show
  1. package/README.en.md +1 -0
  2. package/README.md +1 -0
  3. package/esm/node/host-protocol.d.ts +17 -1
  4. package/esm/node/host-protocol.js +1 -0
  5. package/esm/node/index.d.ts +2 -0
  6. package/esm/node/index.js +2 -0
  7. package/esm/node/package-hosts.d.ts +2 -1
  8. package/esm/node/package-hosts.js +6 -2
  9. package/esm/node/presence-book.d.ts +38 -0
  10. package/esm/node/presence-book.js +159 -0
  11. package/esm/node/presence-server-impl.d.ts +13 -0
  12. package/esm/node/presence-server-impl.js +45 -0
  13. package/esm/node/rooms-bridge.d.ts +6 -1
  14. package/esm/node/rooms-bridge.js +21 -3
  15. package/esm/node/session-list-feed.js +2 -1
  16. package/esm/node/sign-in/access-server-impl.d.ts +5 -1
  17. package/esm/node/sign-in/access-server-impl.js +22 -1
  18. package/esm/node/sign-in/sign-in-flow.d.ts +14 -1
  19. package/esm/node/sign-in/sign-in-flow.js +24 -7
  20. package/esm/node/sign-in/sign-in-provider.d.ts +40 -1
  21. package/esm/node/sign-in/sign-in-provider.js +120 -0
  22. package/esm/node/sign-in/ta-sign-in.d.ts +9 -3
  23. package/esm/node/sign-in/ta-sign-in.js +32 -4
  24. package/esm/node/ta-server-impl.d.ts +7 -0
  25. package/esm/node/ta-server-impl.js +44 -7
  26. package/esm/node/ta-session-registry.d.ts +10 -3
  27. package/esm/node/ta-session-registry.js +29 -3
  28. package/esm/node/ta-session.d.ts +2 -0
  29. package/esm/node/ta-session.js +6 -0
  30. package/esm/node/team/host-team.d.ts +2 -1
  31. package/esm/node/team/host-team.js +10 -1
  32. package/esm/node/team/index.d.ts +1 -0
  33. package/esm/node/team/index.js +1 -0
  34. package/esm/node/team/member-accounts.d.ts +13 -0
  35. package/esm/node/team/member-accounts.js +37 -0
  36. package/esm/node/team/team-calls.d.ts +2 -1
  37. package/esm/node/team/team-calls.js +7 -0
  38. package/esm/node/team/team-gate.js +1 -1
  39. package/esm/node/team/team-protocol.d.ts +5 -1
  40. package/esm/node/team/team-protocol.js +2 -0
  41. package/lib/node/host-protocol.d.ts +17 -1
  42. package/lib/node/host-protocol.js +2 -1
  43. package/lib/node/index.d.ts +2 -0
  44. package/lib/node/index.js +2 -0
  45. package/lib/node/package-hosts.d.ts +2 -1
  46. package/lib/node/package-hosts.js +5 -1
  47. package/lib/node/presence-book.d.ts +39 -0
  48. package/lib/node/presence-book.js +162 -0
  49. package/lib/node/presence-server-impl.d.ts +14 -0
  50. package/lib/node/presence-server-impl.js +48 -0
  51. package/lib/node/rooms-bridge.d.ts +6 -1
  52. package/lib/node/rooms-bridge.js +20 -2
  53. package/lib/node/session-list-feed.js +2 -1
  54. package/lib/node/sign-in/access-server-impl.d.ts +5 -1
  55. package/lib/node/sign-in/access-server-impl.js +21 -0
  56. package/lib/node/sign-in/sign-in-flow.d.ts +14 -1
  57. package/lib/node/sign-in/sign-in-flow.js +24 -7
  58. package/lib/node/sign-in/sign-in-provider.d.ts +40 -1
  59. package/lib/node/sign-in/sign-in-provider.js +120 -0
  60. package/lib/node/sign-in/ta-sign-in.d.ts +9 -3
  61. package/lib/node/sign-in/ta-sign-in.js +32 -4
  62. package/lib/node/ta-server-impl.d.ts +7 -0
  63. package/lib/node/ta-server-impl.js +43 -6
  64. package/lib/node/ta-session-registry.d.ts +10 -3
  65. package/lib/node/ta-session-registry.js +27 -1
  66. package/lib/node/ta-session.d.ts +2 -0
  67. package/lib/node/ta-session.js +6 -0
  68. package/lib/node/team/host-team.d.ts +2 -1
  69. package/lib/node/team/host-team.js +9 -0
  70. package/lib/node/team/index.d.ts +1 -0
  71. package/lib/node/team/index.js +1 -0
  72. package/lib/node/team/member-accounts.d.ts +14 -0
  73. package/lib/node/team/member-accounts.js +40 -0
  74. package/lib/node/team/team-calls.d.ts +2 -1
  75. package/lib/node/team/team-calls.js +7 -0
  76. package/lib/node/team/team-gate.js +1 -1
  77. package/lib/node/team/team-protocol.d.ts +5 -1
  78. package/lib/node/team/team-protocol.js +3 -1
  79. package/package.json +12 -12
@@ -57,15 +57,33 @@ export class SignInFlow {
57
57
  }
58
58
  /** Where the browser goes, and the state that comes back with it. */
59
59
  async start(providerId, origin, bind, now = Date.now()) {
60
- const provider = this.provider(providerId);
61
- if (!provider) {
62
- throw new SignInError(`No provider ${providerId}`);
63
- }
64
60
  this.prune(now);
65
61
  const binding = bind ? this.binds.get(bind) : undefined;
66
62
  if (bind) {
67
63
  this.binds.delete(bind);
68
64
  }
65
+ return this.begin(providerId, SignInUtil.redirectUri(origin, providerId), { bind: (binding === null || binding === void 0 ? void 0 : binding.provider) === providerId }, now);
66
+ }
67
+ /** A sign-in that only says who it is ({@link verify}), coming back to `redirectUri`: the page of a relay, say. */
68
+ startVerify(providerId, redirectUri, now = Date.now()) {
69
+ this.prune(now);
70
+ return this.begin(providerId, redirectUri, { bind: false, verify: true }, now);
71
+ }
72
+ /** The account of a sign-in of {@link startVerify}: no token, nothing let in. */
73
+ async verify(state, code, now = Date.now()) {
74
+ const pending = this.pending.get(state);
75
+ this.pending.delete(state);
76
+ const provider = pending && this.provider(pending.provider);
77
+ if (!(pending === null || pending === void 0 ? void 0 : pending.verify) || !provider || now - pending.at > PENDING_TTL_MS) {
78
+ throw new SignInError('The sign-in expired or was not started here');
79
+ }
80
+ return provider.account({ code, redirectUri: pending.redirectUri, verifier: pending.verifier });
81
+ }
82
+ async begin(providerId, redirectUri, kind, now) {
83
+ const provider = this.provider(providerId);
84
+ if (!provider) {
85
+ throw new SignInError(`No provider ${providerId}`);
86
+ }
69
87
  // The oldest goes first: a flood of starts takes no more memory, and costs only sign-ins as old.
70
88
  for (const oldest of this.pending.keys()) {
71
89
  if (this.pending.size < MAX_PENDING_SIGN_INS) {
@@ -75,8 +93,7 @@ export class SignInFlow {
75
93
  }
76
94
  const state = randomBytes(18).toString('base64url');
77
95
  const { verifier, challenge } = SignInUtil.pkce();
78
- const redirectUri = SignInUtil.redirectUri(origin, provider.id);
79
- this.pending.set(state, { provider: provider.id, verifier, redirectUri, bind: (binding === null || binding === void 0 ? void 0 : binding.provider) === provider.id, at: now });
96
+ this.pending.set(state, { provider: provider.id, verifier, redirectUri, ...kind, at: now });
80
97
  return { url: await provider.authorizeUrl({ redirectUri, state, challenge }), state };
81
98
  }
82
99
  /** The browser came back from the provider with a code: in with a token of its own, or told to wait. */
@@ -84,7 +101,7 @@ export class SignInFlow {
84
101
  const pending = this.pending.get(state);
85
102
  this.pending.delete(state);
86
103
  const provider = this.provider(providerId);
87
- if (!pending || !provider || pending.provider !== providerId || now - pending.at > PENDING_TTL_MS) {
104
+ if (!pending || pending.verify || !provider || pending.provider !== providerId || now - pending.at > PENDING_TTL_MS) {
88
105
  throw new SignInError('The sign-in expired or was not started here');
89
106
  }
90
107
  const account = await provider.account({ code, redirectUri: pending.redirectUri, verifier: pending.verifier });
@@ -1,4 +1,4 @@
1
- import type { TaSignInProvider } from '@celljs/ta-protocol';
1
+ import type { TaSignInKind, TaSignInProvider, TaSignInSetup } from '@celljs/ta-protocol';
2
2
  /** Who signed in, as the provider says: `id` is the account as TA_ALLOWED_USERS names it (`github:<login>`). */
3
3
  export interface SignInAccount {
4
4
  id: string;
@@ -126,6 +126,45 @@ export declare class WeChatSignIn implements SignInProvider {
126
126
  export declare namespace SignInProviders {
127
127
  /** TA_PUBLIC_URL without its trailing `/`. */
128
128
  function publicUrl(env: Record<string, string | undefined>): string | undefined;
129
+ /** Each kind of provider: its name, its fields in sign-in.json with the variable of the environment of each, and its secret's. */
130
+ const KINDS: readonly {
131
+ id: TaSignInKind;
132
+ name: string;
133
+ fields: Record<string, string>;
134
+ required: string[];
135
+ secret: [string, string];
136
+ }[];
137
+ /** How each kind is set up, by the environment first, then sign-in.json; no secret in it. */
138
+ function setups(env: Record<string, string | undefined>, config: Config | undefined): TaSignInSetup[];
139
+ /**
140
+ * sign-in.json with a kind set up anew (`null`: taken out): the fields given, trimmed; an empty secret keeps the
141
+ * one kept. Throws for a kind it does not know, or one without what it needs.
142
+ */
143
+ function withProvider(config: Config | undefined, id: string, input: Record<string, unknown> | null): Config;
144
+ /** sign-in.json of the agent directory: the same providers as the environment, for a Ta not started from a shell. */
145
+ interface Config {
146
+ github?: {
147
+ clientId?: string;
148
+ clientSecret?: string;
149
+ url?: string;
150
+ };
151
+ google?: {
152
+ clientId?: string;
153
+ clientSecret?: string;
154
+ };
155
+ wechat?: {
156
+ appId?: string;
157
+ appSecret?: string;
158
+ };
159
+ oidc?: {
160
+ issuer?: string;
161
+ clientId?: string;
162
+ clientSecret?: string;
163
+ name?: string;
164
+ };
165
+ }
166
+ /** The environment's variables that sign-in.json stands for: what the environment sets wins. */
167
+ function withConfig(env: Record<string, string | undefined>, config: Config | undefined): Record<string, string | undefined>;
129
168
  /**
130
169
  * The providers the operator configured in the environment of ta serve: a provider is offered when it
131
170
  * has its id and its secret.
@@ -195,6 +195,126 @@ export var SignInProviders;
195
195
  return ((_a = env.TA_PUBLIC_URL) === null || _a === void 0 ? void 0 : _a.trim().replace(/\/+$/, '')) || undefined;
196
196
  }
197
197
  SignInProviders.publicUrl = publicUrl;
198
+ /** Each kind of provider: its name, its fields in sign-in.json with the variable of the environment of each, and its secret's. */
199
+ SignInProviders.KINDS = [
200
+ {
201
+ id: 'github',
202
+ name: 'GitHub',
203
+ fields: { clientId: 'TA_GITHUB_CLIENT_ID', url: 'TA_GITHUB_URL' },
204
+ required: ['clientId'],
205
+ secret: ['clientSecret', 'TA_GITHUB_CLIENT_SECRET'],
206
+ },
207
+ {
208
+ id: 'google',
209
+ name: 'Google',
210
+ fields: { clientId: 'TA_GOOGLE_CLIENT_ID' },
211
+ required: ['clientId'],
212
+ secret: ['clientSecret', 'TA_GOOGLE_CLIENT_SECRET'],
213
+ },
214
+ { id: 'wechat', name: 'WeChat', fields: { appId: 'TA_WECHAT_APP_ID' }, required: ['appId'], secret: ['appSecret', 'TA_WECHAT_APP_SECRET'] },
215
+ {
216
+ id: 'oidc',
217
+ name: 'SSO',
218
+ fields: { issuer: 'TA_OIDC_ISSUER', clientId: 'TA_OIDC_CLIENT_ID', name: 'TA_OIDC_NAME' },
219
+ required: ['issuer', 'clientId'],
220
+ secret: ['clientSecret', 'TA_OIDC_CLIENT_SECRET'],
221
+ },
222
+ ];
223
+ /** How each kind is set up, by the environment first, then sign-in.json; no secret in it. */
224
+ function setups(env, config) {
225
+ return SignInProviders.KINDS.map(kind => {
226
+ var _a;
227
+ const file = ((_a = config === null || config === void 0 ? void 0 : config[kind.id]) !== null && _a !== void 0 ? _a : {});
228
+ const fromEnv = !!env[kind.secret[1]] || Object.values(kind.fields).some(key => !!env[key]);
229
+ const value = (field) => (fromEnv ? env[kind.fields[field]] : typeof file[field] === 'string' ? file[field] : undefined);
230
+ const fields = {};
231
+ for (const field of Object.keys(kind.fields)) {
232
+ const one = value(field);
233
+ if (one) {
234
+ fields[field] = one;
235
+ }
236
+ }
237
+ const secret = fromEnv ? !!env[kind.secret[1]] : typeof file[kind.secret[0]] === 'string' && !!file[kind.secret[0]];
238
+ const configured = secret && kind.required.every(field => !!fields[field]);
239
+ const any = fromEnv || Object.keys(fields).length > 0 || secret;
240
+ return {
241
+ id: kind.id,
242
+ name: kind.id === 'oidc' && fields.name ? fields.name : kind.name,
243
+ configured,
244
+ ...(any ? { source: fromEnv ? 'env' : 'file' } : {}),
245
+ fields,
246
+ secret,
247
+ };
248
+ });
249
+ }
250
+ SignInProviders.setups = setups;
251
+ /**
252
+ * sign-in.json with a kind set up anew (`null`: taken out): the fields given, trimmed; an empty secret keeps the
253
+ * one kept. Throws for a kind it does not know, or one without what it needs.
254
+ */
255
+ function withProvider(config, id, input) {
256
+ var _a;
257
+ const kind = SignInProviders.KINDS.find(one => one.id === id);
258
+ if (!kind) {
259
+ throw new SignInError(`No such way to sign in: ${id}`);
260
+ }
261
+ const next = { ...(config !== null && config !== void 0 ? config : {}) };
262
+ if (!input) {
263
+ delete next[kind.id];
264
+ return next;
265
+ }
266
+ const kept = ((_a = next[kind.id]) !== null && _a !== void 0 ? _a : {});
267
+ const one = {};
268
+ for (const field of Object.keys(kind.fields)) {
269
+ const value = typeof input[field] === 'string' ? input[field].trim().slice(0, 500) : '';
270
+ if (value) {
271
+ one[field] = value;
272
+ }
273
+ }
274
+ const secret = typeof input[kind.secret[0]] === 'string' ? input[kind.secret[0]].trim().slice(0, 500) : '';
275
+ const keptSecret = typeof kept[kind.secret[0]] === 'string' ? kept[kind.secret[0]] : '';
276
+ if (secret || keptSecret) {
277
+ one[kind.secret[0]] = secret || keptSecret;
278
+ }
279
+ const missing = [...kind.required, kind.secret[0]].filter(field => !one[field]);
280
+ if (missing.length) {
281
+ throw new SignInError(`${kind.name} needs ${missing.join(', ')}`);
282
+ }
283
+ if (one.issuer && !/^https:\/\//.test(one.issuer) && !/^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?(\/|$)/.test(one.issuer)) {
284
+ throw new SignInError('The issuer is an https address.');
285
+ }
286
+ if (one.url && !/^https?:\/\//.test(one.url)) {
287
+ throw new SignInError('The address of GitHub Enterprise is an http(s) address.');
288
+ }
289
+ next[kind.id] = one;
290
+ return next;
291
+ }
292
+ SignInProviders.withProvider = withProvider;
293
+ /** The environment's variables that sign-in.json stands for: what the environment sets wins. */
294
+ function withConfig(env, config) {
295
+ const { github, google, wechat, oidc } = config !== null && config !== void 0 ? config : {};
296
+ const file = {
297
+ TA_GITHUB_CLIENT_ID: github === null || github === void 0 ? void 0 : github.clientId,
298
+ TA_GITHUB_CLIENT_SECRET: github === null || github === void 0 ? void 0 : github.clientSecret,
299
+ TA_GITHUB_URL: github === null || github === void 0 ? void 0 : github.url,
300
+ TA_GOOGLE_CLIENT_ID: google === null || google === void 0 ? void 0 : google.clientId,
301
+ TA_GOOGLE_CLIENT_SECRET: google === null || google === void 0 ? void 0 : google.clientSecret,
302
+ TA_WECHAT_APP_ID: wechat === null || wechat === void 0 ? void 0 : wechat.appId,
303
+ TA_WECHAT_APP_SECRET: wechat === null || wechat === void 0 ? void 0 : wechat.appSecret,
304
+ TA_OIDC_ISSUER: oidc === null || oidc === void 0 ? void 0 : oidc.issuer,
305
+ TA_OIDC_CLIENT_ID: oidc === null || oidc === void 0 ? void 0 : oidc.clientId,
306
+ TA_OIDC_CLIENT_SECRET: oidc === null || oidc === void 0 ? void 0 : oidc.clientSecret,
307
+ TA_OIDC_NAME: oidc === null || oidc === void 0 ? void 0 : oidc.name,
308
+ };
309
+ const merged = { ...env };
310
+ for (const [key, value] of Object.entries(file)) {
311
+ if (typeof value === 'string' && value && !env[key]) {
312
+ merged[key] = value;
313
+ }
314
+ }
315
+ return merged;
316
+ }
317
+ SignInProviders.withConfig = withConfig;
198
318
  /**
199
319
  * The providers the operator configured in the environment of ta serve: a provider is offered when it
200
320
  * has its id and its secret.
@@ -1,10 +1,16 @@
1
+ import type { TaSignInKind, TaSignInSetup } from '@celljs/ta-protocol';
1
2
  import { SignInFlow } from './sign-in-flow.js';
2
3
  export declare const TA_SIGN_IN: unique symbol;
3
4
  /**
4
- * Signing in to ta serve with an account, besides its token. The providers come from the environment
5
- * (SignInProviders.fromEnv); who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
6
- * (AccessStore). Someone else who signs in is told to wait, and shows up as a request.
5
+ * Signing in to ta serve with an account, besides its token. The providers come from the environment, then
6
+ * sign-in.json of the agent directory (SignInProviders.withConfig), which Settings, Access writes (setProvider) and
7
+ * which is read again then; who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
8
+ * (AccessStore). Someone else who signs in is told to wait, and shows up as a request. Those asking to come in by a
9
+ * link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing.
7
10
  */
8
11
  export declare class TaSignIn extends SignInFlow {
9
12
  constructor();
13
+ setups(): TaSignInSetup[];
14
+ /** Sets a kind up in sign-in.json (readable by the user alone), offered at once; not one the environment sets. */
15
+ setProvider(id: TaSignInKind, fields: Record<string, unknown> | null): void;
10
16
  }
@@ -7,6 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
+ import { readFileSync, writeFileSync } from 'node:fs';
10
11
  import { join } from 'node:path';
11
12
  import { Component } from '@celljs/core';
12
13
  import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
@@ -14,14 +15,41 @@ import { AccessStore, AccessUtil } from './access-store.js';
14
15
  import { SignInFlow } from './sign-in-flow.js';
15
16
  import { SignInProviders } from './sign-in-provider.js';
16
17
  export const TA_SIGN_IN = Symbol('TaSignIn');
18
+ function file() {
19
+ return join(TaBrand.agentDir(), 'sign-in.json');
20
+ }
21
+ /** sign-in.json of the agent directory (SignInProviders.Config); none when it is not there or not JSON. */
22
+ function config() {
23
+ try {
24
+ return JSON.parse(readFileSync(file(), 'utf8'));
25
+ }
26
+ catch (_a) {
27
+ return undefined;
28
+ }
29
+ }
17
30
  /**
18
- * Signing in to ta serve with an account, besides its token. The providers come from the environment
19
- * (SignInProviders.fromEnv); who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
20
- * (AccessStore). Someone else who signs in is told to wait, and shows up as a request.
31
+ * Signing in to ta serve with an account, besides its token. The providers come from the environment, then
32
+ * sign-in.json of the agent directory (SignInProviders.withConfig), which Settings, Access writes (setProvider) and
33
+ * which is read again then; who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
34
+ * (AccessStore). Someone else who signs in is told to wait, and shows up as a request. Those asking to come in by a
35
+ * link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing.
21
36
  */
22
37
  let TaSignIn = class TaSignIn extends SignInFlow {
23
38
  constructor() {
24
- super(SignInProviders.fromEnv(process.env), new AccessStore(join(TaBrand.agentDir(), 'access.json'), AccessUtil.parseList(process.env.TA_ALLOWED_USERS)), SignInProviders.publicUrl(process.env));
39
+ super(SignInProviders.fromEnv(SignInProviders.withConfig(process.env, config())), new AccessStore(join(TaBrand.agentDir(), 'access.json'), AccessUtil.parseList(process.env.TA_ALLOWED_USERS)), SignInProviders.publicUrl(process.env));
40
+ }
41
+ setups() {
42
+ return SignInProviders.setups(process.env, config());
43
+ }
44
+ /** Sets a kind up in sign-in.json (readable by the user alone), offered at once; not one the environment sets. */
45
+ setProvider(id, fields) {
46
+ var _a;
47
+ if (((_a = this.setups().find(one => one.id === id)) === null || _a === void 0 ? void 0 : _a.source) === 'env') {
48
+ throw new Error(`${id} is set up by the environment of ta serve, not here.`);
49
+ }
50
+ const next = SignInProviders.withProvider(config(), id, fields);
51
+ writeFileSync(file(), `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
52
+ this.providers = SignInProviders.fromEnv(SignInProviders.withConfig(process.env, next));
25
53
  }
26
54
  };
27
55
  TaSignIn = __decorate([
@@ -38,6 +38,13 @@ export declare class TaServerImpl implements TaServer {
38
38
  prompt(sessionId: string, input: TaPromptInput): Promise<TaPromptReceipt>;
39
39
  steer(sessionId: string, input: TaPromptInput): Promise<TaPromptReceipt>;
40
40
  followUp(sessionId: string, input: TaPromptInput): Promise<TaPromptReceipt>;
41
+ /**
42
+ * A message as written by who sends it (TaMessageAuthor): the line of one let in, never one a page wrote. The
43
+ * owner's has none, as before anyone was let in.
44
+ */
45
+ protected signed(input: TaPromptInput): TaPromptInput;
46
+ /** Whether the principal of this call may take back a waiting message: the owner any, another only their own. */
47
+ protected mayTakeBack(text: string): boolean;
41
48
  /** Logs how long the run took to end here: with the times the box keeps (stop.times of @celljs/ta-web), where a slow stop went. */
42
49
  abort(sessionId: string): Promise<void>;
43
50
  clearQueue(sessionId: string): Promise<TaCommandResult<'clear_queue'>>;
@@ -15,7 +15,7 @@ import { Rpc } from '@celljs/rpc';
15
15
  import { RpcContext } from '@celljs/rpc/lib/node';
16
16
  import { ExecEnvironmentFactory } from '@celljs/ta-exec';
17
17
  import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
18
- import { TaServer, } from '@celljs/ta-protocol';
18
+ import { TA_OWNER, TaMessageAuthor, TaServer, } from '@celljs/ta-protocol';
19
19
  import { TaSessionCatalog, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
20
20
  import { SessionInfoUtil } from './session-info-util.js';
21
21
  import { SlashCommandUtil } from './slash-command-util.js';
@@ -67,7 +67,7 @@ let TaServerImpl = class TaServerImpl {
67
67
  return this.guard(() => this.sessions.close(sessionId));
68
68
  }
69
69
  async listSessions() {
70
- return this.visibility.sessions(RpcContext.connection(), this.sessions.list().map(session => session.info()));
70
+ return this.sessions.infos(RpcContext.connection());
71
71
  }
72
72
  async watchSessions() {
73
73
  return this.sessions.watch(RpcContext.requireConnection());
@@ -112,16 +112,35 @@ let TaServerImpl = class TaServerImpl {
112
112
  prompt(sessionId, input) {
113
113
  return this.on(sessionId, session => {
114
114
  this.runs.prompted(sessionId);
115
- return session.prompt(input);
115
+ return session.prompt(this.signed(input));
116
116
  });
117
117
  }
118
118
  steer(sessionId, input) {
119
119
  this.runs.prompted(sessionId);
120
- return this.on(sessionId, session => session.steer(input));
120
+ return this.on(sessionId, session => session.steer(this.signed(input)));
121
121
  }
122
122
  followUp(sessionId, input) {
123
123
  this.runs.prompted(sessionId);
124
- return this.on(sessionId, session => session.followUp(input));
124
+ return this.on(sessionId, session => session.followUp(this.signed(input)));
125
+ }
126
+ /**
127
+ * A message as written by who sends it (TaMessageAuthor): the line of one let in, never one a page wrote. The
128
+ * owner's has none, as before anyone was let in.
129
+ */
130
+ signed(input) {
131
+ if (typeof (input === null || input === void 0 ? void 0 : input.message) !== 'string') {
132
+ return input;
133
+ }
134
+ const principal = this.principals.current();
135
+ const member = principal === TA_OWNER ? undefined : this.authority.describe(principal);
136
+ const message = member ? TaMessageAuthor.mark(input.message, principal, member.name) : TaMessageAuthor.strip(input.message);
137
+ return message === input.message ? input : { ...input, message };
138
+ }
139
+ /** Whether the principal of this call may take back a waiting message: the owner any, another only their own. */
140
+ mayTakeBack(text) {
141
+ var _a;
142
+ const principal = this.principals.current();
143
+ return principal === TA_OWNER || ((_a = TaMessageAuthor.of(text)) === null || _a === void 0 ? void 0 : _a.id) === principal;
125
144
  }
126
145
  /** Logs how long the run took to end here: with the times the box keeps (stop.times of @celljs/ta-web), where a slow stop went. */
127
146
  async abort(sessionId) {
@@ -130,10 +149,28 @@ let TaServerImpl = class TaServerImpl {
130
149
  console.log(`[${new Date().toISOString()}] ta serve: stopped ${sessionId.slice(0, 8)} in ${Date.now() - start} ms`);
131
150
  }
132
151
  clearQueue(sessionId) {
133
- return this.on(sessionId, ({ pi }) => pi.clearQueue());
152
+ return this.on(sessionId, session => {
153
+ if (this.principals.current() === TA_OWNER) {
154
+ return session.pi.clearQueue();
155
+ }
156
+ // One let in takes back their own, the others' stay.
157
+ const taken = { steering: [], followUp: [] };
158
+ for (const kind of ['steering', 'followUp']) {
159
+ const texts = session.queued(kind);
160
+ for (let index = texts.length - 1; index >= 0; index--) {
161
+ if (this.mayTakeBack(texts[index]) && session.removeQueued(kind, index)) {
162
+ taken[kind].unshift(texts[index]);
163
+ }
164
+ }
165
+ }
166
+ return taken;
167
+ });
134
168
  }
135
169
  removeQueued(sessionId, kind, index) {
136
- return this.on(sessionId, session => session.removeQueued(kind, index));
170
+ return this.on(sessionId, session => {
171
+ const text = session.queued(kind)[index];
172
+ return text !== undefined && this.mayTakeBack(text) && session.removeQueued(kind, index);
173
+ });
137
174
  }
138
175
  newSession(sessionId, parentSession) {
139
176
  return this.authored(sessionId, ({ runtime }) => runtime.newSession(parentSession ? { parentSession } : undefined));
@@ -1,11 +1,11 @@
1
1
  import type { RpcConnection } from '@celljs/rpc/lib/node';
2
- import type { TaEvent, TaOpenSessionOptions, TaSessionInfo } from '@celljs/ta-protocol';
2
+ import { type TaEvent, type TaOpenSessionOptions, type TaSessionInfo } from '@celljs/ta-protocol';
3
3
  import { ClientChannel } from './client-channel.js';
4
- import { type TaHostOptions, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
4
+ import { type TaHostOptions, TaPresenceBook, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
5
5
  import { TaSessionHandles } from './session-handles.js';
6
6
  import { SessionListFeed } from './session-list-feed.js';
7
7
  import type { TaSession } from './ta-session.js';
8
- import { TaVisibility } from './team/team-protocol.js';
8
+ import { TaPrincipals, TaVisibility } from './team/team-protocol.js';
9
9
  /**
10
10
  * The open sessions. A session's id outlives its memory: closed while idle or by a restart, it opens again
11
11
  * from its file under the same id at the next call (TaSessionHandles), so clients never see it go.
@@ -25,6 +25,10 @@ export declare class DefaultTaSessionRegistry implements TaSessionRegistry {
25
25
  protected readonly waking: Map<string, Promise<TaSession>>;
26
26
  /** What each connection may see of the list (docs/design/ta-team.md): all of it for the owner's. */
27
27
  protected readonly visibility: TaVisibility;
28
+ protected readonly principals: TaPrincipals;
29
+ /** Who else is at each conversation: what each connection is told of the others (TaSessionInfo.people). */
30
+ protected readonly presence: TaPresenceBook;
31
+ protected presenceWatched: boolean;
28
32
  protected readonly feed: SessionListFeed;
29
33
  locate(sessionId: string): {
30
34
  sessionFile?: string;
@@ -34,7 +38,10 @@ export declare class DefaultTaSessionRegistry implements TaSessionRegistry {
34
38
  require(sessionId: string): TaSession;
35
39
  resolve(sessionId: string): Promise<TaSession>;
36
40
  list(): TaSession[];
41
+ infos(connection: RpcConnection | undefined): TaSessionInfo[];
37
42
  watch(connection: RpcConnection): TaSessionInfo[];
43
+ /** Each with the others at it, as the principal of `connection` is told: never themselves. */
44
+ protected withPeople(connection: RpcConnection | undefined, list: TaSessionInfo[]): TaSessionInfo[];
38
45
  get watching(): number;
39
46
  get watchedAt(): number;
40
47
  onSessionEvent(listener: (session: TaSession, event: TaEvent) => void): () => void;
@@ -11,12 +11,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  };
12
12
  import { existsSync } from 'node:fs';
13
13
  import { Autowired, Component, Value } from '@celljs/core';
14
+ import { TA_OWNER } from '@celljs/ta-protocol';
14
15
  import { ClientChannel } from './client-channel.js';
15
- import { TA_HOST_OPTIONS, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
16
+ import { TA_HOST_OPTIONS, TaPresenceBook, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
16
17
  import { TaSessionHandles } from './session-handles.js';
17
18
  import { SessionListFeed } from './session-list-feed.js';
18
19
  import { TaErrors } from './ta-errors.js';
19
- import { TaVisibility } from './team/team-protocol.js';
20
+ import { TaPrincipals, TaVisibility } from './team/team-protocol.js';
20
21
  /**
21
22
  * The open sessions. A session's id outlives its memory: closed while idle or by a restart, it opens again
22
23
  * from its file under the same id at the next call (TaSessionHandles), so clients never see it go.
@@ -32,7 +33,8 @@ let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
32
33
  this.closing = new Map();
33
34
  /** Sessions opening again under their ids: the calls that come meanwhile wait for the same. */
34
35
  this.waking = new Map();
35
- this.feed = new SessionListFeed(() => this.list().map(session => session.info()), undefined, (connection, list) => this.visibility.sessions(connection, list));
36
+ this.presenceWatched = false;
37
+ this.feed = new SessionListFeed(() => this.list().map(session => session.info()), undefined, (connection, list) => this.withPeople(connection, this.visibility.sessions(connection, list)));
36
38
  }
37
39
  locate(sessionId) {
38
40
  const session = this.sessions.get(sessionId);
@@ -79,9 +81,25 @@ let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
79
81
  list() {
80
82
  return [...this.sessions.values()];
81
83
  }
84
+ infos(connection) {
85
+ return this.withPeople(connection, this.visibility.sessions(connection, this.list().map(session => session.info())));
86
+ }
82
87
  watch(connection) {
88
+ // Once a page watches, every component is made: who is where tells the lists again as it changes.
89
+ if (!this.presenceWatched) {
90
+ this.presenceWatched = true;
91
+ this.presence.onChange(() => this.feed.changed());
92
+ }
83
93
  return this.feed.watch(connection);
84
94
  }
95
+ /** Each with the others at it, as the principal of `connection` is told: never themselves. */
96
+ withPeople(connection, list) {
97
+ const principal = connection ? this.principals.of(connection) : TA_OWNER;
98
+ return list.map(info => {
99
+ const people = this.presence.people(info.sessionId, principal, info.isStreaming);
100
+ return people ? { ...info, people } : info;
101
+ });
102
+ }
85
103
  get watching() {
86
104
  return this.feed.watching;
87
105
  }
@@ -186,6 +204,14 @@ __decorate([
186
204
  Autowired({ id: TaVisibility, detached: true }),
187
205
  __metadata("design:type", Object)
188
206
  ], DefaultTaSessionRegistry.prototype, "visibility", void 0);
207
+ __decorate([
208
+ Autowired({ id: TaPrincipals, detached: true }),
209
+ __metadata("design:type", Object)
210
+ ], DefaultTaSessionRegistry.prototype, "principals", void 0);
211
+ __decorate([
212
+ Autowired({ id: TaPresenceBook, detached: true }),
213
+ __metadata("design:type", Object)
214
+ ], DefaultTaSessionRegistry.prototype, "presence", void 0);
189
215
  DefaultTaSessionRegistry = __decorate([
190
216
  Component(TaSessionRegistry)
191
217
  ], DefaultTaSessionRegistry);
@@ -75,6 +75,8 @@ export declare class TaSession {
75
75
  * Takes one waiting message out of the queue. pi only clears the whole of it, so this reaches into its
76
76
  * two lists: the texts it shows and the messages its agent will take, which keep their images.
77
77
  */
78
+ /** The texts of the messages waiting, of one kind, in their order. */
79
+ queued(kind: 'steering' | 'followUp'): readonly string[];
78
80
  removeQueued(kind: 'steering' | 'followUp', index: number): boolean;
79
81
  /** pi's runtime says nothing of a move in the tree: the clients are told to read the conversation again. */
80
82
  navigateTree(targetId: string, options?: Parameters<AgentSession['navigateTree']>[1]): Promise<TaNavigateResult>;
@@ -157,6 +157,12 @@ export class TaSession {
157
157
  * Takes one waiting message out of the queue. pi only clears the whole of it, so this reaches into its
158
158
  * two lists: the texts it shows and the messages its agent will take, which keep their images.
159
159
  */
160
+ /** The texts of the messages waiting, of one kind, in their order. */
161
+ queued(kind) {
162
+ var _a;
163
+ const pi = this.pi;
164
+ return [...((_a = (kind === 'steering' ? pi._steeringMessages : pi._followUpMessages)) !== null && _a !== void 0 ? _a : [])];
165
+ }
160
166
  removeQueued(kind, index) {
161
167
  const pi = this.pi;
162
168
  const texts = kind === 'steering' ? pi._steeringMessages : pi._followUpMessages;
@@ -1,5 +1,5 @@
1
1
  import type { TaHostTeam } from '@celljs/ta-protocol';
2
- import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry } from './team-protocol.js';
2
+ import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberAccounts, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry } from './team-protocol.js';
3
3
  /** The face of the parts of ta serve that let people in, as the server side of packages gets it (TaExtensionHost.team). */
4
4
  export declare class DefaultTaHostTeamProvider implements TaHostTeamProvider {
5
5
  protected readonly principals: TaPrincipals;
@@ -9,5 +9,6 @@ export declare class DefaultTaHostTeamProvider implements TaHostTeamProvider {
9
9
  protected readonly permissions: TaPackagePermissions;
10
10
  protected readonly devices: TaMemberDevices;
11
11
  protected readonly connections: TaConnections;
12
+ protected readonly accounts: TaMemberAccounts;
12
13
  readonly team: TaHostTeam;
13
14
  }
@@ -8,7 +8,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
10
  import { Autowired, Component } from '@celljs/core';
11
- import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry, } from './team-protocol.js';
11
+ import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberAccounts, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry, } from './team-protocol.js';
12
12
  /** The face of the parts of ta serve that let people in, as the server side of packages gets it (TaExtensionHost.team). */
13
13
  let DefaultTaHostTeamProvider = class DefaultTaHostTeamProvider {
14
14
  constructor() {
@@ -31,6 +31,11 @@ let DefaultTaHostTeamProvider = class DefaultTaHostTeamProvider {
31
31
  pair: (principal, name, expiresAt) => this.devices.pair(principal, name, expiresAt),
32
32
  unpair: deviceId => this.devices.unpair(deviceId),
33
33
  devices: () => this.devices.list(),
34
+ accounts: {
35
+ providers: () => this.accounts.providers(),
36
+ start: (provider, redirectUri) => this.accounts.start(provider, redirectUri),
37
+ verify: (state, code) => this.accounts.verify(state, code),
38
+ },
34
39
  };
35
40
  }
36
41
  };
@@ -62,6 +67,10 @@ __decorate([
62
67
  Autowired(TaConnections),
63
68
  __metadata("design:type", Object)
64
69
  ], DefaultTaHostTeamProvider.prototype, "connections", void 0);
70
+ __decorate([
71
+ Autowired(TaMemberAccounts),
72
+ __metadata("design:type", Object)
73
+ ], DefaultTaHostTeamProvider.prototype, "accounts", void 0);
65
74
  DefaultTaHostTeamProvider = __decorate([
66
75
  Component(TaHostTeamProvider)
67
76
  ], DefaultTaHostTeamProvider);
@@ -2,6 +2,7 @@ export * from './authority.js';
2
2
  export * from './call-util.js';
3
3
  export * from './connections.js';
4
4
  export * from './host-team.js';
5
+ export * from './member-accounts.js';
5
6
  export * from './member-devices.js';
6
7
  export * from './package-permissions.js';
7
8
  export * from './principals.js';
@@ -2,6 +2,7 @@ export * from './authority.js';
2
2
  export * from './call-util.js';
3
3
  export * from './connections.js';
4
4
  export * from './host-team.js';
5
+ export * from './member-accounts.js';
5
6
  export * from './member-devices.js';
6
7
  export * from './package-permissions.js';
7
8
  export * from './principals.js';
@@ -0,0 +1,13 @@
1
+ import type { TaAccount, TaSignInProvider } from '@celljs/ta-protocol';
2
+ import type { SignInFlow } from '../sign-in/sign-in-flow.js';
3
+ import { TaMemberAccounts } from './team-protocol.js';
4
+ /** Who one asking to come in is, by an account of the providers ta serve signs in with (TaSignIn): nothing let in by it. */
5
+ export declare class DefaultTaMemberAccounts implements TaMemberAccounts {
6
+ protected readonly flow: SignInFlow;
7
+ providers(): TaSignInProvider[];
8
+ start(provider: string, redirectUri: string): Promise<{
9
+ url: string;
10
+ state: string;
11
+ }>;
12
+ verify(state: string, code: string): Promise<TaAccount>;
13
+ }