@celljs/ta-host 4.19.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.
- package/esm/node/account-profile.d.ts +82 -0
- package/esm/node/account-profile.js +190 -0
- package/esm/node/account-service.d.ts +15 -0
- package/esm/node/account-service.js +48 -2
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/package-hosts.d.ts +8 -1
- package/esm/node/package-hosts.js +22 -5
- package/esm/node/rooms-bridge.d.ts +14 -3
- package/esm/node/rooms-bridge.js +35 -8
- package/esm/node/settings-store.js +4 -2
- package/esm/node/team/team-calls.js +4 -0
- package/lib/node/account-profile.d.ts +83 -0
- package/lib/node/account-profile.js +194 -0
- package/lib/node/account-service.d.ts +15 -0
- package/lib/node/account-service.js +48 -2
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/package-hosts.d.ts +8 -1
- package/lib/node/package-hosts.js +22 -5
- package/lib/node/rooms-bridge.d.ts +14 -3
- package/lib/node/rooms-bridge.js +34 -7
- package/lib/node/settings-store.js +3 -1
- package/lib/node/team/team-calls.js +4 -0
- package/package.json +10 -10
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type Application, ApplicationLifecycle } from '@celljs/core';
|
|
2
|
+
import type { TaIdentity } from '@celljs/ta-protocol';
|
|
3
|
+
import { TaAccountService } from './account-service.js';
|
|
4
|
+
import { type RoomsBridge } from './rooms-bridge.js';
|
|
5
|
+
/** What the computers of an account tell each other of the user (docs/design/ta-me.md 3). */
|
|
6
|
+
export declare const ACCOUNT_PROFILE_TOPIC = "ta.profile";
|
|
7
|
+
/** The user as others see them: their name and picture, and when they last changed it (0: never, by hand). */
|
|
8
|
+
export interface AccountProfileValue {
|
|
9
|
+
name: string;
|
|
10
|
+
picture: string | null;
|
|
11
|
+
at: number;
|
|
12
|
+
}
|
|
13
|
+
/** What this computer keeps of the profile: of which account, when it changed, and what each computer of it was told. */
|
|
14
|
+
export interface AccountProfileState {
|
|
15
|
+
account: string;
|
|
16
|
+
at: number;
|
|
17
|
+
/** Of the name and the picture, to tell a change made here. */
|
|
18
|
+
hash: string;
|
|
19
|
+
/** By computer: the `at` it was told last. */
|
|
20
|
+
told: Record<string, number>;
|
|
21
|
+
}
|
|
22
|
+
/** What the profile needs of ta serve: the identity, the account, and where to keep its state. */
|
|
23
|
+
export interface AccountProfileSide {
|
|
24
|
+
identity(): Promise<TaIdentity>;
|
|
25
|
+
setName(name: string): Promise<TaIdentity>;
|
|
26
|
+
setPicture(picture: string | null): Promise<TaIdentity>;
|
|
27
|
+
/** The name a new identity gets (the user's on this system): one never chosen. */
|
|
28
|
+
defaultName(): string;
|
|
29
|
+
/** The account this computer is in, once let in. */
|
|
30
|
+
account(): {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
} | undefined;
|
|
34
|
+
computers(): {
|
|
35
|
+
id: string;
|
|
36
|
+
}[];
|
|
37
|
+
tell(to: string, value: AccountProfileValue): Promise<void>;
|
|
38
|
+
read(): AccountProfileState | undefined;
|
|
39
|
+
write(state: AccountProfileState | undefined): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The name and the picture of the user, shared by the computers of their account (docs/design/ta-me.md 3): the first
|
|
43
|
+
* sign-in names the user as the account does, unless they chose a name; then a change on one computer goes to the
|
|
44
|
+
* others, the latest kept. Only the name and the picture: the keys of each identity stay its own (ta-sync shares them).
|
|
45
|
+
*/
|
|
46
|
+
export declare class AccountProfile {
|
|
47
|
+
protected readonly side: AccountProfileSide;
|
|
48
|
+
protected readonly now: () => number;
|
|
49
|
+
protected running?: Promise<void>;
|
|
50
|
+
constructor(side: AccountProfileSide, now?: () => number);
|
|
51
|
+
static hash(identity: Pick<TaIdentity, 'name' | 'picture'>): string;
|
|
52
|
+
/** What the user is called in an account: its name, without the way it came by (`iam:su` is su). */
|
|
53
|
+
static nameOf(account: {
|
|
54
|
+
name: string;
|
|
55
|
+
}): string;
|
|
56
|
+
static valid(value: unknown): value is AccountProfileValue;
|
|
57
|
+
/** One look, one at a time: a change here noted, and told to the computers that have not heard it. */
|
|
58
|
+
round(): Promise<void>;
|
|
59
|
+
protected look(): Promise<void>;
|
|
60
|
+
/** In an account for the first time: named as the account, when the user never chose a name here. */
|
|
61
|
+
protected first(account: {
|
|
62
|
+
id: string;
|
|
63
|
+
name: string;
|
|
64
|
+
}, identity: TaIdentity): Promise<AccountProfileState>;
|
|
65
|
+
/** What another computer of the account told: taken when it is newer than what is here. */
|
|
66
|
+
heard(from: {
|
|
67
|
+
id: string;
|
|
68
|
+
}, value: unknown): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
/** AccountProfile for ta serve: TaAccountService and the identity of RoomsBridge; its state in `account-profile.json`. */
|
|
71
|
+
export declare class AccountProfileSync implements ApplicationLifecycle<Application> {
|
|
72
|
+
protected readonly account: TaAccountService;
|
|
73
|
+
protected readonly rooms: RoomsBridge;
|
|
74
|
+
protected profile?: AccountProfile;
|
|
75
|
+
protected timer?: ReturnType<typeof setInterval>;
|
|
76
|
+
protected stopTold?: () => void;
|
|
77
|
+
get file(): string;
|
|
78
|
+
onStart(): void;
|
|
79
|
+
onStop(): void;
|
|
80
|
+
protected read(): AccountProfileState | undefined;
|
|
81
|
+
protected write(state: AccountProfileState | undefined): void;
|
|
82
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
import { chmodSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { ApplicationLifecycle, Autowired, Component } from '@celljs/core';
|
|
13
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
14
|
+
import { RelayHubFiles } from '@celljs/ta-relay/lib/node';
|
|
15
|
+
import { TaAccountService } from './account-service.js';
|
|
16
|
+
import { TA_ACCOUNT } from './host-protocol.js';
|
|
17
|
+
import { ROOMS_BRIDGE } from './rooms-bridge.js';
|
|
18
|
+
/** What the computers of an account tell each other of the user (docs/design/ta-me.md 3). */
|
|
19
|
+
export const ACCOUNT_PROFILE_TOPIC = 'ta.profile';
|
|
20
|
+
/**
|
|
21
|
+
* The name and the picture of the user, shared by the computers of their account (docs/design/ta-me.md 3): the first
|
|
22
|
+
* sign-in names the user as the account does, unless they chose a name; then a change on one computer goes to the
|
|
23
|
+
* others, the latest kept. Only the name and the picture: the keys of each identity stay its own (ta-sync shares them).
|
|
24
|
+
*/
|
|
25
|
+
export class AccountProfile {
|
|
26
|
+
constructor(side, now = Date.now) {
|
|
27
|
+
this.side = side;
|
|
28
|
+
this.now = now;
|
|
29
|
+
}
|
|
30
|
+
static hash(identity) {
|
|
31
|
+
var _a;
|
|
32
|
+
const text = `${identity.name}\n${(_a = identity.picture) !== null && _a !== void 0 ? _a : ''}`;
|
|
33
|
+
let hash = 2166136261;
|
|
34
|
+
for (let index = 0; index < text.length; index++) {
|
|
35
|
+
hash = Math.imul(hash ^ text.charCodeAt(index), 16777619);
|
|
36
|
+
}
|
|
37
|
+
return `${text.length}:${(hash >>> 0).toString(36)}`;
|
|
38
|
+
}
|
|
39
|
+
/** What the user is called in an account: its name, without the way it came by (`iam:su` is su). */
|
|
40
|
+
static nameOf(account) {
|
|
41
|
+
return account.name.replace(/^[a-z][\w-]*:/i, '').trim();
|
|
42
|
+
}
|
|
43
|
+
static valid(value) {
|
|
44
|
+
const one = value;
|
|
45
|
+
return (typeof (one === null || one === void 0 ? void 0 : one.name) === 'string' &&
|
|
46
|
+
one.name.trim().length > 0 &&
|
|
47
|
+
(one.picture === null || typeof one.picture === 'string') &&
|
|
48
|
+
typeof one.at === 'number' &&
|
|
49
|
+
one.at > 0);
|
|
50
|
+
}
|
|
51
|
+
/** One look, one at a time: a change here noted, and told to the computers that have not heard it. */
|
|
52
|
+
round() {
|
|
53
|
+
var _a;
|
|
54
|
+
(_a = this.running) !== null && _a !== void 0 ? _a : (this.running = this.look().finally(() => {
|
|
55
|
+
this.running = undefined;
|
|
56
|
+
}));
|
|
57
|
+
return this.running;
|
|
58
|
+
}
|
|
59
|
+
async look() {
|
|
60
|
+
var _a;
|
|
61
|
+
const account = this.side.account();
|
|
62
|
+
let state = this.side.read();
|
|
63
|
+
if (!account) {
|
|
64
|
+
if (state) {
|
|
65
|
+
this.side.write(undefined);
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
let identity = await this.side.identity();
|
|
70
|
+
if ((state === null || state === void 0 ? void 0 : state.account) !== account.id) {
|
|
71
|
+
state = await this.first(account, identity);
|
|
72
|
+
identity = await this.side.identity();
|
|
73
|
+
}
|
|
74
|
+
else if (AccountProfile.hash(identity) !== state.hash) {
|
|
75
|
+
state = { ...state, at: this.now(), hash: AccountProfile.hash(identity) };
|
|
76
|
+
}
|
|
77
|
+
if (state.at > 0) {
|
|
78
|
+
const value = { name: identity.name, picture: (_a = identity.picture) !== null && _a !== void 0 ? _a : null, at: state.at };
|
|
79
|
+
for (const computer of this.side.computers()) {
|
|
80
|
+
if (state.told[computer.id] !== state.at) {
|
|
81
|
+
try {
|
|
82
|
+
await this.side.tell(computer.id, value);
|
|
83
|
+
state = { ...state, told: { ...state.told, [computer.id]: state.at } };
|
|
84
|
+
}
|
|
85
|
+
catch (_b) {
|
|
86
|
+
// Told on the next look.
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.side.write(state);
|
|
92
|
+
}
|
|
93
|
+
/** In an account for the first time: named as the account, when the user never chose a name here. */
|
|
94
|
+
async first(account, identity) {
|
|
95
|
+
const chosen = identity.name !== this.side.defaultName() || Boolean(identity.picture);
|
|
96
|
+
const name = AccountProfile.nameOf(account);
|
|
97
|
+
if (!chosen && name && name !== identity.name) {
|
|
98
|
+
identity = await this.side.setName(name).catch(() => identity);
|
|
99
|
+
}
|
|
100
|
+
return { account: account.id, at: chosen ? this.now() : 0, hash: AccountProfile.hash(identity), told: {} };
|
|
101
|
+
}
|
|
102
|
+
/** What another computer of the account told: taken when it is newer than what is here. */
|
|
103
|
+
async heard(from, value) {
|
|
104
|
+
var _a;
|
|
105
|
+
await this.running;
|
|
106
|
+
const account = this.side.account();
|
|
107
|
+
const state = this.side.read();
|
|
108
|
+
if (!account || !AccountProfile.valid(value) || ((state === null || state === void 0 ? void 0 : state.account) === account.id && value.at <= state.at)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
let identity = await this.side.identity();
|
|
112
|
+
if (identity.name !== value.name.trim()) {
|
|
113
|
+
identity = await this.side.setName(value.name);
|
|
114
|
+
}
|
|
115
|
+
if (((_a = identity.picture) !== null && _a !== void 0 ? _a : null) !== value.picture) {
|
|
116
|
+
identity = await this.side.setPicture(value.picture);
|
|
117
|
+
}
|
|
118
|
+
const told = (state === null || state === void 0 ? void 0 : state.account) === account.id ? state.told : {};
|
|
119
|
+
this.side.write({ account: account.id, at: value.at, hash: AccountProfile.hash(identity), told: { ...told, [from.id]: value.at } });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** How often the profile is looked at: a change here reaches the other computers within this. */
|
|
123
|
+
const LOOK_MS = 20000;
|
|
124
|
+
const FIRST_LOOK_MS = 5000;
|
|
125
|
+
/** AccountProfile for ta serve: TaAccountService and the identity of RoomsBridge; its state in `account-profile.json`. */
|
|
126
|
+
let AccountProfileSync = class AccountProfileSync {
|
|
127
|
+
get file() {
|
|
128
|
+
return join(TaBrand.agentDir(), 'account-profile.json');
|
|
129
|
+
}
|
|
130
|
+
onStart() {
|
|
131
|
+
var _a, _b, _c, _d;
|
|
132
|
+
const profile = new AccountProfile({
|
|
133
|
+
identity: () => this.rooms.rooms.getIdentity(),
|
|
134
|
+
setName: name => this.rooms.rooms.setName(name),
|
|
135
|
+
setPicture: picture => this.rooms.rooms.setPicture(picture),
|
|
136
|
+
defaultName: () => RelayHubFiles.userName(),
|
|
137
|
+
// A Ta in the cloud keeps its own: the user's computers share theirs.
|
|
138
|
+
account: () => (this.account.self() && !TaAccountService.cloud() ? this.account.account() : undefined),
|
|
139
|
+
computers: () => this.account.computers(),
|
|
140
|
+
tell: (to, value) => this.account.tell(to, ACCOUNT_PROFILE_TOPIC, value),
|
|
141
|
+
read: () => this.read(),
|
|
142
|
+
write: state => this.write(state),
|
|
143
|
+
});
|
|
144
|
+
this.profile = profile;
|
|
145
|
+
const heard = (from, value) => void profile.heard(from, value).catch(() => undefined);
|
|
146
|
+
this.stopTold = this.account.onTold(ACCOUNT_PROFILE_TOPIC, heard);
|
|
147
|
+
const look = () => void profile.round().catch(() => undefined);
|
|
148
|
+
this.timer = setInterval(look, LOOK_MS);
|
|
149
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
150
|
+
// Soon after start too: an account signed in to last time names the user without waiting.
|
|
151
|
+
(_d = (_c = setTimeout(look, FIRST_LOOK_MS)).unref) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
152
|
+
}
|
|
153
|
+
onStop() {
|
|
154
|
+
var _a;
|
|
155
|
+
clearInterval(this.timer);
|
|
156
|
+
(_a = this.stopTold) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
157
|
+
}
|
|
158
|
+
read() {
|
|
159
|
+
try {
|
|
160
|
+
return JSON.parse(readFileSync(this.file, 'utf8'));
|
|
161
|
+
}
|
|
162
|
+
catch (_a) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
write(state) {
|
|
167
|
+
if (state) {
|
|
168
|
+
const temp = `${this.file}.${process.pid}.tmp`;
|
|
169
|
+
writeFileSync(temp, `${JSON.stringify(state)}\n`, { mode: 0o600 });
|
|
170
|
+
chmodSync(temp, 0o600);
|
|
171
|
+
renameSync(temp, this.file);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
rmSync(this.file, { force: true });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
__decorate([
|
|
179
|
+
Autowired(TA_ACCOUNT),
|
|
180
|
+
__metadata("design:type", TaAccountService)
|
|
181
|
+
], AccountProfileSync.prototype, "account", void 0);
|
|
182
|
+
__decorate([
|
|
183
|
+
Autowired(ROOMS_BRIDGE),
|
|
184
|
+
__metadata("design:type", Function)
|
|
185
|
+
], AccountProfileSync.prototype, "rooms", void 0);
|
|
186
|
+
AccountProfileSync = __decorate([
|
|
187
|
+
Component(ApplicationLifecycle)
|
|
188
|
+
], AccountProfileSync);
|
|
189
|
+
export { AccountProfileSync };
|
|
190
|
+
//# sourceMappingURL=account-profile.js.map
|
|
@@ -30,6 +30,15 @@ export declare class TaAccountService implements TaAccountServer, ApplicationLif
|
|
|
30
30
|
protected stopped: boolean;
|
|
31
31
|
protected readonly listeners: Map<string, Set<TaAccountListener>>;
|
|
32
32
|
get file(): string;
|
|
33
|
+
/**
|
|
34
|
+
* A Ta in the cloud (docs/design/ta-me.md 2): its gateway says so, with where the sandbox reaches the gateway's
|
|
35
|
+
* accounts (TA_ACCOUNT_URL) and the address the devices know it by (TA_ACCOUNT_SERVICE).
|
|
36
|
+
*/
|
|
37
|
+
static cloud(env?: Record<string, string | undefined>): {
|
|
38
|
+
url: string;
|
|
39
|
+
service?: string;
|
|
40
|
+
} | undefined;
|
|
41
|
+
protected get kind(): 'computer' | 'cloud';
|
|
33
42
|
/** The name this computer goes by in the account. */
|
|
34
43
|
static computerName(): string;
|
|
35
44
|
onStart(): void;
|
|
@@ -50,11 +59,17 @@ export declare class TaAccountService implements TaAccountServer, ApplicationLif
|
|
|
50
59
|
info(): Promise<TaAccountInfo>;
|
|
51
60
|
signIn(provider: string, service?: string): Promise<TaAccountInfo>;
|
|
52
61
|
protected begin(provider: string, service?: string): Promise<TaAccountInfo>;
|
|
62
|
+
joinAccount(service: string, token: string): Promise<TaAccountInfo>;
|
|
53
63
|
cancelSignIn(): Promise<TaAccountInfo>;
|
|
54
64
|
protected act(run: (agent: AccountAgent) => Promise<AccountView>): Promise<TaAccountInfo>;
|
|
55
65
|
approve(id: string): Promise<TaAccountInfo>;
|
|
56
66
|
remove(id: string): Promise<TaAccountInfo>;
|
|
57
67
|
signOut(): Promise<TaAccountInfo>;
|
|
68
|
+
/** The account this computer is signed in to. */
|
|
69
|
+
account(): {
|
|
70
|
+
id: string;
|
|
71
|
+
name: string;
|
|
72
|
+
} | undefined;
|
|
58
73
|
/** This computer in the account, once let in, for packages (TaExtensionHost.account). */
|
|
59
74
|
self(): {
|
|
60
75
|
id: string;
|
|
@@ -35,6 +35,18 @@ let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
|
35
35
|
get file() {
|
|
36
36
|
return join(TaBrand.agentDir(), 'account.json');
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* A Ta in the cloud (docs/design/ta-me.md 2): its gateway says so, with where the sandbox reaches the gateway's
|
|
40
|
+
* accounts (TA_ACCOUNT_URL) and the address the devices know it by (TA_ACCOUNT_SERVICE).
|
|
41
|
+
*/
|
|
42
|
+
static cloud(env = process.env) {
|
|
43
|
+
const trim = (value) => (value === null || value === void 0 ? void 0 : value.trim().replace(/\/+$/, '')) || undefined;
|
|
44
|
+
const url = env.TA_ACCOUNT_KIND === 'cloud' ? trim(env.TA_ACCOUNT_URL) : undefined;
|
|
45
|
+
return url ? { url, service: trim(env.TA_ACCOUNT_SERVICE) } : undefined;
|
|
46
|
+
}
|
|
47
|
+
get kind() {
|
|
48
|
+
return TaAccountService_1.cloud() ? 'cloud' : 'computer';
|
|
49
|
+
}
|
|
38
50
|
/** The name this computer goes by in the account. */
|
|
39
51
|
static computerName() {
|
|
40
52
|
return hostname().replace(/\.local$/, '');
|
|
@@ -174,7 +186,7 @@ let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
|
174
186
|
this.error = answer.error;
|
|
175
187
|
return SYNC_MS;
|
|
176
188
|
}
|
|
177
|
-
const kept = await AccountAgent.create(signing.way.service, answer.token, answer.account,
|
|
189
|
+
const kept = await AccountAgent.create(signing.way.service, answer.token, answer.account, this.kind, TaAccountService_1.computerName());
|
|
178
190
|
this.write(kept);
|
|
179
191
|
this.agent = this.agentOf(kept);
|
|
180
192
|
this.view = await this.agent.sync();
|
|
@@ -210,7 +222,7 @@ let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
|
210
222
|
const pairings = this.agent.state.pairings;
|
|
211
223
|
const devices = view.devices.map(({ trusted: _trusted, ...device }) => ({
|
|
212
224
|
...device,
|
|
213
|
-
...(device.kind === 'computer' && pairings[device.id] ? { pairing: pairings[device.id] } : {}),
|
|
225
|
+
...((device.kind === 'computer' || device.kind === 'cloud') && pairings[device.id] ? { pairing: pairings[device.id] } : {}),
|
|
214
226
|
}));
|
|
215
227
|
return { service, providers: [], account: view.account, waiting: view.waiting, code: view.code, devices, ...error };
|
|
216
228
|
}
|
|
@@ -232,6 +244,34 @@ let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
|
232
244
|
this.schedule(start.interval * 1000);
|
|
233
245
|
return this.info();
|
|
234
246
|
}
|
|
247
|
+
joinAccount(service, token) {
|
|
248
|
+
return TaErrors.guard(async () => {
|
|
249
|
+
var _a;
|
|
250
|
+
if (typeof service !== 'string' || !/^https?:\/\//.test(service) || typeof token !== 'string' || !token || token.length > 400) {
|
|
251
|
+
throw TaErrors.invalidArgument('A service and a token of the account are needed.', 'Scan the code of this computer again.');
|
|
252
|
+
}
|
|
253
|
+
const cloud = TaAccountService_1.cloud();
|
|
254
|
+
if ((cloud === null || cloud === void 0 ? void 0 : cloud.service) && cloud.service !== service.replace(/\/+$/, '')) {
|
|
255
|
+
throw TaErrors.invalidArgument('This Ta in the cloud joins the account of its gateway only.', 'Sign in with the account of its gateway.');
|
|
256
|
+
}
|
|
257
|
+
// A Ta in the cloud reaches its gateway's accounts from within the deployment.
|
|
258
|
+
const at = (_a = cloud === null || cloud === void 0 ? void 0 : cloud.url) !== null && _a !== void 0 ? _a : service.replace(/\/+$/, '');
|
|
259
|
+
if (this.agent) {
|
|
260
|
+
if (this.agent.state.service !== at) {
|
|
261
|
+
throw TaErrors.invalidArgument('This computer is signed in to another account.', 'Sign out of it on this computer first.');
|
|
262
|
+
}
|
|
263
|
+
return this.info();
|
|
264
|
+
}
|
|
265
|
+
this.signingIn = undefined;
|
|
266
|
+
const kept = await AccountAgent.create(at, token, { id: '', name: '' }, this.kind, TaAccountService_1.computerName());
|
|
267
|
+
this.agent = this.agentOf(kept);
|
|
268
|
+
this.view = await this.agent.sync();
|
|
269
|
+
this.write(this.agent.state);
|
|
270
|
+
this.error = undefined;
|
|
271
|
+
this.schedule(WAITING_SYNC_MS);
|
|
272
|
+
return this.info();
|
|
273
|
+
});
|
|
274
|
+
}
|
|
235
275
|
async cancelSignIn() {
|
|
236
276
|
this.signingIn = undefined;
|
|
237
277
|
return this.info();
|
|
@@ -262,6 +302,12 @@ let TaAccountService = TaAccountService_1 = class TaAccountService {
|
|
|
262
302
|
return this.info();
|
|
263
303
|
});
|
|
264
304
|
}
|
|
305
|
+
/** The account this computer is signed in to. */
|
|
306
|
+
account() {
|
|
307
|
+
var _a, _b;
|
|
308
|
+
var _c;
|
|
309
|
+
return (_c = (_a = this.view) === null || _a === void 0 ? void 0 : _a.account) !== null && _c !== void 0 ? _c : (_b = this.agent) === null || _b === void 0 ? void 0 : _b.state.account;
|
|
310
|
+
}
|
|
265
311
|
/** This computer in the account, once let in, for packages (TaExtensionHost.account). */
|
|
266
312
|
self() {
|
|
267
313
|
const me = this.view && !this.view.waiting ? this.view.devices.find(one => one.self) : undefined;
|
package/esm/node/index.d.ts
CHANGED
package/esm/node/index.js
CHANGED
|
@@ -70,7 +70,14 @@ export declare class PackageHostsImpl implements PackageHosts, ApplicationLifecy
|
|
|
70
70
|
protected readonly teamProvider: TaHostTeamProvider;
|
|
71
71
|
protected readonly running: Map<string, Running>;
|
|
72
72
|
protected extensionHost?: TaExtensionHost;
|
|
73
|
-
|
|
73
|
+
/** The first {@link sync}, begun once the server listens: what asks a host waits for it. */
|
|
74
|
+
protected starting: Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Not awaited: importing two dozen hosts took half of the start of ta serve (60 of 115 ms), and nothing the server
|
|
77
|
+
* answers first needs them. They start after it listens, one a turn of the event loop, so that the first page comes
|
|
78
|
+
* between; {@link answer} and {@link call} wait for them.
|
|
79
|
+
*/
|
|
80
|
+
onStart(): void;
|
|
74
81
|
onStop(): void;
|
|
75
82
|
sync(): Promise<void>;
|
|
76
83
|
answer(name: string): Promise<Omit<TaHostAnswer, 'files'>>;
|
|
@@ -93,9 +93,18 @@ export var PackageHostUtil;
|
|
|
93
93
|
let PackageHostsImpl = class PackageHostsImpl {
|
|
94
94
|
constructor() {
|
|
95
95
|
this.running = new Map();
|
|
96
|
+
/** The first {@link sync}, begun once the server listens: what asks a host waits for it. */
|
|
97
|
+
this.starting = Promise.resolve();
|
|
96
98
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Not awaited: importing two dozen hosts took half of the start of ta serve (60 of 115 ms), and nothing the server
|
|
101
|
+
* answers first needs them. They start after it listens, one a turn of the event loop, so that the first page comes
|
|
102
|
+
* between; {@link answer} and {@link call} wait for them.
|
|
103
|
+
*/
|
|
104
|
+
onStart() {
|
|
105
|
+
this.starting = new Promise(resolve => setImmediate(resolve))
|
|
106
|
+
.then(() => this.sync())
|
|
107
|
+
.catch(error => console.error(`The hosts of the packages failed to start: ${error instanceof Error ? error.message : error}`));
|
|
99
108
|
}
|
|
100
109
|
onStop() {
|
|
101
110
|
for (const name of [...this.running.keys()]) {
|
|
@@ -109,18 +118,23 @@ let PackageHostsImpl = class PackageHostsImpl {
|
|
|
109
118
|
await this.stop(name);
|
|
110
119
|
}
|
|
111
120
|
}
|
|
112
|
-
|
|
121
|
+
const started = [];
|
|
122
|
+
for (const [name, file] of on) {
|
|
113
123
|
if (file && !this.running.has(name)) {
|
|
114
|
-
|
|
124
|
+
started.push(this.start(name, file).service);
|
|
125
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
115
126
|
}
|
|
116
|
-
}
|
|
127
|
+
}
|
|
128
|
+
await Promise.all(started);
|
|
117
129
|
}
|
|
118
130
|
async answer(name) {
|
|
119
131
|
var _a;
|
|
132
|
+
await this.starting;
|
|
120
133
|
return { host: this.hostOf(name), service: await ((_a = this.running.get(name)) === null || _a === void 0 ? void 0 : _a.service) };
|
|
121
134
|
}
|
|
122
135
|
async call(name, method, params) {
|
|
123
136
|
var _a;
|
|
137
|
+
await this.starting;
|
|
124
138
|
const service = (await ((_a = this.running.get(name)) === null || _a === void 0 ? void 0 : _a.service));
|
|
125
139
|
if (!service) {
|
|
126
140
|
throw TaErrors.invalidArgument(`The package ${name} has no host running.`, 'Only packages that come with Ta and are on have one, under ta serve; turn it on, or see the log of ta serve for why it failed.');
|
|
@@ -217,11 +231,14 @@ let PackageHostsImpl = class PackageHostsImpl {
|
|
|
217
231
|
openSession: options => server.openSession(options),
|
|
218
232
|
closeSession: sessionId => server.closeSession(sessionId),
|
|
219
233
|
setSessionName: (sessionId, name) => server.setSessionName(sessionId, name),
|
|
234
|
+
setModel: (sessionId, provider, modelId) => server.setModel(sessionId, provider, modelId),
|
|
220
235
|
prompt: (sessionId, input) => server.prompt(sessionId, input),
|
|
221
236
|
followUp: (sessionId, input) => server.followUp(sessionId, input),
|
|
222
237
|
abort: sessionId => server.abort(sessionId),
|
|
223
238
|
getState: sessionId => server.getState(sessionId),
|
|
224
239
|
getLastAssistantText: sessionId => server.getLastAssistantText(sessionId),
|
|
240
|
+
getSessionStats: sessionId => server.getSessionStats(sessionId),
|
|
241
|
+
getEntries: (sessionId, since) => server.getEntries(sessionId, since),
|
|
225
242
|
relocateDirectory: (from, to) => server.relocateDirectory(from, to),
|
|
226
243
|
getExtensionData: (name, key, cwd) => packages.getExtensionData(name, key, cwd),
|
|
227
244
|
setExtensionData: (name, key, value, cwd) => packages.setExtensionData(name, key, value, cwd),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Application, ApplicationLifecycle } from '@celljs/core';
|
|
2
2
|
import { type RpcConnection } from '@celljs/rpc/lib/node';
|
|
3
|
-
import { type TaFriend, type TaFriendRequest, type TaFriendShare, type TaGroupInvite, type TaIdentity, type TaRoomEvent, type TaRoomInfo, type TaRoomRequest, TaRoomServer, type TaRooms, type TaRoomValue, type TaRoomWatch } from '@celljs/ta-protocol';
|
|
3
|
+
import { type TaFriend, type TaFriendRequest, type TaFriendShare, type TaGroupInvite, type TaIdentity, type TaPictures, type TaRoomEvent, type TaRoomInfo, type TaRoomRequest, TaRoomServer, type TaRooms, type TaRoomValue, type TaRoomWatch } from '@celljs/ta-protocol';
|
|
4
4
|
import type { RelayHubs } from '@celljs/ta-relay/lib/node';
|
|
5
5
|
import { ExtensionDataStore } from './extension-data.js';
|
|
6
6
|
import { type RelayBridge } from './relay-bridge.js';
|
|
@@ -21,12 +21,19 @@ export declare class RoomsBridge implements ApplicationLifecycle<Application> {
|
|
|
21
21
|
readonly rooms: TaRooms;
|
|
22
22
|
onStart(): void;
|
|
23
23
|
onStop(): void;
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* The look of this Ta others see is the double's, as the user picked it on a page (TA_DOUBLE_LOOK); till then the
|
|
26
|
+
* kind its identity is given, kept there for the pages to draw the double as (setting it comes back here).
|
|
27
|
+
*/
|
|
25
28
|
protected keepLook(): Promise<void>;
|
|
26
29
|
/** `watch` undefined: no more. */
|
|
27
30
|
watch(connection: RpcConnection, watch: TaRoomWatch | undefined): void;
|
|
28
31
|
protected broadcast(event: TaRoomEvent): void;
|
|
29
|
-
|
|
32
|
+
/** The picture of this Ta, for what is said of it after a change that does not give it. */
|
|
33
|
+
protected picture(): Promise<{
|
|
34
|
+
picture?: string;
|
|
35
|
+
}>;
|
|
36
|
+
protected static identity({ id, name, look, shared, picture }: TaIdentity): TaIdentity;
|
|
30
37
|
}
|
|
31
38
|
/** The rooms for clients (TaRoomServer): those of RoomsBridge, what goes wrong said as the errors of Ta. */
|
|
32
39
|
export declare class TaRoomServerImpl implements TaRoomServer {
|
|
@@ -34,6 +41,8 @@ export declare class TaRoomServerImpl implements TaRoomServer {
|
|
|
34
41
|
protected get rooms(): TaRooms;
|
|
35
42
|
getIdentity(): Promise<TaIdentity>;
|
|
36
43
|
setName(name: string): Promise<TaIdentity>;
|
|
44
|
+
setPicture(picture: string | null): Promise<TaIdentity>;
|
|
45
|
+
listPictures(): Promise<TaPictures>;
|
|
37
46
|
listRooms(): Promise<TaRoomInfo[]>;
|
|
38
47
|
createRoom(input: {
|
|
39
48
|
kind: string;
|
|
@@ -55,6 +64,8 @@ export declare class TaRoomServerImpl implements TaRoomServer {
|
|
|
55
64
|
removeMember(room: string, member: string): Promise<void>;
|
|
56
65
|
configureRoom(room: string, options: {
|
|
57
66
|
members?: number;
|
|
67
|
+
admins?: string[];
|
|
68
|
+
rules?: Record<string, boolean>;
|
|
58
69
|
}): Promise<TaRoomInfo>;
|
|
59
70
|
leave(room: string): Promise<void>;
|
|
60
71
|
send(room: string, data: unknown, to?: string): Promise<void>;
|
package/esm/node/rooms-bridge.js
CHANGED
|
@@ -11,7 +11,7 @@ var RoomsBridge_1;
|
|
|
11
11
|
import { ApplicationLifecycle, Autowired, Component } from '@celljs/core';
|
|
12
12
|
import { Rpc } from '@celljs/rpc';
|
|
13
13
|
import { RpcContext } from '@celljs/rpc/lib/node';
|
|
14
|
-
import { TA_DOUBLE_LOOK, TaClient, TaRoomServer, TaRoomWatchUtil, } from '@celljs/ta-protocol';
|
|
14
|
+
import { TA_DOUBLE_LOOK, TaClient, TaDoubleLookUtil, TaRoomServer, TaRoomWatchUtil, } from '@celljs/ta-protocol';
|
|
15
15
|
import { ExtensionDataStore } from './extension-data.js';
|
|
16
16
|
import { RELAY_BRIDGE } from './relay-bridge.js';
|
|
17
17
|
import { TaErrors } from './ta-errors.js';
|
|
@@ -28,11 +28,13 @@ let RoomsBridge = RoomsBridge_1 = class RoomsBridge {
|
|
|
28
28
|
this.watchers = new Map();
|
|
29
29
|
this.rooms = {
|
|
30
30
|
getIdentity: async () => RoomsBridge_1.identity(await this.hubs.identity()),
|
|
31
|
-
setName: async (name) => RoomsBridge_1.identity(await this.hubs.setName(name)),
|
|
31
|
+
setName: async (name) => RoomsBridge_1.identity({ ...(await this.hubs.setName(name)), ...(await this.picture()) }),
|
|
32
|
+
setPicture: async (picture) => RoomsBridge_1.identity(await this.hubs.setPicture(picture)),
|
|
33
|
+
listPictures: () => this.hubs.pictures(),
|
|
32
34
|
listRooms: async () => this.hubs.list(),
|
|
33
35
|
createRoom: input => this.hubs.create(input),
|
|
34
36
|
invite: (room, options) => this.hubs.invite(room, options !== null && options !== void 0 ? options : {}),
|
|
35
|
-
configureRoom:
|
|
37
|
+
configureRoom: (room, options) => this.hubs.configure(room, options !== null && options !== void 0 ? options : {}),
|
|
36
38
|
join: link => this.hubs.join(link),
|
|
37
39
|
listRequests: () => this.hubs.requests(),
|
|
38
40
|
answer: (room, ask, accept) => this.hubs.answer(room, ask, accept),
|
|
@@ -82,10 +84,20 @@ let RoomsBridge = RoomsBridge_1 = class RoomsBridge {
|
|
|
82
84
|
(_a = this.stopLook) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
83
85
|
this.hubs.stop();
|
|
84
86
|
}
|
|
85
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* The look of this Ta others see is the double's, as the user picked it on a page (TA_DOUBLE_LOOK); till then the
|
|
89
|
+
* kind its identity is given, kept there for the pages to draw the double as (setting it comes back here).
|
|
90
|
+
*/
|
|
86
91
|
async keepLook() {
|
|
87
92
|
const look = await this.data.get(TA_DOUBLE_LOOK.name, TA_DOUBLE_LOOK.key).catch(() => undefined);
|
|
88
|
-
|
|
93
|
+
if (typeof look === 'string' && look) {
|
|
94
|
+
await this.hubs.setLook(look).catch(() => undefined);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const identity = await this.hubs.identity().catch(() => undefined);
|
|
98
|
+
if (identity) {
|
|
99
|
+
await this.data.set(TA_DOUBLE_LOOK.name, TA_DOUBLE_LOOK.key, TaDoubleLookUtil.given(identity.id)).catch(() => undefined);
|
|
100
|
+
}
|
|
89
101
|
}
|
|
90
102
|
/** `watch` undefined: no more. */
|
|
91
103
|
watch(connection, watch) {
|
|
@@ -108,8 +120,13 @@ let RoomsBridge = RoomsBridge_1 = class RoomsBridge {
|
|
|
108
120
|
}
|
|
109
121
|
}
|
|
110
122
|
}
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
/** The picture of this Ta, for what is said of it after a change that does not give it. */
|
|
124
|
+
async picture() {
|
|
125
|
+
const { picture } = await this.hubs.identity();
|
|
126
|
+
return picture ? { picture } : {};
|
|
127
|
+
}
|
|
128
|
+
static identity({ id, name, look, shared, picture }) {
|
|
129
|
+
return { id, name, ...(look ? { look } : {}), ...(shared ? { shared } : {}), ...(picture ? { picture } : {}) };
|
|
113
130
|
}
|
|
114
131
|
};
|
|
115
132
|
__decorate([
|
|
@@ -135,6 +152,12 @@ let TaRoomServerImpl = class TaRoomServerImpl {
|
|
|
135
152
|
setName(name) {
|
|
136
153
|
return TaErrors.guard(() => this.rooms.setName(name));
|
|
137
154
|
}
|
|
155
|
+
setPicture(picture) {
|
|
156
|
+
return TaErrors.guard(() => this.rooms.setPicture(picture));
|
|
157
|
+
}
|
|
158
|
+
listPictures() {
|
|
159
|
+
return TaErrors.guard(() => this.rooms.listPictures());
|
|
160
|
+
}
|
|
138
161
|
listRooms() {
|
|
139
162
|
return TaErrors.guard(() => this.rooms.listRooms());
|
|
140
163
|
}
|
|
@@ -157,7 +180,11 @@ let TaRoomServerImpl = class TaRoomServerImpl {
|
|
|
157
180
|
return TaErrors.guard(() => this.rooms.removeMember(room, member));
|
|
158
181
|
}
|
|
159
182
|
configureRoom(room, options) {
|
|
160
|
-
return TaErrors.guard(() => this.rooms.configureRoom(room, {
|
|
183
|
+
return TaErrors.guard(() => this.rooms.configureRoom(room, {
|
|
184
|
+
...((options === null || options === void 0 ? void 0 : options.members) === undefined ? {} : { members: options.members }),
|
|
185
|
+
...(Array.isArray(options === null || options === void 0 ? void 0 : options.admins) ? { admins: options.admins.map(String) } : {}),
|
|
186
|
+
...((options === null || options === void 0 ? void 0 : options.rules) && typeof options.rules === 'object' ? { rules: options.rules } : {}),
|
|
187
|
+
}));
|
|
161
188
|
}
|
|
162
189
|
leave(room) {
|
|
163
190
|
return TaErrors.guard(() => this.rooms.leave(room));
|
|
@@ -11,7 +11,7 @@ import { mkdir, readFile, rename, rmdir, stat, writeFile } from 'node:fs/promise
|
|
|
11
11
|
import { dirname, join } from 'node:path';
|
|
12
12
|
import { CODING_AGENT_OPTIONS, ModelRuntimeProvider, PiSdkUtil } from '@celljs/ai-coding-agent';
|
|
13
13
|
import { Autowired, Component, Value } from '@celljs/core';
|
|
14
|
-
import { TA_AGENT_API_PREFIX, TA_SETTINGS } from '@celljs/ta-protocol';
|
|
14
|
+
import { TA_AGENT_API_PREFIX, TA_AGENT_CONFINED_API_SUFFIX, TA_SETTINGS, } from '@celljs/ta-protocol';
|
|
15
15
|
import { ExtensionProviders } from './extension-providers.js';
|
|
16
16
|
import { TaSessionRegistry } from './host-protocol.js';
|
|
17
17
|
import { PiDirectories } from './remote/remote-protocol.js';
|
|
@@ -314,7 +314,9 @@ let TaSettingsStoreImpl = class TaSettingsStoreImpl {
|
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
316
|
const name = (model) => `${model.provider}/${model.id}`;
|
|
317
|
-
const agents = available
|
|
317
|
+
const agents = available
|
|
318
|
+
.filter(model => String(model.api).startsWith(TA_AGENT_API_PREFIX) && !String(model.api).endsWith(TA_AGENT_CONFINED_API_SUFFIX))
|
|
319
|
+
.map(name);
|
|
318
320
|
return { models: available.map(name), agents, modelNames, providerNames };
|
|
319
321
|
}
|
|
320
322
|
catch (_a) {
|
|
@@ -176,6 +176,8 @@ const RULES = {
|
|
|
176
176
|
getAccount: fixed('settings.view'),
|
|
177
177
|
signIn: fixed('settings.manage'),
|
|
178
178
|
cancelSignIn: fixed('settings.manage'),
|
|
179
|
+
// Gives the devices of the account a way into this computer: the owner's alone.
|
|
180
|
+
joinAccount: owner,
|
|
179
181
|
approve: fixed('settings.manage'),
|
|
180
182
|
remove: fixed('settings.manage'),
|
|
181
183
|
signOut: fixed('settings.manage'),
|
|
@@ -205,6 +207,8 @@ const RULES = {
|
|
|
205
207
|
TaRoomServer: Object.fromEntries([
|
|
206
208
|
'getIdentity',
|
|
207
209
|
'setName',
|
|
210
|
+
'setPicture',
|
|
211
|
+
'listPictures',
|
|
208
212
|
'listRooms',
|
|
209
213
|
'createRoom',
|
|
210
214
|
'invite',
|