Skip to content

Commit

Permalink
增加token存活检测
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Apr 10, 2024
1 parent 67286d5 commit 0a44d5e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,32 @@ function generateXssParams() {
};
}

/**
* 获取Token存活状态
*/
async function getTokenLiveStatus(token: string) {
const result = await axios.get("https://ai-role.cn/echo-prod/convs?role=echo", {
headers: {
Authorization: `Bearer ${token}`,
...FAKE_HEADERS,
...generateXssParams()
},
timeout: 15000,
validateStatus: () => true
});
try {
const data = checkResult(result);
return _.isArray(data);
}
catch(err) {
return false;
}
}

export default {
createConversation,
createCompletion,
createCompletionStream,
getTokenLiveStatus,
tokenSplit
};
25 changes: 25 additions & 0 deletions src/api/routes/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import _ from 'lodash';

import Request from '@/lib/request/Request.ts';
import Response from '@/lib/response/Response.ts';
import chat from '@/api/controllers/chat.ts';
import logger from '@/lib/logger.ts';

export default {

prefix: '/token',

post: {

'/check': async (request: Request) => {
request
.validate('body.token', _.isString)
const live = await chat.getTokenLiveStatus(request.body.token);
return {
live
}
}

}

}

0 comments on commit 0a44d5e

Please sign in to comment.