From 288479620743cedc42cd482b65ba2834a52f3f05 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:42:00 +0000 Subject: [PATCH] feat(connector-slack): make ConnectorSlackPlugin's static bot token optional Without a token the plugin registers only the 'slack' client service for caller-held tokens and skips the flow connector, logging that once at boot. With a token, behaviour is unchanged. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01WGufzh5qSa4qh6mCPSykM6 --- .../35791-slack-plugin-token-optional.md | 12 +++ .../src/connector-slack-plugin.test.ts | 87 ++++++++++++++++++- .../src/connector-slack-plugin.ts | 33 ++++++- 3 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 .changeset/35791-slack-plugin-token-optional.md diff --git a/.changeset/35791-slack-plugin-token-optional.md b/.changeset/35791-slack-plugin-token-optional.md new file mode 100644 index 00000000000..fcf4c724e8b --- /dev/null +++ b/.changeset/35791-slack-plugin-token-optional.md @@ -0,0 +1,12 @@ +--- +'@objectstack/connector-slack': patch +--- + +feat(connector-slack): `ConnectorSlackPlugin`'s static bot `token` is optional + +Clause-②: yes (widening) +Metadata-protocol: none + +A deployment that only delivers Slack DMs with each organization's own token now composes the plugin without a placeholder: `new ConnectorSlackPlugin()` or `new ConnectorSlackPlugin({ fetchImpl })` registers the `slack` client service for caller-held tokens and skips the flow `connector_action` connector, logging that once at boot. An empty-string `token` reads the same as an absent one. With a token, behaviour is unchanged: the `slack` flow connector is registered exactly as before. + +`ConnectorSlackPluginOptions` is now `SlackConnectorOptions` without `token`, plus `token?: string`; the constructor's options argument is optional. `SlackConnectorOptions.token` and `createSlackConnector` are unchanged (the connector still needs a token). diff --git a/packages/connectors/connector-slack/src/connector-slack-plugin.test.ts b/packages/connectors/connector-slack/src/connector-slack-plugin.test.ts index 6ed07d02ec4..c652f80712a 100644 --- a/packages/connectors/connector-slack/src/connector-slack-plugin.test.ts +++ b/packages/connectors/connector-slack/src/connector-slack-plugin.test.ts @@ -1,7 +1,7 @@ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. import { describe, it, expect } from 'vitest'; -import { LiteKernel } from '@objectstack/core'; +import { LiteKernel, type PluginContext } from '@objectstack/core'; import { AutomationServicePlugin, type AutomationEngine } from '@objectstack/service-automation'; import { ConnectorSlackPlugin } from './connector-slack-plugin.js'; import type { SlackClientService } from './slack-connector.js'; @@ -99,3 +99,88 @@ describe('ConnectorSlackPlugin — the `slack` client service', () => { await kernel.shutdown(); }); }); + +describe('ConnectorSlackPlugin — composed without a static bot token', () => { + it('registers only the client service: no flow connector, and the client still sends with the caller\'s token', async () => { + const { impl, calls } = stubSlack(); + const kernel = new LiteKernel(); + kernel.use(new AutomationServicePlugin()); + kernel.use(new ConnectorSlackPlugin({ fetchImpl: impl })); + await kernel.bootstrap(); + + const engine = kernel.getService('automation'); + // The automation engine is up (positive control), yet no Slack connector was registered. + expect(engine.getRegisteredNodeTypes()).toContain('connector_action'); + expect(engine.getRegisteredConnectors()).not.toContain('slack'); + + const slack = kernel.getService('slack'); + const out = await slack.client('xoxb-organization-token').call({ method: 'chat.postMessage', params: { channel: 'D1', text: 'hi' } }); + expect(out.ok).toBe(true); + expect(calls).toHaveLength(1); + expect((calls[0].init.headers as Record).Authorization).toBe('Bearer xoxb-organization-token'); + + await kernel.shutdown(); + }); + + it('composes with no options at all', async () => { + const kernel = new LiteKernel(); + kernel.use(new AutomationServicePlugin()); + kernel.use(new ConnectorSlackPlugin()); + await kernel.bootstrap(); + + expect(kernel.getService('automation').getRegisteredConnectors()).not.toContain('slack'); + expect(typeof kernel.getService('slack').client).toBe('function'); + + await kernel.shutdown(); + }); + + it.each([ + ['omitted', undefined], + ['empty', ''], + ])('says so once at boot and never touches the connector registry (token %s)', async (_label, token) => { + const infos: string[] = []; + const registered: string[] = []; + const services = new Map(); + services.set('automation', { + registerConnector: (def: { name: string }) => { registered.push(def.name); }, + unregisterConnector: () => {}, + }); + const ctx = { + registerService: (name: string, svc: unknown) => { services.set(name, svc); }, + getService: (name: string) => services.get(name), + logger: { info: (msg: string) => { infos.push(msg); }, warn: () => {}, error: () => {}, debug: () => {} }, + } as unknown as PluginContext; + + const plugin = new ConnectorSlackPlugin(token === undefined ? {} : { token }); + await plugin.init(ctx); + await plugin.start(ctx); + + expect(registered).toEqual([]); + expect(services.has('slack')).toBe(true); + const tokenLines = infos.filter((m) => m.includes('no static bot token')); + expect(tokenLines).toHaveLength(1); + expect(infos).toHaveLength(1); + + // Teardown with nothing registered is a no-op, not a throw. + await plugin.destroy(); + }); + + it('with a token, the same harness registers the connector (control for the skip)', async () => { + const registered: string[] = []; + const services = new Map([[ + 'automation', + { registerConnector: (def: { name: string }) => { registered.push(def.name); }, unregisterConnector: () => {} }, + ]]); + const ctx = { + registerService: (name: string, svc: unknown) => { services.set(name, svc); }, + getService: (name: string) => services.get(name), + logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }, + } as unknown as PluginContext; + + const plugin = new ConnectorSlackPlugin({ token: 'xoxb-flow-token' }); + await plugin.init(ctx); + await plugin.start(ctx); + + expect(registered).toEqual(['slack']); + }); +}); diff --git a/packages/connectors/connector-slack/src/connector-slack-plugin.ts b/packages/connectors/connector-slack/src/connector-slack-plugin.ts index 2cd3c6fef03..67ae6dad885 100644 --- a/packages/connectors/connector-slack/src/connector-slack-plugin.ts +++ b/packages/connectors/connector-slack/src/connector-slack-plugin.ts @@ -20,7 +20,21 @@ export interface ConnectorRegistrySurface { unregisterConnector(name: string): void; } -export interface ConnectorSlackPluginOptions extends SlackConnectorOptions {} +/** + * The plugin's options: the connector's, with the static bot `token` optional. + * + * The token feeds only the flow `connector_action` connector registered in + * start(). The `slack` client service init() registers takes each caller's own + * token, so a deployment that only delivers Slack DMs with per-organization + * tokens composes the plugin without one — the flow connector is then skipped. + */ +export interface ConnectorSlackPluginOptions extends Omit { + /** + * Slack bot token (`xoxb-…`) for the flow connector. Omit it (or pass an + * empty string) to register only the `slack` client service. + */ + token?: string; +} /** * ConnectorSlackPlugin — registers a Slack Web API connector on the automation @@ -29,7 +43,9 @@ export interface ConnectorSlackPluginOptions extends SlackConnectorOptions {} * can dispatch to `slack.chat.postMessage` without the messaging stack. * * If no automation engine is present the plugin logs and skips — the connector - * has nowhere to register, which is not an error. + * has nowhere to register, which is not an error. Likewise without a static + * `token`: the flow connector has no credential, so start() says so once and + * registers only the `slack` client service (init()). */ export class ConnectorSlackPlugin implements Plugin { name = 'com.objectstack.connector.slack'; @@ -44,7 +60,7 @@ export class ConnectorSlackPlugin implements Plugin { private connectorName?: string; private automation?: ConnectorRegistrySurface; - constructor(options: ConnectorSlackPluginOptions) { + constructor(options: ConnectorSlackPluginOptions = {}) { this.options = options; } @@ -57,6 +73,15 @@ export class ConnectorSlackPlugin implements Plugin { } async start(ctx: PluginContext): Promise { + const { token, ...shared } = this.options; + if (!token) { + ctx.logger.info( + 'ConnectorSlackPlugin: no static bot token — flow Slack connector not registered; ' + + "the 'slack' client service serves caller-held tokens only", + ); + return; + } + let automation: ConnectorRegistrySurface | undefined; try { automation = ctx.getService('automation'); @@ -69,7 +94,7 @@ export class ConnectorSlackPlugin implements Plugin { return; } - const { def, handlers } = createSlackConnector(this.options); + const { def, handlers } = createSlackConnector({ ...shared, token }); automation.registerConnector(def, handlers); this.automation = automation; this.connectorName = def.name;