feat: 增加AI回复手动重试
This commit is contained in:
@@ -137,7 +137,8 @@ async function selectSession(sessionId: number, force = false) {
|
||||
}
|
||||
|
||||
async function send(message: string, complete: (success: boolean) => void) {
|
||||
if (!activeSessionId.value || sending.value) {
|
||||
const sessionId = activeSessionId.value;
|
||||
if (!sessionId || sending.value) {
|
||||
complete(false);
|
||||
return;
|
||||
}
|
||||
@@ -152,15 +153,35 @@ async function send(message: string, complete: (success: boolean) => void) {
|
||||
};
|
||||
messages.value.push(userMessage, assistantMessage);
|
||||
const assistantIndex = messages.value.length - 1;
|
||||
complete(true);
|
||||
await messageList.value?.scrollToMessage(userMessage.id);
|
||||
await runGeneration(sessionId, message, assistantIndex, false);
|
||||
}
|
||||
|
||||
async function retryMessage(messageId: string) {
|
||||
const sessionId = activeSessionId.value;
|
||||
if (!sessionId || sending.value) return;
|
||||
const assistantIndex = messages.value.findIndex((item) => item.id === messageId);
|
||||
const assistantMessage = messages.value[assistantIndex];
|
||||
if (!assistantMessage?.retryQuestion) return;
|
||||
await messageList.value?.scrollToMessage(messageId);
|
||||
await runGeneration(sessionId, assistantMessage.retryQuestion, assistantIndex, true);
|
||||
}
|
||||
|
||||
async function runGeneration(sessionId: number, message: string, assistantIndex: number, retry: boolean) {
|
||||
const currentAssistant = () => messages.value[assistantIndex];
|
||||
currentAssistant().content = "";
|
||||
currentAssistant().reasoning = "";
|
||||
currentAssistant().showReasoning = false;
|
||||
currentAssistant().errorMessage = undefined;
|
||||
currentAssistant().streaming = true;
|
||||
sending.value = true;
|
||||
followingOutput.value = true;
|
||||
activeAbortController.value = new AbortController();
|
||||
let hasContent = false;
|
||||
await messageList.value?.scrollToMessage(userMessage.id);
|
||||
try {
|
||||
await streamChat(
|
||||
activeSessionId.value,
|
||||
sessionId,
|
||||
message,
|
||||
async (chunk) => {
|
||||
if (!hasContent) {
|
||||
@@ -183,21 +204,22 @@ async function send(message: string, complete: (success: boolean) => void) {
|
||||
if (followingOutput.value) await messageList.value?.scrollToBottom();
|
||||
},
|
||||
activeAbortController.value.signal,
|
||||
retry,
|
||||
);
|
||||
currentAssistant().streaming = false;
|
||||
currentAssistant().retryQuestion = undefined;
|
||||
currentAssistant().createdAt = new Date().toISOString();
|
||||
complete(true);
|
||||
await refreshSessionList();
|
||||
await refreshProfile();
|
||||
} catch (error) {
|
||||
currentAssistant().streaming = false;
|
||||
if (error instanceof DOMException && error.name === "AbortError") {
|
||||
currentAssistant().content = currentAssistant().content || "已停止生成";
|
||||
complete(true);
|
||||
currentAssistant().retryQuestion = undefined;
|
||||
} else {
|
||||
currentAssistant().content = currentAssistant().content || "回答生成失败,请稍后重试。";
|
||||
complete(false);
|
||||
handleError(error, "AI 回复失败");
|
||||
currentAssistant().errorMessage = chatErrorMessage(error);
|
||||
currentAssistant().retryQuestion = message;
|
||||
showToast("本次回答未完成,可以点击重新生成");
|
||||
}
|
||||
} finally {
|
||||
sending.value = false;
|
||||
@@ -205,6 +227,14 @@ async function send(message: string, complete: (success: boolean) => void) {
|
||||
}
|
||||
}
|
||||
|
||||
function chatErrorMessage(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "";
|
||||
if (/429|too many|请求过多|排队/i.test(message)) return "当前请求较多,AI 服务暂时繁忙。";
|
||||
if (/timeout|超时/i.test(message)) return "等待 AI 回复超时,请稍后重新生成。";
|
||||
if (/network|网络|连接/i.test(message)) return "网络连接不稳定,本次回答没有完成。";
|
||||
return "AI 回答生成失败,本次不会扣除成功回答额度。";
|
||||
}
|
||||
|
||||
async function stop() {
|
||||
if (!activeSessionId.value || !sending.value) return;
|
||||
activeAbortController.value?.abort();
|
||||
@@ -551,6 +581,7 @@ async function copyText(text: string) {
|
||||
:messages="messages"
|
||||
:loading-session="loadingSession"
|
||||
@follow-change="followingOutput = $event"
|
||||
@retry="retryMessage"
|
||||
/>
|
||||
<ChatComposer :loading="sending" :disabled="!activeSessionId || loadingSession" @send="send" @stop="stop" />
|
||||
<SessionDrawer
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { Bot } from "@lucide/vue";
|
||||
import { Bot, RotateCcw } from "@lucide/vue";
|
||||
import MarkdownIt from "markdown-it";
|
||||
import { computed } from "vue";
|
||||
|
||||
@@ -11,6 +11,12 @@ const props = defineProps<{
|
||||
showReasoning?: boolean;
|
||||
createdAt: string;
|
||||
streaming?: boolean;
|
||||
errorMessage?: string;
|
||||
canRetry?: boolean;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
retry: [];
|
||||
}>();
|
||||
|
||||
const markdown = new MarkdownIt({ breaks: true, html: false, linkify: true });
|
||||
@@ -95,6 +101,13 @@ const displayTime = computed(() => {
|
||||
<span></span><span></span><span></span>
|
||||
思考中
|
||||
</div>
|
||||
<div v-if="errorMessage" class="message-error-state" role="alert">
|
||||
<span>{{ errorMessage }}</span>
|
||||
<button v-if="canRetry" type="button" :disabled="streaming" @click="emit('retry')">
|
||||
<RotateCcw :size="15" aria-hidden="true" />
|
||||
重新生成
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<div v-else class="message-content">{{ renderedContent }}</div>
|
||||
<time v-if="displayTime" :datetime="createdAt">{{ displayTime }}</time>
|
||||
|
||||
@@ -11,6 +11,8 @@ export interface DisplayMessage {
|
||||
showReasoning?: boolean;
|
||||
createdAt: string;
|
||||
streaming?: boolean;
|
||||
errorMessage?: string;
|
||||
retryQuestion?: string;
|
||||
}
|
||||
|
||||
defineProps<{
|
||||
@@ -20,6 +22,7 @@ defineProps<{
|
||||
|
||||
const emit = defineEmits<{
|
||||
followChange: [following: boolean];
|
||||
retry: [messageId: string];
|
||||
}>();
|
||||
|
||||
const scroller = ref<HTMLElement | null>(null);
|
||||
@@ -63,6 +66,9 @@ defineExpose({ scrollToBottom, scrollToMessage });
|
||||
:show-reasoning="message.showReasoning"
|
||||
:created-at="message.createdAt"
|
||||
:streaming="message.streaming"
|
||||
:error-message="message.errorMessage"
|
||||
:can-retry="Boolean(message.retryQuestion)"
|
||||
@retry="emit('retry', message.id)"
|
||||
/>
|
||||
</template>
|
||||
</section>
|
||||
|
||||
@@ -123,6 +123,7 @@ export async function streamChat(
|
||||
onReasoning?: (chunk: string) => void,
|
||||
onStatus?: (message: string, type: string, reasoningVisible: boolean) => void,
|
||||
signal?: AbortSignal,
|
||||
retry = false,
|
||||
) {
|
||||
const token = getToken();
|
||||
const response = await fetch(`${API_BASE}/chat/completions`, {
|
||||
@@ -131,7 +132,7 @@ export async function streamChat(
|
||||
"Content-Type": "application/json",
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
body: JSON.stringify({ sessionId, message }),
|
||||
body: JSON.stringify({ sessionId, message, retry }),
|
||||
signal,
|
||||
});
|
||||
if (!response.ok || !response.body) {
|
||||
|
||||
@@ -1400,6 +1400,51 @@ textarea:focus-visible {
|
||||
.generation-state span:nth-child(2) { animation-delay: 0.14s; }
|
||||
.generation-state span:nth-child(3) { animation-delay: 0.28s; margin-right: 4px; }
|
||||
|
||||
.message-error-state {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 9px 12px;
|
||||
margin-top: 10px;
|
||||
padding: 10px 11px;
|
||||
border: 1px solid #ead5cf;
|
||||
border-radius: 11px;
|
||||
background: #fff8f5;
|
||||
color: #8a5043;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.message-error-state span {
|
||||
flex: 1 1 190px;
|
||||
}
|
||||
|
||||
.message-error-state button {
|
||||
min-height: 32px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 6px;
|
||||
padding: 0 11px;
|
||||
border: 1px solid #bf7765;
|
||||
border-radius: 9px;
|
||||
background: #ffffff;
|
||||
color: #8a4434;
|
||||
font: inherit;
|
||||
font-weight: 700;
|
||||
white-space: nowrap;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.message-error-state button:hover {
|
||||
background: #fff0eb;
|
||||
}
|
||||
|
||||
.message-error-state button:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
@keyframes generation-pulse {
|
||||
0%, 70%, 100% { opacity: 0.25; transform: translateY(0); }
|
||||
35% { opacity: 1; transform: translateY(-2px); }
|
||||
|
||||
Reference in New Issue
Block a user