-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider-plugins.ts
More file actions
374 lines (332 loc) · 12.1 KB
/
Copy pathprovider-plugins.ts
File metadata and controls
374 lines (332 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import path from "path";
import type {
BackendCapabilities,
ModelCapabilities,
ModelOption,
} from "../../../lib/types";
export type ProviderStreamFormat = "ndjson" | "sse-standard" | "openai";
export type ProviderModelsSource = "remote" | "static";
export type ProviderPlugin = {
id: string;
name: string;
baseUrl: string;
modelsPath: string;
chatPath: string;
modelsSource: ProviderModelsSource;
staticModels: ModelOption[];
headers: Record<string, string>;
streamFormat: ProviderStreamFormat;
capabilities: BackendCapabilities;
modelCapabilities: ModelCapabilities;
createdAt: number;
updatedAt: number;
};
const PROVIDER_PLUGINS_ROOT = path.join(process.cwd(), ".standard-ui");
const PROVIDER_PLUGINS_FILE = path.join(PROVIDER_PLUGINS_ROOT, "provider-plugins.json");
const RESERVED_PROVIDER_IDS = new Set(["ollama", "openai", "anthropic"]);
const DEFAULT_PLUGIN_CAPABILITIES: BackendCapabilities = {
systemPrompt: true,
temperature: true,
topP: true,
topK: false,
maxTokens: true,
stopSequences: true,
seed: false,
jsonMode: false,
contextWindow: false,
repeatPenalty: false,
keepAlive: false,
};
const DEFAULT_PLUGIN_MODEL_CAPABILITIES: ModelCapabilities = {
textInput: true,
imageInput: false,
documentInput: false,
audioInput: false,
videoInput: false,
binaryInput: false,
maxAttachments: 8,
maxAttachmentBytes: 20 * 1024 * 1024,
};
type ProviderPluginRecord = ProviderPlugin;
type ProviderPluginInput = {
id?: unknown;
name?: unknown;
baseUrl?: unknown;
modelsPath?: unknown;
chatPath?: unknown;
modelsSource?: unknown;
staticModels?: unknown;
headers?: unknown;
streamFormat?: unknown;
capabilities?: unknown;
modelCapabilities?: unknown;
createdAt?: unknown;
updatedAt?: unknown;
};
function toProviderId(input: string) {
return input
.trim()
.toLowerCase()
.replace(/[^a-z0-9_-]+/g, "-")
.replace(/-+/g, "-")
.replace(/^[-_]+|[-_]+$/g, "")
.slice(0, 48);
}
function normalizeBaseUrl(candidate: unknown) {
const raw = typeof candidate === "string" ? candidate.trim() : "";
if (!raw) {
throw new Error("Base URL is required.");
}
let parsed: URL;
try {
parsed = new URL(raw);
} catch {
throw new Error("Base URL must be a valid absolute URL.");
}
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
throw new Error("Base URL must start with http:// or https://");
}
const normalizedPath = parsed.pathname.replace(/\/+$/, "");
return `${parsed.origin}${normalizedPath}` || parsed.origin;
}
function normalizeEndpointPath(candidate: unknown, fallback: string) {
const raw = typeof candidate === "string" ? candidate.trim() : "";
if (!raw) return fallback;
if (raw.startsWith("http://") || raw.startsWith("https://")) return raw;
return `/${raw.replace(/^\/+/, "")}`;
}
function readBoolean(candidate: unknown, fallback: boolean) {
return typeof candidate === "boolean" ? candidate : fallback;
}
function asRecord(candidate: unknown) {
if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) return null;
return candidate as Record<string, unknown>;
}
function normalizeBackendCapabilities(candidate: unknown): BackendCapabilities {
const record = asRecord(candidate);
return {
systemPrompt: readBoolean(record?.systemPrompt, DEFAULT_PLUGIN_CAPABILITIES.systemPrompt),
temperature: readBoolean(record?.temperature, DEFAULT_PLUGIN_CAPABILITIES.temperature),
topP: readBoolean(record?.topP, DEFAULT_PLUGIN_CAPABILITIES.topP),
topK: readBoolean(record?.topK, DEFAULT_PLUGIN_CAPABILITIES.topK),
maxTokens: readBoolean(record?.maxTokens, DEFAULT_PLUGIN_CAPABILITIES.maxTokens),
stopSequences: readBoolean(record?.stopSequences, DEFAULT_PLUGIN_CAPABILITIES.stopSequences),
seed: readBoolean(record?.seed, DEFAULT_PLUGIN_CAPABILITIES.seed),
jsonMode: readBoolean(record?.jsonMode, DEFAULT_PLUGIN_CAPABILITIES.jsonMode),
contextWindow: readBoolean(record?.contextWindow, DEFAULT_PLUGIN_CAPABILITIES.contextWindow),
repeatPenalty: readBoolean(record?.repeatPenalty, DEFAULT_PLUGIN_CAPABILITIES.repeatPenalty),
keepAlive: readBoolean(record?.keepAlive, DEFAULT_PLUGIN_CAPABILITIES.keepAlive),
};
}
function readPositiveInteger(candidate: unknown, fallback: number) {
if (typeof candidate !== "number" || !Number.isFinite(candidate)) return fallback;
return Math.max(1, Math.round(candidate));
}
function normalizeModelCapabilities(candidate: unknown): ModelCapabilities {
const record = asRecord(candidate);
return {
textInput: readBoolean(record?.textInput, DEFAULT_PLUGIN_MODEL_CAPABILITIES.textInput),
imageInput: readBoolean(record?.imageInput, DEFAULT_PLUGIN_MODEL_CAPABILITIES.imageInput),
documentInput: readBoolean(record?.documentInput, DEFAULT_PLUGIN_MODEL_CAPABILITIES.documentInput),
audioInput: readBoolean(record?.audioInput, DEFAULT_PLUGIN_MODEL_CAPABILITIES.audioInput),
videoInput: readBoolean(record?.videoInput, DEFAULT_PLUGIN_MODEL_CAPABILITIES.videoInput),
binaryInput: readBoolean(record?.binaryInput, DEFAULT_PLUGIN_MODEL_CAPABILITIES.binaryInput),
maxAttachments: readPositiveInteger(
record?.maxAttachments,
DEFAULT_PLUGIN_MODEL_CAPABILITIES.maxAttachments
),
maxAttachmentBytes: readPositiveInteger(
record?.maxAttachmentBytes,
DEFAULT_PLUGIN_MODEL_CAPABILITIES.maxAttachmentBytes
),
};
}
function normalizeHeaders(candidate: unknown) {
const record = asRecord(candidate);
if (!record) return {};
const headers: Record<string, string> = {};
for (const [key, value] of Object.entries(record)) {
if (typeof value !== "string") continue;
const normalizedKey = key.trim();
const normalizedValue = value.trim();
if (!normalizedKey || !normalizedValue) continue;
headers[normalizedKey] = normalizedValue;
}
return headers;
}
function normalizeStaticModels(candidate: unknown, fallbackCapabilities: ModelCapabilities) {
if (!Array.isArray(candidate)) return [];
const deduped = new Map<string, ModelOption>();
for (const entry of candidate) {
if (typeof entry === "string") {
const id = entry.trim();
if (!id) continue;
deduped.set(id, {
id,
capabilities: fallbackCapabilities,
});
continue;
}
const record = asRecord(entry);
if (!record) continue;
const id = typeof record.id === "string" ? record.id.trim() : "";
if (!id) continue;
const meta = typeof record.meta === "string" ? record.meta : undefined;
deduped.set(id, {
id,
meta,
capabilities: normalizeModelCapabilities(record.capabilities ?? fallbackCapabilities),
});
}
return Array.from(deduped.values());
}
function normalizeStreamFormat(candidate: unknown): ProviderStreamFormat {
if (candidate === "ndjson" || candidate === "sse-standard" || candidate === "openai") {
return candidate;
}
return "ndjson";
}
function normalizeModelsSource(candidate: unknown, staticModels: ModelOption[]): ProviderModelsSource {
if (candidate === "remote" || candidate === "static") {
return candidate;
}
return staticModels.length ? "static" : "remote";
}
function readPluginsFile() {
if (!existsSync(PROVIDER_PLUGINS_FILE)) {
return [] as ProviderPluginRecord[];
}
try {
const raw = readFileSync(PROVIDER_PLUGINS_FILE, "utf8");
const parsed = JSON.parse(raw) as { providers?: unknown };
if (!Array.isArray(parsed.providers)) {
return [] as ProviderPluginRecord[];
}
return parsed.providers
.map((entry) => {
const record = asRecord(entry);
if (!record) return null;
try {
return normalizePluginRecord(record, {
preserveTimestamps: true,
});
} catch {
return null;
}
})
.filter((plugin): plugin is ProviderPluginRecord => Boolean(plugin));
} catch {
return [] as ProviderPluginRecord[];
}
}
function writePluginsFile(providers: ProviderPluginRecord[]) {
mkdirSync(PROVIDER_PLUGINS_ROOT, { recursive: true });
writeFileSync(
PROVIDER_PLUGINS_FILE,
`${JSON.stringify({ version: 1, providers }, null, 2)}\n`,
"utf8"
);
}
function normalizePluginRecord(
input: ProviderPluginInput,
options?: {
existing?: ProviderPluginRecord | null;
preserveTimestamps?: boolean;
}
): ProviderPluginRecord {
const existing = options?.existing ?? null;
const rawName = typeof input.name === "string" ? input.name.trim() : "";
if (!rawName) {
throw new Error("Provider name is required.");
}
const requestedId =
typeof input.id === "string" && input.id.trim()
? input.id.trim()
: existing?.id || rawName;
const id = toProviderId(requestedId);
if (!id) {
throw new Error("Provider ID is invalid.");
}
if (RESERVED_PROVIDER_IDS.has(id)) {
throw new Error(`"${id}" is reserved and cannot be used as a custom provider ID.`);
}
const baseUrl = normalizeBaseUrl(input.baseUrl ?? existing?.baseUrl);
const modelCapabilities = normalizeModelCapabilities(
input.modelCapabilities ?? existing?.modelCapabilities
);
const staticModels = normalizeStaticModels(input.staticModels ?? existing?.staticModels, modelCapabilities);
const modelsSource = normalizeModelsSource(input.modelsSource ?? existing?.modelsSource, staticModels);
const now = Date.now();
return {
id,
name: rawName,
baseUrl,
modelsPath: normalizeEndpointPath(input.modelsPath ?? existing?.modelsPath, "/models"),
chatPath: normalizeEndpointPath(input.chatPath ?? existing?.chatPath, "/chat/stream"),
modelsSource,
staticModels,
headers: normalizeHeaders(input.headers ?? existing?.headers),
streamFormat: normalizeStreamFormat(input.streamFormat ?? existing?.streamFormat),
capabilities: normalizeBackendCapabilities(input.capabilities ?? existing?.capabilities),
modelCapabilities,
createdAt:
options?.preserveTimestamps && typeof input.createdAt === "number"
? input.createdAt
: existing?.createdAt ?? now,
updatedAt:
options?.preserveTimestamps && typeof input.updatedAt === "number"
? input.updatedAt
: now,
};
}
export function resolveProviderUrl(baseUrl: string, endpointPath: string) {
const trimmedPath = endpointPath.trim();
if (!trimmedPath) return baseUrl;
if (trimmedPath.startsWith("http://") || trimmedPath.startsWith("https://")) {
return trimmedPath;
}
const normalizedBase = baseUrl.replace(/\/+$/, "");
const normalizedPath = `/${trimmedPath.replace(/^\/+/, "")}`;
return `${normalizedBase}${normalizedPath}`;
}
export function listProviderPlugins() {
return readPluginsFile()
.slice()
.sort((left, right) => left.name.localeCompare(right.name));
}
export function getProviderPluginById(id: string) {
const normalizedId = toProviderId(id);
if (!normalizedId) return null;
return listProviderPlugins().find((plugin) => plugin.id === normalizedId) ?? null;
}
export function upsertProviderPlugin(input: ProviderPluginInput) {
const providers = readPluginsFile();
const normalizedId = toProviderId(typeof input.id === "string" ? input.id : "");
const existing =
providers.find((provider) => provider.id === normalizedId) ??
providers.find((provider) => provider.name === input.name) ??
null;
const normalized = normalizePluginRecord(input, { existing });
const duplicate = providers.find(
(provider) => provider.id === normalized.id && provider.id !== existing?.id
);
if (duplicate) {
throw new Error(`A provider with ID "${normalized.id}" already exists.`);
}
const nextProviders = providers.filter((provider) => provider.id !== normalized.id);
nextProviders.push(normalized);
writePluginsFile(nextProviders);
return normalized;
}
export function deleteProviderPlugin(id: string) {
const normalizedId = toProviderId(id);
if (!normalizedId) return false;
const providers = readPluginsFile();
const nextProviders = providers.filter((provider) => provider.id !== normalizedId);
if (nextProviders.length === providers.length) {
return false;
}
writePluginsFile(nextProviders);
return true;
}