feat: 引入prettier
This commit is contained in:
@@ -23,8 +23,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
if (webSocketStore.connected) {
|
||||
if (typeof data === "string") {
|
||||
webSocketStore.send(data);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
webSocketStore.websocket?.send(data);
|
||||
}
|
||||
}
|
||||
@@ -53,8 +52,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
* 开始录音
|
||||
*/
|
||||
const startRecording = async () => {
|
||||
if (isRecording.value)
|
||||
return;
|
||||
if (isRecording.value) return;
|
||||
messages.value = [];
|
||||
// 确保 WebSocket 已连接
|
||||
if (!webSocketStore.connected) {
|
||||
@@ -62,8 +60,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
// 等待连接建立
|
||||
await new Promise<void>((resolve) => {
|
||||
const check = () => {
|
||||
if (webSocketStore.connected)
|
||||
resolve();
|
||||
if (webSocketStore.connected) resolve();
|
||||
else setTimeout(check, 100);
|
||||
};
|
||||
check();
|
||||
@@ -73,11 +70,14 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
// 获取麦克风音频流
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
// 创建音频上下文,采样率16kHz
|
||||
audioContext = new (window.AudioContext || (window as any).webkitAudioContext)({
|
||||
sampleRate: 16000,
|
||||
audioContext = new (window.AudioContext ||
|
||||
(window as any).webkitAudioContext)({
|
||||
sampleRate: 16000
|
||||
});
|
||||
// 用Blob方式创建AudioWorklet模块的URL
|
||||
const blob = new Blob([audioProcessorCode], { type: "application/javascript" });
|
||||
const blob = new Blob([audioProcessorCode], {
|
||||
type: "application/javascript"
|
||||
});
|
||||
const processorUrl = URL.createObjectURL(blob);
|
||||
// 加载AudioWorklet模块
|
||||
await audioContext.audioWorklet.addModule(processorUrl);
|
||||
@@ -89,7 +89,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
workletNode = new AudioWorkletNode(audioContext, "audio-processor", {
|
||||
numberOfInputs: 1,
|
||||
numberOfOutputs: 1,
|
||||
channelCount: 1,
|
||||
channelCount: 1
|
||||
});
|
||||
// 监听来自AudioWorklet的音频数据
|
||||
workletNode.port.onmessage = (event) => {
|
||||
@@ -104,8 +104,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
mediaStreamSource.connect(workletNode);
|
||||
workletNode.connect(audioContext.destination);
|
||||
isRecording.value = true;
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
// 麦克风权限失败或AudioWorklet加载失败
|
||||
console.error("需要麦克风权限才能录音", err);
|
||||
}
|
||||
@@ -115,8 +114,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
* 停止录音
|
||||
*/
|
||||
const stopRecording = () => {
|
||||
if (!isRecording.value)
|
||||
return;
|
||||
if (!isRecording.value) return;
|
||||
|
||||
// 通知后端录音结束
|
||||
sendMessage(JSON.stringify({ type: "asr_end" }));
|
||||
@@ -124,7 +122,7 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
// 停止所有音轨
|
||||
if (mediaStreamSource?.mediaStream) {
|
||||
const tracks = mediaStreamSource.mediaStream.getTracks();
|
||||
tracks.forEach(track => track.stop());
|
||||
tracks.forEach((track) => track.stop());
|
||||
}
|
||||
|
||||
// 断开音频节点
|
||||
@@ -149,6 +147,6 @@ export const useAsrStore = defineStore("asr", () => {
|
||||
messages,
|
||||
startRecording,
|
||||
stopRecording,
|
||||
sendMessage,
|
||||
sendMessage
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import type { IChatWithLLMRequest, ModelInfo, ModelListInfo, UsageInfo } from "@/interfaces";
|
||||
import type {
|
||||
IChatWithLLMRequest,
|
||||
ModelInfo,
|
||||
ModelListInfo,
|
||||
UsageInfo
|
||||
} from "@/interfaces";
|
||||
import { ChatService } from "@/services";
|
||||
|
||||
export const useChatStore = defineStore("chat", () => {
|
||||
const token = ("sk-fkGVZBrAqvIxLjlF3b5f19EfBb63486c90Fa5a1fBd7076Ee");
|
||||
const token = "sk-fkGVZBrAqvIxLjlF3b5f19EfBb63486c90Fa5a1fBd7076Ee";
|
||||
// 默认模型
|
||||
const modelInfo = ref<ModelInfo | null>(null);
|
||||
// 历史消息
|
||||
@@ -16,23 +21,25 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const chatWithLLM = async (
|
||||
request: IChatWithLLMRequest,
|
||||
onProgress: (content: string) => void, // 接收进度回调
|
||||
getUsageInfo: (object: UsageInfo) => void = () => { },
|
||||
getUsageInfo: (object: UsageInfo) => void = () => {}
|
||||
) => {
|
||||
if (completing.value)
|
||||
throw new Error("正在响应中");
|
||||
if (completing.value) throw new Error("正在响应中");
|
||||
|
||||
completing.value = true; // 开始请求
|
||||
try {
|
||||
await ChatService.ChatWithLLM(token, request, (content) => {
|
||||
onProgress(content);
|
||||
}, (object: UsageInfo) => {
|
||||
getUsageInfo(object);
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
await ChatService.ChatWithLLM(
|
||||
token,
|
||||
request,
|
||||
(content) => {
|
||||
onProgress(content);
|
||||
},
|
||||
(object: UsageInfo) => {
|
||||
getUsageInfo(object);
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("请求失败:", error);
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
completing.value = false;
|
||||
}
|
||||
};
|
||||
@@ -40,12 +47,11 @@ export const useChatStore = defineStore("chat", () => {
|
||||
// 添加消息到历史记录
|
||||
const addMessageToHistory = (message: string) => {
|
||||
const content = message.trim();
|
||||
if (!content)
|
||||
return;
|
||||
if (!content) return;
|
||||
|
||||
historyMessages.value.push({
|
||||
role: "user",
|
||||
content,
|
||||
content
|
||||
});
|
||||
};
|
||||
|
||||
@@ -54,39 +60,51 @@ export const useChatStore = defineStore("chat", () => {
|
||||
historyMessages.value = [];
|
||||
};
|
||||
|
||||
watch(historyMessages, (newVal) => {
|
||||
// 当历史消息变化时,发送请求
|
||||
if (newVal.length > 0) {
|
||||
const lastMessage = newVal[newVal.length - 1];
|
||||
if (lastMessage.role === "user" && modelInfo.value) {
|
||||
chatWithLLM({
|
||||
messages: newVal,
|
||||
model: modelInfo.value?.model_id,
|
||||
}, (content) => {
|
||||
// 处理进度回调
|
||||
if (
|
||||
historyMessages.value.length === 0
|
||||
|| historyMessages.value[historyMessages.value.length - 1].role !== "assistant"
|
||||
) {
|
||||
historyMessages.value.push({
|
||||
role: "assistant",
|
||||
content: "",
|
||||
});
|
||||
}
|
||||
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;
|
||||
}
|
||||
});
|
||||
watch(
|
||||
historyMessages,
|
||||
(newVal) => {
|
||||
// 当历史消息变化时,发送请求
|
||||
if (newVal.length > 0) {
|
||||
const lastMessage = newVal[newVal.length - 1];
|
||||
if (lastMessage.role === "user" && modelInfo.value) {
|
||||
chatWithLLM(
|
||||
{
|
||||
messages: newVal,
|
||||
model: modelInfo.value?.model_id
|
||||
},
|
||||
(content) => {
|
||||
// 处理进度回调
|
||||
if (
|
||||
historyMessages.value.length === 0 ||
|
||||
historyMessages.value[historyMessages.value.length - 1].role !==
|
||||
"assistant"
|
||||
) {
|
||||
historyMessages.value.push({
|
||||
role: "assistant",
|
||||
content: ""
|
||||
});
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, { deep: true });
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
// 模型列表
|
||||
const modelList = ref<ModelListInfo[]>([]);
|
||||
@@ -96,11 +114,21 @@ export const useChatStore = defineStore("chat", () => {
|
||||
try {
|
||||
const response = await ChatService.GetModelList();
|
||||
modelList.value = response.data.data;
|
||||
}
|
||||
catch (error) {
|
||||
} catch (error) {
|
||||
console.error("获取模型列表失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
return { token, completing, chatWithLLM, historyMessages, addMessageToHistory, clearHistoryMessages, getModelList, modelList, modelInfo, onlineCount };
|
||||
return {
|
||||
token,
|
||||
completing,
|
||||
chatWithLLM,
|
||||
historyMessages,
|
||||
addMessageToHistory,
|
||||
clearHistoryMessages,
|
||||
getModelList,
|
||||
modelList,
|
||||
modelInfo,
|
||||
onlineCount
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
export const useUserStore = defineStore("user", () => {
|
||||
return {
|
||||
|
||||
};
|
||||
return {};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user