/** * Chat API * * 聊天相关接口 * */ import { ChatHistoryResponse, ChatHistoryResponseSchema, ChatPreviewsResponse, ChatPreviewsResponseSchema, ChatSendResponse, ChatSendResponseSchema, SendMessageRequest, TipOfferActionRequest, TipOfferActionRequestSchema, TipOfferActionResponse, TipOfferActionResponseSchema, UnlockHistoryRequest, UnlockHistoryResponse, UnlockHistoryResponseSchema, UnlockPrivateRequest, UnlockPrivateResponse, UnlockPrivateResponseSchema, } from "@/data/schemas/chat"; import { ApiPath } from "./api_path"; import { httpClient } from "./http_client"; import { ApiEnvelope, unwrap } from "./response_helper"; export class ChatApi { /** * 发送消息 */ async sendMessage( body: SendMessageRequest, options?: { signal?: AbortSignal }, ): Promise { const env = await httpClient>(ApiPath.chatSend, { method: "POST", body, ...(options?.signal ? { signal: options.signal } : {}), }); return ChatSendResponseSchema.parse(unwrap(env) as Record); } async recordTipOfferAction( body: TipOfferActionRequest, ): Promise { const env = await httpClient>( ApiPath.chatTipOfferActions, { method: "POST", body: TipOfferActionRequestSchema.parse(body), }, ); return TipOfferActionResponseSchema.parse(unwrap(env)); } /** * 获取聊天历史 */ async getHistory( characterId: string, limit = 50, offset = 0, options?: { signal?: AbortSignal }, ): Promise { const env = await httpClient>(ApiPath.chatHistory, { query: { characterId, limit, offset }, ...(options?.signal ? { signal: options.signal } : {}), }); return ChatHistoryResponseSchema.parse( unwrap(env) as Record, ); } async getPreviews( options?: { signal?: AbortSignal }, ): Promise { const env = await httpClient>(ApiPath.chatPreviews, { ...(options?.signal ? { signal: options.signal } : {}), }); return ChatPreviewsResponseSchema.parse(unwrap(env)); } /** * 解锁单条历史付费 / 私密消息 */ async unlockPrivateMessage( body: UnlockPrivateRequest, options?: { signal?: AbortSignal }, ): Promise { const env = await httpClient>( ApiPath.chatUnlockPrivate, { method: "POST", body, ...(options?.signal ? { signal: options.signal } : {}), }, ); return UnlockPrivateResponseSchema.parse( unwrap(env) as Record, ); } /** * 一键解锁历史锁定消息 */ async unlockHistory( body: UnlockHistoryRequest, options?: { signal?: AbortSignal }, ): Promise { const env = await httpClient>( ApiPath.chatUnlockHistory, { method: "POST", body, ...(options?.signal ? { signal: options.signal } : {}), }, ); return UnlockHistoryResponseSchema.parse( unwrap(env) as Record, ); } } /** * 全局单例 */ export const chatApi = new ChatApi();