mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 05:47:11 +00:00
feat: api and api3 support proxy
This commit is contained in:
parent
24c80d159b
commit
f4a073372f
6 changed files with 69 additions and 27 deletions
|
|
@ -1,12 +1,12 @@
|
|||
import fetch from 'node-fetch'
|
||||
import { Config } from '../utils/config.js'
|
||||
|
||||
export async function getConversations (qq = '') {
|
||||
export async function getConversations (qq = '', fetchFn = fetch) {
|
||||
let accessToken = await redis.get('CHATGPT:TOKEN')
|
||||
if (!accessToken) {
|
||||
throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token')
|
||||
}
|
||||
let response = await fetch(`${Config.apiBaseUrl}/conversations?offset=0&limit=20`, {
|
||||
let response = await fetchFn(`${Config.apiBaseUrl}/conversations?offset=0&limit=20`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
@ -33,7 +33,7 @@ export async function getConversations (qq = '') {
|
|||
map[item.id] = cachedConversationLastMessage
|
||||
} else {
|
||||
// 缓存中没有,就去查官方api
|
||||
let conversationDetailResponse = await fetch(`${Config.apiBaseUrl}/api/conversation/${item.id}`, {
|
||||
let conversationDetailResponse = await fetchFn(`${Config.apiBaseUrl}/api/conversation/${item.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
@ -96,12 +96,12 @@ export async function getConversations (qq = '') {
|
|||
return res
|
||||
}
|
||||
|
||||
export async function getLatestMessageIdByConversationId (conversationId) {
|
||||
export async function getLatestMessageIdByConversationId (conversationId, fetchFn = fetch) {
|
||||
let accessToken = await redis.get('CHATGPT:TOKEN')
|
||||
if (!accessToken) {
|
||||
throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token')
|
||||
}
|
||||
let conversationDetailResponse = await fetch(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, {
|
||||
let conversationDetailResponse = await fetchFn(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
@ -124,12 +124,12 @@ export async function getLatestMessageIdByConversationId (conversationId) {
|
|||
}
|
||||
|
||||
// 调用chat.open.com删除某一个对话。该操作不可逆。
|
||||
export async function deleteConversation (conversationId) {
|
||||
export async function deleteConversation (conversationId, fetchFn = fetch) {
|
||||
let accessToken = await redis.get('CHATGPT:TOKEN')
|
||||
if (!accessToken) {
|
||||
throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token')
|
||||
}
|
||||
let response = await fetch(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, {
|
||||
let response = await fetchFn(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
|
|
|||
|
|
@ -3,6 +3,15 @@ import { Config } from '../utils/config.js'
|
|||
import HttpsProxyAgent from 'https-proxy-agent'
|
||||
import _ from 'lodash'
|
||||
import fetch from 'node-fetch'
|
||||
let proxy
|
||||
if (Config.proxy) {
|
||||
try {
|
||||
proxy = (await import('https-proxy-agent')).default
|
||||
} catch (e) {
|
||||
console.warn('未安装https-proxy-agent,请在插件目录下执行pnpm add https-proxy-agent')
|
||||
}
|
||||
}
|
||||
|
||||
export class OfficialChatGPTClient {
|
||||
constructor (opts = {}) {
|
||||
const {
|
||||
|
|
@ -13,6 +22,20 @@ export class OfficialChatGPTClient {
|
|||
this._accessToken = accessToken
|
||||
this._apiReverseUrl = apiReverseUrl
|
||||
this._timeoutMs = timeoutMs
|
||||
this._fetch = (url, options = {}) => {
|
||||
const defaultOptions = Config.proxy
|
||||
? {
|
||||
agent: proxy(Config.proxy)
|
||||
}
|
||||
: {}
|
||||
|
||||
const mergedOptions = {
|
||||
...defaultOptions,
|
||||
...options
|
||||
}
|
||||
|
||||
return fetch(url, mergedOptions)
|
||||
}
|
||||
}
|
||||
|
||||
async sendMessage (prompt, opts = {}) {
|
||||
|
|
@ -59,10 +82,7 @@ export class OfficialChatGPTClient {
|
|||
},
|
||||
referrer: 'https://chat.openai.com/chat'
|
||||
}
|
||||
if (Config.proxy) {
|
||||
option.agent = new HttpsProxyAgent(Config.proxy)
|
||||
}
|
||||
const res = await fetch(url, option)
|
||||
const res = await this._fetch(url, option)
|
||||
const decoder = new TextDecoder('utf-8')
|
||||
const bodyBytes = await res.arrayBuffer()
|
||||
const bodyText = decoder.decode(bodyBytes)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue