diff --git a/src/chatgpt-api.ts b/src/chatgpt-api.ts index 696367c8f..b2ce8b267 100644 --- a/src/chatgpt-api.ts +++ b/src/chatgpt-api.ts @@ -231,7 +231,6 @@ export class ChatGPTAPI { } result.detail = response - onProgress?.(result) } } catch (err) { @@ -299,15 +298,21 @@ export class ChatGPTAPI { } ).then(async (message) => { if (message.detail && !message.detail.usage) { - const promptTokens = numTokens - const completionTokens = await this._getTokenCount(message.text) - message.detail.usage = { - prompt_tokens: promptTokens, - completion_tokens: completionTokens, - total_tokens: promptTokens + completionTokens, - estimated: true + try { + const promptTokens = numTokens + const completionTokens = await this._getTokenCount(message.text) + message.detail.usage = { + prompt_tokens: promptTokens, + completion_tokens: completionTokens, + total_tokens: promptTokens + completionTokens, + estimated: true + } + } catch (err) { + // TODO: this should really never happen, but if it does, + // we should handle notify the user gracefully } } + return this._upsertMessage(message).then(() => message) }) diff --git a/src/types.ts b/src/types.ts index 95874ade3..2c1ad218d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,16 +61,6 @@ export type SendMessageBrowserOptions = { abortSignal?: AbortSignal } -interface CreateChatCompletionStreamResponse - extends openai.CreateChatCompletionDeltaResponse { - usage: CreateCompletionStreamResponseUsage -} - -interface CreateCompletionStreamResponseUsage - extends openai.CreateCompletionResponseUsage { - estimated: true -} - export interface ChatMessage { id: string text: string @@ -100,6 +90,16 @@ export type GetMessageByIdFunction = (id: string) => Promise /** Upserts a chat message to a store. */ export type UpsertMessageFunction = (message: ChatMessage) => Promise +export interface CreateChatCompletionStreamResponse + extends openai.CreateChatCompletionDeltaResponse { + usage: CreateCompletionStreamResponseUsage +} + +export interface CreateCompletionStreamResponseUsage + extends openai.CreateCompletionResponseUsage { + estimated: true +} + /** * https://chat.openapi.com/backend-api/conversation */