This commit is contained in:
@@ -2,7 +2,7 @@ import { format } from "util";
|
||||
|
||||
async function callZhipuAPI(
|
||||
messages: { role: string; content: string }[],
|
||||
model = "glm-4.6",
|
||||
model = process.env.ZHIPU_MODEL_NAME,
|
||||
) {
|
||||
const url = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
|
||||
|
||||
@@ -54,10 +54,7 @@ export async function simpleGetLLMAnswer(
|
||||
return Response.json({
|
||||
status: "success",
|
||||
message: await getLLMAnswer(
|
||||
format(
|
||||
prompt,
|
||||
...args.map((v) => searchParams.get(v)),
|
||||
),
|
||||
format(prompt, ...args.map((v) => searchParams.get(v))),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
TranslationHistoryArraySchema,
|
||||
TranslationHistorySchema,
|
||||
} from "@/lib/interfaces";
|
||||
import { getLocalStorageOperator } from "@/lib/utils";
|
||||
import { getLocalStorageOperator, shallowEqual } from "@/lib/utils";
|
||||
import z from "zod";
|
||||
|
||||
const MAX_HISTORY_LENGTH = 50;
|
||||
@@ -11,9 +11,11 @@ export const tlso = getLocalStorageOperator<
|
||||
typeof TranslationHistoryArraySchema
|
||||
>("translator", TranslationHistoryArraySchema);
|
||||
export const tlsoPush = (item: z.infer<typeof TranslationHistorySchema>) => {
|
||||
tlso.set(
|
||||
[...tlso.get(), item as z.infer<typeof TranslationHistorySchema>].slice(
|
||||
-MAX_HISTORY_LENGTH,
|
||||
),
|
||||
);
|
||||
const oldHistory = tlso.get();
|
||||
if (oldHistory.some((v) => shallowEqual(v, item))) return oldHistory;
|
||||
|
||||
const newHistory = [...oldHistory, item].slice(-MAX_HISTORY_LENGTH);
|
||||
tlso.set(newHistory);
|
||||
|
||||
return newHistory;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"use server";
|
||||
|
||||
import { folder } from "../../../generated/prisma/client";
|
||||
import {
|
||||
folderCreateInput,
|
||||
folderUpdateInput,
|
||||
|
||||
@@ -92,6 +92,7 @@ export const getLocalStorageOperator = <T extends z.ZodTypeAny>(
|
||||
set: (data: z.infer<T>) => {
|
||||
if (!localStorage) return;
|
||||
localStorage.setItem(key, JSON.stringify(data));
|
||||
return data;
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -128,3 +129,22 @@ export const letsFetch = (
|
||||
export function isNonNegativeInteger(str: string): boolean {
|
||||
return /^\d+$/.test(str);
|
||||
}
|
||||
|
||||
export function shallowEqual<T extends object>(obj1: T, obj2: T): boolean {
|
||||
const keys1 = Object.keys(obj1) as Array<keyof T>;
|
||||
const keys2 = Object.keys(obj2) as Array<keyof T>;
|
||||
|
||||
// 首先检查键的数量是否相同
|
||||
if (keys1.length !== keys2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 然后逐个比较键值对
|
||||
for (const key of keys1) {
|
||||
if (obj1[key] !== obj2[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user