From a571867620a5545224156b47a8fb3978cc01326a Mon Sep 17 00:00:00 2001 From: chenhang Date: Wed, 24 Jun 2026 10:19:30 +0800 Subject: [PATCH] fix(chat): hide text for ai image messages --- src/stores/chat/chat-machine.helpers.ts | 39 ++++++++++++------------- src/stores/chat/chat-machine.ts | 1 + 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/stores/chat/chat-machine.helpers.ts b/src/stores/chat/chat-machine.helpers.ts index e0734857..bab25ee1 100644 --- a/src/stores/chat/chat-machine.helpers.ts +++ b/src/stores/chat/chat-machine.helpers.ts @@ -1,22 +1,3 @@ -/** - * Chat 状态机:纯函数 + 数据加载 - * - * 从 `chat-machine.ts` 抽出的"helpers"段: - * - 翻页大小常量 - * - 仓库注入(接口类型 → 单例) - * - DTO ↔ UiMessage 映射(纯函数 —— 可单测) - * - `readAndSyncHistory()` —— local → network → save network to local 3 步 - * - * 设计目标: - * - helpers 无 XState 依赖(可独立 import / 测试) - * - actors 依赖 helpers(import) - * - machine 依赖 actors,并复用 helpers 里的纯状态转换函数 - * - * 历史: - * - `readInitData` + `InitResult` 已删(被 2 个独立 helper 替换) - * - `AuthStorage` import 已删(只 `readInitData` 用过,移到 `chatInit` actor 上层调用) - */ - import type { UiMessage } from "@/data/dto/chat"; import { chatRepository } from "@/data/repositories/chat_repository"; import type { IChatRepository } from "@/data/repositories/interfaces"; @@ -61,7 +42,11 @@ export function localMessagesToUi( ): UiMessage[] { return records.map((m) => ({ ...(m.id ? { id: m.id } : {}), - content: m.content, + content: getAiMessageDisplayContent({ + content: m.content, + isFromAI: m.role === "assistant", + imageUrl: m.imageUrl, + }), isFromAI: m.role === "assistant", date: messageDateFromCreatedAt(m.createdAt), ...(m.imageUrl ? { imageUrl: m.imageUrl } : {}), @@ -77,6 +62,14 @@ function messageDateFromCreatedAt(createdAt: string): string { return todayString(parsed); } +function getAiMessageDisplayContent(input: { + content: string; + isFromAI: boolean; + imageUrl?: string | null; +}): string { + return input.isFromAI && input.imageUrl ? "" : input.content; +} + /** * ChatSendResponse → UiMessage(纯函数) * - 业务事实:后端响应就是 AI 的回复 @@ -85,7 +78,11 @@ function messageDateFromCreatedAt(createdAt: string): string { export function sendResponseToUiMessage(response: ChatSendResponse): UiMessage { return { ...(response.messageId ? { id: response.messageId } : {}), - content: response.reply, + content: getAiMessageDisplayContent({ + content: response.reply, + isFromAI: true, + imageUrl: response.imageUrl, + }), isFromAI: true, date: todayString(new Date(response.timestamp)), ...(response.imageUrl ? { imageUrl: response.imageUrl } : {}), diff --git a/src/stores/chat/chat-machine.ts b/src/stores/chat/chat-machine.ts index 9926fc3c..3304f6ab 100644 --- a/src/stores/chat/chat-machine.ts +++ b/src/stores/chat/chat-machine.ts @@ -243,6 +243,7 @@ export const chatMachine = setup({ if (last?.isFromAI && !last.imageUrl) { messages[messages.length - 1] = { ...last, + content: "", imageUrl: event.imageUrl, }; } else {