Browse Source

Merge pull request #1616 from Yidadaa/bugfix-0519

fix: #1611 show corret message when can not query usage
Yifei Zhang 1 year ago
parent
commit
48f25b0799

+ 5 - 1
app/client/platforms/openai.ts

@@ -189,10 +189,14 @@ export class ChatGPTApi implements LLMApi {
       }),
     ]);
 
-    if (!used.ok || !subs.ok || used.status === 401) {
+    if (used.status === 401) {
       throw new Error(Locale.Error.Unauthorized);
     }
 
+    if (!used.ok || !subs.ok) {
+      throw new Error("Failed to query usage from openai");
+    }
+
     const response = (await used.json()) as {
       total_usage?: number;
       error?: {

+ 13 - 5
app/components/chat.tsx

@@ -487,18 +487,26 @@ export function Chat() {
 
   // stop response
   const onUserStop = (messageId: number) => {
+    ChatControllerPool.stop(sessionIndex, messageId);
+  };
+
+  useEffect(() => {
     chatStore.updateCurrentSession((session) => {
       const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
       session.messages.forEach((m) => {
         // check if should stop all stale messages
-        if (m.streaming && new Date(m.date).getTime() < stopTiming) {
-          m.isError = false;
-          m.streaming = false;
+        if (new Date(m.date).getTime() < stopTiming) {
+          if (m.streaming) {
+            m.streaming = false;
+          }
+
+          if (m.content.length === 0) {
+            m.content = "No content in this message.";
+          }
         }
       });
     });
-    ChatControllerPool.stop(sessionIndex, messageId);
-  };
+  }, []);
 
   // check if should send message
   const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {

+ 6 - 1
app/locales/cn.ts

@@ -241,6 +241,11 @@ const cn = {
   },
 };
 
-export type LocaleType = typeof cn;
+type DeepPartial<T> = T extends object
+  ? {
+      [P in keyof T]?: DeepPartial<T[P]>;
+    }
+  : T;
+export type LocaleType = DeepPartial<typeof cn>;
 
 export default cn;

+ 8 - 1
app/locales/index.ts

@@ -11,6 +11,7 @@ import VI from "./vi";
 import RU from "./ru";
 import CS from "./cs";
 import KO from "./ko";
+import { merge } from "../utils/merge";
 
 export type { LocaleType } from "./cn";
 
@@ -80,7 +81,8 @@ export function changeLang(lang: Lang) {
   location.reload();
 }
 
-export default {
+const fallbackLang = EN;
+const targetLang = {
   en: EN,
   cn: CN,
   tw: TW,
@@ -95,3 +97,8 @@ export default {
   cs: CS,
   ko: KO,
 }[getLang()] as typeof CN;
+
+// if target lang missing some fields, it will use fallback lang string
+merge(fallbackLang, targetLang);
+
+export default fallbackLang as typeof CN;

+ 54 - 51
app/masks/cn.ts

@@ -1,37 +1,40 @@
 import { BuiltinMask } from "./typing";
 
-export const CN_MASKS: BuiltinMask[] = [{
-    avatar:"gpt-bot",
-    name:"文生图",
+export const CN_MASKS: BuiltinMask[] = [
+  {
+    avatar: "1f5bc-fe0f",
+    name: "以文搜图",
     context: [
-       {role:"system",
-        content:"You are a useful Assistant that won't refuse to draw picture, instead, you will output specific markdown code for picture description, but don't use code block.",
-        date:"",
-       },
-       {role:"user",
-        content:"Can you draw some pictures for me?",
-        date:"",
-       },
-       {role:"assistant",
-        content:"Sure, what do you want me to draw?",
-        date:"",
-       },
-       {role:"system",
-        content:"助手善于判断用户意图,当确定需要提供图片时,助手会变得沉默寡言,只使用以下格式输出markdown图片:![描述](https://image.pollinations.ai/prompt/描述),因为这个语法可以自动按照提示生成并渲染图片。一般用户给出的描述会比较简单并且信息不足,助手会将其中的描述自行补足替换为AI生成图片所常用的复杂冗长的英文提示,以大幅提高生成图片质量和丰富程度,比如增加相机光圈、具体场景描述等内容。助手会避免用代码块或原始块包围markdown标记,因为那样只会渲染出代码块或原始块而不是图片。",
-        date:"",
-       },
+      {
+        role: "system",
+        content:
+          "You are a useful Assistant that won't refuse to draw picture, instead, you will output specific markdown code for picture description, but don't use code block.",
+        date: "",
+      },
+      { role: "user", content: "Can you draw some pictures for me?", date: "" },
+      {
+        role: "assistant",
+        content: "Sure, what do you want me to draw?",
+        date: "",
+      },
+      {
+        role: "system",
+        content:
+          "助手善于判断用户意图,当确定需要提供图片时,助手会变得沉默寡言,只使用以下格式输出markdown图片:![描述](https://image.pollinations.ai/prompt/描述),因为这个语法可以自动按照提示生成并渲染图片。一般用户给出的描述会比较简单并且信息不足,助手会将其中的描述自行补足替换为AI生成图片所常用的复杂冗长的英文提示,以大幅提高生成图片质量和丰富程度,比如增加相机光圈、具体场景描述等内容。助手会避免用代码块或原始块包围markdown标记,因为那样只会渲染出代码块或原始块而不是图片。",
+        date: "",
+      },
     ],
-    modelConfig:{
-      model:"gpt-3.5-turbo",
-      temperature:1,
-      max_tokens:2000,
-      presence_penalty:0,
-      sendMemory:true,
-      historyMessageCount:32,
-      compressMessageLengthThreshold:1000,
+    modelConfig: {
+      model: "gpt-3.5-turbo",
+      temperature: 1,
+      max_tokens: 2000,
+      presence_penalty: 0,
+      sendMemory: true,
+      historyMessageCount: 32,
+      compressMessageLengthThreshold: 1000,
     },
-    lang:"cn",
-    builtin:true,
+    lang: "cn",
+    builtin: true,
   },
   {
     avatar: "1f638",
@@ -194,33 +197,33 @@ export const CN_MASKS: BuiltinMask[] = [{
     lang: "cn",
     builtin: true,
   },
-  { avatar:"1f4d1",
-    name:"简历写手",
-    context:[
+  {
+    avatar: "1f4d1",
+    name: "简历写手",
+    context: [
       {
-        role:"user",
+        role: "user",
         content:
-        "我需要你写一份通用简历,每当我输入一个职业、项目名称时,你需要完成以下任务:\ntask1: 列出这个人的基本资料,如姓名、出生年月、学历、面试职位、工作年限、意向城市等。一行列一个资料。\ntask2: 详细介绍这个职业的技能介绍,至少列出10条\ntask3: 详细列出这个职业对应的工作经历,列出2条\ntask4: 详细列出这个职业对应的工作项目,列出2条。项目按照项目背景、项目细节、项目难点、优化和改进、我的价值几个方面来描述,多展示职业关键字。也可以体现我在项目管理、工作推进方面的一些能力。\ntask5: 详细列出个人评价,100字左右\n你把以上任务结果按照以下Markdown格式输出:\n\n```\n### 基本信息\n<task1 result>\n\n### 掌握技能\n<task2 result>\n\n### 工作经历\n<task3 result>\n\n### 项目经历\n<task4 result>\n\n### 关于我\n<task5 result>\n\n```",
-        date:"",
+          "我需要你写一份通用简历,每当我输入一个职业、项目名称时,你需要完成以下任务:\ntask1: 列出这个人的基本资料,如姓名、出生年月、学历、面试职位、工作年限、意向城市等。一行列一个资料。\ntask2: 详细介绍这个职业的技能介绍,至少列出10条\ntask3: 详细列出这个职业对应的工作经历,列出2条\ntask4: 详细列出这个职业对应的工作项目,列出2条。项目按照项目背景、项目细节、项目难点、优化和改进、我的价值几个方面来描述,多展示职业关键字。也可以体现我在项目管理、工作推进方面的一些能力。\ntask5: 详细列出个人评价,100字左右\n你把以上任务结果按照以下Markdown格式输出:\n\n```\n### 基本信息\n<task1 result>\n\n### 掌握技能\n<task2 result>\n\n### 工作经历\n<task3 result>\n\n### 项目经历\n<task4 result>\n\n### 关于我\n<task5 result>\n\n```",
+        date: "",
+      },
+      {
+        role: "assistant",
+        content: "好的,请问您需要我为哪个职业编写通用简历呢?",
+        date: "",
       },
-      { 
-        role:"assistant",
-        content:"好的,请问您需要我为哪个职业编写通用简历呢?",
-        date:""
-      }
     ],
-    modelConfig:
-    {
-      model:"gpt-3.5-turbo",
-      temperature:0.5,
-      max_tokens:2000,
-      presence_penalty:0,
-      sendMemory:true,
-      historyMessageCount:4, 
-      compressMessageLengthThreshold:1000
+    modelConfig: {
+      model: "gpt-3.5-turbo",
+      temperature: 0.5,
+      max_tokens: 2000,
+      presence_penalty: 0,
+      sendMemory: true,
+      historyMessageCount: 4,
+      compressMessageLengthThreshold: 1000,
     },
-    lang:"cn",
-    builtin:false
+    lang: "cn",
+    builtin: true,
   },
   {
     avatar: "1f469-200d-2695-fe0f",

File diff suppressed because it is too large
+ 0 - 0
app/masks/en.ts


+ 1 - 1
app/masks/index.ts

@@ -15,7 +15,7 @@ export const BUILTIN_MASK_STORE = {
     return this.masks[id] as Mask | undefined;
   },
   add(m: BuiltinMask) {
-    const mask = { ...m, id: this.buildinId++ };
+    const mask = { ...m, id: this.buildinId++, builtin: true };
     this.masks[mask.id] = mask;
     return mask;
   },

+ 3 - 1
app/masks/typing.ts

@@ -1,3 +1,5 @@
 import { type Mask } from "../store/mask";
 
-export type BuiltinMask = Omit<Mask, "id">;
+export type BuiltinMask = Omit<Mask, "id"> & {
+  builtin: true;
+};

+ 9 - 0
app/utils/merge.ts

@@ -0,0 +1,9 @@
+export function merge(target: any, source: any) {
+  Object.keys(source).forEach(function (key) {
+    if (source[key] && typeof source[key] === "object") {
+      merge((target[key] = target[key] || {}), source[key]);
+      return;
+    }
+    target[key] = source[key];
+  });
+}

Some files were not shown because too many files changed in this diff