Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(chat): keep queued follow-up answers attached to their own question
LaiYuanRongaa535c22fed1
diff --git a/frontend/src/api/chat/streame.ts b/frontend/src/api/chat/streame.tsindex 8590b4a0b..998e8af46 100644--- a/frontend/src/api/chat/streame.ts+++ b/frontend/src/api/chat/streame.ts@@ -216,7 +216,11 @@ export function useStream() {},onclose: () => {- stopStream();+ // A queued follow-up attaches via continue-stream while the previous+ // POST may still be open waiting for the session title. Both share one+ // controller, so only the current stream may stop it; otherwise the+ // old connection closing cuts the follow-up off mid-answer.+ if (myGeneration === streamGeneration) stopStream();},});@@ -234,6 +238,9 @@ export function useStream() {reloginMessage: i18n.global.t('error.pleaseRelogin'),});} catch (err) {+ // A superseded stream failing must not surface as the current error or+ // abort the stream that replaced it (see onclose).+ if (myGeneration !== streamGeneration) returnerror.value = err instanceof Error ? err.message : String(err)stopStream()}diff --git a/frontend/src/api/chat/streameOverlap.test.ts b/frontend/src/api/chat/streameOverlap.test.tsnew file mode 100644index 000000000..d5782c4dc--- /dev/null+++ b/frontend/src/api/chat/streameOverlap.test.ts@@ -0,0 +1,120 @@+// A queued follow-up attaches via continue-stream while the previous POST may+// still be open (it waits up to 3s for the session title). An older stream+// closing or failing must not cut off the stream that replaced it.+import assert from 'node:assert/strict'+import { after, before, test } from 'node:test'+import { fileURLToPath } from 'node:url'+import { createServer, type ViteDevServer } from 'vite'+import { createSSRApp } from 'vue'+import { renderToString } from 'vue/server-renderer'++type Call = {+ url: string+ options: { signal: AbortSignal, onmessage: (ev: { data: string }) => void, onclose: () => void }+ resolve: () => void+ reject: (err: unknown) => void+}++let server: ViteDevServer+let useStream: typeof import('./streame').useStream+let transport: { calls: Call[] }+const originalStorage = Object.getOwnPropertyDescriptor(globalThis, 'localStorage')+before(async () => {+ Object.defineProperty(globalThis, 'localStorage', { configurable: true, value: {+ getItem: (key: string) => key === 'weknora_token' ? 'test-token' : null,+ } })+ // Each request stays pending until the test closes or fails it.+ const mocks: Record<string, string> = {+ '@microsoft/fetch-event-source': `export const calls = [];+ export function fetchEventSource(url, options) {+ return new Promise((resolve, reject) => { calls.push({ url, options, resolve, reject }) })+ }`,+ '@/utils/index': `export const generateRandomString = () => 'test-request';`,+ '@/i18n': `export default { global: { t: key => key, locale: { value: 'zh-CN' } } };`,+ }+ server = await createServer({+ configFile: false,+ plugins: [{ name: 'stream-overlap-test', enforce: 'pre',+ resolveId(id) { if (id.startsWith('\0mock:')) return id },+ load(id) { if (id.startsWith('\0mock:')) return mocks[id.slice(6)] },+ }],+ optimizeDeps: { noDiscovery: true, entries: [] },+ resolve: { alias: [+ ...Object.keys(mocks).map(find => ({ find, replacement: '\0mock:' + find })),+ { find: '@', replacement: fileURLToPath(new URL('../../', import.meta.url)) },+ ] },+ // No file watching needed in tests (also avoids ENOSPC on hosts with few inotify watches).+ server: { middlewareMode: true, hmr: false, watch: null }, appType: 'custom',+ })+ ;({ useStream } = await server.ssrLoadModule('/src/api/chat/streame.ts'))+ transport = await server.ssrLoadModule('\0mock:@microsoft/fetch-event-source') as typeof transport+})+after(async () => {+ await server?.close()+ if (originalStorage) Object.defineProperty(globalThis, 'localStorage', originalStorage)+ else Reflect.deleteProperty(globalThis, 'localStorage')+})++async function nextCall(count: number): Promise<Call> {+ for (let i = 0; i < 100 && transport.calls.length < count; i++) await new Promise(r => setTimeout(r, 5))+ assert.equal(transport.calls.length, count, 'fetchEventSource was not called')+ return transport.calls[count - 1]+}++async function overlapping() {+ let stream!: ReturnType<typeof useStream>+ await renderToString(createSSRApp({ setup() { stream = useStream(); return () => null } }))+ const chunks: unknown[] = []+ stream.onChunk(data => chunks.push(data))+ const base = transport.calls.length+ // First question: the POST stays open after its answer, waiting for the title.+ const first = stream.startStream({ session_id: 's', query: 'q1', method: 'POST', url: '/api/v1/agent-chat' })+ const a = await nextCall(base + 1)+ // The queued follow-up attaches while that POST is still open.+ const second = stream.startStream({ session_id: 's', query: 'assistant-2', method: 'GET', url: '/api/v1/sessions/continue-stream' })+ const b = await nextCall(base + 2)+ assert.match(b.url, /continue-stream\/s\?message_id=assistant-2$/)+ return { stream, chunks, a, b, first, second }+}++test('an older stream closing does not cut the newer follow-up stream', async () => {+ const { stream, chunks, a, b, first, second } = await overlapping()+ a.options.onclose()+ a.resolve()+ await first+ assert.equal(b.options.signal.aborted, false, 'old onclose aborted the new stream')+ assert.equal(stream.isStreaming.value, true)+ b.options.onmessage({ data: JSON.stringify({ response_type: 'answer', id: 'r2', content: 'hi' }) })+ assert.equal(chunks.length, 1, 'new stream events were dropped after the old one closed')+ // The current stream closing still stops as before.+ b.options.onclose()+ b.resolve()+ await second+ assert.equal(stream.isStreaming.value, false)+ assert.equal(b.options.signal.aborted, true)+ assert.equal(stream.error.value, null)+})++test('an older stream failing does not report an error or cut the newer stream', async () => {+ const { stream, chunks, a, b, first, second } = await overlapping()+ a.reject(new Error('old connection reset'))+ await first+ assert.equal(stream.error.value, null, 'old stream error leaked into the current one')+ assert.equal(b.options.signal.aborted, false)+ b.options.onmessage({ data: JSON.stringify({ response_type: 'complete', id: 'r2' }) })+ assert.equal(chunks.length, 1)+ b.options.onclose()+ b.resolve()+ await second+})++test('the current stream failing still reports the error and stops', async () => {+ const { stream, a, b, first, second } = await overlapping()+ b.reject(new Error('current connection reset'))+ await second+ assert.match(String(stream.error.value), /current connection reset/)+ assert.equal(stream.isStreaming.value, false)+ assert.equal(b.options.signal.aborted, true)+ a.resolve()+ await first+})diff --git a/frontend/src/composables/agentQueryBinding.test.mjs b/frontend/src/composables/agentQueryBinding.test.mjsnew file mode 100644index 000000000..a7d0a6f5f--- /dev/null+++ b/frontend/src/composables/agentQueryBinding.test.mjs@@ -0,0 +1,51 @@+// The next turn's agent_query must not take over an earlier turn's unfinished row.+import assert from 'node:assert/strict'+import { readFileSync } from 'node:fs'+import test from 'node:test'+import vm from 'node:vm'+import ts from 'typescript'+import { resetSteerTurnForReplay } from '../utils/steerStreamFork.ts'++const source = readFileSync(new URL('./useChatStreamHandler.ts', import.meta.url), 'utf8')+const start = source.indexOf("if (data.response_type === 'agent_query')")+const block = source.slice(start, source.indexOf('const isAgentOnlyResponse', start))++function agentQuery(messagesList) {+ const currentAssistantMessageId = { value: '' }+ const process = vm.runInNewContext(ts.transpile(`(data) => { ${block} }`), {+ messagesList, replaySegments: new Map(), resetSteerTurnForReplay, currentAssistantMessageId,+ getTrailingIncompleteAssistant: () => [...messagesList].reverse().find(m => m.role === 'assistant' && !m.is_completed),+ findLastMessage: fn => [...messagesList].reverse().find(fn),+ isAgentStreamSession: () => true, loading: { value: true },+ emitMessageCreated() {}, scrollToBottom() {}, log() {},+ ensureAgentMessageShell() {}, bindServerTurnTimestamps() {}, onAgentQuery() {},+ })+ return { process, currentAssistantMessageId }+}++test('a follow-up agent_query does not take over the previous question\'s unfinished row', () => {+ // The second turn's stream was cut, leaving its row incomplete; the third turn starts.+ const q2 = { id: 'a2', assistant_message_id: 'a2', request_id: 'r2', role: 'assistant', content: 'I am an assistant', is_completed: false }+ const messagesList = [+ { id: 'u2', role: 'user', request_id: 'r2', content: 'Who are you?' }, q2,+ { id: 'u3', role: 'user', request_id: 'r3', content: 'What can you do?' },+ ]+ const { process, currentAssistantMessageId } = agentQuery(messagesList)+ process({ response_type: 'agent_query', id: 'r3', assistant_message_id: 'a3' })+ assert.equal(q2.request_id, 'r2', 'previous row was re-labelled with the new request')+ assert.equal(q2.id, 'a2')+ assert.equal(q2.content, 'I am an assistant')+ assert.deepEqual(messagesList.map(m => m.id), ['u2', 'a2', 'u3', 'a3'])+ assert.equal(messagesList[3].request_id, 'r3')+ assert.equal(currentAssistantMessageId.value, 'a3')+})++test('a placeholder row without a request id is still adopted by agent_query', () => {+ const placeholder = { id: 'tmp', role: 'assistant', content: '', is_completed: false }+ const messagesList = [{ id: 'u1', role: 'user', content: 'q' }, placeholder]+ const { process } = agentQuery(messagesList)+ process({ response_type: 'agent_query', id: 'r1', assistant_message_id: 'a1' })+ assert.equal(placeholder.request_id, 'r1')+ assert.equal(placeholder.id, 'a1')+ assert.equal(messagesList.length, 2)+})diff --git a/frontend/src/composables/useChatStreamHandler.ts b/frontend/src/composables/useChatStreamHandler.tsindex aa36f3168..d5c6e1840 100644--- a/frontend/src/composables/useChatStreamHandler.ts+++ b/frontend/src/composables/useChatStreamHandler.ts@@ -1117,7 +1117,12 @@ export function useChatStreamHandler(options: UseChatStreamHandlerOptions) {if (replay) replaySegments.set(String(data.id), replay)if (data.id) {const earlyMsg = getTrailingIncompleteAssistant()- if (earlyMsg) earlyMsg.request_id = data.id+ // Only adopt a placeholder that has no request yet (or already this+ // one). An earlier turn whose stream was cut stays incomplete;+ // relabelling it would render this turn's answer in that earlier slot.+ if (earlyMsg && (!earlyMsg.request_id || earlyMsg.request_id === data.id)) {+ earlyMsg.request_id = data.id+ }}if (data.assistant_message_id) {currentAssistantMessageId.value = data.assistant_message_id as string