Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/35791-slack-plugin-token-optional.md
Original file line number Diff line number Diff line change
@@ -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).
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<AutomationEngine>('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<SlackClientService>('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<string, string>).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<AutomationEngine>('automation').getRegisteredConnectors()).not.toContain('slack');
expect(typeof kernel.getService<SlackClientService>('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<string, unknown>();
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<string, unknown>([[
'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']);
});
});
33 changes: 29 additions & 4 deletions packages/connectors/connector-slack/src/connector-slack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SlackConnectorOptions, 'token'> {
/**
* 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
Expand All @@ -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';
Expand All @@ -44,7 +60,7 @@ export class ConnectorSlackPlugin implements Plugin {
private connectorName?: string;
private automation?: ConnectorRegistrySurface;

constructor(options: ConnectorSlackPluginOptions) {
constructor(options: ConnectorSlackPluginOptions = {}) {
this.options = options;
}

Expand All @@ -57,6 +73,15 @@ export class ConnectorSlackPlugin implements Plugin {
}

async start(ctx: PluginContext): Promise<void> {
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<ConnectorRegistrySurface>('automation');
Expand All @@ -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;
Expand Down
Loading