Skip to content

Commit 4eec87f

Browse files
bennyliiDeepSeek V4 Pro
andcommitted
修复 Qwen AI (chat.qwen.ai) 对话结束后未销毁的问题
将删除逻辑从 handler 内部的 onEnd 回调(事件处理期间触发,stream 提前断开则丢失)改为 transformedStream.end() 时执行,与 Kimi 修复模式一致。 Co-Authored-By: DeepSeek V4 Pro <noreply@deepseek.com>
1 parent 068876a commit 4eec87f

2 files changed

Lines changed: 14 additions & 27 deletions

File tree

src/main/proxy/adapters/qwen-ai.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,13 @@ export class QwenAiStreamHandler {
370370
private chatId: string = ''
371371
private model: string
372372
private created: number
373-
private onEnd?: (chatId: string) => void
374373
private responseId: string = ''
375374
private content: string = ''
376375
private toolCallsSent: boolean = false
377376

378-
constructor(model: string, onEnd?: (chatId: string) => void) {
377+
constructor(model: string) {
379378
this.model = model
380379
this.created = Math.floor(Date.now() / 1000)
381-
this.onEnd = onEnd
382380
}
383381

384382
setChatId(chatId: string) {
@@ -432,9 +430,6 @@ export class QwenAiStreamHandler {
432430
})}\n\n`
433431
)
434432
transStream.end('data: [DONE]\n\n')
435-
if (this.onEnd && this.chatId) {
436-
this.onEnd(this.chatId)
437-
}
438433
}
439434
}
440435

@@ -611,10 +606,6 @@ export class QwenAiStreamHandler {
611606
}
612607
transStream.write(`data: ${JSON.stringify(finalChunk)}\n\n`)
613608
transStream.end('data: [DONE]\n\n')
614-
615-
if (this.onEnd && this.chatId) {
616-
this.onEnd(this.chatId)
617-
}
618609
}
619610
}
620611
} catch (err) {
@@ -715,10 +706,6 @@ export class QwenAiStreamHandler {
715706
data.choices[0].message.reasoning_content = finalReasoning
716707
}
717708

718-
if (this.onEnd && this.chatId) {
719-
this.onEnd(this.chatId)
720-
}
721-
722709
resolveOnce(data)
723710
}
724711
} else if (phase === null && content) {

src/main/proxy/forwarder.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,22 +1006,22 @@ CRITICAL RULES:
10061006
}
10071007
}
10081008

1009-
const deleteChatCallback = shouldDeleteSession()
1010-
? async (cid: string) => {
1011-
try {
1012-
await adapter.deleteChat(cid)
1013-
} catch (err) {
1014-
console.error('[QwenAI] Failed to delete chat:', err)
1015-
}
1016-
}
1017-
: undefined
1018-
1019-
const handler = new QwenAiStreamHandler(actualModel, deleteChatCallback)
1009+
const handler = new QwenAiStreamHandler(actualModel)
10201010
handler.setChatId(chatId)
10211011

10221012
if (request.stream) {
10231013
const transformedStream = await handler.handleStream(response.data)
10241014

1015+
if (shouldDeleteSession()) {
1016+
const originalEnd = transformedStream.end.bind(transformedStream)
1017+
transformedStream.end = function(chunk?: any, encoding?: any, callback?: any) {
1018+
adapter.deleteChat(chatId).catch(err => {
1019+
console.error('[QwenAI] Failed to delete chat:', err)
1020+
})
1021+
return originalEnd(chunk, encoding, callback)
1022+
}
1023+
}
1024+
10251025
return {
10261026
success: true,
10271027
status: response.status,
@@ -1037,8 +1037,8 @@ CRITICAL RULES:
10371037

10381038
this.applyToolCallsToResponse(result, request.model, request.tools)
10391039

1040-
if (deleteChatCallback) {
1041-
await deleteChatCallback(chatId)
1040+
if (shouldDeleteSession()) {
1041+
await adapter.deleteChat(chatId)
10421042
}
10431043

10441044
return {

0 commit comments

Comments
 (0)