From 5f2745c32a7c5735712e45636a2d19801dee0c55 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Thu, 18 May 2023 02:04:12 +0800 Subject: [PATCH] feat: handle non-stream response --- app/client/platforms/openai.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index c2dbbb4fc..9e3605141 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -86,27 +86,39 @@ export class ChatGPTApi implements LLMApi { ...chatPayload, async onopen(res) { clearTimeout(requestTimeoutId); + const contentType = res.headers.get("content-type"); + console.log( + "[OpenAI] request response content type: ", + contentType, + ); + + if (contentType?.startsWith("text/plain")) { + responseText = await res.clone().text(); + return finish(); + } + if ( !res.ok || res.headers.get("content-type") !== EventStreamContentType || res.status !== 200 ) { - let extraInfo = { error: undefined }; + const responseTexts = [responseText]; + let extraInfo = await res.clone().text(); try { - extraInfo = await res.clone().json(); + const resJson = await res.clone().json(); + extraInfo = prettyObject(resJson); } catch {} if (res.status === 401) { - if (responseText.length > 0) { - responseText += "\n\n"; - } - responseText += Locale.Error.Unauthorized; + responseTexts.push(Locale.Error.Unauthorized); } - if (extraInfo.error) { - responseText += "\n\n" + prettyObject(extraInfo); + if (extraInfo) { + responseTexts.push(extraInfo); } + responseText = responseTexts.join("\n\n"); + return finish(); } },