2026-09-11 13:58:29 +08:00
|
|
|
const name = "dsh-plugin-model-enhancer";
|
|
|
|
|
const inject = ["webServer"];
|
2026-09-09 00:52:52 +08:00
|
|
|
let cachedData = null;
|
|
|
|
|
let lastFetchedAt = 0;
|
2026-09-11 13:58:29 +08:00
|
|
|
const CACHE_TTL_MS = 3600 * 1e3;
|
2026-09-09 00:52:52 +08:00
|
|
|
async function getModelsDevData() {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
if (cachedData && now - lastFetchedAt < CACHE_TTL_MS) {
|
|
|
|
|
return cachedData;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2026-09-11 13:58:29 +08:00
|
|
|
const res = await fetch("https://models.dev/api.json", {
|
2026-09-09 00:52:52 +08:00
|
|
|
headers: {
|
2026-09-11 13:58:29 +08:00
|
|
|
"User-Agent": "DSH-Model-Enhancer/1.0.0"
|
2026-09-09 00:52:52 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(`Failed to fetch models.dev: HTTP ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
const json = await res.json();
|
|
|
|
|
cachedData = json;
|
|
|
|
|
lastFetchedAt = now;
|
|
|
|
|
return json;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (cachedData) {
|
|
|
|
|
return cachedData;
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-11 13:58:29 +08:00
|
|
|
function apply(ctx) {
|
2026-09-09 00:52:52 +08:00
|
|
|
ctx.effect(() => {
|
|
|
|
|
return ctx.webServer.register({
|
2026-09-11 13:58:29 +08:00
|
|
|
kind: "exact",
|
|
|
|
|
path: "/api/models-dev/models",
|
2026-09-09 00:52:52 +08:00
|
|
|
handler: async (req, res) => {
|
2026-09-11 13:58:29 +08:00
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
|
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
|
|
|
if (req.method === "OPTIONS") {
|
2026-09-09 00:52:52 +08:00
|
|
|
res.writeHead(204);
|
|
|
|
|
res.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-11 13:58:29 +08:00
|
|
|
if (req.method !== "GET") {
|
|
|
|
|
res.writeHead(405, { "Content-Type": "application/json" });
|
|
|
|
|
res.end(JSON.stringify({ error: "Method Not Allowed" }));
|
2026-09-09 00:52:52 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const data = await getModelsDevData();
|
|
|
|
|
res.writeHead(200, {
|
2026-09-11 13:58:29 +08:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
"Cache-Control": "public, max-age=3600"
|
2026-09-09 00:52:52 +08:00
|
|
|
});
|
|
|
|
|
res.end(JSON.stringify(data));
|
|
|
|
|
} catch (err) {
|
2026-09-11 13:58:29 +08:00
|
|
|
ctx.logger?.error?.("[model-enhancer] Error fetching models.dev:", err);
|
|
|
|
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
|
|
|
res.end(JSON.stringify({ error: err.message || "Internal Server Error" }));
|
2026-09-09 00:52:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-11 13:58:29 +08:00
|
|
|
}, "dsh-plugin-model-enhancer: webServer proxy route");
|
2026-09-09 00:52:52 +08:00
|
|
|
}
|
2026-09-11 13:58:29 +08:00
|
|
|
export {
|
|
|
|
|
apply,
|
|
|
|
|
getModelsDevData,
|
|
|
|
|
inject,
|
|
|
|
|
name
|
|
|
|
|
};
|