Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

refactor(plugins): type-check the capability ports again

Yudhi Armyndharise0c582934c28
diff --git a/src/core/plugins/plugin-host-port-providers.spec.ts b/src/core/plugins/plugin-host-port-providers.spec.ts
index 0ed06f668..387b47771 100644
--- a/src/core/plugins/plugin-host-port-providers.spec.ts
+++ b/src/core/plugins/plugin-host-port-providers.spec.ts
@@ -20,6 +20,11 @@ import { SessionModule } from '../../modules/session/session.module';
* hooks twice, launching two concurrent auto-start loops on every boot. The same applies to any other
* factory that returns an injected instance (search.module.ts `SEARCH_BOOTSTRAP` returns the registry):
* harmless while the target has no lifecycle hook, doubled the moment one is added.
+ *
+ * `useExisting` is typed `any`, so unlike the factory it replaced it does NOT check that the service
+ * still satisfies the port. That check lives on the classes instead: each one carries
+ * `implements Plugin*Port`, so a signature drift fails `tsc --noEmit` rather than surfacing as a
+ * runtime TypeError inside a plugin capability call.
*/
describe('plugin host port providers', () => {
const MODULES = [PluginsModule, IntegrationModule, MessageModule, SearchModule, SessionModule];
diff --git a/src/modules/integration/conversation-mapping.service.ts b/src/modules/integration/conversation-mapping.service.ts
index 1c375d531..7ce03e55d 100644
--- a/src/modules/integration/conversation-mapping.service.ts
+++ b/src/modules/integration/conversation-mapping.service.ts
@@ -3,6 +3,9 @@ import { InjectRepository } from '@nestjs/typeorm';
import { QueryDeepPartialEntity, Repository } from 'typeorm';
import { ConversationMapping, HandoverState } from './entities/conversation-mapping.entity';
import { isUniqueViolation } from '../../common/utils/db-errors';
+// Type-only: PluginsModule binds this class to PLUGIN_CONVERSATION_MAPPING_PORT with a `useExisting`
+// alias, which TypeScript does not check, so `implements` is what keeps the two in step.
+import type { PluginConversationMappingPort } from '../../core/plugins/plugin-host-ports';
export interface MappingKey {
sessionId: string;
@@ -30,7 +33,7 @@ export class ConversationMappingConflict extends Error {
}
@Injectable()
-export class ConversationMappingService {
+export class ConversationMappingService implements PluginConversationMappingPort {
constructor(@InjectRepository(ConversationMapping, 'data') private readonly repo: Repository<ConversationMapping>) {}
async upsert(key: MappingKey, providerConversationId: string, patch?: Partial<ConversationMapping>): Promise<void> {
diff --git a/src/modules/integration/plugin-instance.service.ts b/src/modules/integration/plugin-instance.service.ts
index 6d0b388a9..33a10d52d 100644
--- a/src/modules/integration/plugin-instance.service.ts
+++ b/src/modules/integration/plugin-instance.service.ts
@@ -5,6 +5,9 @@ import { Repository } from 'typeorm';
import { PluginInstance } from './entities/plugin-instance.entity';
import type { PluginConfigSchema } from '../../core/plugins/plugin.interfaces';
import { redactSecretConfig, restoreSecretConfig, SECRET_SENTINEL } from '../plugins/redact-config';
+// Type-only: the module binds this class to PLUGIN_INSTANCE_PORT with a `useExisting` alias, which
+// TypeScript does not check, so `implements` is what keeps the two in step.
+import type { PluginInstancePort } from '../../core/plugins/plugin-host-ports';
// A supplied ingress secret must be a real, guessing-resistant value; an empty/short one would make the
// public HMAC forgeable. Absent => auto-generate. Trimmed so pasted whitespace can't slip a weak secret in.
@@ -25,7 +28,7 @@ export class InstanceExistsError extends Error {
}
@Injectable()
-export class PluginInstanceService {
+export class PluginInstanceService implements PluginInstancePort {
constructor(@InjectRepository(PluginInstance, 'data') private readonly repo: Repository<PluginInstance>) {}
async mint(
diff --git a/src/modules/message/message.service.ts b/src/modules/message/message.service.ts
index aef529d67..53df06252 100644
--- a/src/modules/message/message.service.ts
+++ b/src/modules/message/message.service.ts
@@ -15,6 +15,9 @@ import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.
import { ChatMediaArchiveService } from '../chat-media/chat-media-archive.service';
import { StorageService, isMissingObjectError } from '../../common/storage/storage.service';
import { MessageSendService, SaveOutgoingMessageData } from './message-send.service';
+// Type-only: the module binds this class to PLUGIN_MESSAGE_PORT with a `useExisting` alias, which
+// TypeScript does not check, so `implements` is what keeps the two in step.
+import type { PluginMessagePort } from '../../core/plugins/plugin-host-ports';
// Re-exported for existing importers (bulk send shares the rendered-template cap with the send path).
export { DEFAULT_TEMPLATE_RENDER_MAX_CHARS } from './message-send.service';
@@ -110,7 +113,7 @@ function inertMimetype(mimetype: string): string {
}
@Injectable()
-export class MessageService {
+export class MessageService implements PluginMessagePort {
private readonly logger = createLogger('MessageService');
constructor(
diff --git a/src/modules/search/search-provider.registry.ts b/src/modules/search/search-provider.registry.ts
index 0c3858b92..2c58df8d3 100644
--- a/src/modules/search/search-provider.registry.ts
+++ b/src/modules/search/search-provider.registry.ts
@@ -1,5 +1,8 @@
import { Injectable } from '@nestjs/common';
import type { SearchProvider } from './search.types';
+// Type-only: the module binds this class to PLUGIN_SEARCH_REGISTRY_PORT with a `useExisting` alias,
+// which TypeScript does not check, so `implements` is what keeps the two in step.
+import type { PluginSearchRegistryPort } from '../../core/plugins/plugin-host-ports';
/**
* Holds the set of registered SearchProviders and the currently active one.
@@ -7,7 +10,7 @@ import type { SearchProvider } from './search.types';
* active() returns null when nothing is registered → the route 501s.
*/
@Injectable()
-export class SearchProviderRegistry {
+export class SearchProviderRegistry implements PluginSearchRegistryPort {
private readonly providers = new Map<string, SearchProvider>();
private activeId: string | null = null;
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index 3c48ff734..a4b3290c4 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -30,6 +30,9 @@ import { resolveFeatureFlags } from '../../config/feature-flags';
import { IWhatsAppEngine, ChatSummary, ChatState } from '../../engine/interfaces/whatsapp-engine.interface';
import { createLogger } from '../../common/services/logger.service';
import { HookManager } from '../../core/hooks';
+// Type-only: the module binds this class to PLUGIN_SESSION_PORT with a `useExisting` alias, which
+// TypeScript does not check, so `implements` is what keeps the two in step.
+import type { PluginSessionPort } from '../../core/plugins/plugin-host-ports';
/** Stagger before the single transient-launch retry; short - the claim is held while it waits. */
const SESSION_START_RETRY_DELAY_MS = 2_000;
@@ -78,7 +81,7 @@ export const AUTOSTART_THROTTLE_MS = 2_000;
* its public surface toward the controller and the feature modules is unchanged by the split.
*/
@Injectable()
-export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicationBootstrap {
+export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicationBootstrap, PluginSessionPort {
private readonly logger = createLogger('SessionService');
// Live engine instances, owned by the shared EngineRegistry (the narrow port feature modules