Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: first time to select kimi,wihch results a loop because of 401 #759

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions src/bots/moonshot/KimiBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,19 @@ export default class KimiBot extends Bot {
*/
async _checkAvailability() {
let available = false;
let userInfoUrl = "https://kimi.moonshot.cn/api/user";
await axios
.get(userInfoUrl, this.getAuthHeader())
.then((response) => {
available = response.data?.status == "normal";
})
.catch(async (error) => {
if (
error.response?.status == 401 &&
error.response?.data?.error_type == "auth.token.invalid"
) {
await this.refreshTokens();
available = await this._checkAvailability();
hwwgh555 marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.error("Error checking Kimi login status:", error);
}
});

try {
await this.refreshTokens();
available = true;
} catch (e) {
available = false;
console.error("Error checking Kimi login status:", e);
}
return available;
}

async refreshTokens() {
let refreshUrl = "https://kimi.moonshot.cn/api/auth/token/refresh";
await axios
return await axios
.get(refreshUrl, {
headers: {
Authorization: `Bearer ${store.state.kimi?.refresh_token}`,
Expand All @@ -64,9 +53,6 @@ export default class KimiBot extends Bot {
access_token: response.data?.access_token,
refresh_token: response.data?.refresh_token,
});
})
.catch((error) => {
console.error("Error refreshing Kimi tokens:", error);
});
}

Expand Down