Skip to content

Commit

Permalink
fix: #289 #367 #353 #369 provide more error message info
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Apr 2, 2023
1 parent 8d60a41 commit b44caee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/api/chat-stream/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ async function createStream(req: NextRequest) {

const res = await requestOpenai(req);

const contentType = res.headers.get("Content-Type") ?? "";
if (!contentType.includes("stream")) {

This comment has been minimized.

Copy link
@gaogao1030

gaogao1030 Apr 2, 2023

Contributor

前天本地调试的时候发现被阻塞住了,后面定位原因就发现这边没对res不是流的情况处理.本来想抽空提个pr的没想到发现这个提交里处理了。错过一次当贡献者的机会…

This comment has been minimized.

Copy link
@gaogao1030

gaogao1030 Apr 2, 2023

Contributor

当时的处理方案比较简陋直接把消息扔进队列后关闭流,思路参考了别的项目.类似于下面的代码。

if (!res.ok) {
          let errorPayload: object = {};
          try {
            errorPayload = await res.json();
          } catch (e) {
            // ignore
          }
          // return custom text
          controller.enqueue(encoder.encode(`OpenAI API error: ${res.status} ${res.statusText} ${JSON.stringify(errorPayload)}`));
          controller.close();
          return;
        }
const content = await (
await res.text()
).replace(/provided:.*. You/, "provided: ***. You");
console.log("[Stream] error ", content);
return "```json\n" + content + "```";
}

const stream = new ReadableStream({
async start(controller) {
function onParse(event: any) {
Expand Down
8 changes: 3 additions & 5 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ export function Chat(props: {
className={styles["chat-body"]}
ref={scrollRef}
onScroll={(e) => onChatBodyScroll(e.currentTarget)}
onMouseOver={() => inputRef.current?.blur()}
onTouchStart={() => inputRef.current?.blur()}
>
{messages.map((message, i) => {
const isUser = message.role === "user";
Expand All @@ -545,11 +547,7 @@ export function Chat(props: {
{Locale.Chat.Typing}
</div>
)}
<div
className={styles["chat-message-item"]}
onMouseOver={() => inputRef.current?.blur()}
onTouchStart={() => inputRef.current?.blur()}
>
<div className={styles["chat-message-item"]}>
{!isUser &&
!(message.preview || message.content.length === 0) && (
<div className={styles["chat-message-top-actions"]}>
Expand Down
2 changes: 1 addition & 1 deletion app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function Markdown(props: { content: string }) {
[
RehypeHighlight,
{
detect: true,
detect: false,
ignoreMissing: true,
},
],
Expand Down
2 changes: 2 additions & 0 deletions app/components/ui-lib.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
justify-content: center;

.toast-content {
max-width: 80vw;
word-break: break-all;
font-size: 14px;
background-color: var(--white);
box-shadow: var(--card-shadow);
Expand Down
10 changes: 10 additions & 0 deletions app/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ChatRequest, ChatReponse } from "./api/openai/typing";
import { filterConfig, Message, ModelConfig, useAccessStore } from "./store";
import Locale from "./locales";
import { showToast } from "./components/ui-lib";

const TIME_OUT_MS = 30000;

Expand Down Expand Up @@ -87,8 +88,17 @@ export async function requestUsage() {
try {
const response = (await res.json()) as {
total_usage: number;
error?: {
type: string;
message: string;
};
};

if (response.error && response.error.type) {
showToast(response.error.message);
return;
}

if (response.total_usage) {
response.total_usage = Math.round(response.total_usage) / 100;
}
Expand Down

1 comment on commit b44caee

@vercel
Copy link

@vercel vercel bot commented on b44caee Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.