File size: 3,458 Bytes
612f16b
7bf1507
612f16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bf1507
612f16b
 
 
 
 
 
 
7bf1507
 
612f16b
 
 
 
 
 
 
 
 
 
 
7bf1507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612f16b
7bf1507
 
 
 
 
 
 
612f16b
 
 
 
 
 
 
 
7bf1507
 
 
 
 
 
 
 
 
612f16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Elysia } from "elysia";
import type { BackendModel } from "$lib/server/models";
import { authPlugin } from "../../authPlugin";
import { authCondition } from "$lib/server/auth";
import { collections } from "$lib/server/database";

export type GETModelsResponse = Array<{
	id: string;
	name: string;
	websiteUrl?: string;
	modelUrl?: string;
	datasetName?: string;
	datasetUrl?: string;
	displayName: string;
	description?: string;
	reasoning: boolean;
	logoUrl?: string;
	providers?: Array<{ provider: string } & Record<string, unknown>>;
	promptExamples?: { title: string; prompt: string }[];
	parameters: BackendModel["parameters"];
	preprompt?: string;
	multimodal: boolean;
	multimodalAcceptedMimetypes?: string[];
	unlisted: boolean;
	hasInferenceAPI: boolean;
	// Mark router entry for UI decoration — always present
	isRouter: boolean;
}>;

export type GETOldModelsResponse = Array<{
	id: string;
	name: string;
	displayName: string;
	transferTo?: string;
}>;

export const modelGroup = new Elysia().group("/models", (app) =>
	app
		.get("/", async () => {
			try {
				const { models } = await import("$lib/server/models");
				return models
					.filter((m) => m.unlisted == false)
					.map((model) => ({
						id: model.id,
						name: model.name,
						websiteUrl: model.websiteUrl,
						modelUrl: model.modelUrl,
						datasetName: model.datasetName,
						datasetUrl: model.datasetUrl,
						displayName: model.displayName,
						description: model.description,
						reasoning: !!model.reasoning,
						logoUrl: model.logoUrl,
						providers: model.providers as unknown as Array<
							{ provider: string } & Record<string, unknown>
						>,
						promptExamples: model.promptExamples,
						parameters: model.parameters,
						preprompt: model.preprompt,
						multimodal: model.multimodal,
						multimodalAcceptedMimetypes: model.multimodalAcceptedMimetypes,
						unlisted: model.unlisted,
						hasInferenceAPI: model.hasInferenceAPI,
						isRouter: model.isRouter,
					})) satisfies GETModelsResponse;
			} catch (e) {
				// Return empty list instead of crashing the whole page
				return [] as GETModelsResponse;
			}
		})
		.get("/old", async () => {
			try {
				const { oldModels } = await import("$lib/server/models");
				return oldModels satisfies GETOldModelsResponse;
			} catch (e) {
				return [] as GETOldModelsResponse;
			}
		})
		.group("/:namespace/:model?", (app) =>
			app
				.derive(async ({ params, error }) => {
					let modelId: string = params.namespace;
					if (params.model) {
						modelId += "/" + params.model;
					}
					try {
						const { models } = await import("$lib/server/models");
						const model = models.find((m) => m.id === modelId);
						if (!model || model.unlisted) {
							return error(404, "Model not found");
						}
						return { model };
					} catch (e) {
						return error(500, "Models not available");
					}
				})
				.get("/", ({ model }) => {
					return model;
				})
				.use(authPlugin)
				.post("/subscribe", async ({ locals, model, error }) => {
					if (!locals.sessionId) {
						return error(401, "Unauthorized");
					}
					await collections.settings.updateOne(
						authCondition(locals),
						{
							$set: {
								activeModel: model.id,
								updatedAt: new Date(),
							},
							$setOnInsert: {
								createdAt: new Date(),
							},
						},
						{
							upsert: true,
						}
					);

					return new Response();
				})
		)
);