diff --git a/legacy/readme.md b/legacy/readme.md index 6a46fea7..473afb55 100644 --- a/legacy/readme.md +++ b/legacy/readme.md @@ -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. diff --git a/legacy/src/chatgpt-unofficial-proxy-api.ts b/legacy/src/chatgpt-unofficial-proxy-api.ts index 6fe2fa6c..87eae0a1 100644 --- a/legacy/src/chatgpt-unofficial-proxy-api.ts +++ b/legacy/src/chatgpt-unofficial-proxy-api.ts @@ -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 { + 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(), diff --git a/legacy/src/utils.ts b/legacy/src/utils.ts new file mode 100644 index 00000000..c849d5ce --- /dev/null +++ b/legacy/src/utils.ts @@ -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) +}