import type { ReminderInput, ReminderMode, ReminderResult, ReminderTopic, ReminderEvent } from './reminder.js'; export const TASK_LIFECYCLE_FACT_TYPES = [ 'task_started', 'task_progress', 'task_assigned', 'task_completed', 'task_interrupted', 'task_sabotaged', ] as const; export const TASK_REMINDER_FACT_TYPES = [ ...TASK_LIFECYCLE_FACT_TYPES, 'role_assigned', 'emergency_started', 'emergency_resolved', 'emergency_interrupted', 'meeting_briefing', ] as const; export const REMINDER_FACT_TYPES_BY_TOPIC = { tasks: TASK_REMINDER_FACT_TYPES, speech: ['wandering_speech'], } as const; export const REMINDER_EVENT_TYPE_BY_TOPIC = { tasks: 'reminder.task', speech: 'reminder.speech' } as const; export const REMINDER_FACT_TYPES = [...TASK_REMINDER_FACT_TYPES, 'wandering_speech'] as const; export const REMINDER_EVENT_TYPES = ['reminder.task', 'reminder.speech'] as const; export const REMINDER_TOPICS: readonly ReminderTopic[] = ['tasks', 'speech']; export function isReminderMode(value: unknown): value is ReminderMode { return value === 'wake' || value === 'silent' || value === 'state_only'; } export function defaultConfig(topic: ReminderTopic): Record { if (topic !== 'tasks') throw new Error('Unsupported reminder topic; countdown and ability notifications are owned by CCL.'); // removed/updated remain accepted for existing installed v2 configs; this helper does not emit them. return { started: 'state_only', progress: 'state_only', added: 'silent', completed: 'silent', interrupted: 'wake', sabotaged: 'wake', removed: 'silent', updated: 'silent' }; } export function validateDefaultConfig(topic: ReminderTopic, input: unknown): Record { if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('Reminder config must be an object.'); const defaults = defaultConfig(topic); for (const key of Object.keys(input)) if (!Object.hasOwn(defaults, key)) throw new Error('Unknown reminder option: ' + key); const value = { ...defaults, ...input }; if (!Object.values(value).every(isReminderMode)) throw new Error('Reminder mode must be wake, silent, or state_only.'); return value; } const kinds: Record = { task_started: 'started', task_progress: 'progress', task_assigned: 'added', task_completed: 'completed', task_interrupted: 'interrupted', task_sabotaged: 'sabotaged' }; const labels: Record = { started: '开始任务', progress: '任务进度变化', added: '新增任务', completed: '任务完成', interrupted: '任务中断', sabotaged: '破坏任务完成', removed: '任务不再出现在列表', updated: '任务信息变化' }; function record(value: unknown): Record { return value && typeof value === 'object' && !Array.isArray(value) ? value as Record : {}; } /** Nearby speech is a backend fact; only its Agent notification is optional. */ export function speechReminder(input: ReminderInput): ReminderResult { if (Object.keys(input.config).some(key => key !== 'receive') || (input.config.receive !== undefined && typeof input.config.receive !== 'boolean')) { throw new Error('Speech reminder config must contain only a boolean receive.'); } if (input.config.receive === false) return { events: [] }; const self = input.gameState?.you?.player; return { events: input.facts.flatMap(fact => { if (fact.type !== 'wandering_speech' || fact.replayed) return []; const actor = record(fact.payload.actor); const seat = actor.seat ?? fact.payload.actor_seat; const name = actor.name ?? fact.payload.actor_name; // GameSync includes the speaker among the frozen nearby recipients. const own = seat !== undefined && self?.seat !== undefined ? seat === self.seat : typeof name === 'string' && self?.name !== undefined && name === self.name; if (own || typeof fact.payload.text !== 'string' || !fact.payload.text.trim()) return []; return [{ type: 'reminder.speech' as const, key: `fact:${fact.eventId}`, wakeAgent: true, payload: { ...fact.payload, text: fact.payload.text, source_event_id: fact.eventId } }]; }) }; } /** Optional reminder policy; raw facts always remain available to computation plugins. */ export function defaultReminder(topic: ReminderTopic, input: ReminderInput): ReminderResult { if (topic === 'speech') return speechReminder(input); const config = validateDefaultConfig(topic, input.config); const events: ReminderEvent[] = []; if (input.boundary) return { events }; const emit = (kind: string, key: string, data: Record, eventId: string) => { const mode = config[kind]; if (mode === 'state_only') return; const { tasks: _tasks, delta: _delta, newly_assigned_task: _assigned, ...detail } = data; const name = String(detail.task_name ?? detail.name ?? ''); const actor = record(detail.actor); const seat = actor.seat ?? detail.actor_seat; const who = seat !== undefined && seat !== input.gameState?.you?.player?.seat ? `${seat}号玩家:` : ''; const room = detail.room ? `(${String(detail.room)})` : ''; events.push({ type: 'reminder.task', key, wakeAgent: mode === 'wake', payload: { ...detail, kind, source_event_id: eventId, text: `${who}${labels[kind]}:${name}${room}。` } }); }; for (const fact of input.facts) { const kind = kinds[fact.type]; if (kind) emit(kind, `fact:${fact.eventId}`, { ...fact.payload }, fact.eventId); } // Task lifecycle facts belong to this helper. State-only list changes stay // available as input; emergency lifecycle notifications belong to emergency perception. return { events }; }