feat: refresh自动获取sessKey作为API Key

This commit is contained in:
ikechan8370 2023-12-07 22:41:44 +08:00
parent f7ee904414
commit 91abd8a959
2 changed files with 81 additions and 4 deletions

View file

@ -20,6 +20,23 @@ import fs from 'fs'
import loader from '../../../lib/plugins/loader.js'
import VoiceVoxTTS, { supportConfigurations as voxRoleList } from '../utils/tts/voicevox.js'
import { supportConfigurations as azureRoleList } from '../utils/tts/microsoft-azure.js'
import fetch from 'node-fetch'
import { getProxy } from '../utils/proxy.js'
let proxy = getProxy()
const newFetch = (url, options = {}) => {
const defaultOptions = Config.proxy
? {
agent: proxy(Config.proxy)
}
: {}
const mergedOptions = {
...defaultOptions,
...options
}
return fetch(url, mergedOptions)
}
export class ChatgptManagement extends plugin {
constructor (e) {
@ -254,6 +271,10 @@ export class ChatgptManagement extends plugin {
reg: '^#chatgpt设置后台(刷新|refresh)(t|T)oken$',
fnc: 'setOpenAIPlatformToken'
},
{
reg: '^#chatgpt设置sessKey$',
fnc: 'getSessKey'
},
{
reg: '^#(chatgpt)?查看回复设置$',
fnc: 'viewUserSetting'
@ -1114,8 +1135,8 @@ azure语音Azure 语音是微软 Azure 平台提供的一项语音服务,
async saveAPIKey () {
if (!this.e.msg) return
let token = this.e.msg
if (!token.startsWith('sk-')) {
await this.reply('OpenAI API Key格式错误', true)
if (!token.startsWith('sk-') && !token.startsWith('sess-')) {
await this.reply('OpenAI API Key格式错误。如果是格式特殊的非官方Key请前往锅巴或工具箱手动设置', true)
this.finish('saveAPIKey')
return
}
@ -1302,7 +1323,58 @@ azure语音Azure 语音是微软 Azure 平台提供的一项语音服务,
async setOpenAIPlatformToken (e) {
this.setContext('doSetOpenAIPlatformToken')
await e.reply('请发送refreshToken\n你可以在已登录的platform.openai.com后台界面打开调试窗口在终端中执行\nJSON.parse(localStorage.getItem(Object.keys(localStorage).filter(k => k.includes(\'auth0\'))[0])).body.refresh_token\n如果仍不能查看余额请退出登录重新获取刷新令牌')
await e.reply('请发送refreshToken\n你可以在已登录的platform.openai.com后台界面打开调试窗口在终端中执行\nJSON.parse(localStorage.getItem(Object.keys(localStorage).filter(k => k.includes(\'auth0\'))[0])).body.refresh_token\n如果仍不能查看余额请退出登录重新获取刷新令牌.设置后可以发送#chatgpt设置sessKey来将sessKey作为API Key使用')
}
async getSessKey (e) {
if (!Config.OpenAiPlatformRefreshToken) {
this.reply('当前未配置platform.openai.com的刷新token请发送【#chatgpt设置后台刷新token】进行配置。')
return false
}
let authHost = 'https://auth0.openai.com'
if (Config.openAiBaseUrl && !Config.openAiBaseUrl.startsWith('https://api.openai.com')) {
authHost = Config.openAiBaseUrl.replace('/v1', '').replace('/v1/', '')
}
let refreshRes = await newFetch(`${authHost}/oauth/token`, {
method: 'POST',
body: JSON.stringify({
refresh_token: Config.OpenAiPlatformRefreshToken,
client_id: 'DRivsnm2Mu42T3KOpqdtwB3NYviHYzwD',
grant_type: 'refresh_token'
})
})
if (refreshRes.status !== 200) {
let errMsg = await refreshRes.json()
if (errMsg.error === 'access_denied') {
await e.reply('刷新令牌失效,请重新发送【#chatgpt设置后台刷新token】进行配置。建议退出platform.openai.com重新登录后再获取和配置')
} else {
await e.reply('获取失败')
}
return false
}
let newToken = await refreshRes.json()
// eslint-disable-next-line camelcase
const { access_token, refresh_token } = newToken
// eslint-disable-next-line camelcase
Config.OpenAiPlatformRefreshToken = refresh_token
let host = Config.openAiBaseUrl.replace('/v1', '').replace('/v1/', '')
let res = await newFetch(`${host}/dashboard/onboarding/login`, {
headers: {
// eslint-disable-next-line camelcase
Authorization: `Bearer ${access_token}`
},
method: 'POST'
})
if (res.status === 200) {
let authRes = await res.json()
let sess = authRes.user.session.sensitive_id
if (sess) {
Config.apiKey = sess
await e.reply('已成功将sessKey设置为apiKey您可以发送#open余额来查看该账号余额')
} else {
await e.reply('设置失败!')
}
}
}
async doSetOpenAIPlatformToken () {