import type { CreationWorkShelfKind } from '../custom-world-home/creationWorkShelf'; import { PLATFORM_TASK_COMPLETION_MESSAGE } from './platformDialogStateModel'; import { buildDraftCompletionDialogSource, collectDraftNoticeKeys, } from './platformDraftGenerationShelfModel'; export type PlatformHostDraftNotification = { key: string; title: string; body: string; }; export type PlatformHostDraftNotificationSender = ( notification: Pick, ) => void | Promise; export function buildPlatformHostDraftReadyNotification( kind: CreationWorkShelfKind, ids: Array, ): PlatformHostDraftNotification | null { const noticeKeys = collectDraftNoticeKeys(kind, ids); if (noticeKeys.length === 0) { return null; } const source = buildDraftCompletionDialogSource(kind, ids); return { key: `draft-ready:${kind}:${source}`, title: '生成完成', body: `${source} ${PLATFORM_TASK_COMPLETION_MESSAGE}`, }; } export function buildPlatformHostDraftFailureNotification( kind: CreationWorkShelfKind, ids: Array, errorMessage?: string | null, ): PlatformHostDraftNotification | null { const noticeKeys = collectDraftNoticeKeys(kind, ids); const normalizedErrorMessage = errorMessage?.trim(); if (noticeKeys.length === 0 || !normalizedErrorMessage) { return null; } const source = buildDraftCompletionDialogSource(kind, ids); return { key: `draft-failed:${kind}:${source}`, title: '生成失败', body: `${source} ${normalizedErrorMessage}`, }; } export function buildPlatformHostDraftNotificationResetKeys( kind: CreationWorkShelfKind, ids: Array, ) { const noticeKeys = collectDraftNoticeKeys(kind, ids); if (noticeKeys.length === 0) { return []; } const keySuffix = buildDraftCompletionDialogSource(kind, ids); return [ `draft-ready:${kind}:${keySuffix}`, `draft-failed:${kind}:${keySuffix}`, ]; } export function sendPlatformHostDraftNotificationOnce( sentKeys: Set, notification: PlatformHostDraftNotification | null, send: PlatformHostDraftNotificationSender, ) { if (!notification || sentKeys.has(notification.key)) { return false; } sentKeys.add(notification.key); void send({ title: notification.title, body: notification.body, }); return true; }