forked from anomalyco/models.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
106 lines (97 loc) · 3.08 KB
/
Copy pathworker.ts
File metadata and controls
106 lines (97 loc) · 3.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
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
export interface Env {
ASSETS: any;
PosthogToken: string;
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
const url = new URL(request.url);
const ip = request.headers.get("cf-connecting-ip") || "unknown";
const country = request.headers.get("cf-ipcountry") || "unknown";
const agent = request.headers.get("user-agent") || "unknown";
if (agent.includes("opencode") || agent.includes("bun")) {
ctx.waitUntil(
fetch("https://us.i.posthog.com/i/v0/e/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
api_key: JSON.parse(env.PosthogToken).value,
event: "hit",
distinct_id: ip,
properties: {
$process_person_profile: false,
user_agent: agent,
country,
path: url.pathname,
},
}),
}),
);
}
if (url.pathname === "/model-schema.json") {
const apiUrl = new URL(url);
apiUrl.pathname = "/_api.json";
const apiResponse = await env.ASSETS.fetch(
new Request(apiUrl.toString(), request),
);
const providers = (await apiResponse.json()) as Record<
string,
{ models: Record<string, unknown> }
>;
const modelIds: string[] = [];
for (const [providerId, provider] of Object.entries(providers)) {
for (const modelId of Object.keys(provider.models)) {
modelIds.push(`${providerId}/${modelId}`);
}
}
const schema = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "https://models.dev/model-schema.json",
$defs: {
Model: {
type: "string",
enum: modelIds.sort(),
description: "AI model identifier in provider/model format",
},
},
};
return new Response(JSON.stringify(schema, null, 2), {
headers: {
"Content-Type": "application/json",
"Cache-Control": "public, max-age=3600",
},
});
}
if (url.pathname === "/api.json") {
url.pathname = "/_api.json";
} else if (
url.pathname === "/" ||
url.pathname === "/index.html" ||
url.pathname === "/index"
) {
url.pathname = "/_index";
} else if (url.pathname.startsWith("/logos/")) {
// Check if the specific provider logo exists in static assets
const logoResponse = await env.ASSETS.fetch(new Request(url.toString(), request));
if (logoResponse.status === 404) {
// Fallback to default logo
const defaultUrl = new URL(url);
defaultUrl.pathname = "/logos/default.svg";
return await env.ASSETS.fetch(new Request(defaultUrl.toString(), request));
}
return logoResponse;
} else {
// redirect to "/"
return new Response(null, {
status: 302,
headers: { Location: "/" },
});
}
return await env.ASSETS.fetch(new Request(url.toString(), request));
},
};