@celljs/ta-host 4.18.0 → 4.19.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.
- package/esm/node/account-service.d.ts +73 -0
- package/esm/node/account-service.js +304 -0
- package/esm/node/host-protocol.d.ts +2 -0
- package/esm/node/host-protocol.js +2 -0
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/package-hosts.d.ts +2 -0
- package/esm/node/package-hosts.js +12 -1
- package/esm/node/relay-bridge.js +1 -1
- package/esm/node/team/team-calls.js +8 -0
- package/lib/node/account-service.d.ts +74 -0
- package/lib/node/account-service.js +307 -0
- package/lib/node/host-protocol.d.ts +2 -0
- package/lib/node/host-protocol.js +3 -1
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/package-hosts.d.ts +2 -0
- package/lib/node/package-hosts.js +11 -0
- package/lib/node/relay-bridge.js +1 -1
- package/lib/node/team/team-calls.js +8 -0
- package/package.json +10 -10
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type Application, ApplicationLifecycle } from '@celljs/core';
|
|
2
|
+
import { type TaAccountInfo, TaAccountServer } from '@celljs/ta-protocol';
|
|
3
|
+
import { AccountAgent, type AccountKept, type AccountView, type AccountWay } from '@celljs/ta-relay';
|
|
4
|
+
import { type RelayBridge } from './relay-bridge.js';
|
|
5
|
+
/** What a package hears from the same package on another computer of the account (TaAccountService.tell). */
|
|
6
|
+
export type TaAccountListener = (from: {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
}, value: unknown) => void;
|
|
10
|
+
/**
|
|
11
|
+
* The account of this computer (docs/design/ta-account.md): signed in, it is in the user's list of devices; it lets
|
|
12
|
+
* in the devices the user lets in, pairs each with this ta serve through the relay, and unpairs those removed. What it
|
|
13
|
+
* keeps (its own keys, whom it trusts, its token) is in `account.json` of the agent directory, for the user alone.
|
|
14
|
+
*/
|
|
15
|
+
export declare class TaAccountService implements TaAccountServer, ApplicationLifecycle<Application> {
|
|
16
|
+
protected readonly bridge: RelayBridge;
|
|
17
|
+
protected agent?: AccountAgent;
|
|
18
|
+
protected view?: AccountView;
|
|
19
|
+
protected signingIn?: {
|
|
20
|
+
way: AccountWay;
|
|
21
|
+
handle: string;
|
|
22
|
+
code: string;
|
|
23
|
+
url: string;
|
|
24
|
+
interval: number;
|
|
25
|
+
expiresAt: number;
|
|
26
|
+
};
|
|
27
|
+
protected error?: string;
|
|
28
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
29
|
+
protected running?: Promise<void>;
|
|
30
|
+
protected stopped: boolean;
|
|
31
|
+
protected readonly listeners: Map<string, Set<TaAccountListener>>;
|
|
32
|
+
get file(): string;
|
|
33
|
+
/** The name this computer goes by in the account. */
|
|
34
|
+
static computerName(): string;
|
|
35
|
+
onStart(): void;
|
|
36
|
+
onStop(): void;
|
|
37
|
+
protected read(): AccountKept | undefined;
|
|
38
|
+
protected write(kept: AccountKept): void;
|
|
39
|
+
protected service(): string;
|
|
40
|
+
/** The gateways whose accounts this computer may sign in with: of the deployment joined, and of this build. */
|
|
41
|
+
protected gateways(): string[];
|
|
42
|
+
protected agentOf(kept: AccountKept): AccountAgent;
|
|
43
|
+
/** Signed out by the service (removed, or the token no longer works): the devices it paired lose their way in. */
|
|
44
|
+
protected forget(): void;
|
|
45
|
+
protected schedule(after: number): void;
|
|
46
|
+
/** One round at a time: a sign-in polled, or the account synced. */
|
|
47
|
+
protected round(): Promise<void>;
|
|
48
|
+
protected pollSignIn(): Promise<number>;
|
|
49
|
+
getAccount(): Promise<TaAccountInfo>;
|
|
50
|
+
info(): Promise<TaAccountInfo>;
|
|
51
|
+
signIn(provider: string, service?: string): Promise<TaAccountInfo>;
|
|
52
|
+
protected begin(provider: string, service?: string): Promise<TaAccountInfo>;
|
|
53
|
+
cancelSignIn(): Promise<TaAccountInfo>;
|
|
54
|
+
protected act(run: (agent: AccountAgent) => Promise<AccountView>): Promise<TaAccountInfo>;
|
|
55
|
+
approve(id: string): Promise<TaAccountInfo>;
|
|
56
|
+
remove(id: string): Promise<TaAccountInfo>;
|
|
57
|
+
signOut(): Promise<TaAccountInfo>;
|
|
58
|
+
/** This computer in the account, once let in, for packages (TaExtensionHost.account). */
|
|
59
|
+
self(): {
|
|
60
|
+
id: string;
|
|
61
|
+
name: string;
|
|
62
|
+
} | undefined;
|
|
63
|
+
/** The other computers of the account this one trusts, for packages (TaExtensionHost.account). */
|
|
64
|
+
computers(): {
|
|
65
|
+
id: string;
|
|
66
|
+
name: string;
|
|
67
|
+
online: boolean;
|
|
68
|
+
}[];
|
|
69
|
+
/** Says `value` to a package on another computer of the account, sealed for it: it hears it on its next look. */
|
|
70
|
+
tell(to: string, topic: string, value: unknown): Promise<void>;
|
|
71
|
+
/** Hears what the same package on other computers of the account tells, by `topic`. */
|
|
72
|
+
onTold(topic: string, listener: TaAccountListener): () => void;
|
|
73
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var TaAccountService_1;
|
|
11
|
+
import { chmodSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { hostname } from 'node:os';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
import { ApplicationLifecycle, Autowired } from '@celljs/core';
|
|
15
|
+
import { Rpc } from '@celljs/rpc';
|
|
16
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
17
|
+
import { TaAccountServer } from '@celljs/ta-protocol';
|
|
18
|
+
import { AccountAgent, AccountCallError, AccountService, RelayPairingUtil, } from '@celljs/ta-relay';
|
|
19
|
+
import { TA_ACCOUNT } from './host-protocol.js';
|
|
20
|
+
import { RELAY_BRIDGE } from './relay-bridge.js';
|
|
21
|
+
import { TaErrors } from './ta-errors.js';
|
|
22
|
+
/** How often a computer says it is there and looks at its account; quicker while a device waits to be let in. */
|
|
23
|
+
const SYNC_MS = 20000;
|
|
24
|
+
const WAITING_SYNC_MS = 4000;
|
|
25
|
+
/**
|
|
26
|
+
* The account of this computer (docs/design/ta-account.md): signed in, it is in the user's list of devices; it lets
|
|
27
|
+
* in the devices the user lets in, pairs each with this ta serve through the relay, and unpairs those removed. What it
|
|
28
|
+
* keeps (its own keys, whom it trusts, its token) is in `account.json` of the agent directory, for the user alone.
|
|
29
|
+
*/
|
|
30
|
+
let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.stopped = false;
|
|
33
|
+
this.listeners = new Map();
|
|
34
|
+
}
|
|
35
|
+
get file() {
|
|
36
|
+
return join(TaBrand.agentDir(), 'account.json');
|
|
37
|
+
}
|
|
38
|
+
/** The name this computer goes by in the account. */
|
|
39
|
+
static computerName() {
|
|
40
|
+
return hostname().replace(/\.local$/, '');
|
|
41
|
+
}
|
|
42
|
+
onStart() {
|
|
43
|
+
const kept = this.read();
|
|
44
|
+
if (kept) {
|
|
45
|
+
this.agent = this.agentOf(kept);
|
|
46
|
+
this.schedule(0);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
onStop() {
|
|
50
|
+
this.stopped = true;
|
|
51
|
+
clearTimeout(this.timer);
|
|
52
|
+
}
|
|
53
|
+
read() {
|
|
54
|
+
var _a;
|
|
55
|
+
try {
|
|
56
|
+
const kept = JSON.parse(readFileSync(this.file, 'utf8'));
|
|
57
|
+
return (kept === null || kept === void 0 ? void 0 : kept.token) && ((_a = kept.self) === null || _a === void 0 ? void 0 : _a.signKey) ? kept : undefined;
|
|
58
|
+
}
|
|
59
|
+
catch (_b) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
write(kept) {
|
|
64
|
+
const temp = `${this.file}.${process.pid}.tmp`;
|
|
65
|
+
writeFileSync(temp, `${JSON.stringify(kept, undefined, 2)}\n`, { mode: 0o600 });
|
|
66
|
+
chmodSync(temp, 0o600);
|
|
67
|
+
renameSync(temp, this.file);
|
|
68
|
+
}
|
|
69
|
+
service() {
|
|
70
|
+
return this.bridge.relayFor('devices');
|
|
71
|
+
}
|
|
72
|
+
/** The gateways whose accounts this computer may sign in with: of the deployment joined, and of this build. */
|
|
73
|
+
gateways() {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
const joined = (_b = (_a = this.bridge.relays.safeRead().config) === null || _a === void 0 ? void 0 : _a.deployment) === null || _b === void 0 ? void 0 : _b.gateway;
|
|
76
|
+
return [joined, process.env.TA_GATEWAY_URL].filter((one) => !!one);
|
|
77
|
+
}
|
|
78
|
+
agentOf(kept) {
|
|
79
|
+
const store = this.bridge.store;
|
|
80
|
+
return new AccountAgent(kept, {
|
|
81
|
+
save: next => this.write(next),
|
|
82
|
+
name: () => TaAccountService_1.computerName(),
|
|
83
|
+
host: () => {
|
|
84
|
+
var _a;
|
|
85
|
+
const config = store.read();
|
|
86
|
+
return config.host ? { relay: (_a = config.url) !== null && _a !== void 0 ? _a : this.service(), id: config.host.id } : undefined;
|
|
87
|
+
},
|
|
88
|
+
pair: async (device) => {
|
|
89
|
+
const pairing = store.pair(this.service(), device.name, { account: device.id });
|
|
90
|
+
this.bridge.refresh();
|
|
91
|
+
return RelayPairingUtil.link({ ...pairing, name: TaAccountService_1.computerName() });
|
|
92
|
+
},
|
|
93
|
+
paired: () => store.read().devices.flatMap(device => (device.account ? [device.account] : [])),
|
|
94
|
+
unpair: id => {
|
|
95
|
+
for (const device of store.read().devices.filter(one => one.account === id)) {
|
|
96
|
+
store.remove(device.id);
|
|
97
|
+
}
|
|
98
|
+
this.bridge.refresh();
|
|
99
|
+
},
|
|
100
|
+
told: (from, topic, value) => {
|
|
101
|
+
var _a;
|
|
102
|
+
for (const listener of (_a = this.listeners.get(topic)) !== null && _a !== void 0 ? _a : []) {
|
|
103
|
+
try {
|
|
104
|
+
listener({ id: from.id, name: from.name }, value);
|
|
105
|
+
}
|
|
106
|
+
catch (_b) {
|
|
107
|
+
// A package's listener is its own.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
signedOut: () => this.forget(),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** Signed out by the service (removed, or the token no longer works): the devices it paired lose their way in. */
|
|
115
|
+
forget() {
|
|
116
|
+
const store = this.bridge.store;
|
|
117
|
+
for (const device of store.read().devices.filter(one => one.account)) {
|
|
118
|
+
store.remove(device.id);
|
|
119
|
+
}
|
|
120
|
+
this.bridge.refresh();
|
|
121
|
+
rmSync(this.file, { force: true });
|
|
122
|
+
this.agent = undefined;
|
|
123
|
+
this.view = undefined;
|
|
124
|
+
}
|
|
125
|
+
schedule(after) {
|
|
126
|
+
var _a, _b;
|
|
127
|
+
clearTimeout(this.timer);
|
|
128
|
+
if (this.stopped || (!this.agent && !this.signingIn)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.timer = setTimeout(() => void this.round(), after);
|
|
132
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
133
|
+
}
|
|
134
|
+
/** One round at a time: a sign-in polled, or the account synced. */
|
|
135
|
+
round() {
|
|
136
|
+
var _a;
|
|
137
|
+
(_a = this.running) !== null && _a !== void 0 ? _a : (this.running = (async () => {
|
|
138
|
+
let next = SYNC_MS;
|
|
139
|
+
try {
|
|
140
|
+
if (this.signingIn) {
|
|
141
|
+
next = await this.pollSignIn();
|
|
142
|
+
}
|
|
143
|
+
else if (this.agent) {
|
|
144
|
+
this.view = await this.agent.sync();
|
|
145
|
+
this.error = undefined;
|
|
146
|
+
if (this.view.waiting || this.view.devices.some(one => one.waiting)) {
|
|
147
|
+
next = WAITING_SYNC_MS;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
this.running = undefined;
|
|
156
|
+
}
|
|
157
|
+
this.schedule(next);
|
|
158
|
+
})());
|
|
159
|
+
return this.running;
|
|
160
|
+
}
|
|
161
|
+
async pollSignIn() {
|
|
162
|
+
const signing = this.signingIn;
|
|
163
|
+
if (Date.now() > signing.expiresAt) {
|
|
164
|
+
this.signingIn = undefined;
|
|
165
|
+
this.error = 'The sign-in ran out of time: begin it again.';
|
|
166
|
+
return SYNC_MS;
|
|
167
|
+
}
|
|
168
|
+
const answer = await AccountService.check(signing.way, signing.handle);
|
|
169
|
+
if (answer.status === 'pending') {
|
|
170
|
+
return signing.interval * 1000;
|
|
171
|
+
}
|
|
172
|
+
this.signingIn = undefined;
|
|
173
|
+
if (answer.status === 'failed') {
|
|
174
|
+
this.error = answer.error;
|
|
175
|
+
return SYNC_MS;
|
|
176
|
+
}
|
|
177
|
+
const kept = await AccountAgent.create(signing.way.service, answer.token, answer.account, 'computer', TaAccountService_1.computerName());
|
|
178
|
+
this.write(kept);
|
|
179
|
+
this.agent = this.agentOf(kept);
|
|
180
|
+
this.view = await this.agent.sync();
|
|
181
|
+
this.error = undefined;
|
|
182
|
+
return this.view.waiting ? WAITING_SYNC_MS : SYNC_MS;
|
|
183
|
+
}
|
|
184
|
+
getAccount() {
|
|
185
|
+
return TaErrors.guard(() => this.info());
|
|
186
|
+
}
|
|
187
|
+
async info() {
|
|
188
|
+
var _a, _b;
|
|
189
|
+
var _c, _d;
|
|
190
|
+
const service = (_d = (_c = (_a = this.agent) === null || _a === void 0 ? void 0 : _a.state.service) !== null && _c !== void 0 ? _c : (_b = this.signingIn) === null || _b === void 0 ? void 0 : _b.way.service) !== null && _d !== void 0 ? _d : this.service();
|
|
191
|
+
const error = this.error ? { error: this.error } : {};
|
|
192
|
+
if (!this.agent) {
|
|
193
|
+
const providers = await AccountService.ways(service, this.gateways()).catch(() => []);
|
|
194
|
+
const signing = this.signingIn;
|
|
195
|
+
return {
|
|
196
|
+
service,
|
|
197
|
+
providers,
|
|
198
|
+
devices: [],
|
|
199
|
+
...(signing ? { signingIn: { provider: signing.way.id, code: signing.code, url: signing.url, expiresAt: signing.expiresAt } } : {}),
|
|
200
|
+
...error,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (!this.view) {
|
|
204
|
+
await this.round();
|
|
205
|
+
}
|
|
206
|
+
const view = this.view;
|
|
207
|
+
if (!view) {
|
|
208
|
+
return { service, providers: [], account: this.agent.state.account, devices: [], ...error };
|
|
209
|
+
}
|
|
210
|
+
const pairings = this.agent.state.pairings;
|
|
211
|
+
const devices = view.devices.map(({ trusted: _trusted, ...device }) => ({
|
|
212
|
+
...device,
|
|
213
|
+
...(device.kind === 'computer' && pairings[device.id] ? { pairing: pairings[device.id] } : {}),
|
|
214
|
+
}));
|
|
215
|
+
return { service, providers: [], account: view.account, waiting: view.waiting, code: view.code, devices, ...error };
|
|
216
|
+
}
|
|
217
|
+
signIn(provider, service) {
|
|
218
|
+
return TaErrors.guard(() => this.begin(String(provider !== null && provider !== void 0 ? provider : ''), typeof service === 'string' ? service : undefined));
|
|
219
|
+
}
|
|
220
|
+
async begin(provider, service) {
|
|
221
|
+
if (this.agent) {
|
|
222
|
+
return this.info();
|
|
223
|
+
}
|
|
224
|
+
const ways = await AccountService.ways(this.service(), this.gateways());
|
|
225
|
+
const way = ways.find(one => one.id === provider && (service === undefined || one.service === service));
|
|
226
|
+
if (!way) {
|
|
227
|
+
throw new AccountCallError(404, `No way to sign in with ${provider} here.`);
|
|
228
|
+
}
|
|
229
|
+
const start = await AccountService.begin(way, TaAccountService_1.computerName());
|
|
230
|
+
this.signingIn = { way, ...start, expiresAt: Date.now() + start.expiresIn * 1000 };
|
|
231
|
+
this.error = undefined;
|
|
232
|
+
this.schedule(start.interval * 1000);
|
|
233
|
+
return this.info();
|
|
234
|
+
}
|
|
235
|
+
async cancelSignIn() {
|
|
236
|
+
this.signingIn = undefined;
|
|
237
|
+
return this.info();
|
|
238
|
+
}
|
|
239
|
+
act(run) {
|
|
240
|
+
return TaErrors.guard(async () => {
|
|
241
|
+
if (!this.agent) {
|
|
242
|
+
throw TaErrors.invalidArgument('This computer is not signed in to an account.', 'Sign in first.');
|
|
243
|
+
}
|
|
244
|
+
this.view = await run(this.agent);
|
|
245
|
+
this.schedule(this.view.devices.some(one => one.waiting) ? WAITING_SYNC_MS : SYNC_MS);
|
|
246
|
+
return this.info();
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
approve(id) {
|
|
250
|
+
return this.act(agent => agent.approve(String(id !== null && id !== void 0 ? id : '')));
|
|
251
|
+
}
|
|
252
|
+
remove(id) {
|
|
253
|
+
return this.act(agent => agent.remove(String(id !== null && id !== void 0 ? id : '')));
|
|
254
|
+
}
|
|
255
|
+
signOut() {
|
|
256
|
+
return TaErrors.guard(async () => {
|
|
257
|
+
if (this.agent) {
|
|
258
|
+
await this.agent.signOut();
|
|
259
|
+
this.forget();
|
|
260
|
+
}
|
|
261
|
+
this.error = undefined;
|
|
262
|
+
return this.info();
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
/** This computer in the account, once let in, for packages (TaExtensionHost.account). */
|
|
266
|
+
self() {
|
|
267
|
+
const me = this.view && !this.view.waiting ? this.view.devices.find(one => one.self) : undefined;
|
|
268
|
+
return me ? { id: me.id, name: me.name } : undefined;
|
|
269
|
+
}
|
|
270
|
+
/** The other computers of the account this one trusts, for packages (TaExtensionHost.account). */
|
|
271
|
+
computers() {
|
|
272
|
+
var _a;
|
|
273
|
+
var _b;
|
|
274
|
+
return ((_b = (_a = this.view) === null || _a === void 0 ? void 0 : _a.devices) !== null && _b !== void 0 ? _b : [])
|
|
275
|
+
.filter(one => one.kind === 'computer' && one.trusted && !one.self)
|
|
276
|
+
.map(one => ({ id: one.id, name: one.name, online: one.online }));
|
|
277
|
+
}
|
|
278
|
+
/** Says `value` to a package on another computer of the account, sealed for it: it hears it on its next look. */
|
|
279
|
+
async tell(to, topic, value) {
|
|
280
|
+
if (!this.agent) {
|
|
281
|
+
throw new AccountCallError(401, 'Sign in to an account first.');
|
|
282
|
+
}
|
|
283
|
+
await this.agent.tell(to, topic, value);
|
|
284
|
+
}
|
|
285
|
+
/** Hears what the same package on other computers of the account tells, by `topic`. */
|
|
286
|
+
onTold(topic, listener) {
|
|
287
|
+
let set = this.listeners.get(topic);
|
|
288
|
+
if (!set) {
|
|
289
|
+
set = new Set();
|
|
290
|
+
this.listeners.set(topic, set);
|
|
291
|
+
}
|
|
292
|
+
set.add(listener);
|
|
293
|
+
return () => set.delete(listener);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
__decorate([
|
|
297
|
+
Autowired(RELAY_BRIDGE),
|
|
298
|
+
__metadata("design:type", Function)
|
|
299
|
+
], TaAccountService.prototype, "bridge", void 0);
|
|
300
|
+
TaAccountService = TaAccountService_1 = __decorate([
|
|
301
|
+
Rpc([TaAccountServer, TA_ACCOUNT, ApplicationLifecycle])
|
|
302
|
+
], TaAccountService);
|
|
303
|
+
export { TaAccountService };
|
|
304
|
+
//# sourceMappingURL=account-service.js.map
|
|
@@ -123,3 +123,5 @@ export interface TaPresenceBook {
|
|
|
123
123
|
/** What `people` gives may have changed. */
|
|
124
124
|
onChange(listener: () => void): () => void;
|
|
125
125
|
}
|
|
126
|
+
/** TaAccountService (account-service.ts): the account of this computer, by this symbol so that the relay bridge needs no import of it. */
|
|
127
|
+
export declare const TA_ACCOUNT: unique symbol;
|
|
@@ -6,4 +6,6 @@ export const KeepAwake = Symbol('KeepAwake');
|
|
|
6
6
|
export const TaPresenceBook = Symbol('TaPresenceBook');
|
|
7
7
|
export const TA_HOST_OPTIONS = 'ta.host';
|
|
8
8
|
export const TA_EXEC_OPTIONS = 'ta.exec';
|
|
9
|
+
/** TaAccountService (account-service.ts): the account of this computer, by this symbol so that the relay bridge needs no import of it. */
|
|
10
|
+
export const TA_ACCOUNT = Symbol('TaAccount');
|
|
9
11
|
//# sourceMappingURL=host-protocol.js.map
|
package/esm/node/index.d.ts
CHANGED
package/esm/node/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
|
|
2
2
|
import { type Application, ApplicationLifecycle } from '@celljs/core';
|
|
3
3
|
import { type TaExtensionHost, type TaHostAnswer, type TaHostSessionEvent, TaPackageServer, type TaPackageService, TaServer } from '@celljs/ta-protocol';
|
|
4
|
+
import type { TaAccountService } from './account-service.js';
|
|
4
5
|
import { TaAppCommands } from './app-commands.js';
|
|
5
6
|
import { TaCredentials } from './credentials.js';
|
|
6
7
|
import { ExtensionDataStore } from './extension-data.js';
|
|
@@ -57,6 +58,7 @@ export declare class PackageHostsImpl implements PackageHosts, ApplicationLifecy
|
|
|
57
58
|
protected readonly server: TaServer;
|
|
58
59
|
protected readonly packages: TaPackageServer;
|
|
59
60
|
protected readonly relay: RelayBridge;
|
|
61
|
+
protected readonly account: TaAccountService;
|
|
60
62
|
protected readonly roomsBridge: RoomsBridge;
|
|
61
63
|
protected readonly voiceBridge: VoiceBridge;
|
|
62
64
|
protected readonly credentials: TaCredentials;
|
|
@@ -19,7 +19,7 @@ import { TaMessageSource, TaPackageServer, TaServer, } from '@celljs/ta-protocol
|
|
|
19
19
|
import { TaAppCommands } from './app-commands.js';
|
|
20
20
|
import { TaCredentials } from './credentials.js';
|
|
21
21
|
import { ExtensionDataStore } from './extension-data.js';
|
|
22
|
-
import { TaPresenceBook, TaSessionRegistry } from './host-protocol.js';
|
|
22
|
+
import { TA_ACCOUNT, TaPresenceBook, TaSessionRegistry } from './host-protocol.js';
|
|
23
23
|
import { TaPageBook } from './page-book.js';
|
|
24
24
|
import { RELAY_BRIDGE } from './relay-bridge.js';
|
|
25
25
|
import { PiDirectories } from './remote/remote-protocol.js';
|
|
@@ -182,6 +182,13 @@ let PackageHostsImpl = class PackageHostsImpl {
|
|
|
182
182
|
},
|
|
183
183
|
prompt: (sessionId, input) => host.prompt(sessionId, mark(input)),
|
|
184
184
|
followUp: (sessionId, input) => host.followUp(sessionId, mark(input)),
|
|
185
|
+
// A package speaks with itself on the user's other computers, under its own name.
|
|
186
|
+
account: {
|
|
187
|
+
self: async () => this.account.self(),
|
|
188
|
+
computers: async () => this.account.computers(),
|
|
189
|
+
tell: (to, value) => this.account.tell(to, name, value),
|
|
190
|
+
onTold: listener => this.account.onTold(name, listener),
|
|
191
|
+
},
|
|
185
192
|
};
|
|
186
193
|
}
|
|
187
194
|
host() {
|
|
@@ -294,6 +301,10 @@ __decorate([
|
|
|
294
301
|
Autowired({ id: RELAY_BRIDGE, detached: true }),
|
|
295
302
|
__metadata("design:type", Function)
|
|
296
303
|
], PackageHostsImpl.prototype, "relay", void 0);
|
|
304
|
+
__decorate([
|
|
305
|
+
Autowired({ id: TA_ACCOUNT, detached: true }),
|
|
306
|
+
__metadata("design:type", Function)
|
|
307
|
+
], PackageHostsImpl.prototype, "account", void 0);
|
|
297
308
|
__decorate([
|
|
298
309
|
Autowired({ id: ROOMS_BRIDGE, detached: true }),
|
|
299
310
|
__metadata("design:type", Function)
|
package/esm/node/relay-bridge.js
CHANGED
|
@@ -352,7 +352,7 @@ let TaRelayServerImpl = class TaRelayServerImpl {
|
|
|
352
352
|
defaultRelay: relay,
|
|
353
353
|
online: this.bridge.online,
|
|
354
354
|
// Those of people let in are theirs, not the user's: ta-team lists them.
|
|
355
|
-
devices: devices.filter(each => !each.member).map(each => device({ ...each, relay: RelayStore.relayOf(each, config) })),
|
|
355
|
+
devices: devices.filter(each => !each.member && !each.account).map(each => device({ ...each, relay: RelayStore.relayOf(each, config) })),
|
|
356
356
|
...(this.keepAwake.supported ? { keepAwake: keepAwake !== false } : {}),
|
|
357
357
|
};
|
|
358
358
|
}
|
|
@@ -172,6 +172,14 @@ const RULES = {
|
|
|
172
172
|
closeTerminal: fixed('commands.terminal'),
|
|
173
173
|
listTerminals: fixed('commands.terminal'),
|
|
174
174
|
},
|
|
175
|
+
TaAccountServer: {
|
|
176
|
+
getAccount: fixed('settings.view'),
|
|
177
|
+
signIn: fixed('settings.manage'),
|
|
178
|
+
cancelSignIn: fixed('settings.manage'),
|
|
179
|
+
approve: fixed('settings.manage'),
|
|
180
|
+
remove: fixed('settings.manage'),
|
|
181
|
+
signOut: fixed('settings.manage'),
|
|
182
|
+
},
|
|
175
183
|
TaRelayServer: {
|
|
176
184
|
getRelay: fixed('settings.view'),
|
|
177
185
|
pair: fixed('settings.manage'),
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Application, ApplicationLifecycle } from '@celljs/core';
|
|
2
|
+
import { type TaAccountInfo, TaAccountServer } from '@celljs/ta-protocol';
|
|
3
|
+
import { AccountAgent, type AccountKept, type AccountView, type AccountWay } from '@celljs/ta-relay';
|
|
4
|
+
import { type RelayBridge } from './relay-bridge';
|
|
5
|
+
/** What a package hears from the same package on another computer of the account (TaAccountService.tell). */
|
|
6
|
+
export type TaAccountListener = (from: {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
}, value: unknown) => void;
|
|
10
|
+
/**
|
|
11
|
+
* The account of this computer (docs/design/ta-account.md): signed in, it is in the user's list of devices; it lets
|
|
12
|
+
* in the devices the user lets in, pairs each with this ta serve through the relay, and unpairs those removed. What it
|
|
13
|
+
* keeps (its own keys, whom it trusts, its token) is in `account.json` of the agent directory, for the user alone.
|
|
14
|
+
*/
|
|
15
|
+
export declare class TaAccountService implements TaAccountServer, ApplicationLifecycle<Application> {
|
|
16
|
+
protected readonly bridge: RelayBridge;
|
|
17
|
+
protected agent?: AccountAgent;
|
|
18
|
+
protected view?: AccountView;
|
|
19
|
+
protected signingIn?: {
|
|
20
|
+
way: AccountWay;
|
|
21
|
+
handle: string;
|
|
22
|
+
code: string;
|
|
23
|
+
url: string;
|
|
24
|
+
interval: number;
|
|
25
|
+
expiresAt: number;
|
|
26
|
+
};
|
|
27
|
+
protected error?: string;
|
|
28
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
29
|
+
protected running?: Promise<void>;
|
|
30
|
+
protected stopped: boolean;
|
|
31
|
+
protected readonly listeners: Map<string, Set<TaAccountListener>>;
|
|
32
|
+
get file(): string;
|
|
33
|
+
/** The name this computer goes by in the account. */
|
|
34
|
+
static computerName(): string;
|
|
35
|
+
onStart(): void;
|
|
36
|
+
onStop(): void;
|
|
37
|
+
protected read(): AccountKept | undefined;
|
|
38
|
+
protected write(kept: AccountKept): void;
|
|
39
|
+
protected service(): string;
|
|
40
|
+
/** The gateways whose accounts this computer may sign in with: of the deployment joined, and of this build. */
|
|
41
|
+
protected gateways(): string[];
|
|
42
|
+
protected agentOf(kept: AccountKept): AccountAgent;
|
|
43
|
+
/** Signed out by the service (removed, or the token no longer works): the devices it paired lose their way in. */
|
|
44
|
+
protected forget(): void;
|
|
45
|
+
protected schedule(after: number): void;
|
|
46
|
+
/** One round at a time: a sign-in polled, or the account synced. */
|
|
47
|
+
protected round(): Promise<void>;
|
|
48
|
+
protected pollSignIn(): Promise<number>;
|
|
49
|
+
getAccount(): Promise<TaAccountInfo>;
|
|
50
|
+
info(): Promise<TaAccountInfo>;
|
|
51
|
+
signIn(provider: string, service?: string): Promise<TaAccountInfo>;
|
|
52
|
+
protected begin(provider: string, service?: string): Promise<TaAccountInfo>;
|
|
53
|
+
cancelSignIn(): Promise<TaAccountInfo>;
|
|
54
|
+
protected act(run: (agent: AccountAgent) => Promise<AccountView>): Promise<TaAccountInfo>;
|
|
55
|
+
approve(id: string): Promise<TaAccountInfo>;
|
|
56
|
+
remove(id: string): Promise<TaAccountInfo>;
|
|
57
|
+
signOut(): Promise<TaAccountInfo>;
|
|
58
|
+
/** This computer in the account, once let in, for packages (TaExtensionHost.account). */
|
|
59
|
+
self(): {
|
|
60
|
+
id: string;
|
|
61
|
+
name: string;
|
|
62
|
+
} | undefined;
|
|
63
|
+
/** The other computers of the account this one trusts, for packages (TaExtensionHost.account). */
|
|
64
|
+
computers(): {
|
|
65
|
+
id: string;
|
|
66
|
+
name: string;
|
|
67
|
+
online: boolean;
|
|
68
|
+
}[];
|
|
69
|
+
/** Says `value` to a package on another computer of the account, sealed for it: it hears it on its next look. */
|
|
70
|
+
tell(to: string, topic: string, value: unknown): Promise<void>;
|
|
71
|
+
/** Hears what the same package on other computers of the account tells, by `topic`. */
|
|
72
|
+
onTold(topic: string, listener: TaAccountListener): () => void;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=account-service.d.ts.map
|
|
@@ -0,0 +1,307 @@
|
|
|
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
|
+
var TaAccountService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.TaAccountService = void 0;
|
|
14
|
+
const node_fs_1 = require("node:fs");
|
|
15
|
+
const node_os_1 = require("node:os");
|
|
16
|
+
const node_path_1 = require("node:path");
|
|
17
|
+
const core_1 = require("@celljs/core");
|
|
18
|
+
const rpc_1 = require("@celljs/rpc");
|
|
19
|
+
const ta_brand_1 = require("@celljs/ta-harness/lib/node/ta-brand");
|
|
20
|
+
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
21
|
+
const ta_relay_1 = require("@celljs/ta-relay");
|
|
22
|
+
const host_protocol_1 = require("./host-protocol");
|
|
23
|
+
const relay_bridge_1 = require("./relay-bridge");
|
|
24
|
+
const ta_errors_1 = require("./ta-errors");
|
|
25
|
+
/** How often a computer says it is there and looks at its account; quicker while a device waits to be let in. */
|
|
26
|
+
const SYNC_MS = 20000;
|
|
27
|
+
const WAITING_SYNC_MS = 4000;
|
|
28
|
+
/**
|
|
29
|
+
* The account of this computer (docs/design/ta-account.md): signed in, it is in the user's list of devices; it lets
|
|
30
|
+
* in the devices the user lets in, pairs each with this ta serve through the relay, and unpairs those removed. What it
|
|
31
|
+
* keeps (its own keys, whom it trusts, its token) is in `account.json` of the agent directory, for the user alone.
|
|
32
|
+
*/
|
|
33
|
+
let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.stopped = false;
|
|
36
|
+
this.listeners = new Map();
|
|
37
|
+
}
|
|
38
|
+
get file() {
|
|
39
|
+
return (0, node_path_1.join)(ta_brand_1.TaBrand.agentDir(), 'account.json');
|
|
40
|
+
}
|
|
41
|
+
/** The name this computer goes by in the account. */
|
|
42
|
+
static computerName() {
|
|
43
|
+
return (0, node_os_1.hostname)().replace(/\.local$/, '');
|
|
44
|
+
}
|
|
45
|
+
onStart() {
|
|
46
|
+
const kept = this.read();
|
|
47
|
+
if (kept) {
|
|
48
|
+
this.agent = this.agentOf(kept);
|
|
49
|
+
this.schedule(0);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
onStop() {
|
|
53
|
+
this.stopped = true;
|
|
54
|
+
clearTimeout(this.timer);
|
|
55
|
+
}
|
|
56
|
+
read() {
|
|
57
|
+
var _a;
|
|
58
|
+
try {
|
|
59
|
+
const kept = JSON.parse((0, node_fs_1.readFileSync)(this.file, 'utf8'));
|
|
60
|
+
return (kept === null || kept === void 0 ? void 0 : kept.token) && ((_a = kept.self) === null || _a === void 0 ? void 0 : _a.signKey) ? kept : undefined;
|
|
61
|
+
}
|
|
62
|
+
catch (_b) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
write(kept) {
|
|
67
|
+
const temp = `${this.file}.${process.pid}.tmp`;
|
|
68
|
+
(0, node_fs_1.writeFileSync)(temp, `${JSON.stringify(kept, undefined, 2)}\n`, { mode: 0o600 });
|
|
69
|
+
(0, node_fs_1.chmodSync)(temp, 0o600);
|
|
70
|
+
(0, node_fs_1.renameSync)(temp, this.file);
|
|
71
|
+
}
|
|
72
|
+
service() {
|
|
73
|
+
return this.bridge.relayFor('devices');
|
|
74
|
+
}
|
|
75
|
+
/** The gateways whose accounts this computer may sign in with: of the deployment joined, and of this build. */
|
|
76
|
+
gateways() {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
const joined = (_b = (_a = this.bridge.relays.safeRead().config) === null || _a === void 0 ? void 0 : _a.deployment) === null || _b === void 0 ? void 0 : _b.gateway;
|
|
79
|
+
return [joined, process.env.TA_GATEWAY_URL].filter((one) => !!one);
|
|
80
|
+
}
|
|
81
|
+
agentOf(kept) {
|
|
82
|
+
const store = this.bridge.store;
|
|
83
|
+
return new ta_relay_1.AccountAgent(kept, {
|
|
84
|
+
save: next => this.write(next),
|
|
85
|
+
name: () => TaAccountService_1.computerName(),
|
|
86
|
+
host: () => {
|
|
87
|
+
var _a;
|
|
88
|
+
const config = store.read();
|
|
89
|
+
return config.host ? { relay: (_a = config.url) !== null && _a !== void 0 ? _a : this.service(), id: config.host.id } : undefined;
|
|
90
|
+
},
|
|
91
|
+
pair: async (device) => {
|
|
92
|
+
const pairing = store.pair(this.service(), device.name, { account: device.id });
|
|
93
|
+
this.bridge.refresh();
|
|
94
|
+
return ta_relay_1.RelayPairingUtil.link({ ...pairing, name: TaAccountService_1.computerName() });
|
|
95
|
+
},
|
|
96
|
+
paired: () => store.read().devices.flatMap(device => (device.account ? [device.account] : [])),
|
|
97
|
+
unpair: id => {
|
|
98
|
+
for (const device of store.read().devices.filter(one => one.account === id)) {
|
|
99
|
+
store.remove(device.id);
|
|
100
|
+
}
|
|
101
|
+
this.bridge.refresh();
|
|
102
|
+
},
|
|
103
|
+
told: (from, topic, value) => {
|
|
104
|
+
var _a;
|
|
105
|
+
for (const listener of (_a = this.listeners.get(topic)) !== null && _a !== void 0 ? _a : []) {
|
|
106
|
+
try {
|
|
107
|
+
listener({ id: from.id, name: from.name }, value);
|
|
108
|
+
}
|
|
109
|
+
catch (_b) {
|
|
110
|
+
// A package's listener is its own.
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
signedOut: () => this.forget(),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/** Signed out by the service (removed, or the token no longer works): the devices it paired lose their way in. */
|
|
118
|
+
forget() {
|
|
119
|
+
const store = this.bridge.store;
|
|
120
|
+
for (const device of store.read().devices.filter(one => one.account)) {
|
|
121
|
+
store.remove(device.id);
|
|
122
|
+
}
|
|
123
|
+
this.bridge.refresh();
|
|
124
|
+
(0, node_fs_1.rmSync)(this.file, { force: true });
|
|
125
|
+
this.agent = undefined;
|
|
126
|
+
this.view = undefined;
|
|
127
|
+
}
|
|
128
|
+
schedule(after) {
|
|
129
|
+
var _a, _b;
|
|
130
|
+
clearTimeout(this.timer);
|
|
131
|
+
if (this.stopped || (!this.agent && !this.signingIn)) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.timer = setTimeout(() => void this.round(), after);
|
|
135
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
136
|
+
}
|
|
137
|
+
/** One round at a time: a sign-in polled, or the account synced. */
|
|
138
|
+
round() {
|
|
139
|
+
var _a;
|
|
140
|
+
(_a = this.running) !== null && _a !== void 0 ? _a : (this.running = (async () => {
|
|
141
|
+
let next = SYNC_MS;
|
|
142
|
+
try {
|
|
143
|
+
if (this.signingIn) {
|
|
144
|
+
next = await this.pollSignIn();
|
|
145
|
+
}
|
|
146
|
+
else if (this.agent) {
|
|
147
|
+
this.view = await this.agent.sync();
|
|
148
|
+
this.error = undefined;
|
|
149
|
+
if (this.view.waiting || this.view.devices.some(one => one.waiting)) {
|
|
150
|
+
next = WAITING_SYNC_MS;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
this.running = undefined;
|
|
159
|
+
}
|
|
160
|
+
this.schedule(next);
|
|
161
|
+
})());
|
|
162
|
+
return this.running;
|
|
163
|
+
}
|
|
164
|
+
async pollSignIn() {
|
|
165
|
+
const signing = this.signingIn;
|
|
166
|
+
if (Date.now() > signing.expiresAt) {
|
|
167
|
+
this.signingIn = undefined;
|
|
168
|
+
this.error = 'The sign-in ran out of time: begin it again.';
|
|
169
|
+
return SYNC_MS;
|
|
170
|
+
}
|
|
171
|
+
const answer = await ta_relay_1.AccountService.check(signing.way, signing.handle);
|
|
172
|
+
if (answer.status === 'pending') {
|
|
173
|
+
return signing.interval * 1000;
|
|
174
|
+
}
|
|
175
|
+
this.signingIn = undefined;
|
|
176
|
+
if (answer.status === 'failed') {
|
|
177
|
+
this.error = answer.error;
|
|
178
|
+
return SYNC_MS;
|
|
179
|
+
}
|
|
180
|
+
const kept = await ta_relay_1.AccountAgent.create(signing.way.service, answer.token, answer.account, 'computer', TaAccountService_1.computerName());
|
|
181
|
+
this.write(kept);
|
|
182
|
+
this.agent = this.agentOf(kept);
|
|
183
|
+
this.view = await this.agent.sync();
|
|
184
|
+
this.error = undefined;
|
|
185
|
+
return this.view.waiting ? WAITING_SYNC_MS : SYNC_MS;
|
|
186
|
+
}
|
|
187
|
+
getAccount() {
|
|
188
|
+
return ta_errors_1.TaErrors.guard(() => this.info());
|
|
189
|
+
}
|
|
190
|
+
async info() {
|
|
191
|
+
var _a, _b;
|
|
192
|
+
var _c, _d;
|
|
193
|
+
const service = (_d = (_c = (_a = this.agent) === null || _a === void 0 ? void 0 : _a.state.service) !== null && _c !== void 0 ? _c : (_b = this.signingIn) === null || _b === void 0 ? void 0 : _b.way.service) !== null && _d !== void 0 ? _d : this.service();
|
|
194
|
+
const error = this.error ? { error: this.error } : {};
|
|
195
|
+
if (!this.agent) {
|
|
196
|
+
const providers = await ta_relay_1.AccountService.ways(service, this.gateways()).catch(() => []);
|
|
197
|
+
const signing = this.signingIn;
|
|
198
|
+
return {
|
|
199
|
+
service,
|
|
200
|
+
providers,
|
|
201
|
+
devices: [],
|
|
202
|
+
...(signing ? { signingIn: { provider: signing.way.id, code: signing.code, url: signing.url, expiresAt: signing.expiresAt } } : {}),
|
|
203
|
+
...error,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
if (!this.view) {
|
|
207
|
+
await this.round();
|
|
208
|
+
}
|
|
209
|
+
const view = this.view;
|
|
210
|
+
if (!view) {
|
|
211
|
+
return { service, providers: [], account: this.agent.state.account, devices: [], ...error };
|
|
212
|
+
}
|
|
213
|
+
const pairings = this.agent.state.pairings;
|
|
214
|
+
const devices = view.devices.map(({ trusted: _trusted, ...device }) => ({
|
|
215
|
+
...device,
|
|
216
|
+
...(device.kind === 'computer' && pairings[device.id] ? { pairing: pairings[device.id] } : {}),
|
|
217
|
+
}));
|
|
218
|
+
return { service, providers: [], account: view.account, waiting: view.waiting, code: view.code, devices, ...error };
|
|
219
|
+
}
|
|
220
|
+
signIn(provider, service) {
|
|
221
|
+
return ta_errors_1.TaErrors.guard(() => this.begin(String(provider !== null && provider !== void 0 ? provider : ''), typeof service === 'string' ? service : undefined));
|
|
222
|
+
}
|
|
223
|
+
async begin(provider, service) {
|
|
224
|
+
if (this.agent) {
|
|
225
|
+
return this.info();
|
|
226
|
+
}
|
|
227
|
+
const ways = await ta_relay_1.AccountService.ways(this.service(), this.gateways());
|
|
228
|
+
const way = ways.find(one => one.id === provider && (service === undefined || one.service === service));
|
|
229
|
+
if (!way) {
|
|
230
|
+
throw new ta_relay_1.AccountCallError(404, `No way to sign in with ${provider} here.`);
|
|
231
|
+
}
|
|
232
|
+
const start = await ta_relay_1.AccountService.begin(way, TaAccountService_1.computerName());
|
|
233
|
+
this.signingIn = { way, ...start, expiresAt: Date.now() + start.expiresIn * 1000 };
|
|
234
|
+
this.error = undefined;
|
|
235
|
+
this.schedule(start.interval * 1000);
|
|
236
|
+
return this.info();
|
|
237
|
+
}
|
|
238
|
+
async cancelSignIn() {
|
|
239
|
+
this.signingIn = undefined;
|
|
240
|
+
return this.info();
|
|
241
|
+
}
|
|
242
|
+
act(run) {
|
|
243
|
+
return ta_errors_1.TaErrors.guard(async () => {
|
|
244
|
+
if (!this.agent) {
|
|
245
|
+
throw ta_errors_1.TaErrors.invalidArgument('This computer is not signed in to an account.', 'Sign in first.');
|
|
246
|
+
}
|
|
247
|
+
this.view = await run(this.agent);
|
|
248
|
+
this.schedule(this.view.devices.some(one => one.waiting) ? WAITING_SYNC_MS : SYNC_MS);
|
|
249
|
+
return this.info();
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
approve(id) {
|
|
253
|
+
return this.act(agent => agent.approve(String(id !== null && id !== void 0 ? id : '')));
|
|
254
|
+
}
|
|
255
|
+
remove(id) {
|
|
256
|
+
return this.act(agent => agent.remove(String(id !== null && id !== void 0 ? id : '')));
|
|
257
|
+
}
|
|
258
|
+
signOut() {
|
|
259
|
+
return ta_errors_1.TaErrors.guard(async () => {
|
|
260
|
+
if (this.agent) {
|
|
261
|
+
await this.agent.signOut();
|
|
262
|
+
this.forget();
|
|
263
|
+
}
|
|
264
|
+
this.error = undefined;
|
|
265
|
+
return this.info();
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/** This computer in the account, once let in, for packages (TaExtensionHost.account). */
|
|
269
|
+
self() {
|
|
270
|
+
const me = this.view && !this.view.waiting ? this.view.devices.find(one => one.self) : undefined;
|
|
271
|
+
return me ? { id: me.id, name: me.name } : undefined;
|
|
272
|
+
}
|
|
273
|
+
/** The other computers of the account this one trusts, for packages (TaExtensionHost.account). */
|
|
274
|
+
computers() {
|
|
275
|
+
var _a;
|
|
276
|
+
var _b;
|
|
277
|
+
return ((_b = (_a = this.view) === null || _a === void 0 ? void 0 : _a.devices) !== null && _b !== void 0 ? _b : [])
|
|
278
|
+
.filter(one => one.kind === 'computer' && one.trusted && !one.self)
|
|
279
|
+
.map(one => ({ id: one.id, name: one.name, online: one.online }));
|
|
280
|
+
}
|
|
281
|
+
/** Says `value` to a package on another computer of the account, sealed for it: it hears it on its next look. */
|
|
282
|
+
async tell(to, topic, value) {
|
|
283
|
+
if (!this.agent) {
|
|
284
|
+
throw new ta_relay_1.AccountCallError(401, 'Sign in to an account first.');
|
|
285
|
+
}
|
|
286
|
+
await this.agent.tell(to, topic, value);
|
|
287
|
+
}
|
|
288
|
+
/** Hears what the same package on other computers of the account tells, by `topic`. */
|
|
289
|
+
onTold(topic, listener) {
|
|
290
|
+
let set = this.listeners.get(topic);
|
|
291
|
+
if (!set) {
|
|
292
|
+
set = new Set();
|
|
293
|
+
this.listeners.set(topic, set);
|
|
294
|
+
}
|
|
295
|
+
set.add(listener);
|
|
296
|
+
return () => set.delete(listener);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
exports.TaAccountService = TaAccountService;
|
|
300
|
+
__decorate([
|
|
301
|
+
(0, core_1.Autowired)(relay_bridge_1.RELAY_BRIDGE),
|
|
302
|
+
__metadata("design:type", Function)
|
|
303
|
+
], TaAccountService.prototype, "bridge", void 0);
|
|
304
|
+
exports.TaAccountService = TaAccountService = TaAccountService_1 = __decorate([
|
|
305
|
+
(0, rpc_1.Rpc)([ta_protocol_1.TaAccountServer, host_protocol_1.TA_ACCOUNT, core_1.ApplicationLifecycle])
|
|
306
|
+
], TaAccountService);
|
|
307
|
+
//# sourceMappingURL=account-service.js.map
|
|
@@ -123,4 +123,6 @@ export interface TaPresenceBook {
|
|
|
123
123
|
/** What `people` gives may have changed. */
|
|
124
124
|
onChange(listener: () => void): () => void;
|
|
125
125
|
}
|
|
126
|
+
/** TaAccountService (account-service.ts): the account of this computer, by this symbol so that the relay bridge needs no import of it. */
|
|
127
|
+
export declare const TA_ACCOUNT: unique symbol;
|
|
126
128
|
//# sourceMappingURL=host-protocol.d.ts.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TA_EXEC_OPTIONS = exports.TA_HOST_OPTIONS = exports.TaPresenceBook = exports.KeepAwake = exports.WebExtensionCatalog = exports.TaSessionCatalog = exports.TaSessionRegistry = exports.TaSessionFactory = void 0;
|
|
3
|
+
exports.TA_ACCOUNT = exports.TA_EXEC_OPTIONS = exports.TA_HOST_OPTIONS = exports.TaPresenceBook = exports.KeepAwake = exports.WebExtensionCatalog = exports.TaSessionCatalog = exports.TaSessionRegistry = exports.TaSessionFactory = void 0;
|
|
4
4
|
exports.TaSessionFactory = Symbol('TaSessionFactory');
|
|
5
5
|
exports.TaSessionRegistry = Symbol('TaSessionRegistry');
|
|
6
6
|
exports.TaSessionCatalog = Symbol('TaSessionCatalog');
|
|
@@ -9,4 +9,6 @@ exports.KeepAwake = Symbol('KeepAwake');
|
|
|
9
9
|
exports.TaPresenceBook = Symbol('TaPresenceBook');
|
|
10
10
|
exports.TA_HOST_OPTIONS = 'ta.host';
|
|
11
11
|
exports.TA_EXEC_OPTIONS = 'ta.exec';
|
|
12
|
+
/** TaAccountService (account-service.ts): the account of this computer, by this symbol so that the relay bridge needs no import of it. */
|
|
13
|
+
exports.TA_ACCOUNT = Symbol('TaAccount');
|
|
12
14
|
//# sourceMappingURL=host-protocol.js.map
|
package/lib/node/index.d.ts
CHANGED
package/lib/node/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./account-service"), exports);
|
|
17
18
|
__exportStar(require("./app-commands"), exports);
|
|
18
19
|
__exportStar(require("./auth-server-impl"), exports);
|
|
19
20
|
__exportStar(require("./client-channel"), exports);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
|
|
2
2
|
import { type Application, ApplicationLifecycle } from '@celljs/core';
|
|
3
3
|
import { type TaExtensionHost, type TaHostAnswer, type TaHostSessionEvent, TaPackageServer, type TaPackageService, TaServer } from '@celljs/ta-protocol';
|
|
4
|
+
import type { TaAccountService } from './account-service';
|
|
4
5
|
import { TaAppCommands } from './app-commands';
|
|
5
6
|
import { TaCredentials } from './credentials';
|
|
6
7
|
import { ExtensionDataStore } from './extension-data';
|
|
@@ -57,6 +58,7 @@ export declare class PackageHostsImpl implements PackageHosts, ApplicationLifecy
|
|
|
57
58
|
protected readonly server: TaServer;
|
|
58
59
|
protected readonly packages: TaPackageServer;
|
|
59
60
|
protected readonly relay: RelayBridge;
|
|
61
|
+
protected readonly account: TaAccountService;
|
|
60
62
|
protected readonly roomsBridge: RoomsBridge;
|
|
61
63
|
protected readonly voiceBridge: VoiceBridge;
|
|
62
64
|
protected readonly credentials: TaCredentials;
|
|
@@ -185,6 +185,13 @@ let PackageHostsImpl = class PackageHostsImpl {
|
|
|
185
185
|
},
|
|
186
186
|
prompt: (sessionId, input) => host.prompt(sessionId, mark(input)),
|
|
187
187
|
followUp: (sessionId, input) => host.followUp(sessionId, mark(input)),
|
|
188
|
+
// A package speaks with itself on the user's other computers, under its own name.
|
|
189
|
+
account: {
|
|
190
|
+
self: async () => this.account.self(),
|
|
191
|
+
computers: async () => this.account.computers(),
|
|
192
|
+
tell: (to, value) => this.account.tell(to, name, value),
|
|
193
|
+
onTold: listener => this.account.onTold(name, listener),
|
|
194
|
+
},
|
|
188
195
|
};
|
|
189
196
|
}
|
|
190
197
|
host() {
|
|
@@ -298,6 +305,10 @@ __decorate([
|
|
|
298
305
|
(0, core_1.Autowired)({ id: relay_bridge_1.RELAY_BRIDGE, detached: true }),
|
|
299
306
|
__metadata("design:type", Function)
|
|
300
307
|
], PackageHostsImpl.prototype, "relay", void 0);
|
|
308
|
+
__decorate([
|
|
309
|
+
(0, core_1.Autowired)({ id: host_protocol_1.TA_ACCOUNT, detached: true }),
|
|
310
|
+
__metadata("design:type", Function)
|
|
311
|
+
], PackageHostsImpl.prototype, "account", void 0);
|
|
301
312
|
__decorate([
|
|
302
313
|
(0, core_1.Autowired)({ id: rooms_bridge_1.ROOMS_BRIDGE, detached: true }),
|
|
303
314
|
__metadata("design:type", Function)
|
package/lib/node/relay-bridge.js
CHANGED
|
@@ -355,7 +355,7 @@ let TaRelayServerImpl = class TaRelayServerImpl {
|
|
|
355
355
|
defaultRelay: relay,
|
|
356
356
|
online: this.bridge.online,
|
|
357
357
|
// Those of people let in are theirs, not the user's: ta-team lists them.
|
|
358
|
-
devices: devices.filter(each => !each.member).map(each => device({ ...each, relay: node_1.RelayStore.relayOf(each, config) })),
|
|
358
|
+
devices: devices.filter(each => !each.member && !each.account).map(each => device({ ...each, relay: node_1.RelayStore.relayOf(each, config) })),
|
|
359
359
|
...(this.keepAwake.supported ? { keepAwake: keepAwake !== false } : {}),
|
|
360
360
|
};
|
|
361
361
|
}
|
|
@@ -176,6 +176,14 @@ const RULES = {
|
|
|
176
176
|
closeTerminal: fixed('commands.terminal'),
|
|
177
177
|
listTerminals: fixed('commands.terminal'),
|
|
178
178
|
},
|
|
179
|
+
TaAccountServer: {
|
|
180
|
+
getAccount: fixed('settings.view'),
|
|
181
|
+
signIn: fixed('settings.manage'),
|
|
182
|
+
cancelSignIn: fixed('settings.manage'),
|
|
183
|
+
approve: fixed('settings.manage'),
|
|
184
|
+
remove: fixed('settings.manage'),
|
|
185
|
+
signOut: fixed('settings.manage'),
|
|
186
|
+
},
|
|
179
187
|
TaRelayServer: {
|
|
180
188
|
getRelay: fixed('settings.view'),
|
|
181
189
|
pair: fixed('settings.manage'),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-host",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.0",
|
|
4
4
|
"description": "ta serve: pi sessions for many clients over WebSocket",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -34,15 +34,15 @@
|
|
|
34
34
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@celljs/ai-agent": "4.
|
|
38
|
-
"@celljs/ai-coding-agent": "4.
|
|
39
|
-
"@celljs/core": "4.
|
|
40
|
-
"@celljs/rpc": "4.
|
|
41
|
-
"@celljs/ta-exec": "4.
|
|
42
|
-
"@celljs/ta-harness": "4.
|
|
43
|
-
"@celljs/ta-protocol": "4.
|
|
44
|
-
"@celljs/ta-relay": "4.
|
|
45
|
-
"@celljs/web": "4.
|
|
37
|
+
"@celljs/ai-agent": "4.19.0",
|
|
38
|
+
"@celljs/ai-coding-agent": "4.19.0",
|
|
39
|
+
"@celljs/core": "4.19.0",
|
|
40
|
+
"@celljs/rpc": "4.19.0",
|
|
41
|
+
"@celljs/ta-exec": "4.19.0",
|
|
42
|
+
"@celljs/ta-harness": "4.19.0",
|
|
43
|
+
"@celljs/ta-protocol": "4.19.0",
|
|
44
|
+
"@celljs/ta-relay": "4.19.0",
|
|
45
|
+
"@celljs/web": "4.19.0",
|
|
46
46
|
"@earendil-works/pi-coding-agent": "^1.5.2"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|