Skip to content

Commit

Permalink
Merge pull request #8 from cheungchazz/main
Browse files Browse the repository at this point in the history
  • Loading branch information
fatwang2 committed Mar 18, 2024
2 parents 637fbe0 + f38eb8b commit 700b659
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
.vercel
.env
wrangler.toml
.wrangler
.wrangler
.vscode
23 changes: 20 additions & 3 deletions openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,25 @@
buffer += decoder.decode(value, { stream: true });

if (buffer.startsWith('data:')) {
buffer_ = buffer.substring(5); // 去掉"data:"及其后的空格,留下JSON部分
}
buffer_ = buffer.substring(5).trim(); // 去掉"data:"及其后的空格,留下JSON部分
} else if (buffer.includes('"error":')) { // 检查是否包含错误信息
try {
const jsonData = JSON.parse(buffer);
if (jsonData.error) {
console.log("发现接口错误信息,将返回错误内容", buffer);

// 创建并返回错误信息的Response对象
const response = new Response(JSON.stringify(jsonData), {
status: 400, // HTTP状态码
headers: { 'Content-Type': 'application/json' }
});
return response;
}
} catch (err) {
console.error('Error parsing JSON:', err);
console.log("接口返回信息:", buffer);
}
}

// 尝试解析第一块数据
try {
Expand All @@ -273,7 +290,7 @@
}
} catch (err) {
console.error('Error parsing JSON:', err);
// 解析出错时的处理
console.log("接口返回信息:", buffer);
}

if (shouldDirectlyReturn) {
Expand Down

0 comments on commit 700b659

Please sign in to comment.