smgc commited on
Commit
3936ea8
1 Parent(s): 81bc8c0

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +15 -7
app.js CHANGED
@@ -10,7 +10,6 @@ const NOTDIAMOND_HEADERS = {
10
  'Content-Type': 'application/json',
11
  'next-action': '8189eb37107121e024940f588629a394a594e6a4'
12
  };
13
- const AUTH_KEY = process.env.AUTH_KEY;
14
 
15
  const DEFAULT_MODEL = 'gpt-4-turbo';
16
 
@@ -27,6 +26,14 @@ const MODEL_MAPPING = {
27
  'perplexity': 'llama-3.1-sonar-large-128k-online'
28
  };
29
 
 
 
 
 
 
 
 
 
30
  function createOpenAIChunk(content, model, finishReason = null) {
31
  return {
32
  id: `chatcmpl-${crypto.randomUUID()}`,
@@ -143,13 +150,13 @@ app.get('/', (req, res) => {
143
  }
144
  },
145
  availableModels: Object.keys(MODEL_MAPPING),
146
- note: "Replace YOUR_AUTH_KEY with the actual authentication key."
147
  });
148
  });
149
 
150
  app.get('/ai/v1/models', (req, res) => {
151
- const authHeader = req.headers['authorization'];
152
- if (!authHeader || !authHeader.startsWith('Bearer ') || authHeader.slice(7) !== AUTH_KEY) {
153
  return res.status(401).json({ error: 'Unauthorized' });
154
  }
155
 
@@ -173,8 +180,8 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
173
  let status = 200;
174
 
175
  try {
176
- const authHeader = req.headers['authorization'];
177
- if (!authHeader || !authHeader.startsWith('Bearer ') || authHeader.slice(7) !== AUTH_KEY) {
178
  status = 401;
179
  throw new Error('Unauthorized');
180
  }
@@ -201,7 +208,8 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
201
 
202
  const headers = {
203
  'Content-Type': 'application/json',
204
- 'next-action': '4e63dabc37fef18cae74cbfd41d1bace49acf47e'
 
205
  };
206
 
207
  const response = await fetch(NOTDIAMOND_URL, {
 
10
  'Content-Type': 'application/json',
11
  'next-action': '8189eb37107121e024940f588629a394a594e6a4'
12
  };
 
13
 
14
  const DEFAULT_MODEL = 'gpt-4-turbo';
15
 
 
26
  'perplexity': 'llama-3.1-sonar-large-128k-online'
27
  };
28
 
29
+ function getAuthKey(req) {
30
+ const authHeader = req.headers['authorization'];
31
+ if (authHeader && authHeader.startsWith('Bearer ')) {
32
+ return authHeader.slice(7);
33
+ }
34
+ return null;
35
+ }
36
+
37
  function createOpenAIChunk(content, model, finishReason = null) {
38
  return {
39
  id: `chatcmpl-${crypto.randomUUID()}`,
 
150
  }
151
  },
152
  availableModels: Object.keys(MODEL_MAPPING),
153
+ note: "Replace YOUR_AUTH_KEY with your actual authentication key."
154
  });
155
  });
156
 
157
  app.get('/ai/v1/models', (req, res) => {
158
+ const authKey = getAuthKey(req);
159
+ if (!authKey) {
160
  return res.status(401).json({ error: 'Unauthorized' });
161
  }
162
 
 
180
  let status = 200;
181
 
182
  try {
183
+ const authKey = getAuthKey(req);
184
+ if (!authKey) {
185
  status = 401;
186
  throw new Error('Unauthorized');
187
  }
 
208
 
209
  const headers = {
210
  'Content-Type': 'application/json',
211
+ 'next-action': '4e63dabc37fef18cae74cbfd41d1bace49acf47e',
212
+ 'Authorization': `Bearer ${authKey}`
213
  };
214
 
215
  const response = await fetch(NOTDIAMOND_URL, {