refactor(chat): move WebSocket lifecycle into chat machine via fromCallback actor

- chat-screen no longer manages WebSocket directly; it now dispatches three
  auth lifecycle events (ChatGuestLogin, ChatNonGuestLogin, ChatLogout)
  derived from auth.loginStatus
- chat-machine internally owns the WebSocket long-lived actor via
  fromCallback, completing the previously deferred migration scope
- removed ChatWebSocket import, getWsToken helper, and direct WS
  effect from chat-screen; renamed dispatchInitialLoad to
  dispatchAuthLifecycle to reflect the new responsibility
- decouples auth from chat machine via event-driven boundary: machine
  stays unaware of loginStatus, guest semantics live at the dispatch
  layer (guest → skip WS, non-guest → connect with token)
This commit is contained in:
2026-06-11 18:57:11 +08:00
parent 4b356f58f2
commit d55694a73e
3 changed files with 310 additions and 185 deletions
+17
View File
@@ -6,12 +6,29 @@
* 历史:原本有 `ChatAuthStatusChanged`chat 机器刷新 isGuest)—— 已**删**。
* 鉴权状态变化(loginStatus)由 chat-screen 通过 `useAuthState()` 订阅,
* chat 机器**不**感知鉴权。
*
* 鉴权生命周期事件(**本轮新增**):
* - `ChatGuestLogin`:游客进入 /chat —— 拉本地消息 + 初始化配额(**不**连 WS)
* - `ChatNonGuestLogin`:非游客进入 /chat —— 拉服务器端首屏 + **连** WStoken 在 payload
* - `ChatLogout`:登出 / 切换用户 —— 断 WS + 清消息
*
* 设计:**所有**WS / 历史 / 配额相关副作用**全部**通过派发事件给 chat 机器处理,
* chat-screen **不**直接创建 ChatWebSocket / 不读 AuthStorage(除 token 取值)。
*/
export type ChatEvent =
// 内部 UI 事件
| { type: "ChatInit" }
| { type: "ChatScreenVisible" }
| { type: "ChatScreenInvisible" }
// 鉴权生命周期(**用户**显式触发登录后由 chat-screen 派)
| { type: "ChatGuestLogin" }
| { type: "ChatNonGuestLogin"; token: string }
| { type: "ChatLogout" }
// 业务事件
| { type: "ChatSendMessage"; content: string }
| { type: "ChatSendImage"; imageBase64: string }
| { type: "ChatLoadMoreHistory" }
// WebSocket / AI 推句(**由** chat 机器内部 actor 派回 —— "机器**自**驱动"
| {
type: "ChatAISentenceReceived";
index: number;