@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.
@@ -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.
@@ -44,7 +44,22 @@ export class GitHubSignIn {
44
44
  static endpoints(url = 'https://github.com') {
45
45
  const base = url.replace(/\/+$/, '');
46
46
  const api = base === 'https://github.com' ? 'https://api.github.com' : `${base}/api/v3`;
47
- return { authorize: `${base}/login/oauth/authorize`, token: `${base}/login/oauth/access_token`, user: `${api}/user` };
47
+ return {
48
+ authorize: `${base}/login/oauth/authorize`,
49
+ token: `${base}/login/oauth/access_token`,
50
+ user: `${api}/user`,
51
+ device: `${base}/login/device/code`,
52
+ };
53
+ }
54
+ /** Who a token of GitHub is. */
55
+ static async user(endpoints, token) {
56
+ const user = await SignInHttp.json(endpoints.user, {
57
+ headers: { Authorization: `Bearer ${token}`, Accept: 'application/vnd.github+json', 'User-Agent': 'Ta' },
58
+ });
59
+ if (!user.login) {
60
+ throw new SignInError('GitHub: the user has no login');
61
+ }
62
+ return { id: `github:${user.login.toLowerCase()}`, name: user.name || user.login, avatar: user.avatar_url };
48
63
  }
49
64
  constructor(clientId, clientSecret, endpoints = GitHubSignIn.endpoints()) {
50
65
  this.clientId = clientId;
@@ -70,15 +85,49 @@ export class GitHubSignIn {
70
85
  if (!token.access_token) {
71
86
  throw new SignInError(`GitHub: ${(_b = (_a = token.error_description) !== null && _a !== void 0 ? _a : token.error) !== null && _b !== void 0 ? _b : 'no access token'}`);
72
87
  }
73
- const user = await SignInHttp.json(this.endpoints.user, {
74
- headers: { Authorization: `Bearer ${token.access_token}`, Accept: 'application/vnd.github+json', 'User-Agent': 'Ta' },
75
- });
76
- if (!user.login) {
77
- throw new SignInError('GitHub: the user has no login');
88
+ return GitHubSignIn.user(this.endpoints, token.access_token);
89
+ }
90
+ }
91
+ /** GitHub's device flow, with Ta's own OAuth app by default; the account is `github:<login>`, as GitHubSignIn's. */
92
+ export class GitHubDeviceSignIn {
93
+ constructor(clientId = GitHubDeviceSignIn.CLIENT_ID, endpoints = GitHubSignIn.endpoints()) {
94
+ this.clientId = clientId;
95
+ this.endpoints = endpoints;
96
+ this.id = 'github';
97
+ this.name = 'GitHub';
98
+ this.device = true;
99
+ }
100
+ async code() {
101
+ var _a, _b, _c, _d;
102
+ const answer = await SignInHttp.json(this.endpoints.device, SignInHttp.form({ client_id: this.clientId, scope: 'read:user' }));
103
+ if (!answer.device_code || !answer.user_code || !answer.verification_uri || !/^https?:\/\//.test(answer.verification_uri)) {
104
+ throw new SignInError(`GitHub: ${(_b = (_a = answer.error_description) !== null && _a !== void 0 ? _a : answer.error) !== null && _b !== void 0 ? _b : 'no code'}`);
78
105
  }
79
- return { id: `github:${user.login.toLowerCase()}`, name: user.name || user.login, avatar: user.avatar_url };
106
+ return {
107
+ deviceCode: answer.device_code,
108
+ userCode: answer.user_code,
109
+ uri: answer.verification_uri,
110
+ interval: Number((_c = answer.interval) !== null && _c !== void 0 ? _c : 5),
111
+ expiresIn: Number((_d = answer.expires_in) !== null && _d !== void 0 ? _d : 900),
112
+ };
113
+ }
114
+ async poll(deviceCode) {
115
+ var _a, _b;
116
+ 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' }));
117
+ if (token.access_token) {
118
+ return GitHubSignIn.user(this.endpoints, token.access_token);
119
+ }
120
+ if (token.error === 'authorization_pending') {
121
+ return 'pending';
122
+ }
123
+ if (token.error === 'slow_down') {
124
+ return 'slowDown';
125
+ }
126
+ throw new SignInError(`GitHub: ${(_b = (_a = token.error_description) !== null && _a !== void 0 ? _a : token.error) !== null && _b !== void 0 ? _b : 'no access token'}`);
80
127
  }
81
128
  }
129
+ /** Ta's own OAuth app on GitHub.com, its device flow on: a client id is public, and the device flow needs no secret. */
130
+ GitHubDeviceSignIn.CLIENT_ID = 'Ov23lip46sRQDlDM1xt7';
82
131
  /**
83
132
  * OpenID Connect, found from the issuer (`/.well-known/openid-configuration`): Google, and the provider of
84
133
  * the operator (Microsoft Entra, Okta, Keycloak, Authing...). The account is `<id>:<email>` when the
@@ -315,6 +364,24 @@ export var SignInProviders;
315
364
  return merged;
316
365
  }
317
366
  SignInProviders.withConfig = withConfig;
367
+ /**
368
+ * GitHub by a code, for those asking to come in, when GitHub is not set up with its secret (which goes there and back
369
+ * instead): `TA_GITHUB_DEVICE` is `off`, or `<client id>` or `<client id>@<address of GitHub>` of an OAuth app of
370
+ * one's own with its device flow on; by default Ta's own, on GitHub.com.
371
+ */
372
+ function device(env) {
373
+ var _a;
374
+ var _b;
375
+ const value = (_b = (_a = env.TA_GITHUB_DEVICE) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
376
+ if (value === 'off' || (env.TA_GITHUB_CLIENT_ID && env.TA_GITHUB_CLIENT_SECRET)) {
377
+ return undefined;
378
+ }
379
+ const at = value.indexOf('@');
380
+ const clientId = at < 0 ? value : value.slice(0, at);
381
+ const url = at < 0 ? undefined : value.slice(at + 1);
382
+ return new GitHubDeviceSignIn(clientId || GitHubDeviceSignIn.CLIENT_ID, GitHubSignIn.endpoints(url || undefined));
383
+ }
384
+ SignInProviders.device = device;
318
385
  /**
319
386
  * The providers the operator configured in the environment of ta serve: a provider is offered when it
320
387
  * 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.js';
3
+ import { type DeviceSignInProvider } from './sign-in-provider.js';
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. */
@@ -32,14 +32,17 @@ function config() {
32
32
  * sign-in.json of the agent directory (SignInProviders.withConfig), which Settings, Access writes (setProvider) and
33
33
  * which is read again then; who may sign in from TA_ALLOWED_USERS and the accounts added in the web client
34
34
  * (AccessStore). Someone else who signs in is told to wait, and shows up as a request. Those asking to come in by a
35
- * link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing.
35
+ * link sign in with the same providers to say who they are (TaHostAccounts), which lets them into nothing, or, when GitHub
36
+ * is not set up, with GitHub by a code, Ta's own app (`device`, SignInProviders.device).
36
37
  */
37
38
  let TaSignIn = class TaSignIn extends SignInFlow {
38
39
  constructor() {
39
40
  super(SignInProviders.fromEnv(SignInProviders.withConfig(process.env, config())), new AccessStore(join(TaBrand.agentDir(), 'access.json'), AccessUtil.parseList(process.env.TA_ALLOWED_USERS)), SignInProviders.publicUrl(process.env));
41
+ this.device = SignInProviders.device(SignInProviders.withConfig(process.env, config()));
40
42
  }
41
43
  setups() {
42
- return SignInProviders.setups(process.env, config());
44
+ const device = this.device;
45
+ return SignInProviders.setups(process.env, config()).map(setup => ((device === null || device === void 0 ? void 0 : device.id) === setup.id && !setup.configured ? { ...setup, device: true } : setup));
43
46
  }
44
47
  /** Sets a kind up in sign-in.json (readable by the user alone), offered at once; not one the environment sets. */
45
48
  setProvider(id, fields) {
@@ -50,6 +53,7 @@ let TaSignIn = class TaSignIn extends SignInFlow {
50
53
  const next = SignInProviders.withProvider(config(), id, fields);
51
54
  writeFileSync(file(), `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
52
55
  this.providers = SignInProviders.fromEnv(SignInProviders.withConfig(process.env, next));
56
+ this.device = SignInProviders.device(SignInProviders.withConfig(process.env, next));
53
57
  }
54
58
  };
55
59
  TaSignIn = __decorate([
@@ -35,6 +35,7 @@ let DefaultTaHostTeamProvider = class DefaultTaHostTeamProvider {
35
35
  providers: () => this.accounts.providers(),
36
36
  start: (provider, redirectUri) => this.accounts.start(provider, redirectUri),
37
37
  verify: (state, code) => this.accounts.verify(state, code),
38
+ typed: (state, waitMs) => this.accounts.typed(state, waitMs),
38
39
  },
39
40
  };
40
41
  }
@@ -1,13 +1,30 @@
1
- import type { TaAccount, TaSignInProvider } from '@celljs/ta-protocol';
2
- import type { SignInFlow } from '../sign-in/sign-in-flow.js';
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';
3
4
  import { TaMemberAccounts } from './team-protocol.js';
4
- /** Who one asking to come in is, by an account of the providers ta serve signs in with (TaSignIn): nothing let in by it. */
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: SignInFlow;
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 {};
@@ -7,22 +7,102 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
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
+ import { randomBytes } from 'node:crypto';
10
11
  import { Autowired, Component } from '@celljs/core';
12
+ import { SignInError } from '../sign-in/sign-in-provider.js';
11
13
  import { TA_SIGN_IN } from '../sign-in/ta-sign-in.js';
12
14
  import { TaMemberAccounts } from './team-protocol.js';
13
- /** Who one asking to come in is, by an account of the providers ta serve signs in with (TaSignIn): nothing let in by it. */
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
+ */
14
22
  let DefaultTaMemberAccounts = class DefaultTaMemberAccounts {
23
+ constructor() {
24
+ this.typing = new Map();
25
+ }
15
26
  providers() {
16
- return this.flow.providers.map(({ id, name }) => ({ id, name }));
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;
17
30
  }
18
- start(provider, redirectUri) {
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
+ }
19
36
  if (!/^https?:\/\/[^/?#]+\/[^?#]*$/.test(redirectUri)) {
20
37
  throw new Error(`Not an address to come back to: ${redirectUri}`);
21
38
  }
22
39
  return this.flow.startVerify(provider, redirectUri);
23
40
  }
24
41
  async verify(state, code) {
25
- const { id, name, avatar } = await this.flow.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 }) {
26
106
  return { id, ...(name ? { name } : {}), ...(avatar ? { avatar } : {}) };
27
107
  }
28
108
  };
@@ -229,6 +229,10 @@ const RULES = {
229
229
  answer: fixed('models.manage'),
230
230
  cancel: fixed('models.manage'),
231
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'),
232
236
  },
233
237
  TaAccessServer: {
234
238
  getSignInConfig: owner,
@@ -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
- return AuthServerUtil.providers(runtime, stored);
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