Skip to content

Commit ee25e4a

Browse files
committed
cap nhat giao dien
1 parent b07760f commit ee25e4a

File tree

10 files changed

+20364
-1159
lines changed

10 files changed

+20364
-1159
lines changed

app/SyncOnFirstLoad.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,8 @@ export default function SyncOnFirstLoad() {
66
const syncStore = useSyncStore();
77

88
useEffect(() => {
9-
// if (syncStore.lastSyncTime === 0) {
10-
// // If this is the first time syncing, call sync()
11-
// alert("[SyncOnFirstLoad] Dong bo hoa du lieu lan dau tien");
12-
13-
console.log("[SyncOnFirstLoad] Dong bo hoa du lieu lan dau tien");
14-
15-
console.log("Thoi gian dong bo lan cuoi: ", syncStore.lastSyncTime);
16-
17-
syncStore.sync();
18-
// }
9+
// Parse cookies using the custom function
10+
// syncStore.sync();
1911
}, []);
2012

2113
return null;

app/api/[provider]/[...path]/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { handle as siliconflowHandler } from "../../siliconflow";
1515
import { handle as xaiHandler } from "../../xai";
1616
import { handle as chatglmHandler } from "../../glm";
1717
import { handle as proxyHandler } from "../../proxy";
18+
import { handle as supabaseHandler } from "../../supabase";
1819

1920
async function handle(
2021
req: NextRequest,
@@ -27,6 +28,9 @@ async function handle(
2728

2829
console.log(`[${params.provider} Route] params `, params);
2930
switch (apiPath) {
31+
case ApiPath.Supabase:
32+
console.log("[Supabase Route] params ", params);
33+
return supabaseHandler(req, { params });
3034
case ApiPath.Azure:
3135
return azureHandler(req, { params });
3236
case ApiPath.Google:

app/api/supabase.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
import { NextRequest, NextResponse } from "next/server";
3+
import cookie from "cookie";
4+
5+
const SUPABASE_URL = process.env.SUPABASE_URL!;
6+
const SUPABASE_ANON_KEY = process.env.SUPABASE_ANON_KEY!;
7+
const AUTHEN_PAGE = process.env.AUTHEN_PAGE!;
8+
9+
export async function handle(
10+
req: NextRequest,
11+
{ params }: { params: { path: string[] } },
12+
) {
13+
// Parse cookies using the 'cookie' library
14+
const cookies = cookie.parse(req.headers.get("cookie") || "");
15+
const authToken = cookies["sb-zzgkylsbdgwoohcbompi-auth-token"];
16+
17+
console.log("[Supabase] authToken", authToken);
18+
19+
if (!authToken) {
20+
return NextResponse.redirect(AUTHEN_PAGE, 302);
21+
}
22+
23+
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
24+
global: {
25+
headers: {
26+
Authorization: `Bearer ${authToken}`,
27+
},
28+
},
29+
});
30+
31+
try {
32+
const { data, error } = await supabase.auth.getUser();
33+
34+
console.log("[Supabase] user", data?.user);
35+
36+
if (error || !data?.user) {
37+
return NextResponse.json(
38+
{ error: error?.message || "Error fetching user data" },
39+
{ status: 401 },
40+
);
41+
}
42+
43+
return NextResponse.json({ user: data.user }, { status: 200 });
44+
} catch (err) {
45+
console.error("Error fetching user data from Supabase:", err);
46+
return NextResponse.json(
47+
{ error: "Internal server error" },
48+
{ status: 500 },
49+
);
50+
}
51+
}

app/client/api.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,16 @@ interface ChatProvider {
132132
usage: () => void;
133133
}
134134

135+
// Khởi tạo API client dựa trên nhà cung cấp mô hình được chỉ định
135136
export class ClientApi {
136137
public llm: LLMApi;
137138

139+
// Hàm khởi tạo nhận vào một provider (nhà cung cấp mô hình AI)
140+
// Mặc định là ModelProvider.GPT nếu không được chỉ định
138141
constructor(provider: ModelProvider = ModelProvider.GPT) {
142+
console.log("[ClientApi] provider ", provider);
143+
144+
// Sử dụng switch để khởi tạo instance tương ứng với provider được chọn
139145
switch (provider) {
140146
case ModelProvider.GeminiPro:
141147
this.llm = new GeminiProApi();
@@ -178,13 +184,18 @@ export class ClientApi {
178184
}
179185
}
180186

187+
// Hàm cấu hình (chưa triển khai chi tiết)
181188
config() {}
182189

190+
// Hàm lấy prompts (chưa triển khai chi tiết)
183191
prompts() {}
184192

193+
// Hàm lấy masks (chưa triển khai chi tiết)
185194
masks() {}
186195

196+
// Hàm chia sẻ cuộc trò chuyện
187197
async share(messages: ChatMessage[], avatarUrl: string | null = null) {
198+
// Chuẩn bị dữ liệu tin nhắn để chia sẻ
188199
const msgs = messages
189200
.map((m) => ({
190201
from: m.role === "user" ? "human" : "gpt",
@@ -197,14 +208,20 @@ export class ClientApi {
197208
"Share from [NextChat]: https://github.com/Yidadaa/ChatGPT-Next-Web",
198209
},
199210
]);
200-
// 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用
211+
// Lưu ý: Không nên sửa đổi dòng thông báo cuối cùng này vì nó dùng cho việc làm sạch dữ liệu sau này
201212
// Please do not modify this message
202213

203214
console.log("[Share]", messages, msgs);
215+
216+
// Lấy cấu hình client
204217
const clientConfig = getClientConfig();
218+
219+
// Xác định URL để chia sẻ dựa trên môi trường (app hay web)
205220
const proxyUrl = "/sharegpt";
206221
const rawUrl = "https://sharegpt.com/api/conversations";
207222
const shareUrl = clientConfig?.isApp ? rawUrl : proxyUrl;
223+
224+
// Gửi yêu cầu POST để chia sẻ cuộc trò chuyện
208225
const res = await fetch(shareUrl, {
209226
body: JSON.stringify({
210227
avatarUrl,
@@ -216,6 +233,7 @@ export class ClientApi {
216233
method: "POST",
217234
});
218235

236+
// Xử lý phản hồi và trả về link chia sẻ
219237
const resJson = await res.json();
220238
console.log("[Share]", resJson);
221239
if (resJson.id) {
@@ -224,6 +242,7 @@ export class ClientApi {
224242
}
225243
}
226244

245+
// Hàm tạo token xác thực Bearer
227246
export function getBearerToken(
228247
apiKey: string,
229248
noBearer: boolean = false,
@@ -233,25 +252,37 @@ export function getBearerToken(
233252
: "";
234253
}
235254

255+
// Hàm kiểm tra chuỗi có hợp lệ không (có độ dài > 0)
236256
export function validString(x: string): boolean {
237257
return x?.length > 0;
238258
}
239259

260+
// Hàm lấy các header cần thiết cho yêu cầu API
240261
export function getHeaders(ignoreHeaders: boolean = false) {
262+
// Lấy store để truy cập các trạng thái liên quan đến quyền truy cập và chat
241263
const accessStore = useAccessStore.getState();
242264
const chatStore = useChatStore.getState();
265+
266+
// Khởi tạo đối tượng headers rỗng
243267
let headers: Record<string, string> = {};
268+
269+
// Nếu không bỏ qua headers thì thêm các header mặc định
244270
if (!ignoreHeaders) {
245271
headers = {
246272
"Content-Type": "application/json",
247273
Accept: "application/json",
248274
};
249275
}
250276

277+
// Lấy cấu hình client
251278
const clientConfig = getClientConfig();
252279

280+
// Hàm getConfig sẽ xác định nhà cung cấp hiện tại và API key tương ứng
253281
function getConfig() {
282+
// Lấy cấu hình mô hình từ session hiện tại
254283
const modelConfig = chatStore.currentSession().mask.modelConfig;
284+
285+
// Kiểm tra loại nhà cung cấp đang được sử dụng
255286
const isGoogle = modelConfig.providerName === ServiceProvider.Google;
256287
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
257288
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
@@ -265,7 +296,11 @@ export function getHeaders(ignoreHeaders: boolean = false) {
265296
const isChatGLM = modelConfig.providerName === ServiceProvider.ChatGLM;
266297
const isSiliconFlow =
267298
modelConfig.providerName === ServiceProvider.SiliconFlow;
299+
300+
// Kiểm tra xem có bật kiểm soát truy cập không
268301
const isEnabledAccessControl = accessStore.enabledAccessControl();
302+
303+
// Xác định API key dựa trên nhà cung cấp đang được sử dụng
269304
const apiKey = isGoogle
270305
? accessStore.googleApiKey
271306
: isAzure
@@ -309,6 +344,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
309344
};
310345
}
311346

347+
// Hàm xác định header nào sẽ được sử dụng để xác thực
312348
function getAuthHeader(): string {
313349
return isAzure
314350
? "api-key"
@@ -319,6 +355,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
319355
: "Authorization";
320356
}
321357

358+
// Lấy các giá trị đã được xác định trong getConfig
322359
const {
323360
isGoogle,
324361
isAzure,
@@ -335,19 +372,24 @@ export function getHeaders(ignoreHeaders: boolean = false) {
335372
apiKey,
336373
isEnabledAccessControl,
337374
} = getConfig();
338-
// when using baidu api in app, not set auth header
375+
376+
// Khi sử dụng API của Baidu trong ứng dụng, không đặt header xác thực
339377
if (isBaidu && clientConfig?.isApp) return headers;
340378

379+
// Xác định tên header xác thực
341380
const authHeader = getAuthHeader();
342381

382+
// Tạo token xác thực
343383
const bearerToken = getBearerToken(
344384
apiKey,
345385
isAzure || isAnthropic || isGoogle,
346386
);
347387

388+
// Nếu có bearer token thì thêm vào headers
348389
if (bearerToken) {
349390
headers[authHeader] = bearerToken;
350391
} else if (isEnabledAccessControl && validString(accessStore.accessCode)) {
392+
// Nếu có mã truy cập thì sử dụng nó để tạo bearer token
351393
headers["Authorization"] = getBearerToken(
352394
ACCESS_CODE_PREFIX + accessStore.accessCode,
353395
);
@@ -356,6 +398,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
356398
return headers;
357399
}
358400

401+
// Hàm tạo instance của ClientApi dựa trên nhà cung cấp dịch vụ
359402
export function getClientApi(provider: ServiceProvider): ClientApi {
360403
switch (provider) {
361404
case ServiceProvider.Google:

app/components/chat.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ function _Chat() {
18131813
const shouldShowClearContextDivider =
18141814
i === clearContextIndex - 1;
18151815

1816-
console.log(message.role);
1816+
// console.log(message.role);
18171817

18181818
return (
18191819
<Fragment key={message.id}>
@@ -1868,9 +1868,9 @@ function _Chat() {
18681868
}}
18691869
></IconButton>
18701870
</div>
1871-
{isUser ? (
1872-
<Avatar avatar={config.avatar} />
1873-
) : (
1871+
1872+
{/* Neu la user thi khong hien thi avatar */}
1873+
{isUser ? null : (
18741874
<>
18751875
{["system"].includes(message.role) ? (
18761876
<Avatar avatar="2699-fe0f" />

app/components/home.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ require("../polyfill");
55
import { useEffect, useState } from "react";
66
import styles from "./home.module.scss";
77

8-
import BotIcon from "../icons/bot.svg";
8+
//icon chebichat logo
9+
import BotIcon from "../icons/chebichat.svg";
910
import LoadingIcon from "../icons/three-dots.svg";
1011

1112
import { getCSSVar, useMobileScreen } from "../utils";
@@ -14,7 +15,7 @@ import dynamic from "next/dynamic";
1415
import { Path, SlotID } from "../constant";
1516
import { ErrorBoundary } from "./error";
1617

17-
import { getISOLang, getLang } from "../locales";
18+
import { getISOLang } from "../locales";
1819

1920
import {
2021
HashRouter as Router,
@@ -212,7 +213,7 @@ function Screen() {
212213
<div
213214
className={clsx(styles.container, {
214215
[styles["tight-container"]]: shouldTightBorder,
215-
[styles["rtl-screen"]]: getLang() === "ar",
216+
// [styles["rtl-screen"]]: getLang() === "ar", // Removed because "ar" is not a possible return value
216217
})}
217218
>
218219
{renderContent()}

app/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export enum Path {
6161

6262
export enum ApiPath {
6363
Cors = "",
64+
Supabase = "/api/supabase",
6465
Azure = "/api/azure",
6566
OpenAI = "/api/alibaba", // Use Alibaba path for OpenAI API
6667
Anthropic = "/api/anthropic",

0 commit comments

Comments
 (0)