@celljs/ta-host 4.14.0 → 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 +2 -1
- package/README.md +2 -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/sign-in/sign-in-provider.d.ts +44 -0
- package/esm/node/sign-in/sign-in-provider.js +74 -7
- package/esm/node/sign-in/ta-sign-in.d.ts +4 -1
- package/esm/node/sign-in/ta-sign-in.js +6 -2
- package/esm/node/team/host-team.js +1 -0
- package/esm/node/team/member-accounts.d.ts +25 -8
- package/esm/node/team/member-accounts.js +84 -4
- package/esm/node/team/team-calls.js +4 -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/sign-in/sign-in-provider.d.ts +44 -0
- package/lib/node/sign-in/sign-in-provider.js +77 -9
- package/lib/node/sign-in/ta-sign-in.d.ts +4 -1
- package/lib/node/sign-in/ta-sign-in.js +6 -2
- package/lib/node/team/host-team.js +1 -0
- package/lib/node/team/member-accounts.d.ts +25 -8
- package/lib/node/team/member-accounts.js +84 -4
- package/lib/node/team/team-calls.js +4 -0
- package/lib/node/web-extension-util.js +1 -0
- package/package.json +10 -10
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomProviderUtil = exports.NO_KEY = void 0;
|
|
4
|
+
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
5
|
+
const ta_errors_1 = require("./ta-errors");
|
|
6
|
+
const OPTION_KEYS = ['contextWindow', 'maxTokens', 'reasoning', 'images'];
|
|
7
|
+
/** A name of a header, as HTTP has them. */
|
|
8
|
+
const HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
9
|
+
/** More tokens than any model has. */
|
|
10
|
+
const MAX_TOKENS = 100000000;
|
|
11
|
+
/** 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. */
|
|
12
|
+
exports.NO_KEY = 'none';
|
|
13
|
+
/**
|
|
14
|
+
* The providers the user adds by their address, as entries of pi's models.json: `baseUrl`, `api`, `apiKey` and
|
|
15
|
+
* `models` of an id pi has no provider for. What else an entry or a model holds (headers, compat, a context window
|
|
16
|
+
* written by hand) stays when Ta changes it.
|
|
17
|
+
*/
|
|
18
|
+
var CustomProviderUtil;
|
|
19
|
+
(function (CustomProviderUtil) {
|
|
20
|
+
function object(value) {
|
|
21
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : undefined;
|
|
22
|
+
}
|
|
23
|
+
function api(value) {
|
|
24
|
+
return ta_protocol_1.TA_CUSTOM_APIS.find(each => each === value);
|
|
25
|
+
}
|
|
26
|
+
/** What a model of models.json says of an option. */
|
|
27
|
+
function option(model, key) {
|
|
28
|
+
switch (key) {
|
|
29
|
+
case 'reasoning':
|
|
30
|
+
return model.reasoning === true;
|
|
31
|
+
case 'images':
|
|
32
|
+
return Array.isArray(model.input) && model.input.includes('image');
|
|
33
|
+
default:
|
|
34
|
+
return typeof model[key] === 'number' ? model[key] : undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** The options every model has alike, and those they do not. */
|
|
38
|
+
function options(models) {
|
|
39
|
+
const options = {};
|
|
40
|
+
const mixed = [];
|
|
41
|
+
for (const key of OPTION_KEYS) {
|
|
42
|
+
const values = new Set(models.map(model => option(model, key)));
|
|
43
|
+
if (values.size > 1) {
|
|
44
|
+
mixed.push(key);
|
|
45
|
+
}
|
|
46
|
+
else if (values.size === 1) {
|
|
47
|
+
const [value] = values;
|
|
48
|
+
if (value !== undefined) {
|
|
49
|
+
options[key] = value;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { options: options, mixed };
|
|
54
|
+
}
|
|
55
|
+
CustomProviderUtil.options = options;
|
|
56
|
+
/** A model with the options set: left out keeps, null or false removes. */
|
|
57
|
+
function withOptions(model, given) {
|
|
58
|
+
const next = { ...model };
|
|
59
|
+
for (const key of OPTION_KEYS) {
|
|
60
|
+
const value = given[key];
|
|
61
|
+
if (value === undefined) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const field = key === 'images' ? 'input' : key;
|
|
65
|
+
if (value === null || value === false) {
|
|
66
|
+
delete next[field];
|
|
67
|
+
}
|
|
68
|
+
else if (key === 'images') {
|
|
69
|
+
next.input = ['text', 'image'];
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
next[field] = value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return next;
|
|
76
|
+
}
|
|
77
|
+
CustomProviderUtil.withOptions = withOptions;
|
|
78
|
+
/** A value pi resolves to itself (one Ta wrote), unescaped; undefined for a variable or a command. */
|
|
79
|
+
function plain(value) {
|
|
80
|
+
if (value.startsWith('!')) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
let text = '';
|
|
84
|
+
for (let at = 0; at < value.length; at++) {
|
|
85
|
+
if (value[at] !== '$') {
|
|
86
|
+
text += value[at];
|
|
87
|
+
}
|
|
88
|
+
else if (value[at + 1] === '$' || value[at + 1] === '!') {
|
|
89
|
+
text += value[++at];
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return text;
|
|
96
|
+
}
|
|
97
|
+
CustomProviderUtil.plain = plain;
|
|
98
|
+
/** The headers a provider sends that are plain values, by name. */
|
|
99
|
+
function storedHeaders(entry) {
|
|
100
|
+
var _a;
|
|
101
|
+
var _b;
|
|
102
|
+
const headers = (_b = object((_a = object(entry)) === null || _a === void 0 ? void 0 : _a.headers)) !== null && _b !== void 0 ? _b : {};
|
|
103
|
+
const found = {};
|
|
104
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
105
|
+
const resolved = typeof value === 'string' ? plain(value) : undefined;
|
|
106
|
+
if (resolved !== undefined) {
|
|
107
|
+
found[name] = resolved;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return found;
|
|
111
|
+
}
|
|
112
|
+
CustomProviderUtil.storedHeaders = storedHeaders;
|
|
113
|
+
/** The headers given, with an empty value taking the one kept of that name. */
|
|
114
|
+
function headersWith(given, kept) {
|
|
115
|
+
const headers = {};
|
|
116
|
+
for (const [name, value] of Object.entries(given !== null && given !== void 0 ? given : {})) {
|
|
117
|
+
const resolved = value || kept[name];
|
|
118
|
+
if (resolved) {
|
|
119
|
+
headers[name] = resolved;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return headers;
|
|
123
|
+
}
|
|
124
|
+
CustomProviderUtil.headersWith = headersWith;
|
|
125
|
+
/** The entries of models.json, by id. */
|
|
126
|
+
function entries(data) {
|
|
127
|
+
var _a;
|
|
128
|
+
return (_a = object(data.providers)) !== null && _a !== void 0 ? _a : {};
|
|
129
|
+
}
|
|
130
|
+
CustomProviderUtil.entries = entries;
|
|
131
|
+
/** An entry Ta shows as one added by its address: an address, an API of the list and models of its own. */
|
|
132
|
+
function read(id, value) {
|
|
133
|
+
var _a;
|
|
134
|
+
const entry = object(value);
|
|
135
|
+
const kind = api(entry === null || entry === void 0 ? void 0 : entry.api);
|
|
136
|
+
if (!entry || !kind || typeof entry.baseUrl !== 'string' || !Array.isArray(entry.models)) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const entries = entry.models.map(model => object(model)).filter((model) => typeof (model === null || model === void 0 ? void 0 : model.id) === 'string');
|
|
140
|
+
const models = entries.map(model => model.id);
|
|
141
|
+
const key = entry.apiKey;
|
|
142
|
+
return {
|
|
143
|
+
id,
|
|
144
|
+
name: typeof entry.name === 'string' && entry.name ? entry.name : id,
|
|
145
|
+
api: kind,
|
|
146
|
+
baseUrl: entry.baseUrl,
|
|
147
|
+
hasKey: typeof key === 'string' && key !== '' && key !== exports.NO_KEY,
|
|
148
|
+
models,
|
|
149
|
+
...options(entries),
|
|
150
|
+
headers: Object.keys((_a = object(entry.headers)) !== null && _a !== void 0 ? _a : {}),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
CustomProviderUtil.read = read;
|
|
154
|
+
function list(data) {
|
|
155
|
+
return Object.entries(entries(data))
|
|
156
|
+
.map(([id, value]) => read(id, value))
|
|
157
|
+
.filter((provider) => !!provider)
|
|
158
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
159
|
+
}
|
|
160
|
+
CustomProviderUtil.list = list;
|
|
161
|
+
/**
|
|
162
|
+
* A key as pi reads it literally: pi runs a value that starts with `!` as a command and puts in the variable of
|
|
163
|
+
* `$NAME`, so `$` is written `$$` and a first `!` `$!`.
|
|
164
|
+
*/
|
|
165
|
+
function literal(key) {
|
|
166
|
+
const escaped = key.replace(/\$/g, '$$$$');
|
|
167
|
+
return escaped.startsWith('!') ? `$${escaped}` : escaped;
|
|
168
|
+
}
|
|
169
|
+
CustomProviderUtil.literal = literal;
|
|
170
|
+
/** The input made plain, or the reason it is not one. */
|
|
171
|
+
function clean(input) {
|
|
172
|
+
const kind = api(input === null || input === void 0 ? void 0 : input.api);
|
|
173
|
+
if (!kind) {
|
|
174
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${String(input === null || input === void 0 ? void 0 : input.api)} is not an API Ta takes.`, `Use one of ${ta_protocol_1.TA_CUSTOM_APIS.join(', ')}.`);
|
|
175
|
+
}
|
|
176
|
+
const baseUrl = typeof input.baseUrl === 'string' ? input.baseUrl.trim().replace(/\/+$/, '') : '';
|
|
177
|
+
let url;
|
|
178
|
+
try {
|
|
179
|
+
url = new URL(baseUrl);
|
|
180
|
+
}
|
|
181
|
+
catch (_a) {
|
|
182
|
+
url = undefined;
|
|
183
|
+
}
|
|
184
|
+
if (!url || (url.protocol !== 'http:' && url.protocol !== 'https:')) {
|
|
185
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${baseUrl || 'An empty address'} is not an address of a service.`, 'Give its http(s) URL, e.g. https://api.example.com/v1.');
|
|
186
|
+
}
|
|
187
|
+
const models = [...new Set((Array.isArray(input.models) ? input.models : []).map(model => String(model).trim()).filter(Boolean))];
|
|
188
|
+
const name = typeof input.name === 'string' && input.name.trim() ? input.name.trim() : url.host;
|
|
189
|
+
const apiKey = typeof input.apiKey === 'string' ? input.apiKey.trim() : undefined;
|
|
190
|
+
return { ...input, name, api: kind, baseUrl, apiKey, models, options: cleanOptions(input.options), headers: cleanHeaders(input.headers) };
|
|
191
|
+
}
|
|
192
|
+
CustomProviderUtil.clean = clean;
|
|
193
|
+
function cleanOptions(given) {
|
|
194
|
+
const options = object(given);
|
|
195
|
+
if (!options) {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
const clean = {};
|
|
199
|
+
for (const key of OPTION_KEYS) {
|
|
200
|
+
const value = options[key];
|
|
201
|
+
if (value === undefined || value === null) {
|
|
202
|
+
clean[key] = value;
|
|
203
|
+
}
|
|
204
|
+
else if (key === 'reasoning' || key === 'images') {
|
|
205
|
+
if (typeof value !== 'boolean') {
|
|
206
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${key} is ${JSON.stringify(value)}, not true or false.`, 'Give a boolean, or null to remove it.');
|
|
207
|
+
}
|
|
208
|
+
clean[key] = value;
|
|
209
|
+
}
|
|
210
|
+
else if (typeof value === 'number' && Number.isInteger(value) && value > 0 && value <= MAX_TOKENS) {
|
|
211
|
+
clean[key] = value;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${key} is ${JSON.stringify(value)}, not a number of tokens.`, 'Give a whole number above 0, or null to remove it.');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return clean;
|
|
218
|
+
}
|
|
219
|
+
function cleanHeaders(given) {
|
|
220
|
+
const headers = object(given);
|
|
221
|
+
if (!headers) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
const clean = {};
|
|
225
|
+
for (const [raw, value] of Object.entries(headers)) {
|
|
226
|
+
const name = raw.trim();
|
|
227
|
+
if (!name) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (!HEADER_NAME.test(name) || typeof value !== 'string' || /[\r\n]/.test(value)) {
|
|
231
|
+
throw ta_errors_1.TaErrors.invalidArgument(`${name} is not a header Ta sends.`, 'Give a name of letters, digits and dashes, and a value of one line.');
|
|
232
|
+
}
|
|
233
|
+
clean[name] = value.trim();
|
|
234
|
+
}
|
|
235
|
+
return clean;
|
|
236
|
+
}
|
|
237
|
+
/** An id for a new provider of that name: of letters, digits and dashes, and none that is taken. */
|
|
238
|
+
function newId(name, taken) {
|
|
239
|
+
const base = name
|
|
240
|
+
.toLowerCase()
|
|
241
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
242
|
+
.replace(/^-+|-+$/g, '')
|
|
243
|
+
.slice(0, 40) || 'custom';
|
|
244
|
+
let id = base;
|
|
245
|
+
for (let n = 2; taken(id); n++) {
|
|
246
|
+
id = `${base}-${n}`;
|
|
247
|
+
}
|
|
248
|
+
return id;
|
|
249
|
+
}
|
|
250
|
+
CustomProviderUtil.newId = newId;
|
|
251
|
+
/** models.json with the provider saved; its other entries, and what Ta does not change of this one, stay. */
|
|
252
|
+
function save(data, id, input) {
|
|
253
|
+
var _a, _b;
|
|
254
|
+
const providers = { ...entries(data) };
|
|
255
|
+
const before = (_a = object(providers[id])) !== null && _a !== void 0 ? _a : {};
|
|
256
|
+
const models = Array.isArray(before.models) ? before.models : [];
|
|
257
|
+
const entry = {
|
|
258
|
+
...before,
|
|
259
|
+
name: input.name,
|
|
260
|
+
baseUrl: input.baseUrl,
|
|
261
|
+
api: input.api,
|
|
262
|
+
models: input.models.map(model => { var _a, _b; return withOptions((_a = object(models.find(each => { var _a; return ((_a = object(each)) === null || _a === void 0 ? void 0 : _a.id) === model; }))) !== null && _a !== void 0 ? _a : { id: model }, (_b = input.options) !== null && _b !== void 0 ? _b : {}); }),
|
|
263
|
+
};
|
|
264
|
+
if (input.headers) {
|
|
265
|
+
const kept = (_b = object(before.headers)) !== null && _b !== void 0 ? _b : {};
|
|
266
|
+
const headers = {};
|
|
267
|
+
for (const [name, value] of Object.entries(input.headers)) {
|
|
268
|
+
if (value) {
|
|
269
|
+
headers[name] = literal(value);
|
|
270
|
+
}
|
|
271
|
+
else if (typeof kept[name] === 'string') {
|
|
272
|
+
headers[name] = kept[name];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (Object.keys(headers).length > 0) {
|
|
276
|
+
entry.headers = headers;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
delete entry.headers;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (input.apiKey) {
|
|
283
|
+
entry.apiKey = literal(input.apiKey);
|
|
284
|
+
}
|
|
285
|
+
else if (input.apiKey === '' || typeof before.apiKey !== 'string') {
|
|
286
|
+
entry.apiKey = exports.NO_KEY;
|
|
287
|
+
}
|
|
288
|
+
providers[id] = entry;
|
|
289
|
+
return { ...data, providers };
|
|
290
|
+
}
|
|
291
|
+
CustomProviderUtil.save = save;
|
|
292
|
+
function remove(data, id) {
|
|
293
|
+
const providers = { ...entries(data) };
|
|
294
|
+
delete providers[id];
|
|
295
|
+
return { ...data, providers };
|
|
296
|
+
}
|
|
297
|
+
CustomProviderUtil.remove = remove;
|
|
298
|
+
/** Where a service lists its models, and the headers that carry the key, by its API. */
|
|
299
|
+
function modelsRequest(kind, baseUrl, key, extra = {}) {
|
|
300
|
+
const request = authRequest(kind, baseUrl, key);
|
|
301
|
+
// Those of the provider last: a gateway may take its key in a header of its own.
|
|
302
|
+
return { url: request.url, headers: { ...request.headers, ...extra } };
|
|
303
|
+
}
|
|
304
|
+
CustomProviderUtil.modelsRequest = modelsRequest;
|
|
305
|
+
function authRequest(kind, baseUrl, key) {
|
|
306
|
+
const base = baseUrl.replace(/\/+$/, '');
|
|
307
|
+
const real = key && key !== exports.NO_KEY ? key : undefined;
|
|
308
|
+
switch (kind) {
|
|
309
|
+
case 'anthropic-messages':
|
|
310
|
+
// pi's address of Anthropic has no /v1, which its client adds.
|
|
311
|
+
return {
|
|
312
|
+
url: /\/v\d+$/.test(base) ? `${base}/models` : `${base}/v1/models`,
|
|
313
|
+
headers: { 'anthropic-version': '2023-06-01', ...(real ? { 'x-api-key': real } : {}) },
|
|
314
|
+
};
|
|
315
|
+
case 'google-generative-ai':
|
|
316
|
+
return { url: `${base}/models`, headers: real ? { 'x-goog-api-key': real } : {} };
|
|
317
|
+
default:
|
|
318
|
+
return { url: `${base}/models`, headers: real ? { Authorization: `Bearer ${real}` } : {} };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/** The ids of a list of models: OpenAI's and Anthropic's `data[].id`, Google's `models[].name` without `models/`. */
|
|
322
|
+
function modelIds(body) {
|
|
323
|
+
var _a;
|
|
324
|
+
const json = (_a = object(body)) !== null && _a !== void 0 ? _a : {};
|
|
325
|
+
const ids = [
|
|
326
|
+
...(Array.isArray(json.data) ? json.data : []).map(model => { var _a; return (_a = object(model)) === null || _a === void 0 ? void 0 : _a.id; }),
|
|
327
|
+
...(Array.isArray(json.models) ? json.models : []).map(model => {
|
|
328
|
+
var _a, _b;
|
|
329
|
+
const item = object(model);
|
|
330
|
+
const name = (_b = (_a = item === null || item === void 0 ? void 0 : item.name) !== null && _a !== void 0 ? _a : item === null || item === void 0 ? void 0 : item.id) !== null && _b !== void 0 ? _b : item === null || item === void 0 ? void 0 : item.model;
|
|
331
|
+
return typeof name === 'string' ? name.replace(/^models\//, '') : undefined;
|
|
332
|
+
}),
|
|
333
|
+
];
|
|
334
|
+
return [...new Set(ids.filter((id) => typeof id === 'string' && id !== ''))].sort((a, b) => a.localeCompare(b));
|
|
335
|
+
}
|
|
336
|
+
CustomProviderUtil.modelIds = modelIds;
|
|
337
|
+
})(CustomProviderUtil || (exports.CustomProviderUtil = CustomProviderUtil = {}));
|
|
338
|
+
//# sourceMappingURL=custom-providers.js.map
|
|
@@ -36,6 +36,8 @@ export interface GitHubEndpoints {
|
|
|
36
36
|
authorize: string;
|
|
37
37
|
token: string;
|
|
38
38
|
user: string;
|
|
39
|
+
/** Where a code to type is asked for (the device flow). */
|
|
40
|
+
device: string;
|
|
39
41
|
}
|
|
40
42
|
/** GitHub's OAuth apps; the account is `github:<login>`. GitHub Enterprise Server has the same at its own address. */
|
|
41
43
|
export declare class GitHubSignIn implements SignInProvider {
|
|
@@ -45,6 +47,8 @@ export declare class GitHubSignIn implements SignInProvider {
|
|
|
45
47
|
readonly id = "github";
|
|
46
48
|
readonly name = "GitHub";
|
|
47
49
|
static endpoints(url?: string): GitHubEndpoints;
|
|
50
|
+
/** Who a token of GitHub is. */
|
|
51
|
+
static user(endpoints: GitHubEndpoints, token: string): Promise<SignInAccount>;
|
|
48
52
|
constructor(clientId: string, clientSecret: string, endpoints?: GitHubEndpoints);
|
|
49
53
|
authorizeUrl({ redirectUri, state, challenge }: {
|
|
50
54
|
redirectUri: string;
|
|
@@ -57,6 +61,40 @@ export declare class GitHubSignIn implements SignInProvider {
|
|
|
57
61
|
verifier: string;
|
|
58
62
|
}): Promise<SignInAccount>;
|
|
59
63
|
}
|
|
64
|
+
/** A code to type at the provider, and how often to ask whether it was (OAuth 2.0's device authorization, RFC 8628). */
|
|
65
|
+
export interface DeviceCode {
|
|
66
|
+
deviceCode: string;
|
|
67
|
+
userCode: string;
|
|
68
|
+
uri: string;
|
|
69
|
+
/** Seconds between asks. */
|
|
70
|
+
interval: number;
|
|
71
|
+
/** Seconds the code is good for. */
|
|
72
|
+
expiresIn: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A provider one signs in with by typing a code at it, the device flow: no secret and no address to come back to, so
|
|
76
|
+
* Ta ships one of its own and anyone's Ta knows who asks to come in without setting anything up (TaHostAccounts). It
|
|
77
|
+
* lets no one into ta serve: that is SignInFlow, with a provider of the operator's.
|
|
78
|
+
*/
|
|
79
|
+
export interface DeviceSignInProvider extends TaSignInProvider {
|
|
80
|
+
readonly device: true;
|
|
81
|
+
code(): Promise<DeviceCode>;
|
|
82
|
+
/** The account once the code was typed and agreed to; `pending` while not yet, `slowDown` when asked too often. */
|
|
83
|
+
poll(deviceCode: string): Promise<SignInAccount | 'pending' | 'slowDown'>;
|
|
84
|
+
}
|
|
85
|
+
/** GitHub's device flow, with Ta's own OAuth app by default; the account is `github:<login>`, as GitHubSignIn's. */
|
|
86
|
+
export declare class GitHubDeviceSignIn implements DeviceSignInProvider {
|
|
87
|
+
protected readonly clientId: string;
|
|
88
|
+
protected readonly endpoints: GitHubEndpoints;
|
|
89
|
+
readonly id = "github";
|
|
90
|
+
readonly name = "GitHub";
|
|
91
|
+
readonly device: true;
|
|
92
|
+
/** Ta's own OAuth app on GitHub.com, its device flow on: a client id is public, and the device flow needs no secret. */
|
|
93
|
+
static readonly CLIENT_ID = "Ov23lip46sRQDlDM1xt7";
|
|
94
|
+
constructor(clientId?: string, endpoints?: GitHubEndpoints);
|
|
95
|
+
code(): Promise<DeviceCode>;
|
|
96
|
+
poll(deviceCode: string): Promise<SignInAccount | 'pending' | 'slowDown'>;
|
|
97
|
+
}
|
|
60
98
|
interface OidcConfiguration {
|
|
61
99
|
authorization_endpoint: string;
|
|
62
100
|
token_endpoint: string;
|
|
@@ -165,6 +203,12 @@ export declare namespace SignInProviders {
|
|
|
165
203
|
}
|
|
166
204
|
/** The environment's variables that sign-in.json stands for: what the environment sets wins. */
|
|
167
205
|
function withConfig(env: Record<string, string | undefined>, config: Config | undefined): Record<string, string | undefined>;
|
|
206
|
+
/**
|
|
207
|
+
* GitHub by a code, for those asking to come in, when GitHub is not set up with its secret (which goes there and back
|
|
208
|
+
* instead): `TA_GITHUB_DEVICE` is `off`, or `<client id>` or `<client id>@<address of GitHub>` of an OAuth app of
|
|
209
|
+
* one's own with its device flow on; by default Ta's own, on GitHub.com.
|
|
210
|
+
*/
|
|
211
|
+
function device(env: Record<string, string | undefined>): DeviceSignInProvider | undefined;
|
|
168
212
|
/**
|
|
169
213
|
* The providers the operator configured in the environment of ta serve: a provider is offered when it
|
|
170
214
|
* has its id and its secret.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SignInProviders = exports.WeChatSignIn = exports.OidcSignIn = exports.GitHubSignIn = exports.SignInHttp = exports.SignInError = void 0;
|
|
3
|
+
exports.SignInProviders = exports.WeChatSignIn = exports.OidcSignIn = exports.GitHubDeviceSignIn = exports.GitHubSignIn = exports.SignInHttp = exports.SignInError = void 0;
|
|
4
4
|
/** A sign-in that failed with the provider (a code refused, an account without what it needs). */
|
|
5
5
|
class SignInError extends Error {
|
|
6
6
|
}
|
|
@@ -48,7 +48,22 @@ class GitHubSignIn {
|
|
|
48
48
|
static endpoints(url = 'https://github.com') {
|
|
49
49
|
const base = url.replace(/\/+$/, '');
|
|
50
50
|
const api = base === 'https://github.com' ? 'https://api.github.com' : `${base}/api/v3`;
|
|
51
|
-
return {
|
|
51
|
+
return {
|
|
52
|
+
authorize: `${base}/login/oauth/authorize`,
|
|
53
|
+
token: `${base}/login/oauth/access_token`,
|
|
54
|
+
user: `${api}/user`,
|
|
55
|
+
device: `${base}/login/device/code`,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Who a token of GitHub is. */
|
|
59
|
+
static async user(endpoints, token) {
|
|
60
|
+
const user = await SignInHttp.json(endpoints.user, {
|
|
61
|
+
headers: { Authorization: `Bearer ${token}`, Accept: 'application/vnd.github+json', 'User-Agent': 'Ta' },
|
|
62
|
+
});
|
|
63
|
+
if (!user.login) {
|
|
64
|
+
throw new SignInError('GitHub: the user has no login');
|
|
65
|
+
}
|
|
66
|
+
return { id: `github:${user.login.toLowerCase()}`, name: user.name || user.login, avatar: user.avatar_url };
|
|
52
67
|
}
|
|
53
68
|
constructor(clientId, clientSecret, endpoints = GitHubSignIn.endpoints()) {
|
|
54
69
|
this.clientId = clientId;
|
|
@@ -74,16 +89,51 @@ class GitHubSignIn {
|
|
|
74
89
|
if (!token.access_token) {
|
|
75
90
|
throw new SignInError(`GitHub: ${(_b = (_a = token.error_description) !== null && _a !== void 0 ? _a : token.error) !== null && _b !== void 0 ? _b : 'no access token'}`);
|
|
76
91
|
}
|
|
77
|
-
|
|
78
|
-
headers: { Authorization: `Bearer ${token.access_token}`, Accept: 'application/vnd.github+json', 'User-Agent': 'Ta' },
|
|
79
|
-
});
|
|
80
|
-
if (!user.login) {
|
|
81
|
-
throw new SignInError('GitHub: the user has no login');
|
|
82
|
-
}
|
|
83
|
-
return { id: `github:${user.login.toLowerCase()}`, name: user.name || user.login, avatar: user.avatar_url };
|
|
92
|
+
return GitHubSignIn.user(this.endpoints, token.access_token);
|
|
84
93
|
}
|
|
85
94
|
}
|
|
86
95
|
exports.GitHubSignIn = GitHubSignIn;
|
|
96
|
+
/** GitHub's device flow, with Ta's own OAuth app by default; the account is `github:<login>`, as GitHubSignIn's. */
|
|
97
|
+
class GitHubDeviceSignIn {
|
|
98
|
+
constructor(clientId = GitHubDeviceSignIn.CLIENT_ID, endpoints = GitHubSignIn.endpoints()) {
|
|
99
|
+
this.clientId = clientId;
|
|
100
|
+
this.endpoints = endpoints;
|
|
101
|
+
this.id = 'github';
|
|
102
|
+
this.name = 'GitHub';
|
|
103
|
+
this.device = true;
|
|
104
|
+
}
|
|
105
|
+
async code() {
|
|
106
|
+
var _a, _b, _c, _d;
|
|
107
|
+
const answer = await SignInHttp.json(this.endpoints.device, SignInHttp.form({ client_id: this.clientId, scope: 'read:user' }));
|
|
108
|
+
if (!answer.device_code || !answer.user_code || !answer.verification_uri || !/^https?:\/\//.test(answer.verification_uri)) {
|
|
109
|
+
throw new SignInError(`GitHub: ${(_b = (_a = answer.error_description) !== null && _a !== void 0 ? _a : answer.error) !== null && _b !== void 0 ? _b : 'no code'}`);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
deviceCode: answer.device_code,
|
|
113
|
+
userCode: answer.user_code,
|
|
114
|
+
uri: answer.verification_uri,
|
|
115
|
+
interval: Number((_c = answer.interval) !== null && _c !== void 0 ? _c : 5),
|
|
116
|
+
expiresIn: Number((_d = answer.expires_in) !== null && _d !== void 0 ? _d : 900),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
async poll(deviceCode) {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
const token = await SignInHttp.json(this.endpoints.token, SignInHttp.form({ client_id: this.clientId, device_code: deviceCode, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }));
|
|
122
|
+
if (token.access_token) {
|
|
123
|
+
return GitHubSignIn.user(this.endpoints, token.access_token);
|
|
124
|
+
}
|
|
125
|
+
if (token.error === 'authorization_pending') {
|
|
126
|
+
return 'pending';
|
|
127
|
+
}
|
|
128
|
+
if (token.error === 'slow_down') {
|
|
129
|
+
return 'slowDown';
|
|
130
|
+
}
|
|
131
|
+
throw new SignInError(`GitHub: ${(_b = (_a = token.error_description) !== null && _a !== void 0 ? _a : token.error) !== null && _b !== void 0 ? _b : 'no access token'}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.GitHubDeviceSignIn = GitHubDeviceSignIn;
|
|
135
|
+
/** Ta's own OAuth app on GitHub.com, its device flow on: a client id is public, and the device flow needs no secret. */
|
|
136
|
+
GitHubDeviceSignIn.CLIENT_ID = 'Ov23lip46sRQDlDM1xt7';
|
|
87
137
|
/**
|
|
88
138
|
* OpenID Connect, found from the issuer (`/.well-known/openid-configuration`): Google, and the provider of
|
|
89
139
|
* the operator (Microsoft Entra, Okta, Keycloak, Authing...). The account is `<id>:<email>` when the
|
|
@@ -322,6 +372,24 @@ var SignInProviders;
|
|
|
322
372
|
return merged;
|
|
323
373
|
}
|
|
324
374
|
SignInProviders.withConfig = withConfig;
|
|
375
|
+
/**
|
|
376
|
+
* GitHub by a code, for those asking to come in, when GitHub is not set up with its secret (which goes there and back
|
|
377
|
+
* instead): `TA_GITHUB_DEVICE` is `off`, or `<client id>` or `<client id>@<address of GitHub>` of an OAuth app of
|
|
378
|
+
* one's own with its device flow on; by default Ta's own, on GitHub.com.
|
|
379
|
+
*/
|
|
380
|
+
function device(env) {
|
|
381
|
+
var _a;
|
|
382
|
+
var _b;
|
|
383
|
+
const value = (_b = (_a = env.TA_GITHUB_DEVICE) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
|
|
384
|
+
if (value === 'off' || (env.TA_GITHUB_CLIENT_ID && env.TA_GITHUB_CLIENT_SECRET)) {
|
|
385
|
+
return undefined;
|
|
386
|
+
}
|
|
387
|
+
const at = value.indexOf('@');
|
|
388
|
+
const clientId = at < 0 ? value : value.slice(0, at);
|
|
389
|
+
const url = at < 0 ? undefined : value.slice(at + 1);
|
|
390
|
+
return new GitHubDeviceSignIn(clientId || GitHubDeviceSignIn.CLIENT_ID, GitHubSignIn.endpoints(url || undefined));
|
|
391
|
+
}
|
|
392
|
+
SignInProviders.device = device;
|
|
325
393
|
/**
|
|
326
394
|
* The providers the operator configured in the environment of ta serve: a provider is offered when it
|
|
327
395
|
* has its id and its secret.
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import type { TaSignInKind, TaSignInSetup } from '@celljs/ta-protocol';
|
|
2
2
|
import { SignInFlow } from './sign-in-flow';
|
|
3
|
+
import { type DeviceSignInProvider } from './sign-in-provider';
|
|
3
4
|
export declare const TA_SIGN_IN: unique symbol;
|
|
4
5
|
/**
|
|
5
6
|
* Signing in to ta serve with an account, besides its token. The providers come from the environment, then
|
|
6
7
|
* sign-in.json of the agent directory (SignInProviders.withConfig), which Settings, Access writes (setProvider) and
|
|
7
8
|
* which is read again then; who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
|
|
8
9
|
* (AccessStore). Someone else who signs in is told to wait, and shows up as a request. Those asking to come in by a
|
|
9
|
-
* link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing
|
|
10
|
+
* link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing, or, when GitHub
|
|
11
|
+
* is not set up, with GitHub by a code, Ta's own app (`device`, SignInProviders.device).
|
|
10
12
|
*/
|
|
11
13
|
export declare class TaSignIn extends SignInFlow {
|
|
14
|
+
device: DeviceSignInProvider | undefined;
|
|
12
15
|
constructor();
|
|
13
16
|
setups(): TaSignInSetup[];
|
|
14
17
|
/** Sets a kind up in sign-in.json (readable by the user alone), offered at once; not one the environment sets. */
|
|
@@ -35,14 +35,17 @@ function config() {
|
|
|
35
35
|
* sign-in.json of the agent directory (SignInProviders.withConfig), which Settings, Access writes (setProvider) and
|
|
36
36
|
* which is read again then; who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
|
|
37
37
|
* (AccessStore). Someone else who signs in is told to wait, and shows up as a request. Those asking to come in by a
|
|
38
|
-
* link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing
|
|
38
|
+
* link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing, or, when GitHub
|
|
39
|
+
* is not set up, with GitHub by a code, Ta's own app (`device`, SignInProviders.device).
|
|
39
40
|
*/
|
|
40
41
|
let TaSignIn = class TaSignIn extends sign_in_flow_1.SignInFlow {
|
|
41
42
|
constructor() {
|
|
42
43
|
super(sign_in_provider_1.SignInProviders.fromEnv(sign_in_provider_1.SignInProviders.withConfig(process.env, config())), new access_store_1.AccessStore((0, node_path_1.join)(ta_brand_1.TaBrand.agentDir(), 'access.json'), access_store_1.AccessUtil.parseList(process.env.TA_ALLOWED_USERS)), sign_in_provider_1.SignInProviders.publicUrl(process.env));
|
|
44
|
+
this.device = sign_in_provider_1.SignInProviders.device(sign_in_provider_1.SignInProviders.withConfig(process.env, config()));
|
|
43
45
|
}
|
|
44
46
|
setups() {
|
|
45
|
-
|
|
47
|
+
const device = this.device;
|
|
48
|
+
return sign_in_provider_1.SignInProviders.setups(process.env, config()).map(setup => ((device === null || device === void 0 ? void 0 : device.id) === setup.id && !setup.configured ? { ...setup, device: true } : setup));
|
|
46
49
|
}
|
|
47
50
|
/** Sets a kind up in sign-in.json (readable by the user alone), offered at once; not one the environment sets. */
|
|
48
51
|
setProvider(id, fields) {
|
|
@@ -53,6 +56,7 @@ let TaSignIn = class TaSignIn extends sign_in_flow_1.SignInFlow {
|
|
|
53
56
|
const next = sign_in_provider_1.SignInProviders.withProvider(config(), id, fields);
|
|
54
57
|
(0, node_fs_1.writeFileSync)(file(), `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
55
58
|
this.providers = sign_in_provider_1.SignInProviders.fromEnv(sign_in_provider_1.SignInProviders.withConfig(process.env, next));
|
|
59
|
+
this.device = sign_in_provider_1.SignInProviders.device(sign_in_provider_1.SignInProviders.withConfig(process.env, next));
|
|
56
60
|
}
|
|
57
61
|
};
|
|
58
62
|
exports.TaSignIn = TaSignIn;
|
|
@@ -38,6 +38,7 @@ let DefaultTaHostTeamProvider = class DefaultTaHostTeamProvider {
|
|
|
38
38
|
providers: () => this.accounts.providers(),
|
|
39
39
|
start: (provider, redirectUri) => this.accounts.start(provider, redirectUri),
|
|
40
40
|
verify: (state, code) => this.accounts.verify(state, code),
|
|
41
|
+
typed: (state, waitMs) => this.accounts.typed(state, waitMs),
|
|
41
42
|
},
|
|
42
43
|
};
|
|
43
44
|
}
|
|
@@ -1,14 +1,31 @@
|
|
|
1
|
-
import type { TaAccount, TaSignInProvider } from '@celljs/ta-protocol';
|
|
2
|
-
import type
|
|
1
|
+
import type { TaAccount, TaAccountStart, TaSignInProvider } from '@celljs/ta-protocol';
|
|
2
|
+
import { type DeviceSignInProvider, type SignInAccount } from '../sign-in/sign-in-provider';
|
|
3
|
+
import { type TaSignIn } from '../sign-in/ta-sign-in';
|
|
3
4
|
import { TaMemberAccounts } from './team-protocol';
|
|
4
|
-
/**
|
|
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
|
+
*/
|
|
5
18
|
export declare class DefaultTaMemberAccounts implements TaMemberAccounts {
|
|
6
|
-
protected readonly flow:
|
|
19
|
+
protected readonly flow: TaSignIn;
|
|
20
|
+
protected readonly typing: Map<string, Typing>;
|
|
7
21
|
providers(): TaSignInProvider[];
|
|
8
|
-
start(provider: string, redirectUri: string): Promise<
|
|
9
|
-
url: string;
|
|
10
|
-
state: string;
|
|
11
|
-
}>;
|
|
22
|
+
start(provider: string, redirectUri: string): Promise<TaAccountStart>;
|
|
12
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;
|
|
13
29
|
}
|
|
30
|
+
export {};
|
|
14
31
|
//# sourceMappingURL=member-accounts.d.ts.map
|