Skip to content

Commit eac581f

Browse files
feat: support ASTRON_MAAS_API_KEY for learning assistant
1 parent 0105652 commit eac581f

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎.env.example‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ASTRON_MAAS_API_KEY=your_astron_maas_api_key_here

‎api/chat.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const API_URL = 'https://maas-coding-api.cn-huabei-1.xf-yun.com/v2/chat/completions'
22
const REQUEST_TIMEOUT = 55000
3+
const API_KEY = process.env.ASTRON_MAAS_API_KEY || process.env.VITE_API_KEY
34

45
async function pipeStream(response, res) {
56
res.status(200)
@@ -54,11 +55,15 @@ module.exports = async function handler(req, res) {
5455
const requestBody = req.body && typeof req.body === 'object' ? req.body : {}
5556

5657
try {
58+
if (!API_KEY) {
59+
return res.status(500).json({ error: 'Missing API key', details: 'Set ASTRON_MAAS_API_KEY or VITE_API_KEY' })
60+
}
61+
5762
const response = await fetch(API_URL, {
5863
method: 'POST',
5964
headers: {
6065
'Content-Type': 'application/json',
61-
'Authorization': `Bearer ${process.env.VITE_API_KEY}`
66+
'Authorization': `Bearer ${API_KEY}`
6267
},
6368
body: JSON.stringify(requestBody),
6469
signal: controller.signal

‎server.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const app = express();
77
const PORT = process.env.PORT || 3001;
88
const API_URL = 'https://maas-coding-api.cn-huabei-1.xf-yun.com/v2/chat/completions';
99
const REQUEST_TIMEOUT = 55000;
10+
const API_KEY = process.env.ASTRON_MAAS_API_KEY || process.env.VITE_API_KEY;
1011
const ALLOWED_ORIGINS = new Set([
1112
'https://iflytek.github.io',
1213
'http://localhost:5173',
@@ -64,11 +65,15 @@ app.post('/api/chat', async (req, res) => {
6465
const requestBody = req.body && typeof req.body === 'object' ? req.body : {};
6566

6667
try {
68+
if (!API_KEY) {
69+
return res.status(500).json({ error: 'Missing API key', details: 'Set ASTRON_MAAS_API_KEY or VITE_API_KEY' });
70+
}
71+
6772
const response = await fetch(API_URL, {
6873
method: 'POST',
6974
headers: {
7075
'Content-Type': 'application/json',
71-
'Authorization': `Bearer ${process.env.VITE_API_KEY}`
76+
'Authorization': `Bearer ${API_KEY}`
7277
},
7378
body: JSON.stringify(requestBody),
7479
signal: controller.signal

0 commit comments

Comments
 (0)