From 7c5e9ea434f2f73fe9def5b03410b0208ffe2f43 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Tue, 19 Sep 2023 11:17:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/claude.ai/index.js | 52 ++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/utils/claude.ai/index.js b/utils/claude.ai/index.js index 2f4266c..d6cfbcc 100644 --- a/utils/claude.ai/index.js +++ b/utils/claude.ai/index.js @@ -156,31 +156,37 @@ export class ClaudeAIClient { if (streamDataRes.status === 307) { throw new Error('claude.ai目前不支持你所在的地区') } - let streamData = streamDataRes.body - // console.log(streamData) - let responseText = '' - let streams = streamData.split('\n\n') - for (let s of streams) { - let jsonStr = s.replace('data: ', '').trim() - try { - let jsonObj = JSON.parse(jsonStr) - if (jsonObj && jsonObj.completion) { - responseText += jsonObj.completion - } - if (this.debug) { - console.log(jsonObj) - } - // console.log(responseText) - } catch (err) { - // ignore error - if (this.debug) { - console.log(jsonStr) + if (streamDataRes.status === 200) { + let streamData = streamDataRes.body + // console.log(streamData) + let responseText = '' + let streams = streamData.split('\n\n') + for (let s of streams) { + let jsonStr = s.replace('data: ', '').trim() + try { + let jsonObj = JSON.parse(jsonStr) + if (jsonObj && jsonObj.completion) { + responseText += jsonObj.completion + } + if (this.debug) { + console.log(jsonObj) + } + // console.log(responseText) + } catch (err) { + // ignore error + if (this.debug) { + console.log(jsonStr) + } } } - } - return { - text: responseText.trim(), - conversationId + return { + text: responseText.trim(), + conversationId + } + } else if (streamDataRes.status === 408) { + throw new Error('claude.ai响应超时,可能是回复文本太多,请调高超时时间重试') + } else { + throw new Error('unknown error') } } }