@celljs/ta-web 4.20.0 → 4.21.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 (75) hide show
  1. package/esm/browser/account/device-account.d.ts +65 -1
  2. package/esm/browser/account/device-account.js +206 -6
  3. package/esm/browser/assistant/call-turn-end.d.ts +55 -0
  4. package/esm/browser/assistant/call-turn-end.js +68 -0
  5. package/esm/browser/assistant/realtime-voice.js +3 -3
  6. package/esm/browser/assistant/voice-call.d.ts +19 -28
  7. package/esm/browser/assistant/voice-call.js +64 -51
  8. package/esm/browser/components/account-menu.d.ts +9 -4
  9. package/esm/browser/components/account-menu.js +59 -22
  10. package/esm/browser/components/account-view.d.ts +33 -7
  11. package/esm/browser/components/account-view.js +140 -79
  12. package/esm/browser/components/cloud-sign-in-view.d.ts +1 -1
  13. package/esm/browser/components/cloud-sign-in-view.js +4 -1
  14. package/esm/browser/components/host-view.d.ts +15 -8
  15. package/esm/browser/components/host-view.js +23 -70
  16. package/esm/browser/components/me-view.d.ts +2 -2
  17. package/esm/browser/components/me-view.js +7 -6
  18. package/esm/browser/components/profile-view.d.ts +11 -0
  19. package/esm/browser/components/profile-view.js +85 -0
  20. package/esm/browser/components/workbench-view.js +42 -7
  21. package/esm/browser/extension/web-api-components.d.ts +2 -2
  22. package/esm/browser/i18n/locales.d.ts +12 -1
  23. package/esm/browser/i18n/messages-ja.js +15 -4
  24. package/esm/browser/i18n/messages-zh-tw.js +15 -4
  25. package/esm/browser/i18n/messages.d.ts +11 -0
  26. package/esm/browser/i18n/messages.js +30 -8
  27. package/esm/browser/style/conversation-column.d.ts +1 -1
  28. package/esm/browser/style/conversation-column.js +2 -1
  29. package/esm/browser/style/dialogs.d.ts +1 -1
  30. package/esm/browser/style/dialogs.js +6 -0
  31. package/esm/browser/style/pages.d.ts +1 -1
  32. package/esm/browser/style/pages.js +6 -0
  33. package/esm/browser/util/turn-end.d.ts +8 -0
  34. package/esm/browser/util/turn-end.js +78 -35
  35. package/esm/browser/workbench-hosts.d.ts +4 -1
  36. package/esm/browser/workbench-hosts.js +17 -0
  37. package/esm/browser/workbench-protocol.d.ts +8 -0
  38. package/lib/browser/account/device-account.d.ts +65 -1
  39. package/lib/browser/account/device-account.js +206 -6
  40. package/lib/browser/assistant/call-turn-end.d.ts +56 -0
  41. package/lib/browser/assistant/call-turn-end.js +72 -0
  42. package/lib/browser/assistant/realtime-voice.js +3 -3
  43. package/lib/browser/assistant/voice-call.d.ts +19 -28
  44. package/lib/browser/assistant/voice-call.js +67 -54
  45. package/lib/browser/components/account-menu.d.ts +9 -4
  46. package/lib/browser/components/account-menu.js +57 -20
  47. package/lib/browser/components/account-view.d.ts +33 -7
  48. package/lib/browser/components/account-view.js +142 -77
  49. package/lib/browser/components/cloud-sign-in-view.d.ts +1 -1
  50. package/lib/browser/components/cloud-sign-in-view.js +4 -1
  51. package/lib/browser/components/host-view.d.ts +15 -8
  52. package/lib/browser/components/host-view.js +22 -68
  53. package/lib/browser/components/me-view.d.ts +2 -2
  54. package/lib/browser/components/me-view.js +7 -6
  55. package/lib/browser/components/profile-view.d.ts +12 -0
  56. package/lib/browser/components/profile-view.js +123 -0
  57. package/lib/browser/components/workbench-view.js +39 -4
  58. package/lib/browser/extension/web-api-components.d.ts +2 -2
  59. package/lib/browser/i18n/locales.d.ts +12 -1
  60. package/lib/browser/i18n/messages-ja.js +15 -4
  61. package/lib/browser/i18n/messages-zh-tw.js +15 -4
  62. package/lib/browser/i18n/messages.d.ts +11 -0
  63. package/lib/browser/i18n/messages.js +30 -8
  64. package/lib/browser/style/conversation-column.d.ts +1 -1
  65. package/lib/browser/style/conversation-column.js +2 -1
  66. package/lib/browser/style/dialogs.d.ts +1 -1
  67. package/lib/browser/style/dialogs.js +6 -0
  68. package/lib/browser/style/pages.d.ts +1 -1
  69. package/lib/browser/style/pages.js +6 -0
  70. package/lib/browser/util/turn-end.d.ts +8 -0
  71. package/lib/browser/util/turn-end.js +78 -35
  72. package/lib/browser/workbench-hosts.d.ts +4 -1
  73. package/lib/browser/workbench-hosts.js +17 -0
  74. package/lib/browser/workbench-protocol.d.ts +8 -0
  75. package/package.json +7 -7
@@ -5,42 +5,46 @@
5
5
  */
6
6
  export var TurnEnd;
7
7
  (function (TurnEnd) {
8
+ /** Sounds of someone who thinks, alone in a sentence: no words, and the user is to say more. */
9
+ const HESITATIONS = ['呃', '额', '啊', '哎', '诶', '欸', '唔', 'えっと', 'えーと', 'あの', 'あのー', 'um', 'umm', 'uh', 'uhh', 'er', 'erm', 'hmm', 'ah'];
10
+ /** Sounds of someone who listens or agrees, alone in a sentence: no words, though they may be an answer ("嗯"). */
11
+ const BACKCHANNELS = ['嗯', '嗯嗯', '哦', '噢', '哦哦', '对对', '对对对', '好好', 'うん', 'ええ', 'はい', 'mm', 'mhm', 'uh-huh', 'oh'];
8
12
  /** Sounds of someone who thinks or listens, alone in a sentence: no words. */
9
- const FILLERS = [
10
- '嗯',
11
- '呃',
12
- '额',
13
- '啊',
14
- '哦',
15
- '噢',
16
- '唔',
17
- '哎',
18
- '诶',
19
- '欸',
20
- '嗯嗯',
21
- '哦哦',
22
- '对对',
23
- '对对对',
24
- '好好',
25
- 'えっと',
26
- 'えーと',
27
- 'あの',
28
- 'あのー',
29
- 'うん',
30
- 'ええ',
31
- 'はい',
32
- 'um',
33
- 'umm',
34
- 'uh',
35
- 'uhh',
36
- 'er',
37
- 'erm',
38
- 'hmm',
39
- 'mm',
40
- 'mhm',
41
- 'uh-huh',
42
- 'ah',
43
- 'oh',
13
+ const FILLERS = [...HESITATIONS, ...BACKCHANNELS];
14
+ /** Words one begins with while thinking ("那个…", "就是…"): alone, nothing is said yet. */
15
+ const OPENERS = [
16
+ '那个',
17
+ '这个',
18
+ '就是',
19
+ '然后',
20
+ '还有',
21
+ '而且',
22
+ '但是',
23
+ '可是',
24
+ '不过',
25
+ '因为',
26
+ '所以',
27
+ '如果',
28
+ '或者',
29
+ '比如',
30
+ '那',
31
+ '就',
32
+ '那個',
33
+ '這個',
34
+ '還有',
35
+ '因為',
36
+ '比如說',
37
+ 'それから',
38
+ 'でも',
39
+ 'だから',
40
+ 'けど',
41
+ 'あと',
42
+ 'and',
43
+ 'but',
44
+ 'so',
45
+ 'because',
46
+ 'like',
47
+ 'then',
44
48
  ];
45
49
  /** Words at the end of what was said that lead on to more: the user is not done. */
46
50
  const LEADING = [
@@ -119,6 +123,45 @@ export var TurnEnd;
119
123
  return FILLERS.some(filler => bare(filler) === letters) ? 0 : letters.length;
120
124
  }
121
125
  TurnEnd.words = words;
126
+ /** The sounds of the list, by their letters, longest first: a sentence of them is taken apart greedily. */
127
+ const SOUNDS = [
128
+ ...HESITATIONS.map(word => [word, 'hesitation']),
129
+ ...OPENERS.map(word => [word, 'hesitation']),
130
+ ...BACKCHANNELS.map(word => [word, 'backchannel']),
131
+ ]
132
+ .map(([word, kind]) => [bare(word), kind])
133
+ .sort((a, b) => b[0].length - a[0].length);
134
+ /**
135
+ * Whether what was said holds sounds alone and no words yet ("哎", "嗯,那个", "uh-huh"): a hesitation when any of
136
+ * them is of someone who thinks, else a backchannel; undefined when it holds words, or nothing, or asks ("啊?",
137
+ * "and then?").
138
+ */
139
+ function sounds(text) {
140
+ if (/[??]\s*$/u.test(text)) {
141
+ return undefined;
142
+ }
143
+ // "uh-huh" is one sound; words of Latin letters are whole words, those of other scripts run together.
144
+ const chunks = text
145
+ .toLowerCase()
146
+ .replace(/(\p{L})-(\p{L})/gu, '$1$2')
147
+ .split(/[^\p{L}\p{N}]+/u)
148
+ .filter(Boolean);
149
+ let kind;
150
+ for (const chunk of chunks) {
151
+ let rest = chunk;
152
+ while (rest) {
153
+ const latin = /^[a-z]+$/.test(rest);
154
+ const found = SOUNDS.find(([word]) => (latin ? rest === word : rest.startsWith(word)));
155
+ if (!found) {
156
+ return undefined;
157
+ }
158
+ kind = kind === 'hesitation' ? kind : found[1];
159
+ rest = rest.slice(found[0].length);
160
+ }
161
+ }
162
+ return kind;
163
+ }
164
+ TurnEnd.sounds = sounds;
122
165
  /** It ends as a sentence does: a full stop, a question or an exclamation mark. */
123
166
  function ended(text) {
124
167
  const trimmed = text.trim().replace(/["'”’」』))]+$/u, '');
@@ -2,7 +2,7 @@ import type { TaConnection } from '@celljs/ta-client';
2
2
  import type { TaServerInfo } from '@celljs/ta-protocol';
3
3
  import { type HostList } from './hosts.js';
4
4
  import { WorkbenchLists } from './workbench-lists.js';
5
- import type { Credential } from './workbench-protocol.js';
5
+ import type { Credential, SavedHost } from './workbench-protocol.js';
6
6
  /** The computers this device signs in to: which is in use, and what is kept of each. */
7
7
  export declare abstract class WorkbenchHosts extends WorkbenchLists {
8
8
  protected list: HostList;
@@ -16,8 +16,11 @@ export declare abstract class WorkbenchHosts extends WorkbenchLists {
16
16
  * used at once when none is.
17
17
  */
18
18
  addAccountHost(link: string, account: string): Promise<void>;
19
+ markAccountHost(hostId: string, account: string): void;
19
20
  /** The computers the account brought: those of `account`, or all of them when the device left the account. */
20
21
  forgetAccountHosts(account?: string): void;
22
+ parkHosts(ids: string[]): SavedHost[];
23
+ restoreHosts(hosts: SavedHost[]): void;
21
24
  switchTo(hostId: string): void;
22
25
  forget(hostId: string): void;
23
26
  rename(hostId: string, name: string): void;
@@ -30,6 +30,10 @@ export class WorkbenchHosts extends WorkbenchLists {
30
30
  * used at once when none is.
31
31
  */
32
32
  async addAccountHost(link, account) {
33
+ // One reached directly already (a Ta in the cloud, by its gateway): not a second entry through the relay.
34
+ if (this.list.hosts.some(one => one.account === account && 'token' in one.credential)) {
35
+ return;
36
+ }
33
37
  const credential = WorkbenchUtil.credential(link);
34
38
  const id = WorkbenchUtil.serverOf(credential, pageHost());
35
39
  if (this.current.status === 'signed-out' && !this.list.current) {
@@ -39,12 +43,25 @@ export class WorkbenchHosts extends WorkbenchLists {
39
43
  }
40
44
  this.keepHosts(HostUtil.keep(this.list, id, credential, account, Date.now()));
41
45
  }
46
+ markAccountHost(hostId, account) {
47
+ this.keepHosts({ ...this.list, hosts: this.list.hosts.map(one => (one.id === hostId ? { ...one, account } : one)) });
48
+ }
42
49
  /** The computers the account brought: those of `account`, or all of them when the device left the account. */
43
50
  forgetAccountHosts(account) {
44
51
  for (const host of this.list.hosts.filter(one => one.account && (account === undefined || one.account === account))) {
45
52
  this.forget(host.id);
46
53
  }
47
54
  }
55
+ parkHosts(ids) {
56
+ const parked = this.list.hosts.filter(one => ids.includes(one.id));
57
+ const current = this.list.current && ids.includes(this.list.current) ? undefined : this.list.current;
58
+ this.keepHosts({ ...(current ? { current } : {}), hosts: this.list.hosts.filter(one => !ids.includes(one.id)) });
59
+ return parked;
60
+ }
61
+ restoreHosts(hosts) {
62
+ const ids = new Set(hosts.map(one => one.id));
63
+ this.keepHosts({ ...(this.list.current ? { current: this.list.current } : {}), hosts: [...this.list.hosts.filter(one => !ids.has(one.id)), ...hosts] });
64
+ }
48
65
  switchTo(hostId) {
49
66
  const host = this.list.hosts.find(one => one.id === hostId);
50
67
  if (!host || (hostId === this.current.host && this.current.status !== 'signed-out')) {
@@ -132,8 +132,16 @@ export interface TaWorkbench {
132
132
  rename(hostId: string, name: string): void;
133
133
  /** A way into a computer of the user's account (docs/design/ta-account.md): kept, used at once when none is. */
134
134
  addAccountHost(link: string, account: string): Promise<void>;
135
+ /** A computer this device brought into its account (docs/design/ta-me.md 4) is its device `account` there. */
136
+ markAccountHost(hostId: string, account: string): void;
135
137
  /** The computers the account brought: those of `account`, or all when the device left the account. */
136
138
  forgetAccountHosts(account?: string): void;
139
+ /**
140
+ * Takes the computers of an account the user switched away from off the list, what the page kept of them kept on the
141
+ * device (docs/design/ta-me.md 4): back as they were with restoreHosts.
142
+ */
143
+ parkHosts(ids: string[]): SavedHost[];
144
+ restoreHosts(hosts: SavedHost[]): void;
137
145
  /** Forgets the computer in use and loads the page again, at the sign-in, with the other computers kept. */
138
146
  signOut(): void;
139
147
  /**
@@ -1,4 +1,4 @@
1
- import type { TaAccountInfo } from '@celljs/ta-protocol';
1
+ import type { TaAccountInfo, TaAccountServer } from '@celljs/ta-protocol';
2
2
  import { AccountAgent, type AccountKept, type AccountView, type AccountWay } from '@celljs/ta-relay';
3
3
  import type { SavedHost } from '../workbench-protocol';
4
4
  /** What a page does about the account: of its ta serve (a computer), or of the device itself (the app). */
@@ -14,9 +14,27 @@ export interface AccountSide {
14
14
  export interface AccountHosts {
15
15
  readonly state: {
16
16
  hosts: SavedHost[];
17
+ host?: string;
18
+ status: string;
19
+ switching?: string;
17
20
  };
21
+ /** The account of the computer in use, through its ta serve. */
22
+ readonly account: TaAccountServer;
23
+ subscribe(listener: () => void): () => void;
18
24
  addAccountHost(link: string, account: string): Promise<void>;
19
25
  forgetAccountHosts(account?: string): void;
26
+ markAccountHost(hostId: string, account: string): void;
27
+ forget(hostId: string): void;
28
+ switchTo(hostId: string): void;
29
+ parkHosts(ids: string[]): SavedHost[];
30
+ restoreHosts(hosts: SavedHost[]): void;
31
+ switchSignIn(): void;
32
+ }
33
+ /** An account this device switched away from (docs/design/ta-me.md 4): back in one tap, with its computers. */
34
+ export interface RecentAccount {
35
+ kept: AccountKept;
36
+ hosts: SavedHost[];
37
+ at: number;
20
38
  }
21
39
  /**
22
40
  * The app as a device of the user's account (docs/design/ta-account.md): its own keys in localStorage; the computers
@@ -42,12 +60,58 @@ export declare class DeviceAccount implements AccountSide {
42
60
  protected timer?: ReturnType<typeof setTimeout>;
43
61
  protected running?: Promise<void>;
44
62
  protected constructor(hosts: AccountHosts, service: () => Promise<string>, gateways: () => Promise<string[]>);
63
+ /** The computers to bring into the account, and the devices of it this device lets in once they ask. */
64
+ protected static joining(): {
65
+ hosts: string[];
66
+ devices: string[];
67
+ };
68
+ protected static keepJoining(next: {
69
+ hosts: string[];
70
+ devices: string[];
71
+ }): void;
72
+ /**
73
+ * A computer connected by its code while this device is in an account (docs/design/ta-me.md 4): it joins the account
74
+ * once connected, with a token of the account this device gets for it, and this device lets it in. Being by the
75
+ * computer to scan its code is as good as tapping Allow.
76
+ */
77
+ bringIn(hostId: string): void;
78
+ protected bringing: boolean;
79
+ /** The computers tried once in this page: one that would not join is not asked again every round. */
80
+ protected readonly tried: Set<string>;
81
+ /** The Ta in the cloud of the account's gateway, in the account too (docs/design/ta-me.md 2): brought in as a scanned computer. */
82
+ protected bringCloud(): void;
83
+ /** The computer in use, if it waits to be brought into the account: asked to join it. */
84
+ protected bringPending(): Promise<void>;
85
+ /** The computers this device brought in, let in as soon as they ask. */
86
+ protected letInBrought(view: AccountView): Promise<AccountView>;
87
+ /** The one of the page, once made. */
88
+ static current(): DeviceAccount | undefined;
45
89
  /** The one of the page. */
46
90
  static of(hosts: AccountHosts, service: () => Promise<string>, gateways: () => Promise<string[]>): DeviceAccount;
47
91
  /** What the device calls itself in the account. */
48
92
  static deviceName(agent?: string): string;
49
93
  protected static read(): AccountKept | undefined;
50
94
  protected agentOf(kept: AccountKept): AccountAgent;
95
+ /** The address of the gateway of a Ta in the cloud kept, as the account service it is. */
96
+ protected static gatewayOf(host: SavedHost): string | undefined;
97
+ /** The computers the account brought: those it paired, and the Ta in the cloud of its gateway (a company's account). */
98
+ protected ownHosts(): SavedHost[];
99
+ /** The accounts switched away from, the latest first. */
100
+ static recent(): RecentAccount[];
101
+ protected static keepRecent(recent: RecentAccount[]): void;
102
+ /** The account in use put aside, its computers with it, for another (WeChat's switching of accounts). */
103
+ protected putAside(): void;
104
+ /** Back to the sign-in, the account in use kept among those signed in to lately. */
105
+ switchAccount(): void;
106
+ /** An account switched away from, in use again, with its computers: the one of them used last opens. */
107
+ resume(account: string): void;
108
+ /** An account switched away from, no longer offered. */
109
+ forgetRecent(account: string): void;
110
+ /**
111
+ * Signed in to a Ta in the cloud: its gateway's account is this device's from now on, one account at a time
112
+ * (docs/design/ta-me.md 2); another in use is put aside, to come back to.
113
+ */
114
+ cloudSignedIn(gateway: string): void;
51
115
  /** A Ta in the cloud signed in to: its token is the account of its gateway, which this device joins by itself. */
52
116
  protected cloudToken(): {
53
117
  service: string;
@@ -6,6 +6,11 @@ const workbench_util_1 = require("../workbench-util");
6
6
  const KEY = 'ta.account';
7
7
  /** Signed out of the account by the user: not signed in again by itself with the token of a Ta in the cloud. */
8
8
  const OFF_KEY = 'ta.account.off';
9
+ /** The accounts switched away from, the latest first. */
10
+ const RECENT_KEY = 'ta.account.recent';
11
+ const MAX_RECENT = 5;
12
+ /** The computers connected by their code while signed in, to bring into the account once connected: by host id. */
13
+ const JOINING_KEY = 'ta.account.joining';
9
14
  const SYNC_MS = 15000;
10
15
  const WAITING_SYNC_MS = 3000;
11
16
  /**
@@ -18,12 +23,111 @@ class DeviceAccount {
18
23
  this.hosts = hosts;
19
24
  this.service = service;
20
25
  this.gateways = gateways;
26
+ this.bringing = false;
27
+ /** The computers tried once in this page: one that would not join is not asked again every round. */
28
+ this.tried = new Set();
21
29
  const kept = DeviceAccount.read();
22
30
  if (kept) {
23
31
  this.agent = this.agentOf(kept);
24
32
  }
25
33
  this.schedule(0);
26
34
  document.addEventListener('visibilitychange', () => document.visibilityState === 'visible' && this.schedule(0));
35
+ hosts.subscribe(() => void this.bringPending());
36
+ }
37
+ /** The computers to bring into the account, and the devices of it this device lets in once they ask. */
38
+ static joining() {
39
+ var _a;
40
+ var _b, _c, _d;
41
+ try {
42
+ const kept = JSON.parse((_b = (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.getItem(JOINING_KEY)) !== null && _b !== void 0 ? _b : '{}');
43
+ return { hosts: (_c = kept.hosts) !== null && _c !== void 0 ? _c : [], devices: (_d = kept.devices) !== null && _d !== void 0 ? _d : [] };
44
+ }
45
+ catch (_e) {
46
+ return { hosts: [], devices: [] };
47
+ }
48
+ }
49
+ static keepJoining(next) {
50
+ var _a;
51
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(JOINING_KEY, JSON.stringify({ hosts: next.hosts.slice(-10), devices: next.devices.slice(-10) }));
52
+ }
53
+ /**
54
+ * A computer connected by its code while this device is in an account (docs/design/ta-me.md 4): it joins the account
55
+ * once connected, with a token of the account this device gets for it, and this device lets it in. Being by the
56
+ * computer to scan its code is as good as tapping Allow.
57
+ */
58
+ bringIn(hostId) {
59
+ var _a;
60
+ if (!this.agent || this.tried.has(hostId) || ((_a = this.hosts.state.hosts.find(one => one.id === hostId)) === null || _a === void 0 ? void 0 : _a.account)) {
61
+ return;
62
+ }
63
+ const joining = DeviceAccount.joining();
64
+ DeviceAccount.keepJoining({ ...joining, hosts: [...joining.hosts.filter(one => one !== hostId), hostId] });
65
+ void this.bringPending();
66
+ }
67
+ /** The Ta in the cloud of the account's gateway, in the account too (docs/design/ta-me.md 2): brought in as a scanned computer. */
68
+ bringCloud() {
69
+ var _a;
70
+ const service = (_a = this.agent) === null || _a === void 0 ? void 0 : _a.state.service;
71
+ for (const host of this.hosts.state.hosts) {
72
+ if (service && !host.account && DeviceAccount.gatewayOf(host) === service) {
73
+ this.bringIn(host.id);
74
+ }
75
+ }
76
+ }
77
+ /** The computer in use, if it waits to be brought into the account: asked to join it. */
78
+ async bringPending() {
79
+ // While switching, the connection is still of the computer before: not the one to bring in.
80
+ const { host, status, switching } = this.hosts.state;
81
+ const joining = DeviceAccount.joining();
82
+ const agent = this.agent;
83
+ if (this.bringing || switching || !agent || !host || status !== 'online' || !joining.hosts.includes(host) || !this.view || this.view.waiting) {
84
+ return;
85
+ }
86
+ this.bringing = true;
87
+ const done = (devices) => DeviceAccount.keepJoining({ hosts: joining.hosts.filter(one => one !== host), devices });
88
+ this.tried.add(host);
89
+ try {
90
+ const saved = this.hosts.state.hosts.find(one => one.id === host);
91
+ const cloud = saved !== undefined && DeviceAccount.gatewayOf(saved) !== undefined;
92
+ const { token } = await agent.invite();
93
+ const info = await this.hosts.account.joinAccount(agent.state.service, token);
94
+ const self = info.devices.find(one => one.self);
95
+ if (cloud && (self === null || self === void 0 ? void 0 : self.kind) !== 'cloud') {
96
+ // A Ta in the cloud its gateway did not say is one (a gateway of before): out again, never a computer.
97
+ await this.hosts.account.signOut();
98
+ done(joining.devices);
99
+ return;
100
+ }
101
+ if (self) {
102
+ this.hosts.markAccountHost(host, self.id);
103
+ }
104
+ done(self && info.waiting ? [...joining.devices, self.id] : joining.devices);
105
+ this.schedule(0);
106
+ }
107
+ catch (_a) {
108
+ // Signed in to another account there, a ta serve of before, or a member who may not: it stays this device's.
109
+ done(joining.devices);
110
+ }
111
+ finally {
112
+ this.bringing = false;
113
+ }
114
+ }
115
+ /** The computers this device brought in, let in as soon as they ask. */
116
+ async letInBrought(view) {
117
+ const joining = DeviceAccount.joining();
118
+ const asking = view.devices.filter(one => one.waiting && joining.devices.includes(one.id));
119
+ let next = view;
120
+ for (const device of asking) {
121
+ next = await this.agent.approve(device.id);
122
+ }
123
+ if (asking.length > 0) {
124
+ DeviceAccount.keepJoining({ ...joining, devices: joining.devices.filter(one => !asking.some(device => device.id === one)) });
125
+ }
126
+ return next;
127
+ }
128
+ /** The one of the page, once made. */
129
+ static current() {
130
+ return DeviceAccount.instance;
27
131
  }
28
132
  /** The one of the page. */
29
133
  static of(hosts, service, gateways) {
@@ -60,14 +164,97 @@ class DeviceAccount {
60
164
  },
61
165
  });
62
166
  }
167
+ /** The address of the gateway of a Ta in the cloud kept, as the account service it is. */
168
+ static gatewayOf(host) {
169
+ return 'token' in host.credential && host.credential.url ? host.credential.url.replace(/\/+$/, '') : undefined;
170
+ }
171
+ /** The computers the account brought: those it paired, and the Ta in the cloud of its gateway (a company's account). */
172
+ ownHosts() {
173
+ var _a;
174
+ const service = (_a = this.agent) === null || _a === void 0 ? void 0 : _a.state.service;
175
+ return this.hosts.state.hosts.filter(one => one.account || (service !== undefined && DeviceAccount.gatewayOf(one) === service));
176
+ }
177
+ /** The accounts switched away from, the latest first. */
178
+ static recent() {
179
+ var _a;
180
+ var _b;
181
+ try {
182
+ const kept = JSON.parse((_b = (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.getItem(RECENT_KEY)) !== null && _b !== void 0 ? _b : '[]');
183
+ return Array.isArray(kept) ? kept.filter(one => { var _a, _b; return ((_a = one === null || one === void 0 ? void 0 : one.kept) === null || _a === void 0 ? void 0 : _a.token) && ((_b = one.kept.self) === null || _b === void 0 ? void 0 : _b.signKey); }) : [];
184
+ }
185
+ catch (_c) {
186
+ return [];
187
+ }
188
+ }
189
+ static keepRecent(recent) {
190
+ var _a;
191
+ try {
192
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(RECENT_KEY, JSON.stringify(recent.slice(0, MAX_RECENT)));
193
+ }
194
+ catch (_b) {
195
+ // Full: not offered again.
196
+ }
197
+ }
198
+ /** The account in use put aside, its computers with it, for another (WeChat's switching of accounts). */
199
+ putAside() {
200
+ var _a, _b;
201
+ if (!this.agent) {
202
+ return;
203
+ }
204
+ const kept = this.agent.state;
205
+ const hosts = this.hosts.parkHosts(this.ownHosts().map(one => one.id));
206
+ DeviceAccount.keepRecent([{ kept, hosts, at: Date.now() }, ...DeviceAccount.recent().filter(one => one.kept.account.id !== kept.account.id)]);
207
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.removeItem(KEY);
208
+ (_b = (0, workbench_util_1.storage)()) === null || _b === void 0 ? void 0 : _b.setItem(OFF_KEY, '1');
209
+ this.agent = undefined;
210
+ this.view = undefined;
211
+ this.error = undefined;
212
+ }
213
+ /** Back to the sign-in, the account in use kept among those signed in to lately. */
214
+ switchAccount() {
215
+ this.putAside();
216
+ this.hosts.switchSignIn();
217
+ }
218
+ /** An account switched away from, in use again, with its computers: the one of them used last opens. */
219
+ resume(account) {
220
+ var _a, _b;
221
+ const record = DeviceAccount.recent().find(one => one.kept.account.id === account);
222
+ if (!record) {
223
+ return;
224
+ }
225
+ this.putAside();
226
+ DeviceAccount.keepRecent(DeviceAccount.recent().filter(one => one.kept.account.id !== account));
227
+ this.hosts.restoreHosts(record.hosts);
228
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.setItem(KEY, JSON.stringify(record.kept));
229
+ (_b = (0, workbench_util_1.storage)()) === null || _b === void 0 ? void 0 : _b.removeItem(OFF_KEY);
230
+ this.agent = this.agentOf(record.kept);
231
+ this.schedule(0);
232
+ const latest = [...record.hosts].sort((a, b) => b.usedAt - a.usedAt)[0];
233
+ if (latest) {
234
+ this.hosts.switchTo(latest.id);
235
+ }
236
+ }
237
+ /** An account switched away from, no longer offered. */
238
+ forgetRecent(account) {
239
+ DeviceAccount.keepRecent(DeviceAccount.recent().filter(one => one.kept.account.id !== account));
240
+ }
241
+ /**
242
+ * Signed in to a Ta in the cloud: its gateway's account is this device's from now on, one account at a time
243
+ * (docs/design/ta-me.md 2); another in use is put aside, to come back to.
244
+ */
245
+ cloudSignedIn(gateway) {
246
+ var _a;
247
+ const service = gateway.replace(/\/+$/, '');
248
+ if (this.agent && this.agent.state.service !== service) {
249
+ this.putAside();
250
+ }
251
+ (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.removeItem(OFF_KEY);
252
+ this.schedule(0);
253
+ }
63
254
  /** A Ta in the cloud signed in to: its token is the account of its gateway, which this device joins by itself. */
64
255
  cloudToken() {
65
- for (const host of this.hosts.state.hosts) {
66
- if ('token' in host.credential && host.credential.url) {
67
- return { service: host.credential.url.replace(/\/+$/, ''), token: host.credential.token };
68
- }
69
- }
70
- return undefined;
256
+ const host = [...this.hosts.state.hosts].sort((a, b) => b.usedAt - a.usedAt).find(one => DeviceAccount.gatewayOf(one));
257
+ return host && 'token' in host.credential ? { service: DeviceAccount.gatewayOf(host), token: host.credential.token } : undefined;
71
258
  }
72
259
  /** Joins the account of the gateway of a Ta in the cloud of this device, unless the user left the account. */
73
260
  async adopt() {
@@ -83,6 +270,10 @@ class DeviceAccount {
83
270
  }
84
271
  forget() {
85
272
  var _a;
273
+ // The Ta in the cloud of a company's account goes with it, as the computers it paired.
274
+ for (const host of this.ownHosts().filter(one => !one.account)) {
275
+ this.hosts.forget(host.id);
276
+ }
86
277
  (_a = (0, workbench_util_1.storage)()) === null || _a === void 0 ? void 0 : _a.removeItem(KEY);
87
278
  this.agent = undefined;
88
279
  this.view = undefined;
@@ -106,7 +297,12 @@ class DeviceAccount {
106
297
  else if (this.agent || (await this.adopt())) {
107
298
  const agent = this.agent;
108
299
  this.view = await agent.sync();
300
+ if (!this.view.waiting) {
301
+ this.bringCloud();
302
+ this.view = await this.letInBrought(this.view);
303
+ }
109
304
  this.error = undefined;
305
+ void this.bringPending();
110
306
  if (this.view.waiting || this.view.devices.some(one => one.waiting)) {
111
307
  next = WAITING_SYNC_MS;
112
308
  }
@@ -145,6 +341,10 @@ class DeviceAccount {
145
341
  (_b = (0, workbench_util_1.storage)()) === null || _b === void 0 ? void 0 : _b.removeItem(OFF_KEY);
146
342
  this.agent = this.agentOf(kept);
147
343
  this.view = await this.agent.sync();
344
+ // The computers connected by their code before join it too, each once it is used.
345
+ const joining = DeviceAccount.joining();
346
+ const scanned = this.hosts.state.hosts.filter(one => 'pairing' in one.credential && !one.account).map(one => one.id);
347
+ DeviceAccount.keepJoining({ ...joining, hosts: [...new Set([...joining.hosts, ...scanned])] });
148
348
  return WAITING_SYNC_MS;
149
349
  }
150
350
  async read() {
@@ -0,0 +1,56 @@
1
+ /** Listens heard nothing this many times in a row, and for SILENT_MS: nobody is there, the call ends. */
2
+ export declare const SILENT_ROUNDS = 4;
3
+ /**
4
+ * How long nobody spoke, the user nor Ta, before the call ends by itself: a recognizer ends a listen after a few
5
+ * seconds of silence, and a user who waits for the work to answer is still there.
6
+ */
7
+ export declare const SILENT_MS: number;
8
+ /** After a failure of the recognizer (not a denial), before it listens again. */
9
+ export declare const RETRY_MS = 600;
10
+ /**
11
+ * The words heard stay the same this long: the user is done, though the recognizer goes on (it hears voices around them
12
+ * and waits for a silence that never comes).
13
+ */
14
+ export declare const END_MS = 1300;
15
+ /**
16
+ * What was heard holds sounds alone ("哎", "嗯", "那个", TurnEnd.sounds): Ta waits this long without new words before it
17
+ * takes the user to be done, and what they say meanwhile, in this listen or the next, goes with it as one sentence. Then
18
+ * a hesitation is dropped and a backchannel ("嗯", "好好", an answer maybe) goes as said.
19
+ */
20
+ export declare const FILLER_MS = 3000;
21
+ /** At most this long a turn, from the first words: talk around the user that keeps the words coming ends there too. */
22
+ export declare const TURN_MS = 15000;
23
+ /** How long a call waits for the user, as CallOptions says. */
24
+ export interface CallTurnTimes {
25
+ /** How long the words heard stay the same before the user is taken to be done; default END_MS. */
26
+ endMs?: number;
27
+ /** How long sounds alone ("哎", "嗯", "那个") wait for words to follow; default FILLER_MS. */
28
+ fillerMs?: number;
29
+ /** How long the user may speak in one turn; default TURN_MS. */
30
+ turnMs?: number;
31
+ }
32
+ /**
33
+ * When the user is done with their turn in a call, by the words heard as they come (TurnEnd): once they stayed the same
34
+ * a while (END_MS, sooner after a sentence, later while they hesitate, FILLER_MS for sounds alone), and at the latest
35
+ * TURN_MS from the first words. It calls `done` then.
36
+ */
37
+ export declare class CallTurnEnd {
38
+ protected readonly times: CallTurnTimes;
39
+ protected readonly done: () => void;
40
+ protected endTimer?: ReturnType<typeof setTimeout>;
41
+ protected turnTimer?: ReturnType<typeof setTimeout>;
42
+ /** When the words heard last changed: sounds alone wait FILLER_MS from it, over listens. */
43
+ protected wordsAt: number;
44
+ constructor(times: CallTurnTimes, done: () => void);
45
+ /** The words heard are `text` now. */
46
+ heard(text: string): void;
47
+ /** A listen goes on from sounds alone the one before ended on: done once they waited FILLER_MS, unless words come. */
48
+ hold(): void;
49
+ /** `said` is sounds alone that did not wait FILLER_MS yet: they wait on for words. */
50
+ waits(said: string): boolean;
51
+ stop(): void;
52
+ protected get fillerMs(): number;
53
+ /** How long the words heard should stay the same before the user is taken to be done. */
54
+ protected waitMs(text: string): number;
55
+ }
56
+ //# sourceMappingURL=call-turn-end.d.ts.map