diff --git a/src/api.ts b/src/api.ts index fd02c1e..9d6ac0f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -157,14 +157,14 @@ export const Submission = { html?: string css?: string js?: string - conversationId?: string + prompt?: string }, ) { - const { conversationId, ...rest } = code + const { prompt, ...rest } = code const data = { task_id: taskId, ...rest, - conversation_id: conversationId || null, + prompt: prompt || null, } const res = await http.post("/submission/", data) return res.data @@ -250,6 +250,15 @@ export const Prompt = { ) ).data }, + + async getMessagesByUserTask( + taskId: number, + userId: number, + ): Promise { + const convs = await this.listConversations(taskId, userId) + if (!convs.length) return [] + return this.getMessages(convs[0].id) + }, } export const Helper = { diff --git a/src/components/ExternalAIPanel.vue b/src/components/ExternalAIPanel.vue index 6ac5bb7..98b1816 100644 --- a/src/components/ExternalAIPanel.vue +++ b/src/components/ExternalAIPanel.vue @@ -101,6 +101,7 @@ async function submit() { html: splitResult.value.html, css: splitResult.value.css, js: splitResult.value.js, + prompt: promptText.value.trim() || undefined, }) message.success("提交成功") promptText.value = "" diff --git a/src/components/PromptPanel.vue b/src/components/PromptPanel.vue index 29e00ca..db7fad9 100644 --- a/src/components/PromptPanel.vue +++ b/src/components/PromptPanel.vue @@ -1,10 +1,6 @@ @@ -147,7 +148,8 @@ const js = computed(() => submission.value.js) // Modal 状态 const codeModal = ref(false) const chainModal = ref(false) -const chainConversationId = ref() +const chainUserId = ref(0) +const chainTaskId = ref(0) // 展开行 const expandedKeys = ref([]) @@ -189,8 +191,9 @@ async function clearAllFlags() { query.flag = null } -function showChain(conversationId: string) { - chainConversationId.value = conversationId +function showChain(userId: number, taskId: number) { + chainUserId.value = userId + chainTaskId.value = taskId chainModal.value = true } @@ -206,7 +209,7 @@ const columns: DataTableColumn[] = [ loading: expandedLoading.has(row.id), onSelect: (id) => getSubmissionByID(id), onDelete: (r, parentId) => handleDelete(r, parentId), - "onShow-chain": (id) => showChain(id), + "onShow-chain": (userId, taskId) => showChain(userId, taskId), }), }, { diff --git a/src/store/prompt.ts b/src/store/prompt.ts index 83a74cb..8aa1e1a 100644 --- a/src/store/prompt.ts +++ b/src/store/prompt.ts @@ -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([]) export const conversationId = ref("") 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 diff --git a/src/utils/type.ts b/src/utils/type.ts index c68a1d4..1224f63 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -77,7 +77,6 @@ export interface SubmissionOut { task_title: string score: number my_score: number - conversation_id?: string flag?: FlagType zone?: "featured" | "low" | "pending" | null submit_count: number