feat: 一些优化,以及token消耗展示

This commit is contained in:
2025-06-29 01:25:42 +08:00
parent 2da3fe3b47
commit dfc817e3e3
8 changed files with 146 additions and 64 deletions

View File

@@ -1,4 +1,4 @@
import type { IChatWithLLMRequest, ModelInfo, ModelListInfo } from "@/interfaces";
import type { IChatWithLLMRequest, ModelInfo, ModelListInfo, UsageInfo } from "@/interfaces";
import { ChatService } from "@/services";
export const useChatStore = defineStore("chat", () => {
@@ -16,6 +16,7 @@ export const useChatStore = defineStore("chat", () => {
const chatWithLLM = async (
request: IChatWithLLMRequest,
onProgress: (content: string) => void, // 接收进度回调
getUsageInfo: (object: UsageInfo) => void = () => { },
) => {
if (completing.value)
throw new Error("正在响应中");
@@ -24,6 +25,8 @@ export const useChatStore = defineStore("chat", () => {
try {
await ChatService.ChatWithLLM(token, request, (content) => {
onProgress(content);
}, (object: UsageInfo) => {
getUsageInfo(object);
});
}
catch (error) {
@@ -71,6 +74,15 @@ export const useChatStore = defineStore("chat", () => {
});
}
historyMessages.value[historyMessages.value.length - 1].content = content;
}, (usageInfo: UsageInfo) => {
// 处理使用usage信息回调
// 如果最后一条消息是助手的回复,则更新使用信息
if (
historyMessages.value.length > 0
&& historyMessages.value[historyMessages.value.length - 1].role === "assistant"
) {
historyMessages.value[historyMessages.value.length - 1].usage = usageInfo;
}
});
}
}