import { definePluginStore, type Token } from 'zhin.js'; import type { GroupSuiteMemoryDb } from './memory-store.js'; export interface PendingStatsIncrement { user_id: string; user_name: string; group_id: string; date: string; count: number; recorded: number; flushed: number; } /** All mutable state owned by one group-suite plugin instance. */ export interface GroupSuiteRuntime { readonly db: GroupSuiteMemoryDb; readonly keywords: Map>; readonly teachCooldowns: Map; readonly statsBuffer: Map; /** Serialized finite batches shared by admitted queries and scheduler calls. */ statsFlush?: Promise; readonly checkinChains: Map>; } export const groupSuiteRuntimeStore = definePluginStore( 'zhin.group-suite.runtime', 'Owner-scoped Group Suite database and ephemeral state', ); export const groupSuiteRuntimeToken = groupSuiteRuntimeStore; export function createGroupSuiteRuntime(db: GroupSuiteMemoryDb): GroupSuiteRuntime { return { db, keywords: new Map(), teachCooldowns: new Map(), statsBuffer: new Map(), checkinChains: new Map(), }; } export function resolveGroupSuiteRuntime(context: { readonly owner?: unknown; use(token: Token): T; }): GroupSuiteRuntime { return groupSuiteRuntimeStore.use(context); }