add stop button
This commit is contained in:
@@ -35,9 +35,16 @@
|
|||||||
:disabled="streaming"
|
:disabled="streaming"
|
||||||
/>
|
/>
|
||||||
<n-button
|
<n-button
|
||||||
|
v-if="streaming"
|
||||||
|
type="error"
|
||||||
|
@click="stopPrompt"
|
||||||
|
>
|
||||||
|
停止
|
||||||
|
</n-button>
|
||||||
|
<n-button
|
||||||
|
v-else
|
||||||
type="primary"
|
type="primary"
|
||||||
:loading="streaming"
|
:disabled="!input.trim() || !connected"
|
||||||
:disabled="!input.trim() || streaming"
|
|
||||||
@click="send"
|
@click="send"
|
||||||
>
|
>
|
||||||
发送
|
发送
|
||||||
@@ -55,7 +62,9 @@ import {
|
|||||||
messages,
|
messages,
|
||||||
streaming,
|
streaming,
|
||||||
streamingContent,
|
streamingContent,
|
||||||
|
connected,
|
||||||
sendPrompt,
|
sendPrompt,
|
||||||
|
stopPrompt,
|
||||||
} from "../../store/prompt"
|
} from "../../store/prompt"
|
||||||
|
|
||||||
const input = ref("")
|
const input = ref("")
|
||||||
|
|||||||
@@ -27,8 +27,10 @@ export function setOnCodeComplete(fn: typeof _onCodeComplete) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ws: WebSocket | null = null
|
let ws: WebSocket | null = null
|
||||||
|
let _currentTaskId = 0
|
||||||
|
|
||||||
export function connectPrompt(taskId: number) {
|
export function connectPrompt(taskId: number) {
|
||||||
|
_currentTaskId = taskId
|
||||||
if (ws) ws.close()
|
if (ws) ws.close()
|
||||||
|
|
||||||
ws = new WebSocket(`${WS_BASE_URL}/ws/prompt/${taskId}/`)
|
ws = new WebSocket(`${WS_BASE_URL}/ws/prompt/${taskId}/`)
|
||||||
@@ -41,6 +43,8 @@ export function connectPrompt(taskId: number) {
|
|||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
|
|
||||||
if (data.type === "init") {
|
if (data.type === "init") {
|
||||||
|
streaming.value = false
|
||||||
|
streamingContent.value = ""
|
||||||
// Skip overwriting messages if HTTP preload already loaded this conversation.
|
// Skip overwriting messages if HTTP preload already loaded this conversation.
|
||||||
// If conversation_id differs (e.g. after "新对话"), always overwrite.
|
// If conversation_id differs (e.g. after "新对话"), always overwrite.
|
||||||
const alreadyLoaded = conversationId.value === data.conversation_id
|
const alreadyLoaded = conversationId.value === data.conversation_id
|
||||||
@@ -105,10 +109,27 @@ export function disconnectPrompt() {
|
|||||||
|
|
||||||
export function sendPrompt(content: string, model: string = "") {
|
export function sendPrompt(content: string, model: string = "") {
|
||||||
if (!ws || ws.readyState !== WebSocket.OPEN) return
|
if (!ws || ws.readyState !== WebSocket.OPEN) return
|
||||||
|
streaming.value = true
|
||||||
messages.value.push({ role: "user", content })
|
messages.value.push({ role: "user", content })
|
||||||
ws.send(JSON.stringify({ type: "message", content, model }))
|
ws.send(JSON.stringify({ type: "message", content, model }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function stopPrompt() {
|
||||||
|
// Remove the user message added to UI (not yet saved in DB)
|
||||||
|
if (
|
||||||
|
messages.value.length > 0 &&
|
||||||
|
messages.value[messages.value.length - 1].role === "user"
|
||||||
|
) {
|
||||||
|
messages.value.pop()
|
||||||
|
}
|
||||||
|
streaming.value = false
|
||||||
|
streamingContent.value = ""
|
||||||
|
// connectPrompt closes old WS (triggering backend disconnect cleanup) then reconnects
|
||||||
|
if (_currentTaskId) {
|
||||||
|
connectPrompt(_currentTaskId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function applyCode(code: {
|
function applyCode(code: {
|
||||||
html: string | null
|
html: string | null
|
||||||
css: string | null
|
css: string | null
|
||||||
|
|||||||
Reference in New Issue
Block a user