forked from continuedev/continue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenRouterModel.ts
More file actions
77 lines (63 loc) · 2.08 KB
/
openRouterModel.ts
File metadata and controls
77 lines (63 loc) · 2.08 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
import { ModelProviderTags } from "../../../components/modelSelection/utils";
import { ModelPackage } from "./models";
interface OpenRouterModel {
id: string;
name: string;
description: string;
context_length: number;
hugging_face_id: string;
}
function convertOpenRouterModelToPackage(model: OpenRouterModel): ModelPackage {
return {
title: model.name,
description: model.description,
refUrl: `https://openrouter.ai/models/${model.id}`,
params: {
title: model.name,
model: model.id,
contextLength: model.context_length,
},
isOpenSource: !!model.hugging_face_id,
tags: [ModelProviderTags.RequiresApiKey],
providerOptions: ["openrouter"],
icon: "openrouter.png",
};
}
async function fetchOpenRouterModelsFromAPI(): Promise<OpenRouterModel[]> {
const OPENROUTER_API_URL = "https://openrouter.ai/api/v1/models";
try {
const response = await fetch(OPENROUTER_API_URL);
if (!response.ok) {
throw new Error(
`Failed to fetch OpenRouter models: ${response.status} ${response.statusText}`,
);
}
const data = await response.json();
if (!data.data || !Array.isArray(data.data)) {
console.warn("Invalid OpenRouter models data structure from API");
return [];
}
return data.data;
} catch (error) {
console.error("Error fetching OpenRouter models from API:", error);
return [];
}
}
export async function getOpenRouterModelsList(): Promise<ModelPackage[]> {
const models: { [key: string]: ModelPackage } = {};
const apiModels = await fetchOpenRouterModelsFromAPI();
apiModels.forEach((model: OpenRouterModel) => {
if (!model.id || !model.name) {
console.warn("Skipping model with missing id or name", model);
return;
}
// Create a unique key from the model id (replace slashes and dots with underscores)
const key = model.id.replace(/[\/.]/g, "_");
try {
models[key] = convertOpenRouterModelToPackage(model);
} catch (error) {
console.error(`Failed to convert model ${model.id}:`, error);
}
});
return Object.values(models);
}