fix
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import { ref } from "vue"
|
||||
import { WS_BASE_URL } from "../utils/const"
|
||||
import { html, css, js } from "./editors"
|
||||
import { Prompt } from "../api"
|
||||
import type { PromptMessage as RawMessage } from "../utils/type"
|
||||
import { user } from "./user"
|
||||
|
||||
export interface PromptMessage {
|
||||
role: "user" | "assistant"
|
||||
@@ -16,8 +13,6 @@ export const messages = ref<PromptMessage[]>([])
|
||||
export const conversationId = ref<string>("")
|
||||
export const connected = ref(false)
|
||||
export const streaming = ref(false)
|
||||
export const historyLoading = ref(false)
|
||||
let _historyLoadId = 0
|
||||
export const streamingContent = ref("")
|
||||
let _onCodeComplete:
|
||||
| ((code: {
|
||||
@@ -96,8 +91,6 @@ export function connectPrompt(taskId: number) {
|
||||
}
|
||||
|
||||
export function disconnectPrompt() {
|
||||
_historyLoadId++ // cancel any in-flight loadHistory
|
||||
historyLoading.value = false // reset here; finally block won't (loadId mismatch)
|
||||
if (ws) {
|
||||
ws.close()
|
||||
ws = null
|
||||
@@ -110,68 +103,12 @@ export function disconnectPrompt() {
|
||||
_onCodeComplete = null
|
||||
}
|
||||
|
||||
export async function loadHistory(taskId: number) {
|
||||
const loadId = ++_historyLoadId
|
||||
historyLoading.value = true
|
||||
try {
|
||||
const convs = await Prompt.listConversations(taskId)
|
||||
console.log(
|
||||
"[loadHistory] convs:",
|
||||
convs.map((c: any) => ({
|
||||
id: c.id,
|
||||
is_active: c.is_active,
|
||||
message_count: c.message_count,
|
||||
username: c.username,
|
||||
})),
|
||||
"user.username:",
|
||||
user.username,
|
||||
)
|
||||
if (loadId !== _historyLoadId) return // navigated away, abort
|
||||
const active = convs.find(
|
||||
(c: { is_active: boolean; message_count: number; username: string }) =>
|
||||
c.is_active && c.message_count > 0 && c.username === user.username,
|
||||
)
|
||||
console.log("[loadHistory] active:", active)
|
||||
if (!active) return
|
||||
const raw: RawMessage[] = await Prompt.getMessages(active.id)
|
||||
console.log("[loadHistory] raw messages:", raw.length)
|
||||
if (loadId !== _historyLoadId) return // navigated away, abort
|
||||
// Only apply if nothing has arrived via WebSocket yet
|
||||
if (messages.value.length > 0) return
|
||||
conversationId.value = active.id
|
||||
messages.value = raw.map((m) => ({
|
||||
role: m.role as "user" | "assistant",
|
||||
content: m.content,
|
||||
code:
|
||||
m.role === "assistant"
|
||||
? { html: m.code_html, css: m.code_css, js: m.code_js }
|
||||
: undefined,
|
||||
created: m.created,
|
||||
}))
|
||||
// Apply code from last assistant message to editors
|
||||
const lastAssistant = [...messages.value]
|
||||
.reverse()
|
||||
.find((m) => m.role === "assistant" && m.code)
|
||||
if (lastAssistant?.code) {
|
||||
applyCode(lastAssistant.code)
|
||||
}
|
||||
} catch {
|
||||
// 静默失败,不影响 WebSocket 正常流程
|
||||
} finally {
|
||||
if (loadId === _historyLoadId) historyLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
export function sendPrompt(content: string) {
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN) return
|
||||
messages.value.push({ role: "user", content })
|
||||
ws.send(JSON.stringify({ type: "message", content }))
|
||||
}
|
||||
|
||||
export function newConversation() {
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN) return
|
||||
ws.send(JSON.stringify({ type: "new_conversation" }))
|
||||
}
|
||||
|
||||
function applyCode(code: {
|
||||
html: string | null
|
||||
|
||||
Reference in New Issue
Block a user