kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: check for invalid conversationId and parentMessageId
rodzic
17b81456d2
commit
539aa6d45c
|
@ -327,7 +327,7 @@ These libraries work with email + password accounts (e.g., they do not support a
|
|||
|
||||
Alternatively, you can manually get an `accessToken` by logging in to the ChatGPT webapp and then opening `https://chat.openai.com/api/auth/session`, which will return a JSON object containing your `accessToken` string.
|
||||
|
||||
Access tokens last for ~8 hours.
|
||||
Access tokens last for days.
|
||||
|
||||
**Note**: using a reverse proxy will expose your access token to a third-party. There shouldn't be any adverse effects possible from this, but please consider the risks before using this method.
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import { v4 as uuidv4 } from 'uuid'
|
|||
import * as types from './types'
|
||||
import { fetch as globalFetch } from './fetch'
|
||||
import { fetchSSE } from './fetch-sse'
|
||||
import { isValidUUIDv4 } from './utils'
|
||||
|
||||
export class ChatGPTUnofficialProxyAPI {
|
||||
protected _accessToken: string
|
||||
|
@ -97,6 +98,30 @@ export class ChatGPTUnofficialProxyAPI {
|
|||
text: string,
|
||||
opts: types.SendMessageBrowserOptions = {}
|
||||
): Promise<types.ChatMessage> {
|
||||
if (!!opts.conversationId !== !!opts.parentMessageId) {
|
||||
throw new Error(
|
||||
'ChatGPTUnofficialProxyAPI.sendMessage: conversationId and parentMessageId must both be set or both be undefined'
|
||||
)
|
||||
}
|
||||
|
||||
if (opts.conversationId && !isValidUUIDv4(opts.conversationId)) {
|
||||
throw new Error(
|
||||
'ChatGPTUnofficialProxyAPI.sendMessage: conversationId is not a valid v4 UUID'
|
||||
)
|
||||
}
|
||||
|
||||
if (opts.parentMessageId && !isValidUUIDv4(opts.parentMessageId)) {
|
||||
throw new Error(
|
||||
'ChatGPTUnofficialProxyAPI.sendMessage: parentMessageId is not a valid v4 UUID'
|
||||
)
|
||||
}
|
||||
|
||||
if (opts.messageId && !isValidUUIDv4(opts.messageId)) {
|
||||
throw new Error(
|
||||
'ChatGPTUnofficialProxyAPI.sendMessage: messageId is not a valid v4 UUID'
|
||||
)
|
||||
}
|
||||
|
||||
const {
|
||||
conversationId,
|
||||
parentMessageId = uuidv4(),
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
const uuidv4Re =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
|
||||
export function isValidUUIDv4(str: string): boolean {
|
||||
return str && uuidv4Re.test(str)
|
||||
}
|
Ładowanie…
Reference in New Issue