Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(plugins): add group auto-translation extension plugin
dallascyclist401eb3c6b033
diff --git a/src/plugins/extensions/extensions.module.ts b/src/plugins/extensions/extensions.module.tsindex 29651cd51..2539eec0b 100644--- a/src/plugins/extensions/extensions.module.ts+++ b/src/plugins/extensions/extensions.module.ts@@ -1,6 +1,7 @@import { Injectable, Module, OnModuleInit } from '@nestjs/common';import { PluginLoaderService, PluginManifest, PluginType } from '../../core/plugins';import { AutoReplyPlugin } from './auto-reply';+import { TranslationPlugin } from './translation';import { createLogger } from '../../common/services/logger.service';/**@@ -28,6 +29,21 @@ export class ExtensionsRegistrar implements OnModuleInit {this.pluginLoader.registerBuiltInPlugin(autoReplyManifest, new AutoReplyPlugin());this.logger.log('Auto-reply reference plugin registered (disabled)');++ const translationManifest: PluginManifest = {+ id: 'translation',+ name: 'Group Auto-Translation',+ version: '1.0.0',+ type: PluginType.EXTENSION,+ description:+ "Auto-translates group messages between participants' languages via LibreTranslate. Configure in-group with /tr commands. Disabled by default.",+ main: 'index.ts',+ permissions: ['messages:send'],+ sessions: ['*'],+ };++ this.pluginLoader.registerBuiltInPlugin(translationManifest, new TranslationPlugin());+ this.logger.log('Translation plugin registered (disabled)');}}diff --git a/src/plugins/extensions/translation/core/command.parser.spec.ts b/src/plugins/extensions/translation/core/command.parser.spec.tsnew file mode 100644index 000000000..138a125ae--- /dev/null+++ b/src/plugins/extensions/translation/core/command.parser.spec.ts@@ -0,0 +1,43 @@+// src/modules/translation/core/command.parser.spec.ts+import { parseCommand } from './command.parser';++describe('parseCommand', () => {+ it('returns null for non-prefixed text', () => {+ expect(parseCommand('hello world', '/tr')).toBeNull();+ });++ it('parses bare commands', () => {+ expect(parseCommand('/tr on', '/tr')).toEqual({ name: 'on' });+ expect(parseCommand('/tr help', '/tr')).toEqual({ name: 'help' });+ });++ it('accepts the /translate alias and is case-insensitive on the verb', () => {+ expect(parseCommand('/translate OFF', '/tr')).toEqual({ name: 'off' });+ });++ it('parses setlang with default me target', () => {+ expect(parseCommand('/tr setlang es', '/tr')).toEqual({+ name: 'setlang',+ lang: 'es',+ target: { kind: 'me' },+ });+ });++ it('parses a number target', () => {+ expect(parseCommand('/tr grant 14155551212', '/tr')).toEqual({+ name: 'grant',+ target: { kind: 'number', number: '14155551212' },+ });+ });++ it('parses a mention target', () => {+ expect(parseCommand('/tr ignore @someone', '/tr')).toEqual({+ name: 'ignore',+ target: { kind: 'mention' },+ });+ });++ it('returns null for an unknown verb', () => {+ expect(parseCommand('/tr frobnicate', '/tr')).toBeNull();+ });+});diff --git a/src/plugins/extensions/translation/core/command.parser.ts b/src/plugins/extensions/translation/core/command.parser.tsnew file mode 100644index 000000000..69f944966--- /dev/null+++ b/src/plugins/extensions/translation/core/command.parser.ts@@ -0,0 +1,64 @@+// src/modules/translation/core/command.parser.ts+import { ParsedCommand, CommandName, CommandTarget } from './ports';++const COMMANDS: ReadonlySet<string> = new Set<CommandName>([+ 'help',+ 'status',+ 'on',+ 'off',+ 'setlang',+ 'auto',+ 'ignore',+ 'unignore',+ 'grant',+ 'revoke',+]);++const NEEDS_TARGET: ReadonlySet<string> = new Set(['setlang', 'auto', 'ignore', 'unignore', 'grant', 'revoke']);++/**+ * Parse a chat message into a control command, or null if it isn't one.+ * Accepts the configured prefix and the `/translate` alias.+ */+export function parseCommand(body: string, prefix: string): ParsedCommand | null {+ const trimmed = body.trim();+ const lower = trimmed.toLowerCase();+ // Check the '/translate' alias BEFORE the configured prefix: a short prefix like+ // '/tr' is a leading substring of '/translate', so testing the prefix first would+ // match '/translate' against '/tr' and strip only two chars. Alias-first avoids that.+ const matched = lower.startsWith('/translate')+ ? '/translate'+ : lower.startsWith(prefix.toLowerCase())+ ? prefix+ : null;+ if (!matched) return null;++ const rest = trimmed.slice(matched.length).trim();+ if (!rest) return null;++ const tokens = rest.split(/\s+/);+ const verb = tokens[0].toLowerCase();+ if (!COMMANDS.has(verb)) return null;++ const name = verb as CommandName;+ const args = tokens.slice(1);++ if (name === 'setlang') {+ const lang = args[0]?.toLowerCase();+ if (!lang) return null;+ return { name, lang, target: parseTarget(args.slice(1)) };+ }++ if (NEEDS_TARGET.has(name)) {+ return { name, target: parseTarget(args) };+ }++ return { name };+}++function parseTarget(args: string[]): CommandTarget {+ const raw = args[0];+ if (!raw || raw.toLowerCase() === 'me') return { kind: 'me' };+ if (raw.startsWith('@')) return { kind: 'mention' };+ return { kind: 'number', number: raw.replace(/[^0-9]/g, '') };+}diff --git a/src/plugins/extensions/translation/core/ports.ts b/src/plugins/extensions/translation/core/ports.tsnew file mode 100644index 000000000..44800ca8e--- /dev/null+++ b/src/plugins/extensions/translation/core/ports.ts@@ -0,0 +1,82 @@+// src/modules/translation/core/ports.ts+// Framework-agnostic contracts for the translation core. NO NestJS/TypeORM/engine imports.++export interface DetectResult {+ lang: string; // ISO 639-1+ confidence: number; // 0..1+}++export interface Translation {+ lang: string;+ text: string;+}++export interface ParticipantState {+ lang: string | null; // null = not learned yet+ source: 'learned' | 'pinned';+ enabled: boolean;+ samples: number;+ /** Candidate language awaiting a 2nd consecutive detection before a learned switch. */+ pendingLang?: string;+ updatedAt: string;+}++export type ParticipantMap = Record<string, ParticipantState>; // key = author WID++export interface GroupState {+ sessionId: string;+ chatId: string;+ active: boolean;+ participants: ParticipantMap;+ delegatedControllers: string[];+ announced: boolean;+}++export interface InboundMessage {+ id: string;+ chatId: string;+ body: string;+ author: string; // sender WID (group participant)+ isGroup: boolean;+ fromMe: boolean;+ mentionedIds: string[];+ pushName?: string;+}++export type CommandName =+ | 'help'+ | 'status'+ | 'on'+ | 'off'+ | 'setlang'+ | 'auto'+ | 'ignore'+ | 'unignore'+ | 'grant'+ | 'revoke';++export type CommandTarget = { kind: 'me' } | { kind: 'mention' } | { kind: 'number'; number: string };++export interface ParsedCommand {+ name: CommandName;+ lang?: string; // setlang only+ target?: CommandTarget; // setlang/auto/ignore/unignore/grant/revoke+}++export interface Translator {+ detect(text: string): Promise<DetectResult>;+ translate(text: string, source: string, target: string): Promise<string>;+ languages(): Promise<string[]>;+ isHealthy(): boolean;+}++export interface ConfigStore {+ load(sessionId: string, chatId: string): Promise<GroupState>;+ save(state: GroupState): Promise<void>;+}++export interface ChatGateway {+ sendText(sessionId: string, chatId: string, text: string): Promise<void>;+ sendCombinedReply(sessionId: string, chatId: string, quotedMessageId: string, text: string): Promise<void>;+ getGroupAdmins(sessionId: string, chatId: string): Promise<string[]>;+}diff --git a/src/plugins/extensions/translation/core/reply.formatter.spec.ts b/src/plugins/extensions/translation/core/reply.formatter.spec.tsnew file mode 100644index 000000000..beb3ffc1c--- /dev/null+++ b/src/plugins/extensions/translation/core/reply.formatter.spec.ts@@ -0,0 +1,36 @@+// src/modules/translation/core/reply.formatter.spec.ts+import { formatCombinedReply, buildHelpText, formatStatus } from './reply.formatter';+import { GroupState } from './ports';++describe('reply.formatter', () => {+ it('formats one line per translation with an uppercased code label', () => {+ const out = formatCombinedReply([+ { lang: 'es', text: 'Hola' },+ { lang: 'fr', text: 'Bonjour' },+ ]);+ expect(out).toContain('Hola');+ expect(out).toContain('Bonjour');+ expect(out.split('\n')).toHaveLength(2);+ expect(out).toMatch(/ES/);+ });++ it('buildHelpText lists key commands with the active prefix', () => {+ const help = buildHelpText('/tr');+ expect(help).toContain('/tr on');+ expect(help).toContain('/tr setlang');+ });++ it('formatStatus reports active state and participants', () => {+ const state: GroupState = {+ sessionId: 's',+ chatId: '[email protected]',+ active: true,+ participants: { '[email protected]': { lang: 'en', source: 'pinned', enabled: true, samples: 3, updatedAt: 'x' } },+ delegatedControllers: [],+ announced: true,+ };+ const out = formatStatus(state, true);+ expect(out).toMatch(/active/i);+ expect(out).toContain('en');+ });+});diff --git a/src/plugins/extensions/translation/core/reply.formatter.ts b/src/plugins/extensions/translation/core/reply.formatter.tsnew file mode 100644index 000000000..4fcac07ee--- /dev/null+++ b/src/plugins/extensions/translation/core/reply.formatter.ts@@ -0,0 +1,60 @@+// src/modules/translation/core/reply.formatter.ts+import { Translation, GroupState } from './ports';++const FLAGS: Record<string, string> = {+ en: '๐ฌ๐ง',+ es: '๐ช๐ธ',+ fr: '๐ซ๐ท',+ de: '๐ฉ๐ช',+ pt: '๐ต๐น',+ it: '๐ฎ๐น',+ nl: '๐ณ๐ฑ',+ ru: '๐ท๐บ',+ ar: '๐ธ๐ฆ',+ zh: '๐จ๐ณ',+ ja: '๐ฏ๐ต',+};++function label(lang: string): string {+ const flag = FLAGS[lang];+ return flag ? `${flag} ${lang.toUpperCase()}` : lang.toUpperCase();+}++export function formatCombinedReply(translations: Translation[]): string {+ return translations.map(t => `${label(t.lang)}: ${t.text}`).join('\n');+}++export function buildHelpText(prefix: string): string {+ return [+ '๐ Translation bot. I am OFF in this group until an admin runs `' + prefix + ' on`.',+ 'Commands:',+ `${prefix} on / ${prefix} off โ enable/disable translation here`,+ `${prefix} setlang <code> [me|@user|number] โ pin a language (default: you)`,+ `${prefix} auto [me|@user|number] โ go back to auto-detect`,+ `${prefix} ignore <@user|number> / ${prefix} unignore <@user|number>`,+ `${prefix} grant <@user|number> / ${prefix} revoke <@user|number> โ delegate control (admins)`,+ `${prefix} status โ show settings`,+ `${prefix} help โ this message`,+ ].join('\n');+}++export function formatStatus(state: GroupState, translatorHealthy: boolean): string {+ const lines: string[] = [];+ lines.push(`Translation: ${state.active ? 'ACTIVE' : 'inactive'}`);+ lines.push(`Translator: ${translatorHealthy ? 'ok' : 'unreachable'}`);+ const entries = Object.entries(state.participants);+ if (entries.length === 0) {+ lines.push('No participants learned yet.');+ } else {+ lines.push('Participants:');+ for (const [wid, p] of entries) {+ const lang = p.lang ?? 'unknown';+ const flags = `${p.source}${p.enabled ? '' : ', ignored'}`;+ lines.push(`โข ${wid}: ${lang} (${flags})`);+ }+ }+ if (state.delegatedControllers.length > 0) {+ lines.push(`Delegated controllers: ${state.delegatedControllers.join(', ')}`);+ }+ return lines.join('\n');+}diff --git a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts b/src/plugins/extensions/translation/core/translation.coordinator.spec.tsnew file mode 100644index 000000000..9800a1049--- /dev/null+++ b/src/plugins/extensions/translation/core/translation.coordinator.spec.ts@@ -0,0 +1,167 @@+// src/modules/translation/core/translation.coordinator.spec.ts+import { TranslationCoordinator, CoordinatorOptions } from './translation.coordinator';+import { ChatGateway, ConfigStore, GroupState, InboundMessage, Translator } from './ports';++const OPTS: CoordinatorOptions = { prefix: '/tr', minLength: 2, maxLength: 2000, denyReply: false };++function freshState(over: Partial<GroupState> = {}): GroupState {+ return {+ sessionId: 's',+ chatId: '[email protected]',+ active: false,+ participants: {},+ delegatedControllers: [],+ announced: false,+ ...over,+ };+}++function makeDeps(state: GroupState) {+ const saved: GroupState[] = [];+ const load = jest.fn().mockResolvedValue(state);+ const save = jest.fn().mockImplementation((s: GroupState) => {+ saved.push(JSON.parse(JSON.stringify(s)) as GroupState);+ return Promise.resolve();+ });+ const sendText = jest.fn().mockResolvedValue(undefined);+ const sendCombinedReply = jest.fn().mockResolvedValue(undefined);+ const getGroupAdmins = jest.fn().mockResolvedValue([]);+ const detect = jest.fn();+ const translate = jest.fn();+ const languages = jest.fn().mockResolvedValue(['en', 'es', 'fr']);+ const isHealthy = jest.fn().mockReturnValue(true);++ const store: ConfigStore = { load, save };+ const gateway: ChatGateway = { sendText, sendCombinedReply, getGroupAdmins };+ const translator: Translator = { detect, translate, languages, isHealthy };++ return {+ store,+ gateway,+ translator,+ saved,+ mocks: { load, save, sendText, sendCombinedReply, getGroupAdmins, detect, translate, languages, isHealthy },+ };+}++function msg(over: Partial<InboundMessage> = {}): InboundMessage {+ return {+ id: 'M1',+ chatId: '[email protected]',+ body: 'hello',+ author: '[email protected]',+ isGroup: true,+ fromMe: false,+ mentionedIds: [],+ ...over,+ };+}++describe('TranslationCoordinator', () => {+ it('ignores non-group and fromMe messages', async () => {+ const { store, gateway, translator, mocks } = makeDeps(freshState());+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ expect(await c.handleMessage('s', msg({ isGroup: false }))).toEqual({ swallow: false });+ expect(await c.handleMessage('s', msg({ fromMe: true }))).toEqual({ swallow: false });+ expect(mocks.sendText).not.toHaveBeenCalled();+ });++ it('announces once on first contact then stays dormant', async () => {+ const { store, gateway, translator, mocks } = makeDeps(freshState());+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ await c.handleMessage('s', msg());+ expect(mocks.sendText).toHaveBeenCalledTimes(1);+ expect(mocks.save).toHaveBeenCalled();+ });++ it('activates only for an admin', async () => {+ const state = freshState({ announced: true });+ const { store, gateway, translator, saved, mocks } = makeDeps(state);+ mocks.getGroupAdmins.mockResolvedValue(['[email protected]']);+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ const res = await c.handleMessage('s', msg({ body: '/tr on' }));+ expect(res).toEqual({ swallow: true });+ expect(saved.at(-1)?.active).toBe(true);+ });++ it('rejects activation from a non-admin (silent by default)', async () => {+ const state = freshState({ announced: true });+ const { store, gateway, translator, saved, mocks } = makeDeps(state);+ mocks.getGroupAdmins.mockResolvedValue(['[email protected]']);+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ const res = await c.handleMessage('s', msg({ body: '/tr on' }));+ expect(res).toEqual({ swallow: true });+ expect(saved.at(-1)?.active ?? false).toBe(false);+ });++ it('translates an active-group message into other participants languages (skipping the source)', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ '[email protected]': { lang: 'en', source: 'learned', enabled: true, samples: 2, updatedAt: 'x' },+ '[email protected]': { lang: 'es', source: 'learned', enabled: true, samples: 2, updatedAt: 'x' },+ },+ });+ const { store, gateway, translator, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'en', confidence: 0.99 });+ mocks.translate.mockResolvedValue('Hola');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ const res = await c.handleMessage('s', msg({ author: '[email protected]', body: 'Hello' }));+ expect(res).toEqual({ swallow: false });+ expect(mocks.translate).toHaveBeenCalledWith('Hello', 'en', 'es');+ expect(mocks.sendCombinedReply).toHaveBeenCalledWith('s', '[email protected]', 'M1', expect.stringContaining('Hola'));+ });++ it('falls back to the sender language and never translates into the source when detection misfires', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ '[email protected]': { lang: 'en', source: 'learned', enabled: true, samples: 3, updatedAt: 'x' },+ '[email protected]': { lang: 'es', source: 'pinned', enabled: true, samples: 3, updatedAt: 'x' },+ },+ });+ const { store, gateway, translator, mocks } = makeDeps(state);+ // Detection misfires on colloquial Spanish, returning 'gl' โ a language the group does not use.+ mocks.detect.mockResolvedValue({ lang: 'gl', confidence: 0.5 });+ mocks.translate.mockResolvedValue('Let me know');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ await c.handleMessage('s', msg({ author: '[email protected]', body: 'Haber dime que debo darte' }));+ // Effective source falls back to the sender's known 'es'; 'en' is the only target.+ expect(mocks.translate).toHaveBeenCalledTimes(1);+ expect(mocks.translate).toHaveBeenCalledWith('Haber dime que debo darte', 'es', 'en');+ // Must never translate a message into the sender's own language.+ expect(mocks.translate).not.toHaveBeenCalledWith(expect.anything(), expect.anything(), 'es');+ });++ it('learns a sender language only after a 2-message debounce', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ '[email protected]': { lang: 'en', source: 'learned', enabled: true, samples: 5, updatedAt: 'x' },+ '[email protected]': { lang: 'es', source: 'learned', enabled: true, samples: 2, updatedAt: 'x' },+ },+ });+ const { store, gateway, translator, saved, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'fr', confidence: 0.99 });+ mocks.translate.mockResolvedValue('x');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ // First foreign detection: lang stays 'en'+ await c.handleMessage('s', msg({ author: '[email protected]', body: 'Bonjour' }));+ expect(saved.at(-1)?.participants['[email protected]'].lang).toBe('en');+ // Second consecutive foreign detection: switches to 'fr'+ await c.handleMessage('s', msg({ author: '[email protected]', body: 'Salut' }));+ expect(saved.at(-1)?.participants['[email protected]'].lang).toBe('fr');+ });++ it('skips trivial messages below minLength', async () => {+ const state = freshState({ announced: true, active: true });+ const { store, gateway, translator, mocks } = makeDeps(state);+ const c = new TranslationCoordinator(translator, store, gateway, OPTS);+ await c.handleMessage('s', msg({ body: '.' }));+ expect(mocks.detect).not.toHaveBeenCalled();+ expect(mocks.sendCombinedReply).not.toHaveBeenCalled();+ });+});diff --git a/src/plugins/extensions/translation/core/translation.coordinator.ts b/src/plugins/extensions/translation/core/translation.coordinator.tsnew file mode 100644index 000000000..d831feeca--- /dev/null+++ b/src/plugins/extensions/translation/core/translation.coordinator.ts@@ -0,0 +1,287 @@+// src/modules/translation/core/translation.coordinator.ts+import {+ ChatGateway,+ ConfigStore,+ GroupState,+ InboundMessage,+ ParsedCommand,+ ParticipantState,+ Translation,+ Translator,+ CommandTarget,+} from './ports';+import { parseCommand } from './command.parser';+import { buildHelpText, formatCombinedReply, formatStatus } from './reply.formatter';++export interface CoordinatorOptions {+ prefix: string;+ minLength: number;+ maxLength: number;+ denyReply: boolean;+}++const URL_OR_EMOJI_ONLY = /^(?:\s|\p{Emoji}|https?:\/\/\S+)+$/u;++/**+ * Compare two WhatsApp IDs tolerantly: exact match, or same user part ignoring+ * an `@domain` and any `:device` suffix (e.g. `[email protected]` === `123:[email protected]`).+ * Note: this does NOT bridge the LID (`@lid`) and phone (`@c.us`) namespaces โ+ * those have different user numbers (see spec ยง16).+ */+function widEquals(a: string, b: string): boolean {+ if (a === b) return true;+ const userPart = (w: string): string => w.split('@')[0].split(':')[0];+ return userPart(a) === userPart(b);+}++export class TranslationCoordinator {+ constructor(+ private readonly translator: Translator,+ private readonly store: ConfigStore,+ private readonly gateway: ChatGateway,+ private readonly opts: CoordinatorOptions,+ ) {}++ async handleMessage(sessionId: string, msg: InboundMessage): Promise<{ swallow: boolean }> {+ if (!msg.isGroup || msg.fromMe || !msg.author) return { swallow: false };++ const state = await this.store.load(sessionId, msg.chatId);++ if (!state.announced) {+ await this.gateway.sendText(sessionId, msg.chatId, buildHelpText(this.opts.prefix));+ state.announced = true;+ await this.store.save(state);+ }++ const command = parseCommand(msg.body, this.opts.prefix);+ if (command) {+ await this.handleCommand(sessionId, msg, state, command);+ return { swallow: true };+ }++ if (!state.active) return { swallow: false };+ await this.translateMessage(sessionId, msg, state);+ return { swallow: false };+ }++ private async translateMessage(sessionId: string, msg: InboundMessage, state: GroupState): Promise<void> {+ const text = msg.body.trim();+ if (text.length < this.opts.minLength || text.length > this.opts.maxLength || URL_OR_EMOJI_ONLY.test(text)) {+ return;+ }++ const sender = this.ensureParticipant(state, msg.author);+ if (!sender.enabled) return;++ let detected: string;+ try {+ detected = (await this.translator.detect(text)).lang;+ } catch {+ return; // translator down โ silent skip+ }+ this.applyLearning(sender, detected);++ // Pick the effective source language. Detection misfires on short/colloquial text โ it often+ // returns a near-neighbour language (e.g. es misread as gl/ca) โ so trust the detected code only+ // when it names a language the group actually uses; otherwise fall back to the sender's known+ // language. Combined with excluding the sender's own language from the targets below, this stops+ // a message ever being "translated" into its own language (the duplicate/echo bug).+ const knownLangs = this.knownLanguages(state);+ const source = knownLangs.includes(detected) ? detected : (sender.lang ?? detected);++ const targets = this.targetLanguages(state, source, sender.lang);+ if (targets.length === 0) {+ await this.store.save(state);+ return;+ }++ const settled = await Promise.allSettled(targets.map(t => this.translator.translate(text, source, t)));+ const translations: Translation[] = [];+ settled.forEach((r, i) => {+ if (r.status === 'fulfilled') translations.push({ lang: targets[i], text: r.value });+ });++ if (translations.length > 0) {+ await this.gateway.sendCombinedReply(sessionId, msg.chatId, msg.id, formatCombinedReply(translations));+ }+ await this.store.save(state);+ }++ /** Distinct languages currently spoken by enabled participants. */+ private knownLanguages(state: GroupState): string[] {+ const langs = new Set<string>();+ for (const p of Object.values(state.participants)) {+ if (p.enabled && p.lang) langs.add(p.lang);+ }+ return [...langs];+ }++ /**+ * Distinct languages of enabled participants, excluding the message source language AND the+ * sender's own language โ a sender never needs their own message translated back to themselves+ * (this also guards against a detection misfire leaving the source language in the target set).+ */+ private targetLanguages(state: GroupState, source: string, senderLang: string | null): string[] {+ const langs = new Set<string>();+ for (const p of Object.values(state.participants)) {+ if (p.enabled && p.lang && p.lang !== source && p.lang !== senderLang) langs.add(p.lang);+ }+ return [...langs];+ }++ /** 2-message debounce: a learned language only switches after a new language is seen twice in a row. */+ private applyLearning(p: ParticipantState, detected: string): void {+ p.samples++;+ if (p.source === 'pinned') return;+ if (p.lang === detected) {+ p.pendingLang = undefined;+ return;+ }+ if (p.pendingLang === detected) {+ p.lang = detected;+ p.pendingLang = undefined;+ } else {+ p.pendingLang = detected;+ if (p.lang === null) p.lang = detected; // cold start: adopt immediately+ }+ p.updatedAt = new Date().toISOString();+ }++ private ensureParticipant(state: GroupState, wid: string): ParticipantState {+ if (!state.participants[wid]) {+ state.participants[wid] = { lang: null, source: 'learned', enabled: true, samples: 0, updatedAt: '' };+ }+ return state.participants[wid];+ }++ private async handleCommand(+ sessionId: string,+ msg: InboundMessage,+ state: GroupState,+ cmd: ParsedCommand,+ ): Promise<void> {+ if (cmd.name === 'help') {+ await this.gateway.sendText(sessionId, msg.chatId, buildHelpText(this.opts.prefix));+ return;+ }+ if (cmd.name === 'status') {+ await this.gateway.sendText(sessionId, msg.chatId, formatStatus(state, this.translator.isHealthy()));+ return;+ }++ const targetsSelf = cmd.target?.kind === 'me';+ const isSelfServe = (cmd.name === 'setlang' || cmd.name === 'auto') && targetsSelf;+ if (!isSelfServe) {+ const admins = await this.gateway.getGroupAdmins(sessionId, msg.chatId);+ const isAdmin = admins.some(a => widEquals(a, msg.author));+ const isController = isAdmin || state.delegatedControllers.some(c => widEquals(c, msg.author));+ const adminOnly = cmd.name === 'grant' || cmd.name === 'revoke';+ if ((adminOnly && !isAdmin) || (!adminOnly && !isController)) {+ // Always reply on denial โ a command must never fail silently.+ await this.gateway.sendText(+ sessionId,+ msg.chatId,+ adminOnly+ ? 'โ Only group admins can use that command.'+ : 'โ Only group admins or delegated users can use that command.',+ );+ return;+ }+ }++ const targetWid = this.resolveTarget(msg, cmd.target);++ switch (cmd.name) {+ case 'on':+ state.active = true;+ await this.confirm(sessionId, msg, 'โ Translation activated.', state);+ return;+ case 'off':+ state.active = false;+ await this.confirm(sessionId, msg, 'โ Translation deactivated.', state);+ return;+ case 'setlang': {+ if (!targetWid || !cmd.lang)+ return this.replyError(sessionId, msg, 'Usage: ' + this.opts.prefix + ' setlang <code> [me|@user|number]');+ const langs = await this.safeLanguages();+ if (langs && !langs.includes(cmd.lang)) {+ return this.replyError(sessionId, msg, `Unsupported language "${cmd.lang}". Supported: ${langs.join(', ')}`);+ }+ const p = this.ensureParticipant(state, targetWid);+ p.lang = cmd.lang;+ p.source = 'pinned';+ p.pendingLang = undefined;+ p.updatedAt = new Date().toISOString();+ await this.confirm(sessionId, msg, `โ Set ${targetWid} to ${cmd.lang}.`, state);+ return;+ }+ case 'auto': {+ if (!targetWid) return this.replyError(sessionId, msg, this.targetHelp());+ const p = this.ensureParticipant(state, targetWid);+ p.source = 'learned';+ p.pendingLang = undefined;+ await this.confirm(sessionId, msg, `โ ${targetWid} set to auto-detect.`, state);+ return;+ }+ case 'ignore':+ case 'unignore': {+ if (!targetWid) return this.replyError(sessionId, msg, this.targetHelp());+ const p = this.ensureParticipant(state, targetWid);+ p.enabled = cmd.name === 'unignore';+ await this.confirm(+ sessionId,+ msg,+ `โ ${cmd.name === 'ignore' ? 'Ignoring' : 'Including'} ${targetWid}.`,+ state,+ );+ return;+ }+ case 'grant':+ case 'revoke': {+ if (!targetWid) return this.replyError(sessionId, msg, this.targetHelp());+ const set = new Set(state.delegatedControllers);+ if (cmd.name === 'grant') set.add(targetWid);+ else set.delete(targetWid);+ state.delegatedControllers = [...set];+ await this.confirm(+ sessionId,+ msg,+ `โ ${cmd.name === 'grant' ? 'Granted' : 'Revoked'} control for ${targetWid}.`,+ state,+ );+ return;+ }+ }+ }++ private resolveTarget(msg: InboundMessage, target?: CommandTarget): string | null {+ if (!target || target.kind === 'me') return msg.author;+ if (target.kind === 'mention') return msg.mentionedIds[0] ?? null;+ // NOTE: a `<number>` target assumes phone-number JID keying (`<number>@c.us`). Under+ // WhatsApp's newer LID scheme participants may be keyed by an opaque `@lid` id instead,+ // so this constructed wid can fail to match the stored participant. The `@mention` and+ // `me` forms resolve to the actual wid and are robust to LID; prefer them. See spec ยง16.+ return `${target.number}@c.us`;+ }++ private async safeLanguages(): Promise<string[] | null> {+ try {+ return await this.translator.languages();+ } catch {+ return null; // can't validate โ allow+ }+ }++ private async confirm(sessionId: string, msg: InboundMessage, text: string, state: GroupState): Promise<void> {+ await this.store.save(state);+ await this.gateway.sendText(sessionId, msg.chatId, text);+ }++ private replyError(sessionId: string, msg: InboundMessage, text: string): Promise<void> {+ return this.gateway.sendText(sessionId, msg.chatId, text);+ }++ private targetHelp(): string {+ return "โ ๏ธ Couldn't identify that user. Target them by @mention, by phone number, or use 'me' for yourself.";+ }+}diff --git a/src/plugins/extensions/translation/index.ts b/src/plugins/extensions/translation/index.tsnew file mode 100644index 000000000..c059f3c6a--- /dev/null+++ b/src/plugins/extensions/translation/index.ts@@ -0,0 +1,98 @@+/**+ * Group auto-translation extension plugin.+ *+ * Ports the former core `translation` module onto the Tier-2 capability layer (#294): the+ * framework-agnostic `core/` (coordinator, parser, formatter, ports) is reused unchanged, with+ * `ChatGateway`/`ConfigStore` implemented over `ctx.messages`/`ctx.engine`/`ctx.storage`.+ * Registered DISABLED by default โ enable via `POST /plugins/translation/enable`.+ */+import { PluginContext, IPlugin } from '../../../core/plugins';+import { HookContext, HookResult } from '../../../core/hooks';+import { IncomingMessage } from '../../../engine/interfaces/whatsapp-engine.interface';+import { TranslationCoordinator, CoordinatorOptions } from './core/translation.coordinator';+import { InboundMessage } from './core/ports';+import { LibreTranslateClient } from './libretranslate.client';+import { PluginChatGateway } from './plugin-chat.gateway';+import { PluginConfigStore } from './plugin-config.store';++function readString(cfg: Record<string, unknown>, key: string, fallback: string): string {+ const v = cfg[key];+ return typeof v === 'string' && v.length > 0 ? v : fallback;+}+function readOptionalString(cfg: Record<string, unknown>, key: string): string | undefined {+ const v = cfg[key];+ return typeof v === 'string' && v.length > 0 ? v : undefined;+}+function readNumber(cfg: Record<string, unknown>, key: string, fallback: number): number {+ const v = cfg[key];+ return typeof v === 'number' && Number.isFinite(v) ? v : fallback;+}+function readBool(cfg: Record<string, unknown>, key: string, fallback: boolean): boolean {+ const v = cfg[key];+ return typeof v === 'boolean' ? v : fallback;+}++export class TranslationPlugin implements IPlugin {+ private coordinator: TranslationCoordinator | null = null;++ onEnable(context: PluginContext): Promise<void> {+ const cfg = context.config;++ const translator = new LibreTranslateClient({+ url: readString(cfg, 'libretranslateUrl', 'http://localhost:7001'),+ apiKey: readOptionalString(cfg, 'libretranslateApiKey'),+ timeoutMs: readNumber(cfg, 'timeoutMs', 5000),+ });+ const store = new PluginConfigStore(context.storage);+ const gateway = new PluginChatGateway(context.messages, context.engine);+ const opts: CoordinatorOptions = {+ prefix: readString(cfg, 'commandPrefix', '/tr'),+ minLength: readNumber(cfg, 'minLength', 2),+ maxLength: readNumber(cfg, 'maxLength', 2000),+ denyReply: readBool(cfg, 'denyReply', false),+ };+ this.coordinator = new TranslationCoordinator(translator, store, gateway, opts);++ context.registerHook('message:received', ctx => this.onMessage(context, ctx as HookContext<IncomingMessage>));+ context.logger.log('Translation plugin enabled', { action: 'translation_enabled' });+ return Promise.resolve();+ }++ onDisable(context: PluginContext): Promise<void> {+ // The loader unregisters this plugin's hooks on disable; drop the coordinator too.+ this.coordinator = null;+ context.logger.log('Translation plugin disabled', { action: 'translation_disabled' });+ return Promise.resolve();+ }++ private async onMessage(context: PluginContext, ctx: HookContext<IncomingMessage>): Promise<HookResult> {+ const msg = ctx.data;+ // Only act on engine-originated inbound messages for a known session. The bot's own sends are+ // `fromMe` and route through `message:sent`, so they never reach here (no translation loop).+ if (!this.coordinator || ctx.source !== 'Engine' || !ctx.sessionId) {+ return { continue: true };+ }+ try {+ const inbound: InboundMessage = {+ id: msg.id,+ chatId: msg.chatId,+ body: msg.body,+ author: msg.author ?? '',+ isGroup: msg.isGroup,+ fromMe: msg.fromMe,+ mentionedIds: msg.mentionedIds ?? [],+ pushName: msg.contact?.pushName,+ };+ const { swallow } = await this.coordinator.handleMessage(ctx.sessionId, inbound);+ return { continue: !swallow };+ } catch (error) {+ context.logger.error('Translation hook failed', error, {+ sessionId: ctx.sessionId,+ action: 'translation_hook_error',+ });+ return { continue: true };+ }+ }+}++export default TranslationPlugin;diff --git a/src/plugins/extensions/translation/libretranslate.client.spec.ts b/src/plugins/extensions/translation/libretranslate.client.spec.tsnew file mode 100644index 000000000..f9afa1593--- /dev/null+++ b/src/plugins/extensions/translation/libretranslate.client.spec.ts@@ -0,0 +1,49 @@+// src/modules/translation/adapters/libretranslate.client.spec.ts+import { LibreTranslateClient } from './libretranslate.client';++describe('LibreTranslateClient', () => {+ const makeFetch = (impl: jest.Mock) => {+ global.fetch = impl;+ };++ afterEach(() => jest.restoreAllMocks());++ it('translate() posts q/source/target and returns translatedText', async () => {+ const fetchMock = jest.fn<Promise<unknown>, [string, RequestInit?]>().mockResolvedValue({+ ok: true,+ json: () => Promise.resolve({ translatedText: 'Hola' }),+ });+ makeFetch(fetchMock);+ const client = new LibreTranslateClient({ url: 'http://lt:7001', timeoutMs: 1000 });+ const out = await client.translate('Hello', 'en', 'es');+ expect(out).toBe('Hola');+ const init = fetchMock.mock.calls[0][1] as RequestInit;+ const body = JSON.parse(init.body as string) as Record<string, unknown>;+ expect(body).toMatchObject({ q: 'Hello', source: 'en', target: 'es' });+ });++ it('detect() returns the top language', async () => {+ makeFetch(+ jest.fn().mockResolvedValue({+ ok: true,+ json: () => Promise.resolve([{ language: 'fr', confidence: 0.97 }]),+ }),+ );+ const client = new LibreTranslateClient({ url: 'http://lt:7001', timeoutMs: 1000 });+ expect(await client.detect('Bonjour')).toEqual({ lang: 'fr', confidence: 0.97 });+ });++ it('opens the circuit after N consecutive failures and reports unhealthy', async () => {+ makeFetch(jest.fn().mockResolvedValue({ ok: false, status: 500, text: () => Promise.resolve('err') }));+ const client = new LibreTranslateClient({+ url: 'http://lt:7001',+ timeoutMs: 1000,+ failureThreshold: 2,+ cooldownMs: 60000,+ });+ await expect(client.translate('a', 'en', 'es')).rejects.toThrow();+ await expect(client.translate('a', 'en', 'es')).rejects.toThrow();+ expect(client.isHealthy()).toBe(false);+ await expect(client.translate('a', 'en', 'es')).rejects.toThrow(/circuit open/i);+ });+});diff --git a/src/plugins/extensions/translation/libretranslate.client.ts b/src/plugins/extensions/translation/libretranslate.client.tsnew file mode 100644index 000000000..4861cc740--- /dev/null+++ b/src/plugins/extensions/translation/libretranslate.client.ts@@ -0,0 +1,86 @@+// src/modules/translation/adapters/libretranslate.client.ts+import { Translator, DetectResult } from './core/ports';+import { createLogger } from '../../../common/services/logger.service';++export interface LibreTranslateOptions {+ url: string;+ apiKey?: string;+ timeoutMs: number;+ failureThreshold?: number;+ cooldownMs?: number;+}++export class LibreTranslateClient implements Translator {+ private readonly logger = createLogger('LibreTranslateClient');+ private readonly base: string;+ private readonly failureThreshold: number;+ private readonly cooldownMs: number;+ private consecutiveFailures = 0;+ private openUntil = 0;++ constructor(private readonly opts: LibreTranslateOptions) {+ this.base = opts.url.replace(/\/+$/, '');+ this.failureThreshold = opts.failureThreshold ?? 5;+ this.cooldownMs = opts.cooldownMs ?? 30000;+ }++ isHealthy(): boolean {+ return this.consecutiveFailures < this.failureThreshold;+ }++ async detect(text: string): Promise<DetectResult> {+ const data = (await this.post('/detect', { q: text })) as Array<{ language: string; confidence: number }>;+ const top = data[0];+ if (!top) throw new Error('LibreTranslate /detect returned no result');+ return { lang: top.language, confidence: top.confidence };+ }++ async translate(text: string, source: string, target: string): Promise<string> {+ const data = (await this.post('/translate', { q: text, source, target, format: 'text' })) as {+ translatedText: string;+ };+ return data.translatedText;+ }++ async languages(): Promise<string[]> {+ const data = (await this.post('/languages', {}, 'GET')) as Array<{ code: string }>;+ return data.map(l => l.code);+ }++ private async post(+ path: string,+ payload: Record<string, unknown>,+ method: 'GET' | 'POST' = 'POST',+ ): Promise<unknown> {+ const now = Date.now();+ if (now < this.openUntil) {+ throw new Error('LibreTranslate circuit open');+ }++ const controller = new AbortController();+ const timer = setTimeout(() => controller.abort(), this.opts.timeoutMs);+ try {+ const body = method === 'POST' ? JSON.stringify({ ...payload, api_key: this.opts.apiKey }) : undefined;+ const res = await fetch(`${this.base}${path}`, {+ method,+ headers: { 'Content-Type': 'application/json' },+ body,+ signal: controller.signal,+ });+ if (!res.ok) {+ throw new Error(`LibreTranslate ${path} -> HTTP ${res.status}`);+ }+ this.consecutiveFailures = 0;+ return await res.json();+ } catch (err) {+ this.consecutiveFailures++;+ if (this.consecutiveFailures >= this.failureThreshold) {+ this.openUntil = Date.now() + this.cooldownMs;+ this.logger.warn(`LibreTranslate circuit opened for ${this.cooldownMs}ms`, { action: 'lt_circuit_open' });+ }+ throw err;+ } finally {+ clearTimeout(timer);+ }+ }+}diff --git a/src/plugins/extensions/translation/manifest.json b/src/plugins/extensions/translation/manifest.jsonnew file mode 100644index 000000000..eb016a9f8--- /dev/null+++ b/src/plugins/extensions/translation/manifest.json@@ -0,0 +1,54 @@+{+ "id": "translation",+ "name": "Group Auto-Translation",+ "version": "1.0.0",+ "type": "extension",+ "description": "Auto-translates group messages between participants' languages via LibreTranslate. Configure in-group with /tr commands. Disabled by default.",+ "main": "index.ts",+ "permissions": ["messages:send"],+ "sessions": ["*"],+ "configSchema": {+ "type": "object",+ "properties": {+ "libretranslateUrl": {+ "type": "string",+ "title": "LibreTranslate URL",+ "description": "Base URL of the LibreTranslate instance (e.g. http://libretranslate:7001).",+ "default": "http://localhost:7001",+ "required": true+ },+ "libretranslateApiKey": {+ "type": "string",+ "title": "LibreTranslate API key",+ "description": "Optional API key, if your LibreTranslate instance requires one.",+ "secret": true+ },+ "timeoutMs": {+ "type": "number",+ "title": "Translate timeout (ms)",+ "default": 5000+ },+ "commandPrefix": {+ "type": "string",+ "title": "Command prefix",+ "default": "/tr"+ },+ "minLength": {+ "type": "number",+ "title": "Min message length to translate",+ "default": 2+ },+ "maxLength": {+ "type": "number",+ "title": "Max message length to translate",+ "default": 2000+ },+ "denyReply": {+ "type": "boolean",+ "title": "Reply on denied commands",+ "description": "If true, reply with an 'admins only' message when a non-admin runs a restricted command.",+ "default": false+ }+ }+ }+}diff --git a/src/plugins/extensions/translation/plugin-chat.gateway.spec.ts b/src/plugins/extensions/translation/plugin-chat.gateway.spec.tsnew file mode 100644index 000000000..aadeb8cae--- /dev/null+++ b/src/plugins/extensions/translation/plugin-chat.gateway.spec.ts@@ -0,0 +1,45 @@+import { PluginChatGateway } from './plugin-chat.gateway';++describe('PluginChatGateway', () => {+ it('sendText routes through ctx.messages.sendText', async () => {+ const messages = { sendText: jest.fn(() => Promise.resolve({})), reply: jest.fn(() => Promise.resolve({})) };+ const engine = { getGroupInfo: jest.fn() };+ const gw = new PluginChatGateway(messages as never, engine as never);+ await gw.sendText('s', '[email protected]', 'hi');+ expect(messages.sendText).toHaveBeenCalledWith('s', '[email protected]', 'hi');+ });++ it('sendCombinedReply routes through ctx.messages.reply', async () => {+ const messages = { sendText: jest.fn(() => Promise.resolve({})), reply: jest.fn(() => Promise.resolve({})) };+ const engine = { getGroupInfo: jest.fn() };+ const gw = new PluginChatGateway(messages as never, engine as never);+ await gw.sendCombinedReply('s', '[email protected]', 'M1', 'Hola');+ expect(messages.reply).toHaveBeenCalledWith('s', '[email protected]', 'M1', 'Hola');+ });++ it('getGroupAdmins includes phone-scheme admins + the LID owner, deduped', async () => {+ const messages = { sendText: jest.fn(), reply: jest.fn() };+ const engine = {+ getGroupInfo: jest.fn(() =>+ Promise.resolve({+ owner: '149207180681386@lid',+ participants: [+ { id: '[email protected]', isAdmin: true, isSuperAdmin: true },+ { id: '[email protected]', isAdmin: false, isSuperAdmin: false },+ ],+ }),+ ),+ };+ const gw = new PluginChatGateway(messages, engine as never);+ const admins = await gw.getGroupAdmins('s', '[email protected]');+ expect(admins).toContain('[email protected]');+ expect(admins).toContain('149207180681386@lid');+ });++ it('getGroupAdmins returns [] when there is no group info', async () => {+ const messages = { sendText: jest.fn(), reply: jest.fn() };+ const engine = { getGroupInfo: jest.fn(() => Promise.resolve(null)) };+ const gw = new PluginChatGateway(messages, engine as never);+ expect(await gw.getGroupAdmins('s', '[email protected]')).toEqual([]);+ });+});diff --git a/src/plugins/extensions/translation/plugin-chat.gateway.ts b/src/plugins/extensions/translation/plugin-chat.gateway.tsnew file mode 100644index 000000000..1a226e21d--- /dev/null+++ b/src/plugins/extensions/translation/plugin-chat.gateway.ts@@ -0,0 +1,35 @@+import { ChatGateway } from './core/ports';+import { PluginMessagingCapability, PluginEngineReadCapability } from '../../../core/plugins';++/**+ * ChatGateway backed by the Tier-2 plugin capability surface: writes go through+ * `ctx.messages` (routed via MessageService, so persistence is preserved), and group-admin+ * reads through `ctx.engine`. It implements the same port the translation core already+ * depends on, so the coordinator/parser/formatter are reused unchanged.+ */+export class PluginChatGateway implements ChatGateway {+ constructor(+ private readonly messages: PluginMessagingCapability,+ private readonly engine: PluginEngineReadCapability,+ ) {}++ async sendText(sessionId: string, chatId: string, text: string): Promise<void> {+ await this.messages.sendText(sessionId, chatId, text);+ }++ async sendCombinedReply(sessionId: string, chatId: string, quotedMessageId: string, text: string): Promise<void> {+ await this.messages.reply(sessionId, chatId, quotedMessageId, text);+ }++ async getGroupAdmins(sessionId: string, chatId: string): Promise<string[]> {+ const info = await this.engine.getGroupInfo(sessionId, chatId);+ if (!info) return [];+ const admins = info.participants.filter(p => p.isAdmin || p.isSuperAdmin).map(p => p.id);+ // Participant ids can be in the phone (@c.us) scheme while message authors arrive as LID+ // (@lid). The group `owner` is reported in the author's scheme, so include it to recognize+ // the group creator across that split. Non-owner admins on a differing scheme can be granted+ // control via `/tr grant`.+ if (info.owner) admins.push(info.owner);+ return [...new Set(admins)];+ }+}diff --git a/src/plugins/extensions/translation/plugin-config.store.spec.ts b/src/plugins/extensions/translation/plugin-config.store.spec.tsnew file mode 100644index 000000000..34abb2487--- /dev/null+++ b/src/plugins/extensions/translation/plugin-config.store.spec.ts@@ -0,0 +1,44 @@+import { PluginConfigStore } from './plugin-config.store';+import { GroupState } from './core/ports';++function makeStorage() {+ const data = new Map<string, unknown>();+ return {+ get: jest.fn((k: string) => Promise.resolve(data.has(k) ? data.get(k) : null)),+ set: jest.fn((k: string, v: unknown) => {+ data.set(k, v);+ return Promise.resolve();+ }),+ delete: jest.fn((k: string) => {+ data.delete(k);+ return Promise.resolve();+ }),+ list: jest.fn(() => Promise.resolve([...data.keys()])),+ };+}++describe('PluginConfigStore', () => {+ it('returns a default inactive state for an unknown group', async () => {+ const store = new PluginConfigStore(makeStorage() as never);+ const state = await store.load('s', '[email protected]');+ expect(state).toMatchObject({ sessionId: 's', chatId: '[email protected]', active: false, announced: false });+ expect(state.participants).toEqual({});+ expect(state.delegatedControllers).toEqual([]);+ });++ it('round-trips a saved state under a per-group key', async () => {+ const storage = makeStorage();+ const store = new PluginConfigStore(storage as never);+ const state: GroupState = {+ sessionId: 's',+ chatId: '[email protected]',+ active: true,+ participants: { '111@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 1, updatedAt: 'x' } },+ delegatedControllers: ['222@lid'],+ announced: true,+ };+ await store.save(state);+ expect(storage.set).toHaveBeenCalledWith('group:s:[email protected]', state);+ expect(await store.load('s', '[email protected]')).toEqual(state);+ });+});diff --git a/src/plugins/extensions/translation/plugin-config.store.ts b/src/plugins/extensions/translation/plugin-config.store.tsnew file mode 100644index 000000000..4a081e891--- /dev/null+++ b/src/plugins/extensions/translation/plugin-config.store.ts@@ -0,0 +1,34 @@+import { ConfigStore, GroupState } from './core/ports';+import { PluginStorage } from '../../../core/plugins';++/**+ * ConfigStore backed by the plugin's KV storage (`ctx.storage`). Persists one GroupState+ * JSON document per (sessionId, chatId) and returns a default inactive state for groups the+ * bot hasn't met yet. Replaces the TypeORM entity/migration the core-module version used โ+ * a plugin owns its own state via the capability surface.+ */+export class PluginConfigStore implements ConfigStore {+ constructor(private readonly storage: PluginStorage) {}++ private key(sessionId: string, chatId: string): string {+ return `group:${sessionId}:${chatId}`;+ }++ async load(sessionId: string, chatId: string): Promise<GroupState> {+ const stored = await this.storage.get<GroupState>(this.key(sessionId, chatId));+ return (+ stored ?? {+ sessionId,+ chatId,+ active: false,+ participants: {},+ delegatedControllers: [],+ announced: false,+ }+ );+ }++ async save(state: GroupState): Promise<void> {+ await this.storage.set(this.key(state.sessionId, state.chatId), state);+ }+}