isididiidid commited on
Commit
a760633
·
verified ·
1 Parent(s): 89d69ea

Create hf.js

Browse files
Files changed (1) hide show
  1. hf.js +195 -0
hf.js ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const morgan = require('morgan');
3
+ const { createProxyMiddleware } = require('http-proxy-middleware');
4
+ const app = express();
5
+
6
+ app.use(morgan('dev'));
7
+
8
+ app.get('/hf/v1/models', (req, res) => {
9
+ const models = {
10
+ "object": "list",
11
+ "data": [
12
+ {
13
+ "id": "claude-3-5-sonnet-20241022",
14
+ "object": "model",
15
+ "created": 1706745938,
16
+ "owned_by": "cursor"
17
+ },
18
+ {
19
+ "id": "claude-3-opus",
20
+ "object": "model",
21
+ "created": 1706745938,
22
+ "owned_by": "cursor"
23
+ },
24
+ {
25
+ "id": "claude-3-5-haiku",
26
+ "object": "model",
27
+ "created": 1706745938,
28
+ "owned_by": "cursor"
29
+ },
30
+ {
31
+ "id": "claude-3-5-sonnet",
32
+ "object": "model",
33
+ "created": 1706745938,
34
+ "owned_by": "cursor"
35
+ },
36
+ {
37
+ "id": "cursor-small",
38
+ "object": "model",
39
+ "created": 1706745938,
40
+ "owned_by": "cursor"
41
+ },
42
+ {
43
+ "id": "gemini-exp-1206",
44
+ "object": "model",
45
+ "created": 1706745938,
46
+ "owned_by": "cursor"
47
+ },
48
+ {
49
+ "id": "gemini-2.0-flash-exp",
50
+ "object": "model",
51
+ "created": 1706745938,
52
+ "owned_by": "cursor"
53
+ },
54
+ {
55
+ "id": "gemini-2.0-flash-thinking-exp",
56
+ "object": "model",
57
+ "created": 1706745938,
58
+ "owned_by": "cursor"
59
+ },
60
+ {
61
+ "id": "gpt-3.5-turbo",
62
+ "object": "model",
63
+ "created": 1706745938,
64
+ "owned_by": "cursor"
65
+ },
66
+ {
67
+ "id": "gpt-4",
68
+ "object": "model",
69
+ "created": 1706745938,
70
+ "owned_by": "cursor"
71
+ },
72
+ {
73
+ "id": "gpt-4-turbo-2024-04-09",
74
+ "object": "model",
75
+ "created": 1706745938,
76
+ "owned_by": "cursor"
77
+ },
78
+ {
79
+ "id": "gpt-40",
80
+ "object": "model",
81
+ "created": 1706745938,
82
+ "owned_by": "cursor"
83
+ },
84
+ {
85
+ "id": "gpt-40-mini",
86
+ "object": "model",
87
+ "created": 1706745938,
88
+ "owned_by": "cursor"
89
+ },
90
+ {
91
+ "id": "o1-mini",
92
+ "object": "model",
93
+ "created": 1706745938,
94
+ "owned_by": "cursor"
95
+ },
96
+ {
97
+ "id": "o1-preview",
98
+ "object": "model",
99
+ "created": 1706745938,
100
+ "owned_by": "cursor"
101
+ }
102
+ ]
103
+ };
104
+ res.json(models);
105
+ });
106
+
107
+ app.use('/hf/v1/chat/completions', createProxyMiddleware({
108
+ target: 'http://localhost:3010/v1/chat/completions',
109
+ changeOrigin: true
110
+ }));
111
+
112
+ app.get('/', (req, res) => {
113
+ const htmlContent = `
114
+ <!DOCTYPE html>
115
+ <html lang="en">
116
+ <head>
117
+ <meta charset="UTF-8">
118
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
119
+ <title>Cursor To OpenAI</title>
120
+ <style>
121
+ body {
122
+ padding: 20px;
123
+ font-family: Arial, sans-serif;
124
+ max-width: 800px;
125
+ margin: 0 auto;
126
+ line-height: 1.6;
127
+ }
128
+ .models {
129
+ margin-top: 20px;
130
+ background: #f5f5f5;
131
+ padding: 20px;
132
+ border-radius: 8px;
133
+ }
134
+ .model-item {
135
+ margin: 8px 0;
136
+ padding: 12px;
137
+ background: white;
138
+ border-radius: 6px;
139
+ box-shadow: 0 1px 3px rgba(0,0,0,0.1);
140
+ }
141
+ .model-provider {
142
+ color: #666;
143
+ font-size: 0.9em;
144
+ margin-left: 10px;
145
+ }
146
+ .info {
147
+ background: #e8f4ff;
148
+ padding: 15px;
149
+ border-radius: 6px;
150
+ margin-bottom: 20px;
151
+ }
152
+ </style>
153
+ </head>
154
+ <body>
155
+ <div class="info">
156
+ <h2>Cursor To OpenAI Server</h2>
157
+ <p>聊天来源: 自定义(兼容 OpenAI)</p>
158
+ <p>自定义端点(基本URL):<span id="endpoint-url"></span></p>
159
+ <p>自定义API密钥:[抓取的Cursor Cookie,格式为user_...]</p>
160
+ </div>
161
+ <div class="models">
162
+ <h3>支持的模型列表:</h3>
163
+ <div id="model-list"></div>
164
+ </div>
165
+ <script>
166
+ const url = new URL(window.location.href);
167
+ const link = url.protocol + '//' + url.host + '/hf/v1';
168
+ document.getElementById('endpoint-url').textContent = link;
169
+
170
+ fetch(link + '/models')
171
+ .then(response => response.json())
172
+ .then(data => {
173
+ const modelList = document.getElementById('model-list');
174
+ data.data.forEach(model => {
175
+ const div = document.createElement('div');
176
+ div.className = 'model-item';
177
+ div.innerHTML = \`\${model.id}<span class="model-provider">(\${model.owned_by})</span>\`;
178
+ modelList.appendChild(div);
179
+ });
180
+ })
181
+ .catch(error => {
182
+ console.error('Error fetching models:', error);
183
+ document.getElementById('model-list').textContent = '获取模型列表失败';
184
+ });
185
+ </script>
186
+ </body>
187
+ </html>
188
+ `;
189
+ res.send(htmlContent);
190
+ });
191
+
192
+ const port = process.env.HF_PORT || 7860;
193
+ app.listen(port, () => {
194
+ console.log(`HF Proxy server is running at PORT: ${port}`);
195
+ });