@sorbetai/control 0.0.0-stage → 0.1.0-alpha.18
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/LICENSE +21 -0
- package/README.md +78 -2
- package/dist/control-NCdUIEUB.d.ts +306 -0
- package/dist/control-sqUS607F.js +1555 -0
- package/dist/control-sqUS607F.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +3 -0
- package/dist/provider-runtime.d.ts +173 -0
- package/dist/provider-runtime.js +3 -0
- package/package.json +47 -3
|
@@ -0,0 +1,1555 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import * as s from "@earendil-works/pi-schema";
|
|
5
|
+
import { decode } from "@sorbetai/schema";
|
|
6
|
+
import { appendFile, chmod, link, mkdir, open, readFile, readdir, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
7
|
+
import { FileArtifactStore } from "@sorbetai/artifacts/node";
|
|
8
|
+
import { readUnitSessions } from "@sorbetai/runtime/node";
|
|
9
|
+
import { Mutex, isError, isPlainObject, isString, pickBy } from "es-toolkit";
|
|
10
|
+
import { randomUUID } from "node:crypto";
|
|
11
|
+
import { builtinModels, getBuiltinModels, getBuiltinProviders } from "@earendil-works/pi-ai/providers/all";
|
|
12
|
+
import { setTimeout } from "node:timers/promises";
|
|
13
|
+
import { createProvider, envApiKeyAuth } from "@earendil-works/pi-ai";
|
|
14
|
+
import { anthropicMessagesApi } from "@earendil-works/pi-ai/api/anthropic-messages.lazy";
|
|
15
|
+
import { openAICompletionsApi } from "@earendil-works/pi-ai/api/openai-completions.lazy";
|
|
16
|
+
import { openAIResponsesApi } from "@earendil-works/pi-ai/api/openai-responses.lazy";
|
|
17
|
+
|
|
18
|
+
//#region src/compatibility.ts
|
|
19
|
+
const manifest = decode(s.obj({
|
|
20
|
+
name: s.str(),
|
|
21
|
+
version: s.str()
|
|
22
|
+
}, { open: true }), JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")));
|
|
23
|
+
/**
|
|
24
|
+
* Package/API identity reported by the control facade. `api` is the stable surface name a
|
|
25
|
+
* Host negotiates against; `packageVersion` is read from package.json so it cannot drift from the
|
|
26
|
+
* published artifact.
|
|
27
|
+
*/
|
|
28
|
+
const SORBET_CONTROL_COMPATIBILITY = Object.freeze({
|
|
29
|
+
api: "sorbet.control",
|
|
30
|
+
apiVersion: 1,
|
|
31
|
+
packageName: manifest.name,
|
|
32
|
+
packageVersion: manifest.version
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/errors.ts
|
|
37
|
+
const SorbetControlErrorCode = {
|
|
38
|
+
ControlClosed: "control_closed",
|
|
39
|
+
CustomProviderNotFound: "custom_provider_not_found",
|
|
40
|
+
CustomProviderInUse: "custom_provider_in_use",
|
|
41
|
+
CustomProviderConflict: "custom_provider_conflict",
|
|
42
|
+
ProviderNotFound: "provider_not_found",
|
|
43
|
+
ProviderProtocolUnsupported: "provider_protocol_unsupported",
|
|
44
|
+
InvalidProviderEndpoint: "invalid_provider_endpoint",
|
|
45
|
+
ModelNotFound: "model_not_found",
|
|
46
|
+
ProviderReferencesUnavailable: "provider_references_unavailable"
|
|
47
|
+
};
|
|
48
|
+
/** A rejected control operation. `code` is the stable classification Hosts branch on. */
|
|
49
|
+
var SorbetControlError = class extends Error {
|
|
50
|
+
name = "SorbetControlError";
|
|
51
|
+
constructor(code, message, options) {
|
|
52
|
+
super(message, options);
|
|
53
|
+
this.code = code;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/providers/reviewer-selection.ts
|
|
59
|
+
const ProviderReviewerSelectionSchema = s.union([s.obj({ mode: s.lit("session") }, { open: false }), s.obj({
|
|
60
|
+
mode: s.lit("model"),
|
|
61
|
+
providerId: s.str({ minLength: 1 }),
|
|
62
|
+
modelId: s.str({ minLength: 1 })
|
|
63
|
+
}, { open: false })]);
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/local-reference.ts
|
|
67
|
+
function createLocalProviderReferences(dataDirectory) {
|
|
68
|
+
return { listReferencedProviderIds: async (options) => {
|
|
69
|
+
try {
|
|
70
|
+
options?.signal?.throwIfAborted();
|
|
71
|
+
await rejectLegacySessions(dataDirectory);
|
|
72
|
+
const ids = /* @__PURE__ */ new Set();
|
|
73
|
+
const unitsDirectory = join(dataDirectory, "units");
|
|
74
|
+
const entries = await readdir(unitsDirectory, { withFileTypes: true }).catch((cause) => {
|
|
75
|
+
if (isError(cause) && Reflect.get(cause, "code") === "ENOENT") return [];
|
|
76
|
+
throw cause;
|
|
77
|
+
});
|
|
78
|
+
for (const entry of entries) {
|
|
79
|
+
options?.signal?.throwIfAborted();
|
|
80
|
+
if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
81
|
+
const unitDirectory = join(unitsDirectory, entry.name);
|
|
82
|
+
const sessions = await readUnitSessions({
|
|
83
|
+
unitId: entry.name,
|
|
84
|
+
filePath: join(unitDirectory, "unit.jsonl"),
|
|
85
|
+
artifactStore: await FileArtifactStore.open(join(unitDirectory, "artifacts"), { create: false }),
|
|
86
|
+
signal: options?.signal
|
|
87
|
+
});
|
|
88
|
+
for (const session of sessions) {
|
|
89
|
+
if (!isPlainObject(session.metadata)) throw new Error(`Committed Session ${session.sessionId} metadata is not an object`);
|
|
90
|
+
const providerId = Reflect.get(session.metadata, "providerId");
|
|
91
|
+
if (!isString(providerId) || providerId.length === 0) throw new Error(`Committed Session ${session.sessionId} metadata has no providerId`);
|
|
92
|
+
ids.add(providerId);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const reviewerId = await reviewerProviderId(dataDirectory, options?.signal);
|
|
96
|
+
if (reviewerId !== void 0) ids.add(reviewerId);
|
|
97
|
+
return [...ids];
|
|
98
|
+
} catch (cause) {
|
|
99
|
+
if (options?.signal?.aborted === true) throw cause;
|
|
100
|
+
throw new SorbetControlError(SorbetControlErrorCode.ProviderReferencesUnavailable, `Provider references could not be read safely: ${isError(cause) ? cause.message : String(cause)}`, { cause });
|
|
101
|
+
}
|
|
102
|
+
} };
|
|
103
|
+
}
|
|
104
|
+
async function rejectLegacySessions(dataDirectory) {
|
|
105
|
+
if ((await readdir(join(dataDirectory, "sessions")).catch((cause) => {
|
|
106
|
+
if (isError(cause) && Reflect.get(cause, "code") === "ENOENT") return [];
|
|
107
|
+
throw cause;
|
|
108
|
+
})).length > 0) throw new Error("Legacy Session state requires migration");
|
|
109
|
+
}
|
|
110
|
+
async function reviewerProviderId(dataDirectory, signal) {
|
|
111
|
+
signal?.throwIfAborted();
|
|
112
|
+
let raw;
|
|
113
|
+
try {
|
|
114
|
+
raw = await readFile(join(dataDirectory, "approval-reviewer-model.json"), {
|
|
115
|
+
encoding: "utf8",
|
|
116
|
+
signal
|
|
117
|
+
});
|
|
118
|
+
} catch (cause) {
|
|
119
|
+
if (isError(cause) && Reflect.get(cause, "code") === "ENOENT") return void 0;
|
|
120
|
+
throw cause;
|
|
121
|
+
}
|
|
122
|
+
if (raw.trim().length === 0) return void 0;
|
|
123
|
+
const parsed = decode(ProviderReviewerSelectionSchema, JSON.parse(raw));
|
|
124
|
+
return parsed.mode === "model" ? parsed.providerId : void 0;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/providers/installation-id.ts
|
|
129
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
130
|
+
async function installationId(dataDirectory) {
|
|
131
|
+
await mkdir(dataDirectory, {
|
|
132
|
+
recursive: true,
|
|
133
|
+
mode: 448
|
|
134
|
+
});
|
|
135
|
+
const path = join(dataDirectory, "installation-id");
|
|
136
|
+
try {
|
|
137
|
+
return await readInstallationId(path);
|
|
138
|
+
} catch (cause) {
|
|
139
|
+
if (!isError(cause) || Reflect.get(cause, "code") !== "ENOENT") throw cause;
|
|
140
|
+
}
|
|
141
|
+
const temporary = join(dataDirectory, `.installation-id-${randomUUID()}`);
|
|
142
|
+
const id = randomUUID();
|
|
143
|
+
try {
|
|
144
|
+
const file = await open(temporary, "wx", 384);
|
|
145
|
+
try {
|
|
146
|
+
await file.writeFile(id, "utf8");
|
|
147
|
+
await file.sync();
|
|
148
|
+
} finally {
|
|
149
|
+
await file.close();
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
await link(temporary, path);
|
|
153
|
+
} catch (cause) {
|
|
154
|
+
if (!isError(cause) || Reflect.get(cause, "code") !== "EEXIST") throw cause;
|
|
155
|
+
}
|
|
156
|
+
} finally {
|
|
157
|
+
await unlink(temporary).catch((cause) => {
|
|
158
|
+
if (!isError(cause) || Reflect.get(cause, "code") !== "ENOENT") throw cause;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (process.platform !== "win32") {
|
|
162
|
+
const directory = await open(dataDirectory, "r");
|
|
163
|
+
try {
|
|
164
|
+
await directory.sync();
|
|
165
|
+
} finally {
|
|
166
|
+
await directory.close();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return readInstallationId(path);
|
|
170
|
+
}
|
|
171
|
+
async function readInstallationId(path) {
|
|
172
|
+
const stored = await readFile(path, "utf8");
|
|
173
|
+
if (!UUID.test(stored)) throw new Error(`Invalid installation UUID in ${path}`);
|
|
174
|
+
return stored;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/providers/json-mutation-lease.ts
|
|
179
|
+
const SQLITE_BUSY = 5;
|
|
180
|
+
const LOCK_RETRY_MILLISECONDS = 25;
|
|
181
|
+
async function acquireJsonMutationLease(filePath, signal) {
|
|
182
|
+
signal?.throwIfAborted();
|
|
183
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
184
|
+
const database = new DatabaseSync(`${filePath}.lock`, { timeout: 0 });
|
|
185
|
+
let acquired = false;
|
|
186
|
+
try {
|
|
187
|
+
do {
|
|
188
|
+
signal?.throwIfAborted();
|
|
189
|
+
try {
|
|
190
|
+
database.exec("BEGIN IMMEDIATE");
|
|
191
|
+
acquired = true;
|
|
192
|
+
} catch (cause) {
|
|
193
|
+
if (!isError(cause) || Reflect.get(cause, "errcode") !== SQLITE_BUSY) throw cause;
|
|
194
|
+
await setTimeout(LOCK_RETRY_MILLISECONDS, void 0, { signal });
|
|
195
|
+
}
|
|
196
|
+
} while (!acquired);
|
|
197
|
+
} catch (cause) {
|
|
198
|
+
database.close();
|
|
199
|
+
throw cause;
|
|
200
|
+
}
|
|
201
|
+
return sqliteTransactionLease(database);
|
|
202
|
+
}
|
|
203
|
+
function sqliteTransactionLease(database) {
|
|
204
|
+
let released = false;
|
|
205
|
+
return { release: () => {
|
|
206
|
+
if (released) return;
|
|
207
|
+
released = true;
|
|
208
|
+
try {
|
|
209
|
+
database.exec("ROLLBACK");
|
|
210
|
+
} finally {
|
|
211
|
+
database.close();
|
|
212
|
+
}
|
|
213
|
+
} };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/providers/json-map-store.ts
|
|
218
|
+
/**
|
|
219
|
+
* A JSON document persisted as one file that only its owner may read or write:
|
|
220
|
+
* created with `0o600`, decoded through `schema`, and replaced atomically by a
|
|
221
|
+
* temporary write plus rename under the cross-process mutation lease.
|
|
222
|
+
*/
|
|
223
|
+
var JsonMapStore = class {
|
|
224
|
+
#filePath;
|
|
225
|
+
#schema;
|
|
226
|
+
#ready;
|
|
227
|
+
constructor(filePath, schema) {
|
|
228
|
+
this.#filePath = filePath;
|
|
229
|
+
this.#schema = schema;
|
|
230
|
+
this.#ready = this.#initialize();
|
|
231
|
+
}
|
|
232
|
+
get filePath() {
|
|
233
|
+
return this.#filePath;
|
|
234
|
+
}
|
|
235
|
+
/** Resolves once the owning directory and file exist with owner-only permissions. */
|
|
236
|
+
get ready() {
|
|
237
|
+
return this.#ready;
|
|
238
|
+
}
|
|
239
|
+
async load() {
|
|
240
|
+
const text = await readFile(this.#filePath, "utf8");
|
|
241
|
+
if (text.length === 0) return /* @__PURE__ */ new Map();
|
|
242
|
+
const encoded = JSON.parse(text);
|
|
243
|
+
return new Map(Object.entries(decode(this.#schema, encoded)));
|
|
244
|
+
}
|
|
245
|
+
/** Serializes `change` plus its persist under the mutation lease; persists only when it returns true. */
|
|
246
|
+
async mutate(change, options = {}) {
|
|
247
|
+
await this.#ready;
|
|
248
|
+
const lease = await acquireJsonMutationLease(this.#filePath, options.signal);
|
|
249
|
+
try {
|
|
250
|
+
const entries = await this.load();
|
|
251
|
+
if (change(entries)) await this.persist(entries);
|
|
252
|
+
} finally {
|
|
253
|
+
lease.release();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** Commits the whole document atomically: a sibling temporary file renamed over the target. */
|
|
257
|
+
async persist(entries) {
|
|
258
|
+
const temporaryPath = `${this.#filePath}.${process.pid}.${randomUUID()}.tmp`;
|
|
259
|
+
try {
|
|
260
|
+
await writeFile(temporaryPath, `${JSON.stringify(Object.fromEntries(entries), void 0, 2)}\n`, {
|
|
261
|
+
encoding: "utf8",
|
|
262
|
+
flag: "wx",
|
|
263
|
+
mode: 384
|
|
264
|
+
});
|
|
265
|
+
await rename(temporaryPath, this.#filePath);
|
|
266
|
+
} finally {
|
|
267
|
+
await rm(temporaryPath, { force: true }).catch(() => void 0);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async #initialize() {
|
|
271
|
+
await mkdir(dirname(this.#filePath), {
|
|
272
|
+
recursive: true,
|
|
273
|
+
mode: 448
|
|
274
|
+
});
|
|
275
|
+
await appendFile(this.#filePath, "", {
|
|
276
|
+
encoding: "utf8",
|
|
277
|
+
mode: 384
|
|
278
|
+
});
|
|
279
|
+
await chmod(this.#filePath, 384);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/providers/credential-store.ts
|
|
285
|
+
const ApiKeyCredentialSchema = s.obj({
|
|
286
|
+
type: s.lit("api_key"),
|
|
287
|
+
key: s.opt(s.str()),
|
|
288
|
+
env: s.opt(s.map(s.str()))
|
|
289
|
+
}, { open: false });
|
|
290
|
+
const OAuthCredentialSchema = s.obj({
|
|
291
|
+
type: s.lit("oauth"),
|
|
292
|
+
refresh: s.str(),
|
|
293
|
+
access: s.str(),
|
|
294
|
+
expires: s.num()
|
|
295
|
+
}, { open: true });
|
|
296
|
+
const CredentialSchema = s.union([ApiKeyCredentialSchema, OAuthCredentialSchema]);
|
|
297
|
+
const CredentialDocumentSchema = s.map(CredentialSchema);
|
|
298
|
+
/**
|
|
299
|
+
* File-backed credentials with owner-only permissions and atomic replacement.
|
|
300
|
+
* Reads observe committed external changes. Mutations serialize across processes and hold the
|
|
301
|
+
* transaction while Pi refreshes OAuth credentials inside the update callback.
|
|
302
|
+
*/
|
|
303
|
+
var JsonCredentialStore = class extends JsonMapStore {
|
|
304
|
+
#mutation = new Mutex();
|
|
305
|
+
constructor(filePath) {
|
|
306
|
+
super(filePath, CredentialDocumentSchema);
|
|
307
|
+
}
|
|
308
|
+
async read(providerId, options) {
|
|
309
|
+
options?.signal?.throwIfAborted();
|
|
310
|
+
await this.ready;
|
|
311
|
+
const credential = (await this.load()).get(providerId);
|
|
312
|
+
options?.signal?.throwIfAborted();
|
|
313
|
+
return credential === void 0 ? void 0 : structuredClone(credential);
|
|
314
|
+
}
|
|
315
|
+
async list(options) {
|
|
316
|
+
options?.signal?.throwIfAborted();
|
|
317
|
+
await this.ready;
|
|
318
|
+
const credentials = await this.load();
|
|
319
|
+
options?.signal?.throwIfAborted();
|
|
320
|
+
return [...credentials].map(([providerId, credential]) => ({
|
|
321
|
+
providerId,
|
|
322
|
+
type: credential.type
|
|
323
|
+
}));
|
|
324
|
+
}
|
|
325
|
+
modify(providerId, update, options) {
|
|
326
|
+
return this.#enqueue(async () => {
|
|
327
|
+
options?.signal?.throwIfAborted();
|
|
328
|
+
await this.ready;
|
|
329
|
+
const lease = await acquireJsonMutationLease(this.filePath, options?.signal);
|
|
330
|
+
try {
|
|
331
|
+
options?.signal?.throwIfAborted();
|
|
332
|
+
const currentCredentials = await this.load();
|
|
333
|
+
const current = currentCredentials.get(providerId);
|
|
334
|
+
const next = await update(current === void 0 ? void 0 : structuredClone(current));
|
|
335
|
+
options?.signal?.throwIfAborted();
|
|
336
|
+
if (next === void 0) return current === void 0 ? void 0 : structuredClone(current);
|
|
337
|
+
const credentials = new Map(currentCredentials);
|
|
338
|
+
credentials.set(providerId, structuredClone(next));
|
|
339
|
+
await this.persist(credentials);
|
|
340
|
+
return structuredClone(next);
|
|
341
|
+
} finally {
|
|
342
|
+
lease.release();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
delete(providerId, options) {
|
|
347
|
+
return this.#enqueue(async () => {
|
|
348
|
+
options?.signal?.throwIfAborted();
|
|
349
|
+
await this.ready;
|
|
350
|
+
const lease = await acquireJsonMutationLease(this.filePath, options?.signal);
|
|
351
|
+
try {
|
|
352
|
+
options?.signal?.throwIfAborted();
|
|
353
|
+
const credentials = await this.load();
|
|
354
|
+
if (!credentials.delete(providerId)) return;
|
|
355
|
+
await this.persist(credentials);
|
|
356
|
+
} finally {
|
|
357
|
+
lease.release();
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
async #enqueue(operation) {
|
|
362
|
+
await this.#mutation.acquire();
|
|
363
|
+
try {
|
|
364
|
+
return await operation();
|
|
365
|
+
} finally {
|
|
366
|
+
this.#mutation.release();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/providers/contracts.ts
|
|
373
|
+
const CustomProviderThinkingLevelMapSchema = s.obj({
|
|
374
|
+
off: s.opt(s.union([s.str(), s.nul()])),
|
|
375
|
+
minimal: s.opt(s.union([s.str(), s.nul()])),
|
|
376
|
+
low: s.opt(s.union([s.str(), s.nul()])),
|
|
377
|
+
medium: s.opt(s.union([s.str(), s.nul()])),
|
|
378
|
+
high: s.opt(s.union([s.str(), s.nul()])),
|
|
379
|
+
xhigh: s.opt(s.union([s.str(), s.nul()])),
|
|
380
|
+
max: s.opt(s.union([s.str(), s.nul()]))
|
|
381
|
+
}, { open: false });
|
|
382
|
+
const CustomProviderProtocolSchema = s.union([
|
|
383
|
+
s.lit("anthropic-messages"),
|
|
384
|
+
s.lit("openai-responses"),
|
|
385
|
+
s.lit("openai-completions")
|
|
386
|
+
]);
|
|
387
|
+
const CustomProviderModelProperties = {
|
|
388
|
+
id: s.str({ minLength: 1 }),
|
|
389
|
+
name: s.opt(s.str({ minLength: 1 })),
|
|
390
|
+
reasoning: s.opt(s.bool()),
|
|
391
|
+
thinkingLevelMap: s.opt(CustomProviderThinkingLevelMapSchema),
|
|
392
|
+
input: s.opt(s.arr(s.union([s.lit("text"), s.lit("image")]), {
|
|
393
|
+
minItems: 1,
|
|
394
|
+
uniqueItems: true
|
|
395
|
+
})),
|
|
396
|
+
contextWindow: s.opt(s.int({ minimum: 1 })),
|
|
397
|
+
maxTokens: s.opt(s.int({ minimum: 1 }))
|
|
398
|
+
};
|
|
399
|
+
const CustomProviderModelSchema = s.obj(CustomProviderModelProperties, { open: false });
|
|
400
|
+
const CreateCustomProviderInputSchema = s.obj({
|
|
401
|
+
name: s.str({ minLength: 1 }),
|
|
402
|
+
protocol: CustomProviderProtocolSchema,
|
|
403
|
+
baseUrl: s.str({ minLength: 1 }),
|
|
404
|
+
catalogRefs: s.opt(s.arr(s.str({ minLength: 1 }), {
|
|
405
|
+
minItems: 1,
|
|
406
|
+
uniqueItems: true
|
|
407
|
+
})),
|
|
408
|
+
models: s.arr(CustomProviderModelSchema)
|
|
409
|
+
}, { open: false });
|
|
410
|
+
const UpdateCustomProviderInputSchema = CreateCustomProviderInputSchema;
|
|
411
|
+
const CustomProviderDefinitionSchema = s.obj({
|
|
412
|
+
id: s.str({ minLength: 1 }),
|
|
413
|
+
name: s.str({ minLength: 1 }),
|
|
414
|
+
protocol: CustomProviderProtocolSchema,
|
|
415
|
+
baseUrl: s.str({ minLength: 1 }),
|
|
416
|
+
catalogRefs: s.opt(s.arr(s.str({ minLength: 1 }), {
|
|
417
|
+
minItems: 1,
|
|
418
|
+
uniqueItems: true
|
|
419
|
+
})),
|
|
420
|
+
models: s.arr(CustomProviderModelSchema)
|
|
421
|
+
}, { open: false });
|
|
422
|
+
const ProviderProtocolSchema = s.str({ minLength: 1 });
|
|
423
|
+
const ProviderEndpointConfigurationProperties = {
|
|
424
|
+
apiType: ProviderProtocolSchema,
|
|
425
|
+
baseUrl: s.str({ minLength: 1 })
|
|
426
|
+
};
|
|
427
|
+
const ProviderEndpointConfigurationSchema = s.obj(ProviderEndpointConfigurationProperties, { open: false });
|
|
428
|
+
const ProviderEndpointOverrideSchema = s.obj({
|
|
429
|
+
...ProviderEndpointConfigurationProperties,
|
|
430
|
+
headers: s.opt(s.map(s.str()))
|
|
431
|
+
}, { open: false });
|
|
432
|
+
const ConfigurableProviderSchema = s.obj({
|
|
433
|
+
providerId: s.str({ minLength: 1 }),
|
|
434
|
+
supported: s.arr(ProviderProtocolSchema, {
|
|
435
|
+
minItems: 1,
|
|
436
|
+
uniqueItems: true
|
|
437
|
+
}),
|
|
438
|
+
required: s.bool(),
|
|
439
|
+
current: s.union([ProviderEndpointConfigurationSchema, s.nul()])
|
|
440
|
+
}, { open: false });
|
|
441
|
+
|
|
442
|
+
//#endregion
|
|
443
|
+
//#region src/providers/model-catalog.ts
|
|
444
|
+
const BUILTIN_CATALOG_REFS = getBuiltinProviders();
|
|
445
|
+
const BUILTIN_MODELS_BY_CATALOG = /* @__PURE__ */ new Map();
|
|
446
|
+
for (const catalogRef of BUILTIN_CATALOG_REFS) {
|
|
447
|
+
const modelsById = /* @__PURE__ */ new Map();
|
|
448
|
+
for (const model of getBuiltinModels(catalogRef)) modelsById.set(model.id, model);
|
|
449
|
+
BUILTIN_MODELS_BY_CATALOG.set(catalogRef, modelsById);
|
|
450
|
+
}
|
|
451
|
+
const CATALOG_INDEX_CACHE_ROOT = { children: /* @__PURE__ */ new Map() };
|
|
452
|
+
const CATALOG_ROUTE_SUFFIXES = [
|
|
453
|
+
":free",
|
|
454
|
+
":nitro",
|
|
455
|
+
":exacto",
|
|
456
|
+
":extended",
|
|
457
|
+
":online"
|
|
458
|
+
];
|
|
459
|
+
function normalizeCatalogRefs(catalogRefs) {
|
|
460
|
+
const normalized = [];
|
|
461
|
+
const seen = /* @__PURE__ */ new Set();
|
|
462
|
+
for (const catalogRef of catalogRefs) {
|
|
463
|
+
const value = catalogRef.trim().toLowerCase();
|
|
464
|
+
if (value.length === 0 || seen.has(value)) continue;
|
|
465
|
+
seen.add(value);
|
|
466
|
+
normalized.push(value);
|
|
467
|
+
}
|
|
468
|
+
return normalized;
|
|
469
|
+
}
|
|
470
|
+
function cachedCatalogIndex(catalogRefs) {
|
|
471
|
+
let node = CATALOG_INDEX_CACHE_ROOT;
|
|
472
|
+
for (const catalogRef of normalizeCatalogRefs(catalogRefs)) {
|
|
473
|
+
let child = node.children.get(catalogRef);
|
|
474
|
+
if (child === void 0) {
|
|
475
|
+
child = { children: /* @__PURE__ */ new Map() };
|
|
476
|
+
node.children.set(catalogRef, child);
|
|
477
|
+
}
|
|
478
|
+
node = child;
|
|
479
|
+
}
|
|
480
|
+
return node;
|
|
481
|
+
}
|
|
482
|
+
function addIfAbsent(index, key, model) {
|
|
483
|
+
if (key.length > 0 && !index.has(key)) index.set(key, model);
|
|
484
|
+
}
|
|
485
|
+
function modelBasename(modelId) {
|
|
486
|
+
const separator = modelId.lastIndexOf("/");
|
|
487
|
+
if (separator < 1 || separator === modelId.length - 1) return void 0;
|
|
488
|
+
return modelId.slice(separator + 1);
|
|
489
|
+
}
|
|
490
|
+
function buildCatalogIndex(catalogRefs, modelsByCatalog) {
|
|
491
|
+
const modelsById = /* @__PURE__ */ new Map();
|
|
492
|
+
const catalogs = normalizeCatalogRefs(catalogRefs).flatMap((catalogRef) => {
|
|
493
|
+
const catalog = modelsByCatalog.get(catalogRef);
|
|
494
|
+
return catalog === void 0 ? [] : [{
|
|
495
|
+
catalogRef,
|
|
496
|
+
catalog
|
|
497
|
+
}];
|
|
498
|
+
});
|
|
499
|
+
for (const { catalog } of catalogs) for (const [modelId, model] of catalog) {
|
|
500
|
+
addIfAbsent(modelsById, modelId, model);
|
|
501
|
+
addIfAbsent(modelsById, modelId.trim(), model);
|
|
502
|
+
}
|
|
503
|
+
for (const { catalogRef, catalog } of catalogs) for (const [modelId, model] of catalog) {
|
|
504
|
+
const trimmedModelId = modelId.trim();
|
|
505
|
+
addIfAbsent(modelsById, `${catalogRef}/${modelId}`, model);
|
|
506
|
+
addIfAbsent(modelsById, `${catalogRef}/${trimmedModelId}`, model);
|
|
507
|
+
}
|
|
508
|
+
const basenameMatches = /* @__PURE__ */ new Map();
|
|
509
|
+
for (const { catalog } of catalogs) for (const [modelId, model] of catalog) {
|
|
510
|
+
const basename = modelBasename(modelId.trim());
|
|
511
|
+
if (basename === void 0) continue;
|
|
512
|
+
basenameMatches.set(basename, basenameMatches.has(basename) ? null : model);
|
|
513
|
+
}
|
|
514
|
+
for (const [basename, model] of basenameMatches) if (model !== null) addIfAbsent(modelsById, basename, model);
|
|
515
|
+
return modelsById;
|
|
516
|
+
}
|
|
517
|
+
const ALL_BUILTIN_MODELS = buildCatalogIndex(BUILTIN_CATALOG_REFS, BUILTIN_MODELS_BY_CATALOG);
|
|
518
|
+
function modelIdWithoutRouteSuffix(modelId) {
|
|
519
|
+
const suffix = CATALOG_ROUTE_SUFFIXES.find((candidate) => modelId.endsWith(candidate));
|
|
520
|
+
if (suffix === void 0) return void 0;
|
|
521
|
+
const baseModelId = modelId.slice(0, -suffix.length);
|
|
522
|
+
return baseModelId.length === 0 ? void 0 : baseModelId;
|
|
523
|
+
}
|
|
524
|
+
function lookupCatalogModel(index, modelId) {
|
|
525
|
+
const exact = index.get(modelId) ?? index.get(modelId.trim());
|
|
526
|
+
if (exact !== void 0) return exact;
|
|
527
|
+
const baseModelId = modelIdWithoutRouteSuffix(modelId);
|
|
528
|
+
if (baseModelId === void 0) return void 0;
|
|
529
|
+
return index.get(baseModelId) ?? index.get(baseModelId.trim());
|
|
530
|
+
}
|
|
531
|
+
function lookupNamespacedCatalogModel(modelId, catalogRefs, modelsByCatalog) {
|
|
532
|
+
const separator = modelId.indexOf("/");
|
|
533
|
+
if (separator < 1 || separator === modelId.length - 1) return void 0;
|
|
534
|
+
const route = modelId.slice(0, separator).trim().toLowerCase();
|
|
535
|
+
const namespace = route === "codex" ? "openai" : route;
|
|
536
|
+
const normalizedRefs = catalogRefs === void 0 ? [] : normalizeCatalogRefs(catalogRefs);
|
|
537
|
+
if (!(normalizedRefs.length === 0 ? [...modelsByCatalog.keys()] : normalizedRefs).includes(namespace)) return void 0;
|
|
538
|
+
const catalog = modelsByCatalog.get(namespace);
|
|
539
|
+
return catalog === void 0 ? void 0 : lookupCatalogModel(catalog, route === "codex" ? modelId.slice(separator + 1).toLowerCase() : modelId.slice(separator + 1));
|
|
540
|
+
}
|
|
541
|
+
function catalogModelIndex(catalogRefs) {
|
|
542
|
+
if (catalogRefs === void 0) return ALL_BUILTIN_MODELS;
|
|
543
|
+
const normalizedRefs = normalizeCatalogRefs(catalogRefs);
|
|
544
|
+
if (normalizedRefs.length === 0) return ALL_BUILTIN_MODELS;
|
|
545
|
+
const cacheNode = cachedCatalogIndex(normalizedRefs);
|
|
546
|
+
if (cacheNode.index !== void 0) return cacheNode.index;
|
|
547
|
+
const index = buildCatalogIndex(catalogRefs, BUILTIN_MODELS_BY_CATALOG);
|
|
548
|
+
cacheNode.index = index;
|
|
549
|
+
return index;
|
|
550
|
+
}
|
|
551
|
+
function findModelAcrossCatalogs(modelId) {
|
|
552
|
+
return lookupNamespacedCatalogModel(modelId, void 0, BUILTIN_MODELS_BY_CATALOG) ?? lookupCatalogModel(ALL_BUILTIN_MODELS, modelId);
|
|
553
|
+
}
|
|
554
|
+
function findCatalogModel(modelId, catalogRefs) {
|
|
555
|
+
return lookupNamespacedCatalogModel(modelId, catalogRefs, BUILTIN_MODELS_BY_CATALOG) ?? lookupCatalogModel(catalogModelIndex(catalogRefs), modelId);
|
|
556
|
+
}
|
|
557
|
+
function catalogModelMetadata(model) {
|
|
558
|
+
if (model === void 0) return {};
|
|
559
|
+
const { id: _id, name: _name, api: _api, provider: _provider, baseUrl: _baseUrl, headers: _headers,...metadata } = model;
|
|
560
|
+
return metadata;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
//#endregion
|
|
564
|
+
//#region src/providers/custom-providers.ts
|
|
565
|
+
const apiFactories = {
|
|
566
|
+
"anthropic-messages": anthropicMessagesApi,
|
|
567
|
+
"openai-responses": openAIResponsesApi,
|
|
568
|
+
"openai-completions": openAICompletionsApi
|
|
569
|
+
};
|
|
570
|
+
const RemoteModelSchema = s.obj({
|
|
571
|
+
...CustomProviderModelProperties,
|
|
572
|
+
display_name: s.opt(s.str({ minLength: 1 })),
|
|
573
|
+
context_window: s.opt(s.union([s.int({ minimum: 1 }), s.nul()])),
|
|
574
|
+
context_length: s.opt(s.union([s.int({ minimum: 1 }), s.nul()])),
|
|
575
|
+
max_output_tokens: s.opt(s.union([s.int({ minimum: 1 }), s.nul()])),
|
|
576
|
+
modalities: s.opt(s.union([s.obj({ input: s.opt(s.arr(s.str())) }, { open: true }), s.nul()])),
|
|
577
|
+
reasoning_levels: s.opt(s.union([s.arr(s.str()), s.nul()])),
|
|
578
|
+
supported_reasoning_levels: s.opt(s.union([s.arr(s.obj({ effort: s.str() }, { open: true })), s.nul()]))
|
|
579
|
+
}, { open: true });
|
|
580
|
+
const SupportedInputModalities = ["text", "image"];
|
|
581
|
+
const ThinkingLevels = [
|
|
582
|
+
"off",
|
|
583
|
+
"minimal",
|
|
584
|
+
"low",
|
|
585
|
+
"medium",
|
|
586
|
+
"high",
|
|
587
|
+
"xhigh",
|
|
588
|
+
"max"
|
|
589
|
+
];
|
|
590
|
+
const RemoteModelListSchema = s.union([s.obj({
|
|
591
|
+
data: s.arr(RemoteModelSchema),
|
|
592
|
+
models: s.opt({ not: {} })
|
|
593
|
+
}, { open: true }), s.obj({
|
|
594
|
+
data: s.opt({ not: {} }),
|
|
595
|
+
models: s.arr(RemoteModelSchema)
|
|
596
|
+
}, { open: true })]);
|
|
597
|
+
function customProviderFromDefinition(definition, catalog) {
|
|
598
|
+
const provider = createProvider({
|
|
599
|
+
id: definition.id,
|
|
600
|
+
name: definition.name,
|
|
601
|
+
baseUrl: definition.baseUrl,
|
|
602
|
+
auth: { apiKey: envApiKeyAuth(`${definition.name} API key`, []) },
|
|
603
|
+
models: definition.models.map((model) => modelFromDefinition(definition, model, catalog)),
|
|
604
|
+
fetchModels: (context) => fetchModels(definition, context, catalog),
|
|
605
|
+
filterModels: catalog === void 0 ? void 0 : (models) => advertisedModels(definition, catalog, models),
|
|
606
|
+
api: apiFactories[definition.protocol]()
|
|
607
|
+
});
|
|
608
|
+
const configured = new Map(definition.models.map((model) => [model.id, model]));
|
|
609
|
+
let cached = (catalog?.remoteModels(definition) ?? []).map((model) => modelFromDefinition(definition, {
|
|
610
|
+
...model,
|
|
611
|
+
...configured.get(model.id)
|
|
612
|
+
}, catalog));
|
|
613
|
+
const refresh = provider.refreshModels;
|
|
614
|
+
return {
|
|
615
|
+
...provider,
|
|
616
|
+
getModels: () => {
|
|
617
|
+
const models = provider.getModels();
|
|
618
|
+
const ids = new Set(models.map((model) => model.id));
|
|
619
|
+
return [...models, ...cached.filter((model) => !ids.has(model.id))];
|
|
620
|
+
},
|
|
621
|
+
getAllModels: () => {
|
|
622
|
+
const models = provider.getAllModels?.() ?? provider.getModels();
|
|
623
|
+
const ids = new Set(models.map((model) => model.id));
|
|
624
|
+
return [...models, ...cached.filter((model) => !ids.has(model.id))];
|
|
625
|
+
},
|
|
626
|
+
refreshModels: refresh === void 0 ? void 0 : (context) => refresh({
|
|
627
|
+
...context,
|
|
628
|
+
publish: (publication) => context.publish({
|
|
629
|
+
...publication,
|
|
630
|
+
update: () => {
|
|
631
|
+
publication.update?.();
|
|
632
|
+
if (publication.persist !== void 0) cached = [];
|
|
633
|
+
}
|
|
634
|
+
})
|
|
635
|
+
})
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Discovery decides which model ids an endpoint currently offers; the configured
|
|
640
|
+
* list only supplies their metadata. Without this, a model the endpoint stopped
|
|
641
|
+
* advertising stays selectable forever, because Pi merges the configured
|
|
642
|
+
* baseline with discovered models and never subtracts. Filtering instead of
|
|
643
|
+
* deleting keeps the entry resolvable by id, so a Session that still records it
|
|
644
|
+
* can be resumed after it disappears upstream.
|
|
645
|
+
*/
|
|
646
|
+
function advertisedModels(definition, catalog, models) {
|
|
647
|
+
const discovered = catalog.remoteModels(definition);
|
|
648
|
+
if (discovered.length === 0) return models;
|
|
649
|
+
const advertised = new Set(discovered.map((model) => model.id));
|
|
650
|
+
return models.filter((model) => advertised.has(model.id));
|
|
651
|
+
}
|
|
652
|
+
async function fetchModels(definition, context, catalog) {
|
|
653
|
+
const baseUrl = definition.baseUrl.endsWith("/") ? definition.baseUrl : `${definition.baseUrl}/`;
|
|
654
|
+
const endpoint = new URL("models", baseUrl);
|
|
655
|
+
const headers = { accept: "application/json" };
|
|
656
|
+
const credential = context.credential;
|
|
657
|
+
const token = credentialToken(credential);
|
|
658
|
+
if (token !== void 0) if (definition.protocol === "anthropic-messages") {
|
|
659
|
+
headers["x-api-key"] = token;
|
|
660
|
+
headers["anthropic-version"] = "2023-06-01";
|
|
661
|
+
} else headers.authorization = `Bearer ${token}`;
|
|
662
|
+
const response = await fetch(endpoint, {
|
|
663
|
+
headers,
|
|
664
|
+
signal: context.signal
|
|
665
|
+
});
|
|
666
|
+
if (!response.ok) throw new Error(`Model discovery failed with HTTP ${response.status}`);
|
|
667
|
+
const payload = decode(RemoteModelListSchema, await response.json());
|
|
668
|
+
const discovered = (payload.data ?? payload.models ?? []).map((model) => ({
|
|
669
|
+
id: model.id,
|
|
670
|
+
name: model.name ?? model.display_name ?? model.id,
|
|
671
|
+
...pickBy({
|
|
672
|
+
reasoning: model.reasoning,
|
|
673
|
+
input: model.input ?? remoteInput(model),
|
|
674
|
+
contextWindow: model.contextWindow ?? model.context_window ?? model.context_length ?? void 0,
|
|
675
|
+
maxTokens: model.maxTokens ?? model.max_output_tokens ?? void 0,
|
|
676
|
+
thinkingLevelMap: model.thinkingLevelMap ?? remoteThinkingLevelMap(model)
|
|
677
|
+
}, (value) => value !== void 0)
|
|
678
|
+
}));
|
|
679
|
+
context.signal.throwIfAborted();
|
|
680
|
+
if (discovered.length > 0) await catalog?.rememberRemoteModels(definition, discovered);
|
|
681
|
+
context.signal.throwIfAborted();
|
|
682
|
+
const configuredModels = new Map(definition.models.map((model) => [model.id, model]));
|
|
683
|
+
return discovered.map((remoteModel) => modelFromDefinition(definition, {
|
|
684
|
+
...remoteModel,
|
|
685
|
+
...configuredModels.get(remoteModel.id)
|
|
686
|
+
}, catalog));
|
|
687
|
+
}
|
|
688
|
+
function remoteInput(model) {
|
|
689
|
+
const advertised = model.modalities?.input;
|
|
690
|
+
if (advertised === void 0) return void 0;
|
|
691
|
+
const input = [];
|
|
692
|
+
for (const modality of SupportedInputModalities) if (advertised.includes(modality)) input.push(modality);
|
|
693
|
+
return input.length === 0 ? void 0 : input;
|
|
694
|
+
}
|
|
695
|
+
function remoteThinkingLevelMap(model) {
|
|
696
|
+
const advertised = model.supported_reasoning_levels?.map(({ effort }) => effort) ?? model.reasoning_levels ?? [];
|
|
697
|
+
if (advertised.length === 0) return void 0;
|
|
698
|
+
const supported = new Set(advertised);
|
|
699
|
+
const map = {};
|
|
700
|
+
for (const level of ThinkingLevels) {
|
|
701
|
+
let effort;
|
|
702
|
+
if (level === "off") {
|
|
703
|
+
if (supported.has("none")) effort = "none";
|
|
704
|
+
else if (supported.has("off")) effort = "off";
|
|
705
|
+
} else if (supported.has(level)) effort = level;
|
|
706
|
+
map[level] = effort ?? null;
|
|
707
|
+
}
|
|
708
|
+
return map;
|
|
709
|
+
}
|
|
710
|
+
function credentialToken(credential) {
|
|
711
|
+
if (credential?.type === "api_key") return credential.key;
|
|
712
|
+
if (credential?.type === "oauth") return credential.access;
|
|
713
|
+
}
|
|
714
|
+
function modelFromDefinition(definition, model, catalog) {
|
|
715
|
+
const catalogModel = findCatalogModel(model.id, definition.catalogRefs);
|
|
716
|
+
const catalogMetadata = {
|
|
717
|
+
...catalogModelMetadata(catalogModel),
|
|
718
|
+
...catalog?.find(model.id, definition.catalogRefs)
|
|
719
|
+
};
|
|
720
|
+
if (catalogModel !== void 0 && catalogMetadata.contextWindow !== void 0) catalogMetadata.contextWindow = Math.min(catalogModel.contextWindow, catalogMetadata.contextWindow);
|
|
721
|
+
const metadata = {
|
|
722
|
+
...catalogMetadata,
|
|
723
|
+
...catalog?.remoteModel(definition, model.id)
|
|
724
|
+
};
|
|
725
|
+
return {
|
|
726
|
+
...metadata,
|
|
727
|
+
id: model.id,
|
|
728
|
+
name: model.name ?? catalog?.remoteModel(definition, model.id)?.name ?? catalogModel?.name ?? model.id,
|
|
729
|
+
api: definition.protocol,
|
|
730
|
+
provider: definition.id,
|
|
731
|
+
baseUrl: definition.baseUrl,
|
|
732
|
+
reasoning: model.reasoning ?? metadata.reasoning ?? false,
|
|
733
|
+
thinkingLevelMap: model.thinkingLevelMap ?? metadata.thinkingLevelMap,
|
|
734
|
+
input: model.input ?? metadata.input ?? ["text"],
|
|
735
|
+
cost: metadata.cost ?? {
|
|
736
|
+
input: 0,
|
|
737
|
+
output: 0,
|
|
738
|
+
cacheRead: 0,
|
|
739
|
+
cacheWrite: 0
|
|
740
|
+
},
|
|
741
|
+
contextWindow: model.contextWindow ?? metadata.contextWindow ?? 128e3,
|
|
742
|
+
maxTokens: model.maxTokens ?? metadata.maxTokens ?? 8192
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
//#endregion
|
|
747
|
+
//#region src/providers/models-dev.ts
|
|
748
|
+
const ModelSchema = s.obj({
|
|
749
|
+
id: s.str(),
|
|
750
|
+
name: s.str(),
|
|
751
|
+
reasoning: s.opt(s.bool()),
|
|
752
|
+
reasoning_options: s.opt(s.arr(s.obj({
|
|
753
|
+
type: s.str(),
|
|
754
|
+
values: s.opt(s.arr(s.union([s.str(), s.nul()])))
|
|
755
|
+
}, { open: true }))),
|
|
756
|
+
modalities: s.opt(s.obj({ input: s.arr(s.str()) }, { open: true })),
|
|
757
|
+
limit: s.opt(s.obj({
|
|
758
|
+
context: s.num(),
|
|
759
|
+
output: s.num()
|
|
760
|
+
}, { open: true })),
|
|
761
|
+
cost: s.opt(s.obj({
|
|
762
|
+
input: s.num(),
|
|
763
|
+
output: s.num(),
|
|
764
|
+
cache_read: s.opt(s.num()),
|
|
765
|
+
cache_write: s.opt(s.num())
|
|
766
|
+
}, { open: true }))
|
|
767
|
+
}, { open: true });
|
|
768
|
+
const CatalogSchema = s.map(s.obj({ models: s.map(ModelSchema) }, { open: true }));
|
|
769
|
+
const DiscoveredSchema = s.map(s.obj({
|
|
770
|
+
baseUrl: s.str(),
|
|
771
|
+
protocol: CustomProviderProtocolSchema,
|
|
772
|
+
models: s.arr(CustomProviderModelSchema)
|
|
773
|
+
}, { open: true }));
|
|
774
|
+
const CacheSchema = s.obj({
|
|
775
|
+
checkedAt: s.num(),
|
|
776
|
+
catalog: CatalogSchema,
|
|
777
|
+
discovered: s.opt(DiscoveredSchema)
|
|
778
|
+
}, { open: true });
|
|
779
|
+
/** Optional metadata enrichment; Provider dispatch and authentication remain owned by Pi. */
|
|
780
|
+
var ModelsDevCatalog = class ModelsDevCatalog {
|
|
781
|
+
#filePath;
|
|
782
|
+
#catalog = {};
|
|
783
|
+
#modelsByCatalog = /* @__PURE__ */ new Map();
|
|
784
|
+
#allModels = /* @__PURE__ */ new Map();
|
|
785
|
+
#orderedModels = /* @__PURE__ */ new WeakMap();
|
|
786
|
+
#checkedAt = 0;
|
|
787
|
+
#refreshing;
|
|
788
|
+
#discovered = {};
|
|
789
|
+
#mutation = new Mutex();
|
|
790
|
+
constructor(filePath) {
|
|
791
|
+
this.#filePath = filePath;
|
|
792
|
+
}
|
|
793
|
+
static async open(filePath) {
|
|
794
|
+
const catalog = new ModelsDevCatalog(filePath);
|
|
795
|
+
try {
|
|
796
|
+
const stored = decode(CacheSchema, JSON.parse(await readFile(filePath, "utf8")));
|
|
797
|
+
catalog.#install(stored.catalog);
|
|
798
|
+
catalog.#checkedAt = stored.checkedAt;
|
|
799
|
+
catalog.#discovered = stored.discovered ?? {};
|
|
800
|
+
} catch {}
|
|
801
|
+
return catalog;
|
|
802
|
+
}
|
|
803
|
+
find(modelId, catalogRefs) {
|
|
804
|
+
let index = this.#allModels;
|
|
805
|
+
if (catalogRefs !== void 0 && normalizeCatalogRefs(catalogRefs).length > 0) {
|
|
806
|
+
index = this.#orderedModels.get(catalogRefs) ?? buildCatalogIndex(catalogRefs, this.#modelsByCatalog);
|
|
807
|
+
this.#orderedModels.set(catalogRefs, index);
|
|
808
|
+
}
|
|
809
|
+
const model = lookupNamespacedCatalogModel(modelId, catalogRefs, this.#modelsByCatalog) ?? lookupCatalogModel(index, modelId);
|
|
810
|
+
if (model === void 0) return;
|
|
811
|
+
const metadata = {};
|
|
812
|
+
if (model.reasoning !== void 0) metadata.reasoning = model.reasoning;
|
|
813
|
+
const efforts = model.reasoning_options?.find((option) => option.type === "effort")?.values;
|
|
814
|
+
if (efforts !== void 0) {
|
|
815
|
+
metadata.thinkingLevelMap = {};
|
|
816
|
+
for (const level of [
|
|
817
|
+
"off",
|
|
818
|
+
"minimal",
|
|
819
|
+
"low",
|
|
820
|
+
"medium",
|
|
821
|
+
"high",
|
|
822
|
+
"xhigh",
|
|
823
|
+
"max"
|
|
824
|
+
]) {
|
|
825
|
+
const effort = level === "off" ? "none" : level;
|
|
826
|
+
metadata.thinkingLevelMap[level] = efforts.includes(effort) ? effort : null;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
const input = [];
|
|
830
|
+
if (model.modalities?.input.includes("text")) input.push("text");
|
|
831
|
+
if (model.modalities?.input.includes("image")) input.push("image");
|
|
832
|
+
if (input.length > 0) metadata.input = input;
|
|
833
|
+
if (model.limit?.context && model.limit.context > 0) metadata.contextWindow = model.limit.context;
|
|
834
|
+
if (model.limit?.output && model.limit.output > 0) metadata.maxTokens = model.limit.output;
|
|
835
|
+
if (model.cost !== void 0) metadata.cost = {
|
|
836
|
+
input: model.cost.input,
|
|
837
|
+
output: model.cost.output,
|
|
838
|
+
cacheRead: model.cost.cache_read ?? 0,
|
|
839
|
+
cacheWrite: model.cost.cache_write ?? 0
|
|
840
|
+
};
|
|
841
|
+
return metadata;
|
|
842
|
+
}
|
|
843
|
+
remoteModel(definition, modelId) {
|
|
844
|
+
return this.remoteModels(definition).find((model) => model.id === modelId);
|
|
845
|
+
}
|
|
846
|
+
remoteModels(definition) {
|
|
847
|
+
const cached = this.#discovered[definition.id];
|
|
848
|
+
if (cached?.baseUrl !== definition.baseUrl || cached.protocol !== definition.protocol) return [];
|
|
849
|
+
return cached.models;
|
|
850
|
+
}
|
|
851
|
+
rememberRemoteModels(definition, models) {
|
|
852
|
+
return this.#enqueue(async () => {
|
|
853
|
+
const discovered = {
|
|
854
|
+
...this.#discovered,
|
|
855
|
+
[definition.id]: {
|
|
856
|
+
baseUrl: definition.baseUrl,
|
|
857
|
+
protocol: definition.protocol,
|
|
858
|
+
models: [...models]
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
await this.#persist(this.#catalog, this.#checkedAt, discovered);
|
|
862
|
+
this.#discovered = discovered;
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
async refresh(options) {
|
|
866
|
+
if (options?.allowNetwork === false || options?.signal?.aborted) return;
|
|
867
|
+
if (!options?.force && Date.now() - this.#checkedAt < 864e5) return;
|
|
868
|
+
if (this.#refreshing !== void 0) return this.#refreshing;
|
|
869
|
+
this.#refreshing = this.#fetch(options?.signal);
|
|
870
|
+
try {
|
|
871
|
+
await this.#refreshing;
|
|
872
|
+
} finally {
|
|
873
|
+
this.#refreshing = void 0;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
forgetRemoteModels(providerId) {
|
|
877
|
+
return this.#enqueue(async () => {
|
|
878
|
+
const discovered = { ...this.#discovered };
|
|
879
|
+
delete discovered[providerId];
|
|
880
|
+
await this.#persist(this.#catalog, this.#checkedAt, discovered);
|
|
881
|
+
this.#discovered = discovered;
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
async #fetch(signal) {
|
|
885
|
+
const timeout = AbortSignal.timeout(5e3);
|
|
886
|
+
const requestSignal = signal === void 0 ? timeout : AbortSignal.any([signal, timeout]);
|
|
887
|
+
try {
|
|
888
|
+
const response = await fetch("https://models.dev/api.json", { signal: requestSignal });
|
|
889
|
+
if (!response.ok) return;
|
|
890
|
+
const catalog = decode(CatalogSchema, await response.json());
|
|
891
|
+
requestSignal.throwIfAborted();
|
|
892
|
+
await this.#enqueue(async () => {
|
|
893
|
+
requestSignal.throwIfAborted();
|
|
894
|
+
const checkedAt = Date.now();
|
|
895
|
+
await this.#persist(catalog, checkedAt, this.#discovered);
|
|
896
|
+
this.#install(catalog);
|
|
897
|
+
this.#checkedAt = checkedAt;
|
|
898
|
+
});
|
|
899
|
+
} catch {}
|
|
900
|
+
}
|
|
901
|
+
#install(catalog) {
|
|
902
|
+
this.#catalog = catalog;
|
|
903
|
+
this.#modelsByCatalog = new Map(Object.entries(catalog).map(([ref, provider]) => [ref, new Map(Object.entries(provider.models))]));
|
|
904
|
+
this.#allModels = buildCatalogIndex([...this.#modelsByCatalog.keys()], this.#modelsByCatalog);
|
|
905
|
+
this.#orderedModels = /* @__PURE__ */ new WeakMap();
|
|
906
|
+
}
|
|
907
|
+
async #persist(catalog, checkedAt, discovered) {
|
|
908
|
+
const temporaryPath = `${this.#filePath}.${randomUUID()}.tmp`;
|
|
909
|
+
try {
|
|
910
|
+
await mkdir(dirname(this.#filePath), {
|
|
911
|
+
recursive: true,
|
|
912
|
+
mode: 448
|
|
913
|
+
});
|
|
914
|
+
await writeFile(temporaryPath, JSON.stringify({
|
|
915
|
+
checkedAt,
|
|
916
|
+
catalog,
|
|
917
|
+
discovered
|
|
918
|
+
}), { mode: 384 });
|
|
919
|
+
await rename(temporaryPath, this.#filePath);
|
|
920
|
+
} finally {
|
|
921
|
+
await rm(temporaryPath, { force: true });
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
async #enqueue(operation) {
|
|
925
|
+
await this.#mutation.acquire();
|
|
926
|
+
try {
|
|
927
|
+
await operation();
|
|
928
|
+
} finally {
|
|
929
|
+
this.#mutation.release();
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
//#endregion
|
|
935
|
+
//#region src/providers/store.ts
|
|
936
|
+
const CustomProviderDocumentSchema = s.map(CustomProviderDefinitionSchema);
|
|
937
|
+
/** Persists non-secret custom Provider definitions with owner-only permissions. */
|
|
938
|
+
var JsonCustomProviderStore = class extends JsonMapStore {
|
|
939
|
+
constructor(filePath) {
|
|
940
|
+
super(filePath, CustomProviderDocumentSchema);
|
|
941
|
+
}
|
|
942
|
+
async list(options = {}) {
|
|
943
|
+
options.signal?.throwIfAborted();
|
|
944
|
+
await this.ready;
|
|
945
|
+
const providers = [...(await this.load()).values()].map((provider) => structuredClone(provider));
|
|
946
|
+
options.signal?.throwIfAborted();
|
|
947
|
+
return providers;
|
|
948
|
+
}
|
|
949
|
+
create(provider, options) {
|
|
950
|
+
return this.mutate((providers) => {
|
|
951
|
+
if (providers.has(provider.id)) throw new Error(`Custom Provider ${provider.id} already exists`);
|
|
952
|
+
providers.set(provider.id, structuredClone(provider));
|
|
953
|
+
return true;
|
|
954
|
+
}, options);
|
|
955
|
+
}
|
|
956
|
+
replace(provider, options) {
|
|
957
|
+
return this.mutate((providers) => {
|
|
958
|
+
if (!providers.has(provider.id)) throw new Error(`Custom Provider ${provider.id} does not exist`);
|
|
959
|
+
providers.set(provider.id, structuredClone(provider));
|
|
960
|
+
return true;
|
|
961
|
+
}, options);
|
|
962
|
+
}
|
|
963
|
+
delete(providerId, options) {
|
|
964
|
+
return this.mutate((providers) => providers.delete(providerId), options);
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
//#endregion
|
|
969
|
+
//#region src/providers/endpoint-store.ts
|
|
970
|
+
const ProviderEndpointDocumentSchema = s.map(ProviderEndpointConfigurationSchema);
|
|
971
|
+
/**
|
|
972
|
+
* Remembers which endpoint each Provider slot points at so a Host that configures
|
|
973
|
+
* one does not have to repeat it on every launch.
|
|
974
|
+
*
|
|
975
|
+
* Only the protocol and base URL are stored. Request headers carry credentials,
|
|
976
|
+
* so they stay in the live process: a Host that supplies headers must re-apply
|
|
977
|
+
* them after a restart, while the address it supplied survives.
|
|
978
|
+
*/
|
|
979
|
+
/** Persists non-secret Provider endpoint selection with owner-only permissions. */
|
|
980
|
+
var JsonProviderEndpointStore = class extends JsonMapStore {
|
|
981
|
+
constructor(filePath) {
|
|
982
|
+
super(filePath, ProviderEndpointDocumentSchema);
|
|
983
|
+
}
|
|
984
|
+
async list() {
|
|
985
|
+
await this.ready;
|
|
986
|
+
return [...await this.load()].map(([providerId, configuration]) => ({
|
|
987
|
+
providerId,
|
|
988
|
+
configuration
|
|
989
|
+
}));
|
|
990
|
+
}
|
|
991
|
+
set(providerId, configuration) {
|
|
992
|
+
return this.mutate((endpoints) => {
|
|
993
|
+
endpoints.set(providerId, structuredClone(configuration));
|
|
994
|
+
return true;
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
delete(providerId) {
|
|
998
|
+
return this.mutate((endpoints) => endpoints.delete(providerId));
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
|
|
1002
|
+
//#endregion
|
|
1003
|
+
//#region src/agent-models.ts
|
|
1004
|
+
function encodeModelSelector(providerId, modelId) {
|
|
1005
|
+
return `${encodeURIComponent(providerId)}/${encodeURIComponent(modelId)}`;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
//#endregion
|
|
1009
|
+
//#region src/providers/models.ts
|
|
1010
|
+
async function listAvailableControlModels(models, providerId) {
|
|
1011
|
+
return (await models.getAvailable(providerId)).map((model) => ({
|
|
1012
|
+
selector: encodeModelSelector(model.provider, model.id),
|
|
1013
|
+
providerId: model.provider,
|
|
1014
|
+
modelId: model.id,
|
|
1015
|
+
displayName: model.name
|
|
1016
|
+
}));
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
//#endregion
|
|
1020
|
+
//#region src/providers/catalog.ts
|
|
1021
|
+
const ProviderEndpointProtocols = new Set(["http:", "https:"]);
|
|
1022
|
+
/** Owns mutable Provider configuration and its synchronization with Pi Models. */
|
|
1023
|
+
var NodeProviderCatalog = class {
|
|
1024
|
+
credentials;
|
|
1025
|
+
models;
|
|
1026
|
+
directory;
|
|
1027
|
+
#customProviders;
|
|
1028
|
+
#providerEndpoints;
|
|
1029
|
+
#references;
|
|
1030
|
+
#createId;
|
|
1031
|
+
#metadataCatalog;
|
|
1032
|
+
constructor(options) {
|
|
1033
|
+
this.credentials = options.credentials;
|
|
1034
|
+
this.models = options.models;
|
|
1035
|
+
this.directory = options.directory;
|
|
1036
|
+
this.#customProviders = options.customProviders;
|
|
1037
|
+
this.#providerEndpoints = options.providerEndpoints;
|
|
1038
|
+
this.#references = options.references;
|
|
1039
|
+
this.#createId = options.createId;
|
|
1040
|
+
this.#metadataCatalog = options.metadataCatalog;
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Restores the endpoint each Provider slot pointed at before this process.
|
|
1044
|
+
*
|
|
1045
|
+
* A stored entry outlives the Provider it names: a definition can disappear
|
|
1046
|
+
* with a Plugin or a Pi release, and a stale address must not prevent startup.
|
|
1047
|
+
* Such entries are dropped so the file stops describing Providers that are gone.
|
|
1048
|
+
*/
|
|
1049
|
+
async applyPersistedEndpoints() {
|
|
1050
|
+
for (const { providerId, configuration } of await this.#providerEndpoints.list()) {
|
|
1051
|
+
if (!this.directory.has(providerId)) {
|
|
1052
|
+
await this.#providerEndpoints.delete(providerId);
|
|
1053
|
+
continue;
|
|
1054
|
+
}
|
|
1055
|
+
try {
|
|
1056
|
+
this.directory.set(providerId, configuration);
|
|
1057
|
+
} catch {
|
|
1058
|
+
await this.#providerEndpoints.delete(providerId);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
listCustom(options) {
|
|
1063
|
+
return this.#customProviders.list(options);
|
|
1064
|
+
}
|
|
1065
|
+
async createCustom(input, options) {
|
|
1066
|
+
const definition = decode(CustomProviderDefinitionSchema, {
|
|
1067
|
+
...decode(CreateCustomProviderInputSchema, input),
|
|
1068
|
+
id: this.#createId()
|
|
1069
|
+
});
|
|
1070
|
+
if (this.directory.has(definition.id)) throw new SorbetControlError(SorbetControlErrorCode.CustomProviderConflict, `Provider ${definition.id} already exists`);
|
|
1071
|
+
await this.#customProviders.create(definition, options);
|
|
1072
|
+
this.directory.add(customProviderFromDefinition(definition, this.#metadataCatalog), definition.protocol);
|
|
1073
|
+
return definition;
|
|
1074
|
+
}
|
|
1075
|
+
async updateCustom(providerId, input, options) {
|
|
1076
|
+
if ((await this.#customProviders.list(options)).find((provider) => provider.id === providerId) === void 0) throw new SorbetControlError(SorbetControlErrorCode.CustomProviderNotFound, `Unknown custom Provider ${providerId}`);
|
|
1077
|
+
const definition = decode(CustomProviderDefinitionSchema, {
|
|
1078
|
+
...decode(UpdateCustomProviderInputSchema, input),
|
|
1079
|
+
id: providerId
|
|
1080
|
+
});
|
|
1081
|
+
await this.#customProviders.replace(definition, options);
|
|
1082
|
+
this.directory.add(customProviderFromDefinition(definition, this.#metadataCatalog), definition.protocol);
|
|
1083
|
+
return definition;
|
|
1084
|
+
}
|
|
1085
|
+
async deleteCustom(providerId, options) {
|
|
1086
|
+
if ((await this.#customProviders.list(options)).find((entry) => entry.id === providerId) === void 0) throw new SorbetControlError(SorbetControlErrorCode.CustomProviderNotFound, `Unknown custom Provider ${providerId}`);
|
|
1087
|
+
if ((await this.#references.listReferencedProviderIds(options)).includes(providerId)) throw new SorbetControlError(SorbetControlErrorCode.CustomProviderInUse, `Custom Provider ${providerId} is used by a Session or the approval reviewer`);
|
|
1088
|
+
await this.#customProviders.delete(providerId, options);
|
|
1089
|
+
await this.#metadataCatalog?.forgetRemoteModels(providerId);
|
|
1090
|
+
this.directory.remove(providerId);
|
|
1091
|
+
await this.credentials.delete(providerId);
|
|
1092
|
+
await this.#providerEndpoints.delete(providerId);
|
|
1093
|
+
}
|
|
1094
|
+
listConfigurable() {
|
|
1095
|
+
return this.directory.list();
|
|
1096
|
+
}
|
|
1097
|
+
async setEndpoint(providerId, override) {
|
|
1098
|
+
let parsed;
|
|
1099
|
+
try {
|
|
1100
|
+
parsed = decode(ProviderEndpointOverrideSchema, override);
|
|
1101
|
+
const endpoint = new URL(parsed.baseUrl);
|
|
1102
|
+
if (!ProviderEndpointProtocols.has(endpoint.protocol)) throw new TypeError();
|
|
1103
|
+
} catch {
|
|
1104
|
+
throw new SorbetControlError(SorbetControlErrorCode.InvalidProviderEndpoint, "Invalid Provider endpoint configuration");
|
|
1105
|
+
}
|
|
1106
|
+
this.directory.set(providerId, parsed);
|
|
1107
|
+
await this.#providerEndpoints.set(providerId, {
|
|
1108
|
+
apiType: parsed.apiType,
|
|
1109
|
+
baseUrl: parsed.baseUrl
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
async disableEndpoint(providerId) {
|
|
1113
|
+
this.directory.disable(providerId);
|
|
1114
|
+
await this.#providerEndpoints.delete(providerId);
|
|
1115
|
+
}
|
|
1116
|
+
async refresh(options) {
|
|
1117
|
+
if ((await this.#customProviders.list()).some((provider) => options?.providers === void 0 || options.providers.includes(provider.id))) await this.#metadataCatalog?.refresh(options);
|
|
1118
|
+
const result = await this.models.refresh(options);
|
|
1119
|
+
if (!result.aborted) await this.#reconcileDiscoveredModels(options, result.errors);
|
|
1120
|
+
return result;
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* Makes each persisted definition match what its endpoint currently advertises.
|
|
1124
|
+
*
|
|
1125
|
+
* Availability already ignores a model the endpoint stopped offering, but
|
|
1126
|
+
* leaving it in `providers.json` keeps a dead catalog in the file that every
|
|
1127
|
+
* later discovery has to re-evaluate. Surviving entries keep their configured
|
|
1128
|
+
* metadata; models only the endpoint knows are recorded as bare ids so a later
|
|
1129
|
+
* discovery restores them without the user editing the file.
|
|
1130
|
+
*/
|
|
1131
|
+
async #reconcileDiscoveredModels(options, errors) {
|
|
1132
|
+
const catalog = this.#metadataCatalog;
|
|
1133
|
+
if (catalog === void 0) return;
|
|
1134
|
+
for (const definition of await this.#customProviders.list()) {
|
|
1135
|
+
if (options?.providers !== void 0 && !options.providers.includes(definition.id)) continue;
|
|
1136
|
+
if (errors.has(definition.id)) continue;
|
|
1137
|
+
const advertised = catalog.remoteModels(definition);
|
|
1138
|
+
if (advertised.length === 0) continue;
|
|
1139
|
+
const configured = new Map(definition.models.map((model) => [model.id, model]));
|
|
1140
|
+
const models = advertised.map((model) => configured.get(model.id) ?? { id: model.id });
|
|
1141
|
+
if (sameModelIds(definition.models, models)) continue;
|
|
1142
|
+
const reconciled = {
|
|
1143
|
+
...definition,
|
|
1144
|
+
models
|
|
1145
|
+
};
|
|
1146
|
+
await this.#customProviders.replace(reconciled);
|
|
1147
|
+
this.directory.add(customProviderFromDefinition(reconciled, catalog), reconciled.protocol);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
availableModels(providerId) {
|
|
1151
|
+
return listAvailableControlModels(this.models, providerId);
|
|
1152
|
+
}
|
|
1153
|
+
model(providerId, modelId) {
|
|
1154
|
+
const model = this.models.getModel(providerId, modelId);
|
|
1155
|
+
if (model === void 0) throw new SorbetControlError(SorbetControlErrorCode.ModelNotFound, `Unknown model ${providerId}/${modelId}`);
|
|
1156
|
+
return model;
|
|
1157
|
+
}
|
|
1158
|
+
};
|
|
1159
|
+
function sameModelIds(left, right) {
|
|
1160
|
+
if (left.length !== right.length) return false;
|
|
1161
|
+
const ids = new Set(left.map((model) => model.id));
|
|
1162
|
+
return right.every((model) => ids.has(model.id));
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
//#endregion
|
|
1166
|
+
//#region src/providers/directory.ts
|
|
1167
|
+
const AcpProtocolByPiApi = {
|
|
1168
|
+
"anthropic-messages": "anthropic",
|
|
1169
|
+
"openai-completions": "openai",
|
|
1170
|
+
"openai-responses": "openai",
|
|
1171
|
+
"openai-codex-responses": "openai",
|
|
1172
|
+
"azure-openai-responses": "azure",
|
|
1173
|
+
"bedrock-converse-stream": "bedrock",
|
|
1174
|
+
"google-vertex": "vertex"
|
|
1175
|
+
};
|
|
1176
|
+
/** Owns stable Pi Provider definitions and process-local endpoint selection. */
|
|
1177
|
+
var ProviderDirectory = class {
|
|
1178
|
+
#models;
|
|
1179
|
+
#entries = /* @__PURE__ */ new Map();
|
|
1180
|
+
constructor(models) {
|
|
1181
|
+
this.#models = models;
|
|
1182
|
+
for (const provider of models.getProviders()) this.#record(provider);
|
|
1183
|
+
}
|
|
1184
|
+
list() {
|
|
1185
|
+
return [...this.#entries.entries()].map(([providerId, entry]) => ({
|
|
1186
|
+
providerId,
|
|
1187
|
+
supported: [...entry.supported],
|
|
1188
|
+
required: false,
|
|
1189
|
+
current: entry.current === null ? null : { ...entry.current }
|
|
1190
|
+
})).sort((left, right) => left.providerId.localeCompare(right.providerId));
|
|
1191
|
+
}
|
|
1192
|
+
has(providerId) {
|
|
1193
|
+
return this.#entries.has(providerId) || this.#models.getProvider(providerId) !== void 0;
|
|
1194
|
+
}
|
|
1195
|
+
add(provider, protocolHint) {
|
|
1196
|
+
this.#models.setProvider(provider);
|
|
1197
|
+
this.#record(provider, protocolHint);
|
|
1198
|
+
}
|
|
1199
|
+
remove(providerId) {
|
|
1200
|
+
this.#entries.delete(providerId);
|
|
1201
|
+
this.#models.deleteProvider(providerId);
|
|
1202
|
+
}
|
|
1203
|
+
set(providerId, override) {
|
|
1204
|
+
const entry = this.#entries.get(providerId);
|
|
1205
|
+
if (entry === void 0) throw new SorbetControlError(SorbetControlErrorCode.ProviderNotFound, `Unknown configurable Provider ${providerId}`);
|
|
1206
|
+
if (!entry.supported.includes(override.apiType)) throw new SorbetControlError(SorbetControlErrorCode.ProviderProtocolUnsupported, `Provider ${providerId} does not support protocol ${override.apiType}`);
|
|
1207
|
+
this.#models.setProvider(overriddenProvider(entry.native, override));
|
|
1208
|
+
entry.current = {
|
|
1209
|
+
apiType: override.apiType,
|
|
1210
|
+
baseUrl: override.baseUrl
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
disable(providerId) {
|
|
1214
|
+
const entry = this.#entries.get(providerId);
|
|
1215
|
+
if (entry === void 0) return;
|
|
1216
|
+
this.#models.deleteProvider(providerId);
|
|
1217
|
+
entry.current = null;
|
|
1218
|
+
}
|
|
1219
|
+
#record(provider, protocolHint) {
|
|
1220
|
+
const nativeConfiguration = nativeConfigurationFor(provider, protocolHint);
|
|
1221
|
+
if (nativeConfiguration === void 0) {
|
|
1222
|
+
this.#entries.delete(provider.id);
|
|
1223
|
+
return;
|
|
1224
|
+
}
|
|
1225
|
+
this.#entries.set(provider.id, {
|
|
1226
|
+
native: provider,
|
|
1227
|
+
supported: [nativeConfiguration.apiType],
|
|
1228
|
+
current: nativeConfiguration
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
function nativeConfigurationFor(provider, protocolHint) {
|
|
1233
|
+
let models;
|
|
1234
|
+
try {
|
|
1235
|
+
models = provider.getModels();
|
|
1236
|
+
} catch {
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
const protocols = new Set((protocolHint === void 0 ? models.map((model) => model.api) : [protocolHint]).map(protocolForPiApi));
|
|
1240
|
+
if (protocols.size !== 1) return;
|
|
1241
|
+
const baseUrls = new Set(provider.baseUrl === void 0 ? models.map((model) => model.baseUrl) : [provider.baseUrl]);
|
|
1242
|
+
if (baseUrls.size !== 1) return;
|
|
1243
|
+
const apiType = protocols.values().next().value;
|
|
1244
|
+
const baseUrl = baseUrls.values().next().value;
|
|
1245
|
+
if (apiType === void 0 || baseUrl === void 0 || baseUrl.length === 0) return;
|
|
1246
|
+
return {
|
|
1247
|
+
apiType,
|
|
1248
|
+
baseUrl
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
function protocolForPiApi(api) {
|
|
1252
|
+
return AcpProtocolByPiApi[api] ?? `_pi/${api}`;
|
|
1253
|
+
}
|
|
1254
|
+
function overriddenProvider(native, override) {
|
|
1255
|
+
const headers = { ...override.headers ?? {} };
|
|
1256
|
+
const auth = overrideAuth(native, override.baseUrl, headers);
|
|
1257
|
+
const filterModels = native.filterModels;
|
|
1258
|
+
const fetchDeferred = native.fetchDeferred;
|
|
1259
|
+
const cancelDeferred = native.cancelDeferred;
|
|
1260
|
+
const models = () => native.getModels().map(({ headers: _nativeHeaders,...model }) => ({
|
|
1261
|
+
...model,
|
|
1262
|
+
baseUrl: override.baseUrl
|
|
1263
|
+
}));
|
|
1264
|
+
return {
|
|
1265
|
+
id: native.id,
|
|
1266
|
+
name: native.name,
|
|
1267
|
+
baseUrl: override.baseUrl,
|
|
1268
|
+
auth,
|
|
1269
|
+
getModels: models,
|
|
1270
|
+
...filterModels === void 0 ? {} : { filterModels },
|
|
1271
|
+
stream(model, context, options) {
|
|
1272
|
+
const requestOptions = Object.assign({}, options);
|
|
1273
|
+
delete requestOptions.apiKey;
|
|
1274
|
+
requestOptions.headers = { ...headers };
|
|
1275
|
+
return native.stream({
|
|
1276
|
+
...model,
|
|
1277
|
+
baseUrl: override.baseUrl,
|
|
1278
|
+
headers: void 0
|
|
1279
|
+
}, context, requestOptions);
|
|
1280
|
+
},
|
|
1281
|
+
streamSimple: (model, context, options) => {
|
|
1282
|
+
const requestOptions = { ...options };
|
|
1283
|
+
delete requestOptions.apiKey;
|
|
1284
|
+
const userAgent = Object.entries(options?.headers ?? {}).find(([name]) => name.toLowerCase() === "user-agent");
|
|
1285
|
+
const hasConfiguredUserAgent = Object.keys(headers).some((name) => name.toLowerCase() === "user-agent");
|
|
1286
|
+
requestOptions.headers = { ...headers };
|
|
1287
|
+
if (userAgent !== void 0 && !hasConfiguredUserAgent) requestOptions.headers[userAgent[0]] = userAgent[1];
|
|
1288
|
+
return native.streamSimple({
|
|
1289
|
+
...model,
|
|
1290
|
+
baseUrl: override.baseUrl,
|
|
1291
|
+
headers: void 0
|
|
1292
|
+
}, context, requestOptions);
|
|
1293
|
+
},
|
|
1294
|
+
...fetchDeferred === void 0 ? {} : { fetchDeferred: (model, handle, options) => {
|
|
1295
|
+
const requestOptions = { ...options };
|
|
1296
|
+
delete requestOptions.apiKey;
|
|
1297
|
+
requestOptions.headers = { ...headers };
|
|
1298
|
+
return fetchDeferred({
|
|
1299
|
+
...model,
|
|
1300
|
+
baseUrl: override.baseUrl,
|
|
1301
|
+
headers: void 0
|
|
1302
|
+
}, handle, requestOptions);
|
|
1303
|
+
} },
|
|
1304
|
+
...cancelDeferred === void 0 ? {} : { cancelDeferred: async (model, handle, options) => {
|
|
1305
|
+
const requestOptions = { ...options };
|
|
1306
|
+
delete requestOptions.apiKey;
|
|
1307
|
+
requestOptions.headers = { ...headers };
|
|
1308
|
+
await cancelDeferred({
|
|
1309
|
+
...model,
|
|
1310
|
+
baseUrl: override.baseUrl,
|
|
1311
|
+
headers: void 0
|
|
1312
|
+
}, handle, requestOptions);
|
|
1313
|
+
} }
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
function overrideAuth(native, baseUrl, headers) {
|
|
1317
|
+
const apiKey = {
|
|
1318
|
+
name: `${native.name} ACP endpoint`,
|
|
1319
|
+
check: async () => ({
|
|
1320
|
+
source: "ACP endpoint",
|
|
1321
|
+
type: "api_key"
|
|
1322
|
+
}),
|
|
1323
|
+
resolve: async () => ({
|
|
1324
|
+
auth: {
|
|
1325
|
+
baseUrl,
|
|
1326
|
+
headers: { ...headers }
|
|
1327
|
+
},
|
|
1328
|
+
source: "ACP endpoint"
|
|
1329
|
+
})
|
|
1330
|
+
};
|
|
1331
|
+
const nativeOAuth = native.auth.oauth;
|
|
1332
|
+
if (nativeOAuth === void 0) return { apiKey };
|
|
1333
|
+
const oauth = {
|
|
1334
|
+
name: nativeOAuth.name,
|
|
1335
|
+
isSubscription: nativeOAuth.isSubscription,
|
|
1336
|
+
loginLabel: nativeOAuth.loginLabel,
|
|
1337
|
+
login: nativeOAuth.login,
|
|
1338
|
+
refresh: async (credential) => credential,
|
|
1339
|
+
toAuth: async () => ({
|
|
1340
|
+
baseUrl,
|
|
1341
|
+
headers: { ...headers }
|
|
1342
|
+
})
|
|
1343
|
+
};
|
|
1344
|
+
Reflect.deleteProperty(oauth, "login");
|
|
1345
|
+
return {
|
|
1346
|
+
apiKey,
|
|
1347
|
+
oauth
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
//#endregion
|
|
1352
|
+
//#region src/runtime.ts
|
|
1353
|
+
/**
|
|
1354
|
+
* Composes the Provider runtime for one Sorbet data directory: credential,
|
|
1355
|
+
* custom-Provider, endpoint and models.dev stores plus the mutable Pi Models
|
|
1356
|
+
* catalog. Both `@sorbetai/control` and `@sorbetai/node-agent` build their
|
|
1357
|
+
* Provider stack through this function, so there is one implementation.
|
|
1358
|
+
*/
|
|
1359
|
+
async function createSorbetProviderRuntime(options) {
|
|
1360
|
+
const dataDirectory = options.dataDirectory;
|
|
1361
|
+
const credentials = options.credentialStore ?? new JsonCredentialStore(join(dataDirectory, "credentials.json"));
|
|
1362
|
+
await credentials.list();
|
|
1363
|
+
const customProviderStore = options.customProviderStore ?? new JsonCustomProviderStore(join(dataDirectory, "providers.json"));
|
|
1364
|
+
const providerEndpoints = options.providerEndpointStore ?? new JsonProviderEndpointStore(join(dataDirectory, "provider-endpoints.json"));
|
|
1365
|
+
await providerEndpoints.list();
|
|
1366
|
+
const metadataCatalog = await ModelsDevCatalog.open(join(dataDirectory, "models-dev.json"));
|
|
1367
|
+
const mutableModels = await (options.createModels?.({ credentials }) ?? builtinModels({ credentials }));
|
|
1368
|
+
const directory = new ProviderDirectory(mutableModels);
|
|
1369
|
+
for (const definition of await customProviderStore.list()) {
|
|
1370
|
+
if (mutableModels.getProvider(definition.id) !== void 0) throw new SorbetControlError(SorbetControlErrorCode.CustomProviderConflict, `Custom Provider ${definition.id} conflicts with an existing Provider`);
|
|
1371
|
+
directory.add(customProviderFromDefinition(definition, metadataCatalog), definition.protocol);
|
|
1372
|
+
}
|
|
1373
|
+
const models = deviceBoundModels(mutableModels, dataDirectory);
|
|
1374
|
+
const catalog = new NodeProviderCatalog({
|
|
1375
|
+
metadataCatalog,
|
|
1376
|
+
credentials,
|
|
1377
|
+
models,
|
|
1378
|
+
directory,
|
|
1379
|
+
customProviders: customProviderStore,
|
|
1380
|
+
providerEndpoints,
|
|
1381
|
+
references: options.references ?? { listReferencedProviderIds: async () => [] },
|
|
1382
|
+
createId: options.createId ?? randomUUID
|
|
1383
|
+
});
|
|
1384
|
+
await catalog.applyPersistedEndpoints();
|
|
1385
|
+
return {
|
|
1386
|
+
credentials,
|
|
1387
|
+
models,
|
|
1388
|
+
catalog,
|
|
1389
|
+
login: async (providerId, type, interaction, operationOptions) => {
|
|
1390
|
+
return models.login(providerId, type, {
|
|
1391
|
+
...interaction,
|
|
1392
|
+
signal: operationOptions?.signal ?? interaction.signal
|
|
1393
|
+
});
|
|
1394
|
+
},
|
|
1395
|
+
refresh: (refreshOptions) => catalog.refresh(refreshOptions)
|
|
1396
|
+
};
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* OAuth login binds the persisted installation identity so refreshed
|
|
1400
|
+
* credentials stay attributed to this installation across processes. The bound
|
|
1401
|
+
* surface also hides MutableModels setters: Provider membership changes go
|
|
1402
|
+
* through the catalog so persisted definitions stay authoritative.
|
|
1403
|
+
*/
|
|
1404
|
+
function deviceBoundModels(models, dataDirectory) {
|
|
1405
|
+
const login = async (providerId, type, interaction, loginOptions) => {
|
|
1406
|
+
if (type !== "oauth") return models.login(providerId, type, interaction, loginOptions);
|
|
1407
|
+
const id = await installationId(dataDirectory);
|
|
1408
|
+
return models.login(providerId, type, interaction, {
|
|
1409
|
+
...loginOptions,
|
|
1410
|
+
getDeviceId: () => id
|
|
1411
|
+
});
|
|
1412
|
+
};
|
|
1413
|
+
return Object.freeze({
|
|
1414
|
+
getProviders: models.getProviders.bind(models),
|
|
1415
|
+
getProvider: models.getProvider.bind(models),
|
|
1416
|
+
getModels: models.getModels.bind(models),
|
|
1417
|
+
getModel: models.getModel.bind(models),
|
|
1418
|
+
getModelsOfType: models.getModelsOfType.bind(models),
|
|
1419
|
+
getModelOfType: models.getModelOfType.bind(models),
|
|
1420
|
+
getAllModels: models.getAllModels.bind(models),
|
|
1421
|
+
refresh: models.refresh.bind(models),
|
|
1422
|
+
checkAuth: models.checkAuth.bind(models),
|
|
1423
|
+
getAvailable: models.getAvailable.bind(models),
|
|
1424
|
+
getAvailableOfType: models.getAvailableOfType.bind(models),
|
|
1425
|
+
getAllAvailable: models.getAllAvailable.bind(models),
|
|
1426
|
+
getAuth: models.getAuth.bind(models),
|
|
1427
|
+
login,
|
|
1428
|
+
logout: models.logout.bind(models),
|
|
1429
|
+
stream: models.stream.bind(models),
|
|
1430
|
+
complete: models.complete.bind(models),
|
|
1431
|
+
streamSimple: models.streamSimple.bind(models),
|
|
1432
|
+
completeSimple: models.completeSimple.bind(models),
|
|
1433
|
+
streamDeferred: models.streamDeferred.bind(models),
|
|
1434
|
+
fetchDeferred: models.fetchDeferred.bind(models),
|
|
1435
|
+
cancelDeferred: models.cancelDeferred.bind(models),
|
|
1436
|
+
generateImages: models.generateImages.bind(models),
|
|
1437
|
+
classify: models.classify.bind(models)
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
//#endregion
|
|
1442
|
+
//#region src/control.ts
|
|
1443
|
+
const DEFAULT_DATA_DIRECTORY = join(homedir(), ".sorbet");
|
|
1444
|
+
var DefaultSorbetControl = class {
|
|
1445
|
+
dataDirectory;
|
|
1446
|
+
compatibility = SORBET_CONTROL_COMPATIBILITY;
|
|
1447
|
+
#runtime;
|
|
1448
|
+
#operations = /* @__PURE__ */ new Set();
|
|
1449
|
+
#closing;
|
|
1450
|
+
constructor(dataDirectory, runtime) {
|
|
1451
|
+
this.dataDirectory = dataDirectory;
|
|
1452
|
+
this.#runtime = runtime;
|
|
1453
|
+
}
|
|
1454
|
+
get authentication() {
|
|
1455
|
+
return {
|
|
1456
|
+
methods: (operationOptions) => this.#operate(async (signal) => {
|
|
1457
|
+
signal?.throwIfAborted();
|
|
1458
|
+
return this.#runtime.models.getProviders().flatMap((provider) => [...provider.auth.apiKey?.login === void 0 ? [] : [{
|
|
1459
|
+
providerId: provider.id,
|
|
1460
|
+
providerName: provider.name,
|
|
1461
|
+
type: "api_key",
|
|
1462
|
+
name: provider.auth.apiKey.name
|
|
1463
|
+
}], ...provider.auth.oauth?.login === void 0 ? [] : [{
|
|
1464
|
+
providerId: provider.id,
|
|
1465
|
+
providerName: provider.name,
|
|
1466
|
+
type: "oauth",
|
|
1467
|
+
name: provider.auth.oauth.name
|
|
1468
|
+
}]]);
|
|
1469
|
+
}, operationOptions),
|
|
1470
|
+
login: (input) => this.#operate(async (signal) => {
|
|
1471
|
+
await this.#runtime.login(input.providerId, input.type, {
|
|
1472
|
+
...input.interaction,
|
|
1473
|
+
signal: signal ?? input.interaction.signal
|
|
1474
|
+
}, { signal });
|
|
1475
|
+
}, { signal: input.signal })
|
|
1476
|
+
};
|
|
1477
|
+
}
|
|
1478
|
+
get credentials() {
|
|
1479
|
+
return {
|
|
1480
|
+
list: (operationOptions) => this.#operate((signal) => this.#runtime.credentials.list({ signal }), operationOptions),
|
|
1481
|
+
logout: (providerId, operationOptions) => this.#operate((signal) => this.#runtime.credentials.delete(providerId, { signal }), operationOptions)
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
get providers() {
|
|
1485
|
+
return {
|
|
1486
|
+
listCustom: (options) => this.#operate((signal) => this.#runtime.catalog.listCustom({ signal }), options),
|
|
1487
|
+
createCustom: (input, options) => this.#operate((signal) => this.#runtime.catalog.createCustom(input, { signal }), options),
|
|
1488
|
+
updateCustom: (providerId, input, options) => this.#operate((signal) => this.#runtime.catalog.updateCustom(providerId, input, { signal }), options),
|
|
1489
|
+
deleteCustom: (providerId, options) => this.#operate((signal) => this.#runtime.catalog.deleteCustom(providerId, { signal }), options),
|
|
1490
|
+
refresh: (refreshOptions) => this.#operate(() => this.#runtime.catalog.refresh(refreshOptions), { signal: refreshOptions?.signal }),
|
|
1491
|
+
availableModels: (options) => this.#operate(() => this.#runtime.catalog.availableModels(options?.providerId), options),
|
|
1492
|
+
listConfigurable: (options) => this.#operate(async () => this.#runtime.catalog.listConfigurable(), options),
|
|
1493
|
+
setEndpoint: (providerId, override, options) => this.#operate(() => this.#runtime.catalog.setEndpoint(providerId, override), options),
|
|
1494
|
+
disableEndpoint: (providerId, options) => this.#operate(() => this.#runtime.catalog.disableEndpoint(providerId), options)
|
|
1495
|
+
};
|
|
1496
|
+
}
|
|
1497
|
+
installationId() {
|
|
1498
|
+
return this.#operate(() => installationId(this.dataDirectory));
|
|
1499
|
+
}
|
|
1500
|
+
close() {
|
|
1501
|
+
this.#closing ??= Promise.allSettled([...this.#operations]).then(() => void 0);
|
|
1502
|
+
return this.#closing;
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1505
|
+
* Admits one operation. Once admitted, the operation runs to its real
|
|
1506
|
+
* outcome even if `close()` is called — close only stops new admissions and
|
|
1507
|
+
* drains admitted work. Per-operation failures stay on that operation's
|
|
1508
|
+
* promise; `close()` never rethrows them.
|
|
1509
|
+
*/
|
|
1510
|
+
#operate(operation, operationOptions) {
|
|
1511
|
+
if (this.#closing !== void 0) return Promise.reject(new SorbetControlError(SorbetControlErrorCode.ControlClosed, "Sorbet control handle is closed"));
|
|
1512
|
+
const signal = operationOptions?.signal;
|
|
1513
|
+
let result;
|
|
1514
|
+
try {
|
|
1515
|
+
result = operation(signal);
|
|
1516
|
+
} catch (cause) {
|
|
1517
|
+
result = Promise.reject(cause);
|
|
1518
|
+
}
|
|
1519
|
+
this.#operations.add(result);
|
|
1520
|
+
result.then(() => this.#operations.delete(result), () => this.#operations.delete(result));
|
|
1521
|
+
return result;
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
/**
|
|
1525
|
+
* Opens the local control plane for one Sorbet data directory.
|
|
1526
|
+
*
|
|
1527
|
+
* Only control-plane resources are initialized: Provider stores, the models.dev
|
|
1528
|
+
* catalog, the mutable Provider registry, and the installation identity. No
|
|
1529
|
+
* Sessions, tools, plugins, MCP connections, sandboxing, or the durable
|
|
1530
|
+
* executor are started.
|
|
1531
|
+
*
|
|
1532
|
+
* The opening signal only cancels initialization. Once a control operation is
|
|
1533
|
+
* admitted, it settles to its real outcome rather than reporting an abort that
|
|
1534
|
+
* could imply an atomic write did not occur.
|
|
1535
|
+
*/
|
|
1536
|
+
function openSorbetControl(options = {}) {
|
|
1537
|
+
return openComposedSorbetControl(options);
|
|
1538
|
+
}
|
|
1539
|
+
/** Composition entry for Sorbet hosts that supply Provider stores or a Pi registry. */
|
|
1540
|
+
async function openComposedSorbetControl(options) {
|
|
1541
|
+
options.signal?.throwIfAborted();
|
|
1542
|
+
const dataDirectory = resolve(options.dataDirectory ?? process.env.SORBET_DATA_DIR ?? DEFAULT_DATA_DIRECTORY);
|
|
1543
|
+
const authority = options.references ?? createLocalProviderReferences(dataDirectory);
|
|
1544
|
+
const runtime = await createSorbetProviderRuntime({
|
|
1545
|
+
...options,
|
|
1546
|
+
dataDirectory,
|
|
1547
|
+
references: authority
|
|
1548
|
+
});
|
|
1549
|
+
options.signal?.throwIfAborted();
|
|
1550
|
+
return new DefaultSorbetControl(dataDirectory, runtime);
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
//#endregion
|
|
1554
|
+
export { JsonCredentialStore as C, SorbetControlErrorCode as D, SorbetControlError as E, SORBET_CONTROL_COMPATIBILITY as O, UpdateCustomProviderInputSchema as S, ProviderReviewerSelectionSchema as T, CustomProviderProtocolSchema as _, NodeProviderCatalog as a, ProviderEndpointOverrideSchema as b, JsonCustomProviderStore as c, findModelAcrossCatalogs as d, ConfigurableProviderSchema as f, CustomProviderModelSchema as g, CustomProviderModelProperties as h, ProviderDirectory as i, catalogModelIndex as l, CustomProviderDefinitionSchema as m, openSorbetControl as n, encodeModelSelector as o, CreateCustomProviderInputSchema as p, createSorbetProviderRuntime as r, JsonProviderEndpointStore as s, openComposedSorbetControl as t, findCatalogModel as u, ProviderEndpointConfigurationProperties as v, createLocalProviderReferences as w, ProviderProtocolSchema as x, ProviderEndpointConfigurationSchema as y };
|
|
1555
|
+
//# sourceMappingURL=control-sqUS607F.js.map
|