isididiidid commited on
Commit
7f5fe1b
·
verified ·
1 Parent(s): 0de2466

Upload hf.js

Browse files
Files changed (1) hide show
  1. hf.js +327 -0
hf.js ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const morgan = require('morgan');
3
+ const { createProxyMiddleware } = require('http-proxy-middleware');
4
+ const axios = require('axios');
5
+ const fs = require('fs');
6
+ const app = express();
7
+
8
+ app.use(morgan('dev'));
9
+
10
+ app.get('/hf/v1/models', (req, res) => {
11
+ const models = {
12
+ "object": "list",
13
+ "data": [
14
+ {
15
+ "id": "claude-3.5-sonnet",
16
+ "object": "model",
17
+ "created": 1706745938,
18
+ "owned_by": "cursor"
19
+ },
20
+ {
21
+ "id": "gpt-4",
22
+ "object": "model",
23
+ "created": 1706745938,
24
+ "owned_by": "cursor"
25
+ },
26
+ {
27
+ "id": "gpt-4o",
28
+ "object": "model",
29
+ "created": 1706745938,
30
+ "owned_by": "cursor"
31
+ },
32
+ {
33
+ "id": "claude-3-opus",
34
+ "object": "model",
35
+ "created": 1706745938,
36
+ "owned_by": "cursor"
37
+ },
38
+ {
39
+ "id": "gpt-3.5-turbo",
40
+ "object": "model",
41
+ "created": 1706745938,
42
+ "owned_by": "cursor"
43
+ },
44
+ {
45
+ "id": "gpt-4-turbo-2024-04-09",
46
+ "object": "model",
47
+ "created": 1706745938,
48
+ "owned_by": "cursor"
49
+ },
50
+ {
51
+ "id": "gpt-4o-128k",
52
+ "object": "model",
53
+ "created": 1706745938,
54
+ "owned_by": "cursor"
55
+ },
56
+ {
57
+ "id": "gemini-1.5-flash-500k",
58
+ "object": "model",
59
+ "created": 1706745938,
60
+ "owned_by": "cursor"
61
+ },
62
+ {
63
+ "id": "claude-3-haiku-200k",
64
+ "object": "model",
65
+ "created": 1706745938,
66
+ "owned_by": "cursor"
67
+ },
68
+ {
69
+ "id": "claude-3-5-sonnet-200k",
70
+ "object": "model",
71
+ "created": 1706745938,
72
+ "owned_by": "cursor"
73
+ },
74
+ {
75
+ "id": "claude-3-5-sonnet-20241022",
76
+ "object": "model",
77
+ "created": 1706745938,
78
+ "owned_by": "cursor"
79
+ },
80
+ {
81
+ "id": "gpt-4o-mini",
82
+ "object": "model",
83
+ "created": 1706745938,
84
+ "owned_by": "cursor"
85
+ },
86
+ {
87
+ "id": "o1-mini",
88
+ "object": "model",
89
+ "created": 1706745938,
90
+ "owned_by": "cursor"
91
+ },
92
+ {
93
+ "id": "o1-preview",
94
+ "object": "model",
95
+ "created": 1706745938,
96
+ "owned_by": "cursor"
97
+ },
98
+ {
99
+ "id": "o1",
100
+ "object": "model",
101
+ "created": 1706745938,
102
+ "owned_by": "cursor"
103
+ },
104
+ {
105
+ "id": "claude-3.5-haiku",
106
+ "object": "model",
107
+ "created": 1706745938,
108
+ "owned_by": "cursor"
109
+ },
110
+ {
111
+ "id": "gemini-exp-1206",
112
+ "object": "model",
113
+ "created": 1706745938,
114
+ "owned_by": "cursor"
115
+ },
116
+ {
117
+ "id": "gemini-2.0-flash-thinking-exp",
118
+ "object": "model",
119
+ "created": 1706745938,
120
+ "owned_by": "cursor"
121
+ },
122
+ {
123
+ "id": "gemini-2.0-flash-exp",
124
+ "object": "model",
125
+ "created": 1706745938,
126
+ "owned_by": "cursor"
127
+ },
128
+ {
129
+ "id": "deepseek-v3",
130
+ "object": "model",
131
+ "created": 1706745938,
132
+ "owned_by": "cursor"
133
+ },
134
+ {
135
+ "id": "deepseek-r1",
136
+ "object": "model",
137
+ "created": 1706745938,
138
+ "owned_by": "cursor"
139
+ }
140
+ ]
141
+ };
142
+ res.json(models);
143
+ });
144
+
145
+ app.use('/hf/v1/chat/completions', createProxyMiddleware({
146
+ target: 'http://localhost:3010/v1/chat/completions',
147
+ changeOrigin: true,
148
+ onProxyReq: (proxyReq, req, res) => {
149
+ // 将请求数据写回代理请求
150
+ if (req.body) {
151
+ const bodyData = req.body;
152
+ proxyReq.write(bodyData);
153
+ }
154
+ }
155
+ }));
156
+
157
+ app.get('/', (req, res) => {
158
+ const htmlContent = `
159
+ <!DOCTYPE html>
160
+ <html lang="en">
161
+ <head>
162
+ <meta charset="UTF-8">
163
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
164
+ <title>Cursor To OpenAI</title>
165
+ <style>
166
+ :root {
167
+ --primary-color: #2563eb;
168
+ --bg-color: #f8fafc;
169
+ --card-bg: #ffffff;
170
+ }
171
+
172
+ body {
173
+ padding: 20px;
174
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
175
+ max-width: 1000px;
176
+ margin: 0 auto;
177
+ line-height: 1.6;
178
+ background: var(--bg-color);
179
+ color: #1a1a1a;
180
+ }
181
+ .container {
182
+ padding: 20px;
183
+ }
184
+ .header {
185
+ text-align: center;
186
+ margin-bottom: 40px;
187
+ }
188
+ .header h1 {
189
+ color: var(--primary-color);
190
+ font-size: 2.5em;
191
+ margin-bottom: 10px;
192
+ }
193
+ .info {
194
+ background: #fff;
195
+ padding: 25px;
196
+ border-radius: 12px;
197
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
198
+ margin-bottom: 30px;
199
+ border: 1px solid #e5e7eb;
200
+ }
201
+ .info-item {
202
+ margin: 15px 0;
203
+ padding: 10px;
204
+ background: #f8fafc;
205
+ border-radius: 8px;
206
+ border: 1px solid #e5e7eb;
207
+ }
208
+ .info-label {
209
+ color: #4b5563;
210
+ font-size: 0.9em;
211
+ margin-bottom: 5px;
212
+ }
213
+ .info-value {
214
+ color: var(--primary-color);
215
+ font-weight: 500;
216
+ }
217
+ .models {
218
+ background: var(--card-bg);
219
+ padding: 25px;
220
+ border-radius: 12px;
221
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
222
+ border: 1px solid #e5e7eb;
223
+ }
224
+ .models h3 {
225
+ color: #1a1a1a;
226
+ margin-bottom: 20px;
227
+ font-size: 1.5em;
228
+ border-bottom: 2px solid #e5e7eb;
229
+ padding-bottom: 10px;
230
+ }
231
+ .model-item {
232
+ margin: 12px 0;
233
+ padding: 15px;
234
+ background: #f8fafc;
235
+ border-radius: 8px;
236
+ border: 1px solid #e5e7eb;
237
+ transition: all 0.3s ease;
238
+ display: flex;
239
+ justify-content: space-between;
240
+ align-items: center;
241
+ }
242
+ .model-item:hover {
243
+ transform: translateY(-2px);
244
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
245
+ }
246
+ .model-name {
247
+ font-weight: 500;
248
+ color: #1a1a1a;
249
+ }
250
+ .model-provider {
251
+ color: #6b7280;
252
+ font-size: 0.9em;
253
+ padding: 4px 8px;
254
+ background: #f1f5f9;
255
+ border-radius: 4px;
256
+ }
257
+ @media (max-width: 768px) {
258
+ body {
259
+ padding: 10px;
260
+ }
261
+
262
+ .container {
263
+ padding: 10px;
264
+ }
265
+ }
266
+ </style>
267
+ </head>
268
+ <body>
269
+ <div class="container">
270
+ <div class="header">
271
+ <h1>Cursor To OpenAI Server</h1>
272
+ <p>高性能 AI 模型代理服务</p>
273
+ </div>
274
+ <div class="info">
275
+ <h2>配置信息</h2>
276
+ <div class="info-item">
277
+ <div class="info-label">聊天来源</div>
278
+ <div class="info-value">自定义(兼容 OpenAI)</div>
279
+ </div>
280
+ <div class="info-item">
281
+ <div class="info-label">自定义端点(基本URL)</div>
282
+ <div class="info-value" id="endpoint-url"></div>
283
+ </div>
284
+ <div class="info-item">
285
+ <div class="info-label">自定义API密钥</div>
286
+ <div class="info-value">抓取的Cursor Cookie,格式为user_...</div>
287
+ </div>
288
+ </div>
289
+ <div class="models">
290
+ <h3>支持的模型列表</h3>
291
+ <div id="model-list"></div>
292
+ </div>
293
+ </div>
294
+ <script>
295
+ const url = new URL(window.location.href);
296
+ const link = url.protocol + '//' + url.host + '/hf/v1';
297
+ document.getElementById('endpoint-url').textContent = link;
298
+ fetch(link + '/models')
299
+ .then(response => response.json())
300
+ .then(data => {
301
+ const modelList = document.getElementById('model-list');
302
+ data.data.forEach(model => {
303
+ const div = document.createElement('div');
304
+ div.className = 'model-item';
305
+ div.innerHTML = \`
306
+ <span class="model-name">\${model.id}</span>
307
+ <span class="model-provider">\${model.owned_by}</span>
308
+ \`;
309
+ modelList.appendChild(div);
310
+ });
311
+ })
312
+ .catch(error => {
313
+ console.error('Error fetching models:', error);
314
+ document.getElementById('model-list').textContent = '获取模型列表失败';
315
+ });
316
+ </script>
317
+ </body>
318
+ </html>
319
+ `;
320
+ res.send(htmlContent);
321
+ });
322
+
323
+ const port = process.env.HF_PORT || 7860;
324
+ app.listen(port, () => {
325
+ console.log(`HF Proxy server is running at PORT: ${port}`);
326
+ });
327
+