Skip to content

Commit 495dcc3

Browse files
committed
feat: optimize error output
1 parent eeb429f commit 495dcc3

11 files changed

+34
-411
lines changed

app/history/page.tsx

-52
This file was deleted.

app/layout.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import "./globals.css";
22
import type { Metadata, Viewport } from "next";
33
import React from "react";
4-
import { Analytics } from "@vercel/analytics/react";
5-
import { ThemeProvider } from "@/components/theme-provider";
64
import Umami from "@/components/umami";
5+
import { ThemeProvider } from "next-themes";
76

87
export const metadata: Metadata = {
98
title: "AI 算卦 - 在线卜卦 GPT4 解读",
@@ -34,14 +33,13 @@ export default function RootLayout({
3433
<html lang="cn" suppressHydrationWarning>
3534
<body>
3635
<ThemeProvider
36+
enableSystem
3737
attribute="class"
3838
defaultTheme="system"
39-
enableSystem
4039
disableTransitionOnChange
4140
>
4241
{children}
4342
</ThemeProvider>
44-
{process.env.VERCEL && <Analytics />}
4543
<Umami />
4644
</body>
4745
</html>

app/server.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { streamText } from "ai";
33
import { createOpenAI } from "@ai-sdk/openai";
44
import { createStreamableValue } from "ai/rsc";
5+
import { ERROR_PREFIX } from "@/lib/constant";
56

67
const model = process.env.OPENAI_MODEL ?? "gpt-3.5-turbo";
78
const openai = createOpenAI({ baseURL: process.env.OPENAI_BASE_URL });
@@ -15,6 +16,7 @@ export async function getAnswer(
1516
guaName: string,
1617
guaChange: string,
1718
) {
19+
console.log(prompt, guaName, guaChange);
1820
const stream = createStreamableValue();
1921
try {
2022
// const res = await fetch(
@@ -68,7 +70,7 @@ export async function getAnswer(
6870
break;
6971
case "error":
7072
const err = part.error as any;
71-
buffer += err.message ?? err.toString();
73+
stream.update(ERROR_PREFIX + (err.message ?? err.toString()));
7274
break;
7375
}
7476
}

components/divination.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getAnswer } from "@/app/server";
1313
import { readStreamableValue } from "ai/rsc";
1414
import { Button } from "./ui/button";
1515
import { BrainCircuit, ListRestart } from "lucide-react";
16+
import { ERROR_PREFIX } from "@/lib/constant";
1617

1718
const AUTO_DELAY = 1000;
1819

@@ -39,6 +40,10 @@ function Divination() {
3940
if (data) {
4041
let ret = "";
4142
for await (const delta of readStreamableValue(data)) {
43+
if (delta.startsWith(ERROR_PREFIX)) {
44+
setError(delta.slice(ERROR_PREFIX.length));
45+
return;
46+
}
4247
ret += delta;
4348
setCompletion(ret);
4449
}
@@ -209,7 +214,7 @@ function Divination() {
209214
)}
210215

211216
{!inputQuestion && hexagramList.length != 0 && (
212-
<div className="flex gap-2">
217+
<div className="flex max-w-md gap-2">
213218
<Hexagram list={hexagramList} />
214219
{showResult && (
215220
<div className="flex flex-col justify-around">

components/result-ai.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ function ResultAI({
5555
return (
5656
<div className="h-0 w-full flex-1 sm:max-w-md md:max-w-2xl">
5757
{isLoading && (
58-
<div className="h-0">
59-
<div className="relative -top-8 flex w-fit items-center text-muted-foreground sm:left-2">
58+
<div className="flex h-0">
59+
<span className="flex-1" />
60+
<div className="relative -top-4 flex w-fit items-center pr-1 text-muted-foreground sm:left-2 sm:pr-3">
6061
<RotateCw size={16} className="animate-spin" />
6162
<span className="ml-1 text-sm">AI 分析中...</span>
6263
</div>

components/result.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface ResultObj {
99

1010
function Result(props: ResultObj) {
1111
return (
12-
<div className="flex flex-col items-start justify-center gap-2">
12+
<div className="flex flex-col items-start justify-center gap-2 sm:gap-3">
1313
<a
1414
className="flex items-center gap-1 font-medium text-primary/80 underline underline-offset-4 transition-colors hover:text-primary/100"
1515
href={`https://zhouyi.sunls.de/${props.guaMark}/`}

components/theme-provider.tsx

-9
This file was deleted.

lib/constant.ts

-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
11
export const ERROR_PREFIX = "::error::";
2-
3-
export interface History {
4-
date: string;
5-
prompt: string;
6-
gua: string;
7-
change: string;
8-
}
9-
102
export const VERSION = "1.2.3";

lib/utils.ts

-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,3 @@ import { twMerge } from "tailwind-merge";
44
export function cn(...inputs: ClassValue[]) {
55
return twMerge(clsx(inputs));
66
}
7-
8-
export function getLocaleTime(): string {
9-
return fmtLocaleTime(new Date());
10-
}
11-
12-
export function fmtLocaleTime(time: Date): string {
13-
return time.toLocaleString("zh-CN", {
14-
timeZone: "Asia/Shanghai",
15-
hourCycle: "h23",
16-
});
17-
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@ai-sdk/openai": "^1.0.8",
1313
"@radix-ui/react-dropdown-menu": "^2.1.3",
1414
"@radix-ui/react-slot": "^1.1.1",
15-
"@vercel/analytics": "^1.4.1",
1615
"ai": "^4.0.18",
1716
"aimless.js": "^1.0.4",
1817
"class-variance-authority": "^0.7.1",

0 commit comments

Comments
 (0)