refactor(playground): extract completion choice handling

- move non-streaming choice application into the message streaming utilities.
- keep the chat handler focused on request orchestration and message updates.
This commit is contained in:
QuentinHsu
2026-05-29 23:12:53 +08:00
parent 47d4d74bd6
commit 76469cb944
2 changed files with 20 additions and 12 deletions
@@ -26,8 +26,8 @@ import {
updateAssistantMessageWithError,
updateLastAssistantMessage,
finalizeMessage,
updateCurrentVersionContent,
parseRequestErrorDetails,
applyChatCompletionChoice,
} from '../lib'
import type { Message, PlaygroundConfig, ParameterEnabled } from '../types'
import { useStreamRequest } from './use-stream-request'
@@ -143,16 +143,9 @@ export function useChatHandler({
}
onMessageUpdate((prev) =>
updateLastAssistantMessage(prev, (message) => ({
...finalizeMessage(
updateCurrentVersionContent(
message,
choice.message?.content || ''
),
choice.message?.reasoning_content
),
status: MESSAGE_STATUS.COMPLETE,
}))
updateLastAssistantMessage(prev, (message) =>
applyChatCompletionChoice(message, choice)
)
)
} catch (error: unknown) {
if (abortController.signal.aborted) return
@@ -17,7 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { ERROR_MESSAGES, MESSAGE_ROLES, MESSAGE_STATUS } from '../constants'
import type { Message } from '../types'
import type { ChatCompletionResponse, Message } from '../types'
import {
getCurrentVersion,
hasMessageContent,
@@ -102,6 +102,21 @@ export function finalizeMessage(
}
}
type ChatCompletionChoice = ChatCompletionResponse['choices'][number]
export function applyChatCompletionChoice(
message: Message,
choice: ChatCompletionChoice
): Message {
return {
...finalizeMessage(
updateCurrentVersionContent(message, choice.message?.content || ''),
choice.message?.reasoning_content
),
status: MESSAGE_STATUS.COMPLETE,
}
}
/**
* Sanitize messages loaded from storage.
* Converts stuck loading/streaming messages to stable state.