@celljs/ta-host 4.13.1 → 4.15.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/README.en.md +3 -1
- package/README.md +3 -1
- package/esm/node/auth-server-impl.d.ts +12 -2
- package/esm/node/auth-server-impl.js +102 -3
- package/esm/node/custom-providers.d.ts +50 -0
- package/esm/node/custom-providers.js +335 -0
- package/esm/node/host-protocol.d.ts +17 -1
- package/esm/node/host-protocol.js +1 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/package-hosts.d.ts +2 -1
- package/esm/node/package-hosts.js +6 -2
- package/esm/node/presence-book.d.ts +38 -0
- package/esm/node/presence-book.js +159 -0
- package/esm/node/presence-server-impl.d.ts +13 -0
- package/esm/node/presence-server-impl.js +45 -0
- package/esm/node/rooms-bridge.d.ts +6 -1
- package/esm/node/rooms-bridge.js +21 -3
- package/esm/node/session-list-feed.js +2 -1
- package/esm/node/sign-in/access-server-impl.d.ts +5 -1
- package/esm/node/sign-in/access-server-impl.js +22 -1
- package/esm/node/sign-in/sign-in-flow.d.ts +14 -1
- package/esm/node/sign-in/sign-in-flow.js +24 -7
- package/esm/node/sign-in/sign-in-provider.d.ts +84 -1
- package/esm/node/sign-in/sign-in-provider.js +194 -7
- package/esm/node/sign-in/ta-sign-in.d.ts +12 -3
- package/esm/node/sign-in/ta-sign-in.js +36 -4
- package/esm/node/ta-server-impl.d.ts +7 -0
- package/esm/node/ta-server-impl.js +44 -7
- package/esm/node/ta-session-registry.d.ts +10 -3
- package/esm/node/ta-session-registry.js +29 -3
- package/esm/node/ta-session.d.ts +2 -0
- package/esm/node/ta-session.js +6 -0
- package/esm/node/team/host-team.d.ts +2 -1
- package/esm/node/team/host-team.js +11 -1
- package/esm/node/team/index.d.ts +1 -0
- package/esm/node/team/index.js +1 -0
- package/esm/node/team/member-accounts.d.ts +30 -0
- package/esm/node/team/member-accounts.js +117 -0
- package/esm/node/team/team-calls.d.ts +2 -1
- package/esm/node/team/team-calls.js +11 -0
- package/esm/node/team/team-gate.js +1 -1
- package/esm/node/team/team-protocol.d.ts +5 -1
- package/esm/node/team/team-protocol.js +2 -0
- package/esm/node/web-extension-util.js +1 -0
- package/lib/node/auth-server-impl.d.ts +12 -2
- package/lib/node/auth-server-impl.js +101 -2
- package/lib/node/custom-providers.d.ts +51 -0
- package/lib/node/custom-providers.js +338 -0
- package/lib/node/host-protocol.d.ts +17 -1
- package/lib/node/host-protocol.js +2 -1
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/package-hosts.d.ts +2 -1
- package/lib/node/package-hosts.js +5 -1
- package/lib/node/presence-book.d.ts +39 -0
- package/lib/node/presence-book.js +162 -0
- package/lib/node/presence-server-impl.d.ts +14 -0
- package/lib/node/presence-server-impl.js +48 -0
- package/lib/node/rooms-bridge.d.ts +6 -1
- package/lib/node/rooms-bridge.js +20 -2
- package/lib/node/session-list-feed.js +2 -1
- package/lib/node/sign-in/access-server-impl.d.ts +5 -1
- package/lib/node/sign-in/access-server-impl.js +21 -0
- package/lib/node/sign-in/sign-in-flow.d.ts +14 -1
- package/lib/node/sign-in/sign-in-flow.js +24 -7
- package/lib/node/sign-in/sign-in-provider.d.ts +84 -1
- package/lib/node/sign-in/sign-in-provider.js +197 -9
- package/lib/node/sign-in/ta-sign-in.d.ts +12 -3
- package/lib/node/sign-in/ta-sign-in.js +36 -4
- package/lib/node/ta-server-impl.d.ts +7 -0
- package/lib/node/ta-server-impl.js +43 -6
- package/lib/node/ta-session-registry.d.ts +10 -3
- package/lib/node/ta-session-registry.js +27 -1
- package/lib/node/ta-session.d.ts +2 -0
- package/lib/node/ta-session.js +6 -0
- package/lib/node/team/host-team.d.ts +2 -1
- package/lib/node/team/host-team.js +10 -0
- package/lib/node/team/index.d.ts +1 -0
- package/lib/node/team/index.js +1 -0
- package/lib/node/team/member-accounts.d.ts +31 -0
- package/lib/node/team/member-accounts.js +120 -0
- package/lib/node/team/team-calls.d.ts +2 -1
- package/lib/node/team/team-calls.js +11 -0
- package/lib/node/team/team-gate.js +1 -1
- package/lib/node/team/team-protocol.d.ts +5 -1
- package/lib/node/team/team-protocol.js +3 -1
- package/lib/node/web-extension-util.js +1 -0
- package/package.json +12 -12
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TaHostTeam } from '@celljs/ta-protocol';
|
|
2
|
-
import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry } from './team-protocol.js';
|
|
2
|
+
import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberAccounts, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry } from './team-protocol.js';
|
|
3
3
|
/** The face of the parts of ta serve that let people in, as the server side of packages gets it (TaExtensionHost.team). */
|
|
4
4
|
export declare class DefaultTaHostTeamProvider implements TaHostTeamProvider {
|
|
5
5
|
protected readonly principals: TaPrincipals;
|
|
@@ -9,5 +9,6 @@ export declare class DefaultTaHostTeamProvider implements TaHostTeamProvider {
|
|
|
9
9
|
protected readonly permissions: TaPackagePermissions;
|
|
10
10
|
protected readonly devices: TaMemberDevices;
|
|
11
11
|
protected readonly connections: TaConnections;
|
|
12
|
+
protected readonly accounts: TaMemberAccounts;
|
|
12
13
|
readonly team: TaHostTeam;
|
|
13
14
|
}
|
|
@@ -8,7 +8,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { Autowired, Component } from '@celljs/core';
|
|
11
|
-
import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry, } from './team-protocol.js';
|
|
11
|
+
import { TaAuthority, TaConnections, TaHostTeamProvider, TaMemberAccounts, TaMemberDevices, TaPackagePermissions, TaPrincipals, TaRunActors, TaSharedDataRegistry, } from './team-protocol.js';
|
|
12
12
|
/** The face of the parts of ta serve that let people in, as the server side of packages gets it (TaExtensionHost.team). */
|
|
13
13
|
let DefaultTaHostTeamProvider = class DefaultTaHostTeamProvider {
|
|
14
14
|
constructor() {
|
|
@@ -31,6 +31,12 @@ let DefaultTaHostTeamProvider = class DefaultTaHostTeamProvider {
|
|
|
31
31
|
pair: (principal, name, expiresAt) => this.devices.pair(principal, name, expiresAt),
|
|
32
32
|
unpair: deviceId => this.devices.unpair(deviceId),
|
|
33
33
|
devices: () => this.devices.list(),
|
|
34
|
+
accounts: {
|
|
35
|
+
providers: () => this.accounts.providers(),
|
|
36
|
+
start: (provider, redirectUri) => this.accounts.start(provider, redirectUri),
|
|
37
|
+
verify: (state, code) => this.accounts.verify(state, code),
|
|
38
|
+
typed: (state, waitMs) => this.accounts.typed(state, waitMs),
|
|
39
|
+
},
|
|
34
40
|
};
|
|
35
41
|
}
|
|
36
42
|
};
|
|
@@ -62,6 +68,10 @@ __decorate([
|
|
|
62
68
|
Autowired(TaConnections),
|
|
63
69
|
__metadata("design:type", Object)
|
|
64
70
|
], DefaultTaHostTeamProvider.prototype, "connections", void 0);
|
|
71
|
+
__decorate([
|
|
72
|
+
Autowired(TaMemberAccounts),
|
|
73
|
+
__metadata("design:type", Object)
|
|
74
|
+
], DefaultTaHostTeamProvider.prototype, "accounts", void 0);
|
|
65
75
|
DefaultTaHostTeamProvider = __decorate([
|
|
66
76
|
Component(TaHostTeamProvider)
|
|
67
77
|
], DefaultTaHostTeamProvider);
|
package/esm/node/team/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './authority.js';
|
|
|
2
2
|
export * from './call-util.js';
|
|
3
3
|
export * from './connections.js';
|
|
4
4
|
export * from './host-team.js';
|
|
5
|
+
export * from './member-accounts.js';
|
|
5
6
|
export * from './member-devices.js';
|
|
6
7
|
export * from './package-permissions.js';
|
|
7
8
|
export * from './principals.js';
|
package/esm/node/team/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './authority.js';
|
|
|
2
2
|
export * from './call-util.js';
|
|
3
3
|
export * from './connections.js';
|
|
4
4
|
export * from './host-team.js';
|
|
5
|
+
export * from './member-accounts.js';
|
|
5
6
|
export * from './member-devices.js';
|
|
6
7
|
export * from './package-permissions.js';
|
|
7
8
|
export * from './principals.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { TaAccount, TaAccountStart, TaSignInProvider } from '@celljs/ta-protocol';
|
|
2
|
+
import { type DeviceSignInProvider, type SignInAccount } from '../sign-in/sign-in-provider.js';
|
|
3
|
+
import { type TaSignIn } from '../sign-in/ta-sign-in.js';
|
|
4
|
+
import { TaMemberAccounts } from './team-protocol.js';
|
|
5
|
+
/** A sign-in by a code, its device code kept here: who types the code sees it alone. */
|
|
6
|
+
interface Typing {
|
|
7
|
+
provider: DeviceSignInProvider;
|
|
8
|
+
deviceCode: string;
|
|
9
|
+
interval: number;
|
|
10
|
+
next: number;
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
account?: TaAccount;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Who one asking to come in is, by an account of the providers ta serve signs in with (TaSignIn), or by a code typed at
|
|
16
|
+
* GitHub with Ta's own app when GitHub is not set up (TaSignIn.device): nothing let in by it.
|
|
17
|
+
*/
|
|
18
|
+
export declare class DefaultTaMemberAccounts implements TaMemberAccounts {
|
|
19
|
+
protected readonly flow: TaSignIn;
|
|
20
|
+
protected readonly typing: Map<string, Typing>;
|
|
21
|
+
providers(): TaSignInProvider[];
|
|
22
|
+
start(provider: string, redirectUri: string): Promise<TaAccountStart>;
|
|
23
|
+
verify(state: string, code: string): Promise<TaAccount>;
|
|
24
|
+
typed(state: string, waitMs?: number): Promise<TaAccount | undefined>;
|
|
25
|
+
/** GitHub by a code, when ta serve has no GitHub of its own. */
|
|
26
|
+
protected device(): DeviceSignInProvider | undefined;
|
|
27
|
+
protected startTyping(device: DeviceSignInProvider): Promise<TaAccountStart>;
|
|
28
|
+
protected of({ id, name, avatar }: SignInAccount): TaAccount;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
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 { randomBytes } from 'node:crypto';
|
|
11
|
+
import { Autowired, Component } from '@celljs/core';
|
|
12
|
+
import { SignInError } from '../sign-in/sign-in-provider.js';
|
|
13
|
+
import { TA_SIGN_IN } from '../sign-in/ta-sign-in.js';
|
|
14
|
+
import { TaMemberAccounts } from './team-protocol.js';
|
|
15
|
+
/** Sign-ins by a code under way at once: anyone holding a link may start one. */
|
|
16
|
+
const MAX_TYPING = 200;
|
|
17
|
+
const WAIT_MS = 25000;
|
|
18
|
+
/**
|
|
19
|
+
* Who one asking to come in is, by an account of the providers ta serve signs in with (TaSignIn), or by a code typed at
|
|
20
|
+
* GitHub with Ta's own app when GitHub is not set up (TaSignIn.device): nothing let in by it.
|
|
21
|
+
*/
|
|
22
|
+
let DefaultTaMemberAccounts = class DefaultTaMemberAccounts {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.typing = new Map();
|
|
25
|
+
}
|
|
26
|
+
providers() {
|
|
27
|
+
const own = this.flow.providers.map(({ id, name }) => ({ id, name }));
|
|
28
|
+
const device = this.device();
|
|
29
|
+
return device ? [...own, { id: device.id, name: device.name, device: true }] : own;
|
|
30
|
+
}
|
|
31
|
+
async start(provider, redirectUri) {
|
|
32
|
+
const device = this.device();
|
|
33
|
+
if ((device === null || device === void 0 ? void 0 : device.id) === provider) {
|
|
34
|
+
return this.startTyping(device);
|
|
35
|
+
}
|
|
36
|
+
if (!/^https?:\/\/[^/?#]+\/[^?#]*$/.test(redirectUri)) {
|
|
37
|
+
throw new Error(`Not an address to come back to: ${redirectUri}`);
|
|
38
|
+
}
|
|
39
|
+
return this.flow.startVerify(provider, redirectUri);
|
|
40
|
+
}
|
|
41
|
+
async verify(state, code) {
|
|
42
|
+
return this.of(await this.flow.verify(state, code));
|
|
43
|
+
}
|
|
44
|
+
async typed(state, waitMs = WAIT_MS) {
|
|
45
|
+
const typing = this.typing.get(state);
|
|
46
|
+
if (!typing) {
|
|
47
|
+
throw new SignInError('The sign-in expired or was not started here');
|
|
48
|
+
}
|
|
49
|
+
const until = Date.now() + Math.min(Math.max(0, waitMs), WAIT_MS);
|
|
50
|
+
// Asked no more often than the provider says; one waiting already asks for both.
|
|
51
|
+
while (!typing.account) {
|
|
52
|
+
if (Date.now() >= typing.expiresAt) {
|
|
53
|
+
this.typing.delete(state);
|
|
54
|
+
throw new SignInError('The code ran out: sign in again');
|
|
55
|
+
}
|
|
56
|
+
const sleep = typing.next - Date.now();
|
|
57
|
+
if (Date.now() + Math.max(0, sleep) > until) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
if (sleep > 0) {
|
|
61
|
+
await new Promise(resolve => setTimeout(resolve, sleep));
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
typing.next = Date.now() + typing.interval;
|
|
65
|
+
let answer;
|
|
66
|
+
try {
|
|
67
|
+
answer = await typing.provider.poll(typing.deviceCode);
|
|
68
|
+
}
|
|
69
|
+
catch (failure) {
|
|
70
|
+
// Refused, or the code is no good: over. The network failing is not, and is asked again.
|
|
71
|
+
if (failure instanceof SignInError) {
|
|
72
|
+
this.typing.delete(state);
|
|
73
|
+
}
|
|
74
|
+
throw failure;
|
|
75
|
+
}
|
|
76
|
+
if (answer === 'slowDown') {
|
|
77
|
+
typing.interval += 5000;
|
|
78
|
+
typing.next = Date.now() + typing.interval;
|
|
79
|
+
}
|
|
80
|
+
else if (answer !== 'pending') {
|
|
81
|
+
typing.account = this.of(answer);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return typing.account;
|
|
85
|
+
}
|
|
86
|
+
/** GitHub by a code, when ta serve has no GitHub of its own. */
|
|
87
|
+
device() {
|
|
88
|
+
const device = this.flow.device;
|
|
89
|
+
return device && !this.flow.providers.some(one => one.id === device.id) ? device : undefined;
|
|
90
|
+
}
|
|
91
|
+
async startTyping(device) {
|
|
92
|
+
const now = Date.now();
|
|
93
|
+
for (const [state, typing] of this.typing) {
|
|
94
|
+
if (now >= typing.expiresAt || this.typing.size >= MAX_TYPING) {
|
|
95
|
+
this.typing.delete(state);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const code = await device.code();
|
|
99
|
+
const state = randomBytes(18).toString('base64url');
|
|
100
|
+
const interval = Math.max(0, code.interval) * 1000;
|
|
101
|
+
const expiresAt = now + code.expiresIn * 1000;
|
|
102
|
+
this.typing.set(state, { provider: device, deviceCode: code.deviceCode, interval, next: now + interval, expiresAt });
|
|
103
|
+
return { state, device: { code: code.userCode, uri: code.uri, expiresAt } };
|
|
104
|
+
}
|
|
105
|
+
of({ id, name, avatar }) {
|
|
106
|
+
return { id, ...(name ? { name } : {}), ...(avatar ? { avatar } : {}) };
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
__decorate([
|
|
110
|
+
Autowired(TA_SIGN_IN),
|
|
111
|
+
__metadata("design:type", Function)
|
|
112
|
+
], DefaultTaMemberAccounts.prototype, "flow", void 0);
|
|
113
|
+
DefaultTaMemberAccounts = __decorate([
|
|
114
|
+
Component(TaMemberAccounts)
|
|
115
|
+
], DefaultTaMemberAccounts);
|
|
116
|
+
export { DefaultTaMemberAccounts };
|
|
117
|
+
//# sourceMappingURL=member-accounts.js.map
|
|
@@ -9,7 +9,8 @@ export interface TaCallNeedOne {
|
|
|
9
9
|
* directory it opens), nothing (`anyone`: any principal let in), or nothing either and open to one holding a link of
|
|
10
10
|
* invitation too (`invite`: asking to come in).
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
/** `owner`: the owner alone, whatever a role gives (secrets of the owner's, such as those of providers to sign in with). */
|
|
13
|
+
export type TaCallNeed = TaCallNeedOne | TaCallNeedOne[] | 'anyone' | 'invite' | 'owner';
|
|
13
14
|
/** What finding the resource of a call needs: where a session is, the default directory, what packages declare. */
|
|
14
15
|
export interface TaCallLookup {
|
|
15
16
|
session(sessionId: string): {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isAbsolute } from 'node:path';
|
|
2
2
|
const anyone = () => 'anyone';
|
|
3
|
+
const owner = () => 'owner';
|
|
3
4
|
const fixed = (action) => () => ({ action });
|
|
4
5
|
/** The conversation of the session of the first parameter. */
|
|
5
6
|
const session = (action) => (params, lookup) => {
|
|
@@ -213,6 +214,10 @@ const RULES = {
|
|
|
213
214
|
'watch',
|
|
214
215
|
'unwatch',
|
|
215
216
|
].map(method => [method, fixed('settings.manage')])),
|
|
217
|
+
// What a page is at: each page's own, and only of conversations it may see (TaPresenceServerImpl).
|
|
218
|
+
TaPresenceServer: {
|
|
219
|
+
report: anyone,
|
|
220
|
+
},
|
|
216
221
|
TaSettingsServer: {
|
|
217
222
|
getSettings: fixed('settings.view'),
|
|
218
223
|
updateSettings: fixed('settings.manage'),
|
|
@@ -224,8 +229,14 @@ const RULES = {
|
|
|
224
229
|
answer: fixed('models.manage'),
|
|
225
230
|
cancel: fixed('models.manage'),
|
|
226
231
|
logout: fixed('models.manage'),
|
|
232
|
+
listCustomProviders: fixed('models.manage'),
|
|
233
|
+
saveCustomProvider: fixed('models.manage'),
|
|
234
|
+
removeCustomProvider: fixed('models.manage'),
|
|
235
|
+
fetchCustomModels: fixed('models.manage'),
|
|
227
236
|
},
|
|
228
237
|
TaAccessServer: {
|
|
238
|
+
getSignInConfig: owner,
|
|
239
|
+
setSignInProvider: owner,
|
|
229
240
|
getAccess: fixed('members.view'),
|
|
230
241
|
bindUrl: fixed('members.roles'),
|
|
231
242
|
allow: fixed('members.roles'),
|
|
@@ -68,7 +68,7 @@ let TaTeamGate = class TaTeamGate {
|
|
|
68
68
|
packageAction: (pkg, method, kind) => this.permissions.host(pkg, method, kind),
|
|
69
69
|
});
|
|
70
70
|
// A call of no rule is the owner's; one holding a link of invitation only asks to come in.
|
|
71
|
-
if (!need || (principal.startsWith(TA_INVITE_PREFIX) && need !== 'invite')) {
|
|
71
|
+
if (!need || need === 'owner' || (principal.startsWith(TA_INVITE_PREFIX) && need !== 'invite')) {
|
|
72
72
|
refused({ action: name, askable: false });
|
|
73
73
|
return;
|
|
74
74
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
2
|
-
import type { TaAuditEvent, TaAuthorizer, TaDenial, TaHostTeam, TaMemberInfo, TaResource, TaSessionInfo, TaSharedData, TaSharedRef, TaTeamDevice, TaTeamPairing } from '@celljs/ta-protocol';
|
|
2
|
+
import type { TaAuditEvent, TaAuthorizer, TaDenial, TaHostAccounts, TaHostTeam, TaMemberInfo, TaResource, TaSessionInfo, TaSharedData, TaSharedRef, TaTeamDevice, TaTeamPairing } from '@celljs/ta-protocol';
|
|
3
3
|
/**
|
|
4
4
|
* The parts by which ta serve lets people in besides its user (docs/design/ta-team.md 4, 14). Each has one concern, and
|
|
5
5
|
* the rest of ta serve depends on these interfaces only: the sessions, the data of packages and the terminals ask
|
|
@@ -61,6 +61,10 @@ export interface TaMemberDevices {
|
|
|
61
61
|
unpair(deviceId: string): void;
|
|
62
62
|
list(): TaTeamDevice[];
|
|
63
63
|
}
|
|
64
|
+
/** Who one asking to come in is, by an account (TaHostAccounts). */
|
|
65
|
+
export declare const TaMemberAccounts: unique symbol;
|
|
66
|
+
export interface TaMemberAccounts extends TaHostAccounts {
|
|
67
|
+
}
|
|
64
68
|
/** The connections open, by principal. */
|
|
65
69
|
export declare const TaConnections: unique symbol;
|
|
66
70
|
export interface TaConnections {
|
|
@@ -15,6 +15,8 @@ export const TaSharedDataRegistry = Symbol('TaSharedDataRegistry');
|
|
|
15
15
|
export const TaPackagePermissions = Symbol('TaPackagePermissions');
|
|
16
16
|
/** The devices of those let in, through the relay. */
|
|
17
17
|
export const TaMemberDevices = Symbol('TaMemberDevices');
|
|
18
|
+
/** Who one asking to come in is, by an account (TaHostAccounts). */
|
|
19
|
+
export const TaMemberAccounts = Symbol('TaMemberAccounts');
|
|
18
20
|
/** The connections open, by principal. */
|
|
19
21
|
export const TaConnections = Symbol('TaConnections');
|
|
20
22
|
/**
|
|
@@ -22,6 +22,7 @@ export var WebExtensionUtil;
|
|
|
22
22
|
origin: pkg.origin,
|
|
23
23
|
enabled: (_e = pkg.enabled) !== null && _e !== void 0 ? _e : true,
|
|
24
24
|
source: pkg.source,
|
|
25
|
+
root: pkg.root,
|
|
25
26
|
entry,
|
|
26
27
|
hash: hash(readFileSync(join(pkg.root, entry), 'utf8')),
|
|
27
28
|
title: text((_a = manifest === null || manifest === void 0 ? void 0 : manifest.pi) === null || _a === void 0 ? void 0 : _a.title),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type CodingAgentOptions, ModelRuntimeProvider } from '@celljs/ai-coding-agent';
|
|
2
2
|
import { type RpcConnection } from '@celljs/rpc/lib/node';
|
|
3
|
-
import { type TaAuthEvent, type TaAuthProvider, TaAuthServer, type TaAuthType } from '@celljs/ta-protocol';
|
|
3
|
+
import { type TaAuthEvent, type TaAuthProvider, TaAuthServer, type TaAuthType, type TaCustomProvider, type TaCustomProviderInput } from '@celljs/ta-protocol';
|
|
4
4
|
import { ExtensionProviders } from './extension-providers';
|
|
5
5
|
import { TaSessionRegistry } from './host-protocol';
|
|
6
6
|
type ModelRuntime = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).ModelRuntime;
|
|
@@ -26,7 +26,7 @@ declare class Login {
|
|
|
26
26
|
}
|
|
27
27
|
export declare namespace AuthServerUtil {
|
|
28
28
|
/** The ways to sign in, as pi's `/login` lists them, by name. */
|
|
29
|
-
function providers(runtime: ModelRuntime, stored: Set<string>): TaAuthProvider[];
|
|
29
|
+
function providers(runtime: ModelRuntime, stored: Set<string>, custom?: Set<string>): TaAuthProvider[];
|
|
30
30
|
/** pi's AuthEvent as the protocol has it: plain data, and only what a client shows. */
|
|
31
31
|
function event(event: AuthEvent): TaAuthEvent;
|
|
32
32
|
}
|
|
@@ -41,12 +41,22 @@ export declare class TaAuthServerImpl implements TaAuthServer {
|
|
|
41
41
|
protected readonly extensionProviders: ExtensionProviders;
|
|
42
42
|
protected readonly codingAgentOptions?: CodingAgentOptions;
|
|
43
43
|
protected readonly logins: Map<string, Login>;
|
|
44
|
+
/** Writes of models.json, one after another. */
|
|
45
|
+
protected writing: Promise<unknown>;
|
|
44
46
|
protected readonly watched: WeakSet<RpcConnection>;
|
|
45
47
|
listProviders(): Promise<TaAuthProvider[]>;
|
|
46
48
|
login(providerId: string, type: TaAuthType): Promise<string>;
|
|
47
49
|
answer(loginId: string, promptId: string, value: string): Promise<boolean>;
|
|
48
50
|
cancel(loginId: string): Promise<void>;
|
|
49
51
|
logout(providerId: string): Promise<void>;
|
|
52
|
+
listCustomProviders(): Promise<TaCustomProvider[]>;
|
|
53
|
+
saveCustomProvider(input: TaCustomProviderInput): Promise<TaCustomProvider>;
|
|
54
|
+
removeCustomProvider(id: string): Promise<void>;
|
|
55
|
+
fetchCustomModels(input: Pick<TaCustomProviderInput, 'id' | 'api' | 'baseUrl' | 'apiKey' | 'headers'>): Promise<string[]>;
|
|
56
|
+
protected modelsFile(): Promise<string>;
|
|
57
|
+
protected readModels(): Promise<Record<string, unknown>>;
|
|
58
|
+
/** models.json changed by `change`, written whole and only the user may read it: it holds keys. */
|
|
59
|
+
protected writeModels<T>(change: (data: Record<string, unknown>) => [Record<string, unknown>, T]): Promise<T>;
|
|
50
60
|
protected run(runtime: ModelRuntime, login: Login, providerId: string, type: TaAuthType): Promise<void>;
|
|
51
61
|
/**
|
|
52
62
|
* Open sessions that have no model, because none was signed in when they opened, take one of the
|
|
@@ -11,11 +11,14 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.TaAuthServerImpl = exports.AuthServerUtil = void 0;
|
|
13
13
|
const node_crypto_1 = require("node:crypto");
|
|
14
|
+
const promises_1 = require("node:fs/promises");
|
|
15
|
+
const node_path_1 = require("node:path");
|
|
14
16
|
const ai_coding_agent_1 = require("@celljs/ai-coding-agent");
|
|
15
17
|
const core_1 = require("@celljs/core");
|
|
16
18
|
const rpc_1 = require("@celljs/rpc");
|
|
17
19
|
const node_1 = require("@celljs/rpc/lib/node");
|
|
18
20
|
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
21
|
+
const custom_providers_1 = require("./custom-providers");
|
|
19
22
|
const extension_providers_1 = require("./extension-providers");
|
|
20
23
|
const host_protocol_1 = require("./host-protocol");
|
|
21
24
|
const ta_errors_1 = require("./ta-errors");
|
|
@@ -75,7 +78,7 @@ class Login {
|
|
|
75
78
|
var AuthServerUtil;
|
|
76
79
|
(function (AuthServerUtil) {
|
|
77
80
|
/** The ways to sign in, as pi's `/login` lists them, by name. */
|
|
78
|
-
function providers(runtime, stored) {
|
|
81
|
+
function providers(runtime, stored, custom = new Set()) {
|
|
79
82
|
var _a;
|
|
80
83
|
var _b;
|
|
81
84
|
const list = [];
|
|
@@ -91,6 +94,10 @@ var AuthServerUtil;
|
|
|
91
94
|
stored: stored.has(provider.id),
|
|
92
95
|
subscription: ((_a = provider.auth.oauth) === null || _a === void 0 ? void 0 : _a.isSubscription) === true,
|
|
93
96
|
};
|
|
97
|
+
if (custom.has(provider.id)) {
|
|
98
|
+
list.push({ ...base, type: 'api_key', interactive: true, custom: true });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
94
101
|
if (provider.auth.oauth) {
|
|
95
102
|
list.push({ ...base, type: 'oauth', interactive: true });
|
|
96
103
|
}
|
|
@@ -125,6 +132,8 @@ var AuthServerUtil;
|
|
|
125
132
|
let TaAuthServerImpl = class TaAuthServerImpl {
|
|
126
133
|
constructor() {
|
|
127
134
|
this.logins = new Map();
|
|
135
|
+
/** Writes of models.json, one after another. */
|
|
136
|
+
this.writing = Promise.resolve();
|
|
128
137
|
this.watched = new WeakSet();
|
|
129
138
|
}
|
|
130
139
|
listProviders() {
|
|
@@ -133,7 +142,10 @@ let TaAuthServerImpl = class TaAuthServerImpl {
|
|
|
133
142
|
// Those of pi packages count, as in a session: an agent of ta-agents needs no sign-in here.
|
|
134
143
|
await this.extensionProviders.ensure();
|
|
135
144
|
const stored = new Set((await runtime.listCredentials()).map(credential => credential.providerId));
|
|
136
|
-
|
|
145
|
+
// A models.json that does not read lists no provider as added here, rather than none at all.
|
|
146
|
+
const models = await this.readModels().catch(() => ({}));
|
|
147
|
+
const custom = new Set(custom_providers_1.CustomProviderUtil.list(models).map(provider => provider.id));
|
|
148
|
+
return AuthServerUtil.providers(runtime, stored, custom);
|
|
137
149
|
});
|
|
138
150
|
}
|
|
139
151
|
login(providerId, type) {
|
|
@@ -169,6 +181,93 @@ let TaAuthServerImpl = class TaAuthServerImpl {
|
|
|
169
181
|
logout(providerId) {
|
|
170
182
|
return ta_errors_1.TaErrors.guard(async () => (await this.modelRuntimeProvider.provide()).logout(providerId));
|
|
171
183
|
}
|
|
184
|
+
listCustomProviders() {
|
|
185
|
+
return ta_errors_1.TaErrors.guard(async () => custom_providers_1.CustomProviderUtil.list(await this.readModels()));
|
|
186
|
+
}
|
|
187
|
+
saveCustomProvider(input) {
|
|
188
|
+
return ta_errors_1.TaErrors.guard(async () => {
|
|
189
|
+
const clean = custom_providers_1.CustomProviderUtil.clean(input);
|
|
190
|
+
if (clean.models.length === 0) {
|
|
191
|
+
throw ta_errors_1.TaErrors.invalidArgument('A provider needs a model.', 'Give the id of at least one model of the service.');
|
|
192
|
+
}
|
|
193
|
+
const runtime = await this.modelRuntimeProvider.provide();
|
|
194
|
+
// Their providers' ids are taken too.
|
|
195
|
+
await this.extensionProviders.ensure();
|
|
196
|
+
const id = await this.writeModels(data => {
|
|
197
|
+
var _a;
|
|
198
|
+
const entries = custom_providers_1.CustomProviderUtil.entries(data);
|
|
199
|
+
if (clean.id !== undefined && !custom_providers_1.CustomProviderUtil.read(clean.id, entries[clean.id])) {
|
|
200
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${clean.id} is not a provider added by its address.`, 'Pick one of listCustomProviders, or leave out the id to add one.');
|
|
201
|
+
}
|
|
202
|
+
const id = (_a = clean.id) !== null && _a !== void 0 ? _a : custom_providers_1.CustomProviderUtil.newId(clean.name, taken => taken in entries || !!runtime.getProvider(taken));
|
|
203
|
+
return [custom_providers_1.CustomProviderUtil.save(data, id, clean), id];
|
|
204
|
+
});
|
|
205
|
+
await runtime.refresh({ providers: [id] });
|
|
206
|
+
await this.giveModels(runtime, id);
|
|
207
|
+
const saved = custom_providers_1.CustomProviderUtil.read(id, custom_providers_1.CustomProviderUtil.entries(await this.readModels())[id]);
|
|
208
|
+
if (!saved) {
|
|
209
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${id} could not be saved.`, 'Look at models.json of the agent directory.');
|
|
210
|
+
}
|
|
211
|
+
return saved;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
removeCustomProvider(id) {
|
|
215
|
+
return ta_errors_1.TaErrors.guard(async () => {
|
|
216
|
+
await this.writeModels(data => {
|
|
217
|
+
if (!custom_providers_1.CustomProviderUtil.read(id, custom_providers_1.CustomProviderUtil.entries(data)[id])) {
|
|
218
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${id} is not a provider added by its address.`, 'Pick one of listCustomProviders.');
|
|
219
|
+
}
|
|
220
|
+
return [custom_providers_1.CustomProviderUtil.remove(data, id), id];
|
|
221
|
+
});
|
|
222
|
+
await (await this.modelRuntimeProvider.provide()).refresh({ providers: [id] });
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
fetchCustomModels(input) {
|
|
226
|
+
return ta_errors_1.TaErrors.guard(async () => {
|
|
227
|
+
var _a;
|
|
228
|
+
const clean = custom_providers_1.CustomProviderUtil.clean({ ...input, models: [] });
|
|
229
|
+
// Not typed again: the key kept of the provider, as pi resolves it.
|
|
230
|
+
const key = clean.apiKey || (clean.id ? (_a = (await (await this.modelRuntimeProvider.provide()).getAuth(clean.id))) === null || _a === void 0 ? void 0 : _a.auth.apiKey : undefined);
|
|
231
|
+
// A header left empty is the one kept, when it is a value and not a variable or a command.
|
|
232
|
+
const kept = clean.id ? custom_providers_1.CustomProviderUtil.storedHeaders(custom_providers_1.CustomProviderUtil.entries(await this.readModels())[clean.id]) : {};
|
|
233
|
+
const headers = clean.headers ? custom_providers_1.CustomProviderUtil.headersWith(clean.headers, kept) : kept;
|
|
234
|
+
const request = custom_providers_1.CustomProviderUtil.modelsRequest(clean.api, clean.baseUrl, key, headers);
|
|
235
|
+
const response = await fetch(request.url, { headers: request.headers, signal: AbortSignal.timeout(15000) });
|
|
236
|
+
if (!response.ok) {
|
|
237
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${request.url} answered ${response.status} ${response.statusText}`.trim(), 'Check the address and the key, or type the ids of the models.');
|
|
238
|
+
}
|
|
239
|
+
return custom_providers_1.CustomProviderUtil.modelIds(await response.json());
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
async modelsFile() {
|
|
243
|
+
var _a;
|
|
244
|
+
var _b;
|
|
245
|
+
const agentDir = (_b = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _b !== void 0 ? _b : (await ai_coding_agent_1.PiSdkUtil.load()).getAgentDir();
|
|
246
|
+
return (0, node_path_1.join)(agentDir, 'models.json');
|
|
247
|
+
}
|
|
248
|
+
async readModels() {
|
|
249
|
+
try {
|
|
250
|
+
return JSON.parse(await (0, promises_1.readFile)(await this.modelsFile(), 'utf8'));
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
if (error.code === 'ENOENT') {
|
|
254
|
+
return {};
|
|
255
|
+
}
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/** models.json changed by `change`, written whole and only the user may read it: it holds keys. */
|
|
260
|
+
writeModels(change) {
|
|
261
|
+
const write = this.writing.then(async () => {
|
|
262
|
+
const file = await this.modelsFile();
|
|
263
|
+
const [data, result] = change(await this.readModels());
|
|
264
|
+
await (0, promises_1.writeFile)(`${file}.tmp`, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 });
|
|
265
|
+
await (0, promises_1.rename)(`${file}.tmp`, file);
|
|
266
|
+
return result;
|
|
267
|
+
});
|
|
268
|
+
this.writing = write.catch(() => undefined);
|
|
269
|
+
return write;
|
|
270
|
+
}
|
|
172
271
|
async run(runtime, login, providerId, type) {
|
|
173
272
|
try {
|
|
174
273
|
const settings = await this.settingsManager();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type TaCustomApi, type TaCustomModelOptions, type TaCustomProvider, type TaCustomProviderInput } from '@celljs/ta-protocol';
|
|
2
|
+
type Json = Record<string, unknown>;
|
|
3
|
+
type OptionKey = keyof TaCustomModelOptions;
|
|
4
|
+
/** The key of a provider added without one (a server on the user's computer): pi offers a provider's models only once it has a key. */
|
|
5
|
+
export declare const NO_KEY = "none";
|
|
6
|
+
/**
|
|
7
|
+
* The providers the user adds by their address, as entries of pi's models.json: `baseUrl`, `api`, `apiKey` and
|
|
8
|
+
* `models` of an id pi has no provider for. What else an entry or a model holds (headers, compat, a context window
|
|
9
|
+
* written by hand) stays when Ta changes it.
|
|
10
|
+
*/
|
|
11
|
+
export declare namespace CustomProviderUtil {
|
|
12
|
+
/** The options every model has alike, and those they do not. */
|
|
13
|
+
function options(models: Json[]): {
|
|
14
|
+
options: TaCustomModelOptions;
|
|
15
|
+
mixed: OptionKey[];
|
|
16
|
+
};
|
|
17
|
+
/** A model with the options set: left out keeps, null or false removes. */
|
|
18
|
+
function withOptions(model: Json, given: NonNullable<TaCustomProviderInput['options']>): Json;
|
|
19
|
+
/** A value pi resolves to itself (one Ta wrote), unescaped; undefined for a variable or a command. */
|
|
20
|
+
function plain(value: string): string | undefined;
|
|
21
|
+
/** The headers a provider sends that are plain values, by name. */
|
|
22
|
+
function storedHeaders(entry: unknown): Record<string, string>;
|
|
23
|
+
/** The headers given, with an empty value taking the one kept of that name. */
|
|
24
|
+
function headersWith(given: Record<string, string> | undefined, kept: Record<string, string>): Record<string, string>;
|
|
25
|
+
/** The entries of models.json, by id. */
|
|
26
|
+
function entries(data: Json): Json;
|
|
27
|
+
/** An entry Ta shows as one added by its address: an address, an API of the list and models of its own. */
|
|
28
|
+
function read(id: string, value: unknown): TaCustomProvider | undefined;
|
|
29
|
+
function list(data: Json): TaCustomProvider[];
|
|
30
|
+
/**
|
|
31
|
+
* A key as pi reads it literally: pi runs a value that starts with `!` as a command and puts in the variable of
|
|
32
|
+
* `$NAME`, so `$` is written `$$` and a first `!` `$!`.
|
|
33
|
+
*/
|
|
34
|
+
function literal(key: string): string;
|
|
35
|
+
/** The input made plain, or the reason it is not one. */
|
|
36
|
+
function clean(input: TaCustomProviderInput): Required<Pick<TaCustomProviderInput, 'name' | 'api' | 'baseUrl' | 'models'>> & TaCustomProviderInput;
|
|
37
|
+
/** An id for a new provider of that name: of letters, digits and dashes, and none that is taken. */
|
|
38
|
+
function newId(name: string, taken: (id: string) => boolean): string;
|
|
39
|
+
/** models.json with the provider saved; its other entries, and what Ta does not change of this one, stay. */
|
|
40
|
+
function save(data: Json, id: string, input: ReturnType<typeof clean>): Json;
|
|
41
|
+
function remove(data: Json, id: string): Json;
|
|
42
|
+
/** Where a service lists its models, and the headers that carry the key, by its API. */
|
|
43
|
+
function modelsRequest(kind: TaCustomApi, baseUrl: string, key: string | undefined, extra?: Record<string, string>): {
|
|
44
|
+
url: string;
|
|
45
|
+
headers: Record<string, string>;
|
|
46
|
+
};
|
|
47
|
+
/** The ids of a list of models: OpenAI's and Anthropic's `data[].id`, Google's `models[].name` without `models/`. */
|
|
48
|
+
function modelIds(body: unknown): string[];
|
|
49
|
+
}
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=custom-providers.d.ts.map
|