@celljs/ta-host 4.14.0 → 4.15.1
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/extension-data.js +7 -3
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/jpeg-encoder.d.ts +7 -0
- package/esm/node/jpeg-encoder.js +268 -0
- package/esm/node/png-thumbnail.d.ts +40 -0
- package/esm/node/png-thumbnail.js +384 -0
- package/esm/node/serve-updates.d.ts +38 -0
- package/esm/node/serve-updates.js +89 -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/ta-server-impl.d.ts +3 -0
- package/esm/node/ta-server-impl.js +33 -9
- 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 +5 -0
- package/esm/node/web-extension-util.js +1 -0
- package/esm/node/workspace-files.d.ts +23 -0
- package/esm/node/workspace-files.js +86 -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/extension-data.js +6 -2
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/jpeg-encoder.d.ts +8 -0
- package/lib/node/jpeg-encoder.js +271 -0
- package/lib/node/png-thumbnail.d.ts +41 -0
- package/lib/node/png-thumbnail.js +387 -0
- package/lib/node/serve-updates.d.ts +39 -0
- package/lib/node/serve-updates.js +92 -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/ta-server-impl.d.ts +3 -0
- package/lib/node/ta-server-impl.js +33 -9
- 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 +5 -0
- package/lib/node/web-extension-util.js +1 -0
- package/lib/node/workspace-files.d.ts +23 -0
- package/lib/node/workspace-files.js +86 -0
- package/package.json +10 -10
|
@@ -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
|
|
@@ -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
|
|
@@ -129,8 +129,12 @@ let ExtensionDataStoreImpl = class ExtensionDataStoreImpl {
|
|
|
129
129
|
.then(async () => {
|
|
130
130
|
const text = ExtensionDataUtil.update(await this.read(file), key, value);
|
|
131
131
|
await (0, promises_1.mkdir)((0, node_path_1.dirname)(file), { recursive: true });
|
|
132
|
-
|
|
133
|
-
|
|
132
|
+
// A file of its own: another Ta of this agent directory (one starting as one stops) writing
|
|
133
|
+
// the same name at once would have one rename a file the other is halfway through, and the
|
|
134
|
+
// next read of it, unparsed, would leave every other key out.
|
|
135
|
+
const temporary = `${file}.${(0, node_crypto_1.randomUUID)()}.tmp`;
|
|
136
|
+
await (0, promises_1.writeFile)(temporary, text, { mode: 0o600 });
|
|
137
|
+
await (0, promises_1.rename)(temporary, file);
|
|
134
138
|
for (const listener of this.listeners) {
|
|
135
139
|
try {
|
|
136
140
|
listener(name, key, directory);
|
package/lib/node/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export * from './relay-bridge';
|
|
|
18
18
|
export * from './remote';
|
|
19
19
|
export * from './remote-ui-context';
|
|
20
20
|
export * from './rooms-bridge';
|
|
21
|
+
export * from './serve-updates';
|
|
21
22
|
export * from './session-catalog';
|
|
22
23
|
export * from './session-files';
|
|
23
24
|
export * from './session-handles';
|
package/lib/node/index.js
CHANGED
|
@@ -34,6 +34,7 @@ __exportStar(require("./relay-bridge"), exports);
|
|
|
34
34
|
__exportStar(require("./remote"), exports);
|
|
35
35
|
__exportStar(require("./remote-ui-context"), exports);
|
|
36
36
|
__exportStar(require("./rooms-bridge"), exports);
|
|
37
|
+
__exportStar(require("./serve-updates"), exports);
|
|
37
38
|
__exportStar(require("./session-catalog"), exports);
|
|
38
39
|
__exportStar(require("./session-files"), exports);
|
|
39
40
|
__exportStar(require("./session-handles"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A baseline JPEG of opaque RGBA, without a library: YCbCr without subsampling (the letters of a screenshot keep
|
|
3
|
+
* their colour), the tables of the standard scaled to `quality` (1–100, as libjpeg reads it).
|
|
4
|
+
*/
|
|
5
|
+
export declare namespace JpegEncoder {
|
|
6
|
+
function encode(rgba: Uint8Array, width: number, height: number, quality?: number): Uint8Array;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=jpeg-encoder.d.ts.map
|