(chatKey: string)
| 1833 | } |
| 1834 | |
| 1835 | async function stopGeneration(chatKey: string): Promise<boolean> { |
| 1836 | const state = getSessionState(chatKey) |
| 1837 | if (!state || !state.isAIThinking) return false |
| 1838 | |
| 1839 | state.isAborted = true |
| 1840 | state.isAIThinking = false |
| 1841 | state.isLoadingSource = false |
| 1842 | state.currentToolStatus = null |
| 1843 | setAgentPhase(state, 'aborted') |
| 1844 | |
| 1845 | // 停止时优先定位真实仍在流式写入的会话缓冲,而不是当前页面正在查看的缓冲。 |
| 1846 | const runningBufferKey = |
| 1847 | activeTask.value?.chatKey === chatKey |
| 1848 | ? (activeTask.value.aiChatId ?? DRAFT_AI_CHAT_KEY) |
| 1849 | : getDisplayedBufferKey(state) |
| 1850 | const runningBuffer = state.aiChatBuffers[runningBufferKey] |
| 1851 | const lastMessage = runningBuffer ? runningBuffer.messages[runningBuffer.messages.length - 1] : undefined |
| 1852 | if (lastMessage && lastMessage.role === 'assistant' && lastMessage.isStreaming) { |
| 1853 | lastMessage.isStreaming = false |
| 1854 | lastMessage.content += '\n\n_(已停止生成)_' |
| 1855 | } |
| 1856 | |
| 1857 | if (state.currentAgentRequestId) { |
| 1858 | try { |
| 1859 | await useAgentStreamService().abort(state.currentAgentRequestId) |
| 1860 | } catch (error) { |
| 1861 | console.error('[AI] 中止 Agent 请求失败:', error) |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | state.currentRequestId = '' |
| 1866 | state.currentAgentRequestId = '' |
| 1867 | clearActiveTask(chatKey) |
| 1868 | return true |
| 1869 | } |
| 1870 | |
| 1871 | async function stopActiveTask(): Promise<boolean> { |
| 1872 | if (!activeTask.value) return false |
no test coverage detected