()
| 7878 | // pending line says "Thinking..." and the trace is timed from here. |
| 7879 | if (meta?.withheld) { |
| 7880 | if (!reasoningStartTime) { |
| 7881 | reasoningStartTime = streamOpenedAt ?? Date.now(); |
| 7882 | streamingMessage.reasoningWithheld = true; |
| 7883 | this.updateSessionStreamingPhase(session.id, 'thinking'); |
| 7884 | streamingMessage.streamingPhase = this.getSessionStreamingState(session.id).phase; |
| 7885 | if (typingId) this.updateTypingIndicator(typingId, 'thinking'); |
| 7886 | } |
| 7887 | if (firstChunkReceived) { |
| 7888 | // Thinking again after part of the answer: the trace says so until the answer continues. |
| 7889 | reasoningEndTime = null; |
| 7890 | resumedWithheldThinking = this.resumeThinkingDisplay(streamingMessage, streamedReasoning, session.id); |
| 7891 | } |
| 7892 | return; |
| 7893 | } |
| 7894 | // Handle reasoning trace streaming |
| 7895 | if (!firstChunkReceived) { |
| 7896 | firstChunkReceived = true; |
| 7897 | this.markPendingTurnStarted(streamingMessage); |
| 7898 | reasoningStartTime = streamOpenedAt ?? Date.now(); |
| 7899 | // Clear pending flag now that we have actual content |
| 7900 | streamingMessage.streamingPending = false; |
| 7901 | streamingMessage.streamingPhase = null; |
| 7902 | streamingMessage.reasoning = reasoningChunk; |
| 7903 | streamingMessage.streamingReasoning = true; |
| 7904 | streamedReasoning = reasoningChunk; |
| 7905 | await chatDB.saveMessage(streamingMessage); |
| 7906 | // Only update UI if still viewing the same session |
| 7907 | if (this.chatArea && this.isViewingSession(session.id)) { |
| 7908 | await this.chatArea.appendMessage(streamingMessage); |
| 7909 | } |
| 7910 | } else { |
| 7911 | streamedReasoning += reasoningChunk; |
| 7912 | // Thinking after an answer segment (tools): the trace settles again at the next content. |
| 7913 | reasoningEndTime = null; |
| 7914 | streamingMessage.reasoning = streamedReasoning; |
| 7915 | streamingMessage.streamingReasoning = true; |
| 7916 | reasoningStartTime ??= streamOpenedAt ?? Date.now(); |
| 7917 | // Save reasoning frequently so session switch can restore state |
| 7918 | await chatDB.saveMessage(streamingMessage); |
| 7919 | } |
| 7920 | |
| 7921 | // Only update UI if still viewing the same session |
| 7922 | if (turnStopped()) return; |
| 7923 | if (this.chatArea && this.isViewingSession(session.id)) { |
| 7924 | this.chatArea.updateStreamingReasoning(streamingMessageId, streamedReasoning); |
| 7925 | } |
| 7926 | }, |
| 7927 | retryReasoningEnabled, |
| 7928 | retryReasoningEffort, |
| 7929 | streamingMessageId |
| 7930 | ); |
| 7931 | |
| 7932 | // Save the final message content with token data, reasoning, and citations |
| 7933 | stillWorking.dispose(); |
| 7934 | this.markPendingTurnStarted(streamingMessage); |
| 7935 | streamingMessage.content = streamedContent; |
| 7936 | if (streamingMessage.scrubber) { |
| 7937 | streamingMessage.scrubber.redactedResponse = streamedContent; |
no test coverage detected