Skip to content

Commit 2884796

Browse files
committed
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzh5qSa4qh6mCPSykM6
1 parent 9348f02 commit 2884796

3 files changed

Lines changed: 127 additions & 5 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/connector-slack': patch
3+
---
4+
5+
feat(connector-slack): `ConnectorSlackPlugin`'s static bot `token` is optional
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
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.
11+
12+
`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).

‎packages/connectors/connector-slack/src/connector-slack-plugin.test.ts‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { describe, it, expect } from 'vitest';
4-
import { LiteKernel } from '@objectstack/core';
4+
import { LiteKernel, type PluginContext } from '@objectstack/core';
55
import { AutomationServicePlugin, type AutomationEngine } from '@objectstack/service-automation';
66
import { ConnectorSlackPlugin } from './connector-slack-plugin.js';
77
import type { SlackClientService } from './slack-connector.js';
@@ -99,3 +99,88 @@ describe('ConnectorSlackPlugin — the `slack` client service', () => {
9999
await kernel.shutdown();
100100
});
101101
});
102+
103+
describe('ConnectorSlackPlugin — composed without a static bot token', () => {
104+
it('registers only the client service: no flow connector, and the client still sends with the caller\'s token', async () => {
105+
const { impl, calls } = stubSlack();
106+
const kernel = new LiteKernel();
107+
kernel.use(new AutomationServicePlugin());
108+
kernel.use(new ConnectorSlackPlugin({ fetchImpl: impl }));
109+
await kernel.bootstrap();
110+
111+
const engine = kernel.getService<AutomationEngine>('automation');
112+
// The automation engine is up (positive control), yet no Slack connector was registered.
113+
expect(engine.getRegisteredNodeTypes()).toContain('connector_action');
114+
expect(engine.getRegisteredConnectors()).not.toContain('slack');
115+
116+
const slack = kernel.getService<SlackClientService>('slack');
117+
const out = await slack.client('xoxb-organization-token').call({ method: 'chat.postMessage', params: { channel: 'D1', text: 'hi' } });
118+
expect(out.ok).toBe(true);
119+
expect(calls).toHaveLength(1);
120+
expect((calls[0].init.headers as Record<string, string>).Authorization).toBe('Bearer xoxb-organization-token');
121+
122+
await kernel.shutdown();
123+
});
124+
125+
it('composes with no options at all', async () => {
126+
const kernel = new LiteKernel();
127+
kernel.use(new AutomationServicePlugin());
128+
kernel.use(new ConnectorSlackPlugin());
129+
await kernel.bootstrap();
130+
131+
expect(kernel.getService<AutomationEngine>('automation').getRegisteredConnectors()).not.toContain('slack');
132+
expect(typeof kernel.getService<SlackClientService>('slack').client).toBe('function');
133+
134+
await kernel.shutdown();
135+
});
136+
137+
it.each([
138+
['omitted', undefined],
139+
['empty', ''],
140+
])('says so once at boot and never touches the connector registry (token %s)', async (_label, token) => {
141+
const infos: string[] = [];
142+
const registered: string[] = [];
143+
const services = new Map<string, unknown>();
144+
services.set('automation', {
145+
registerConnector: (def: { name: string }) => { registered.push(def.name); },
146+
unregisterConnector: () => {},
147+
});
148+
const ctx = {
149+
registerService: (name: string, svc: unknown) => { services.set(name, svc); },
150+
getService: (name: string) => services.get(name),
151+
logger: { info: (msg: string) => { infos.push(msg); }, warn: () => {}, error: () => {}, debug: () => {} },
152+
} as unknown as PluginContext;
153+
154+
const plugin = new ConnectorSlackPlugin(token === undefined ? {} : { token });
155+
await plugin.init(ctx);
156+
await plugin.start(ctx);
157+
158+
expect(registered).toEqual([]);
159+
expect(services.has('slack')).toBe(true);
160+
const tokenLines = infos.filter((m) => m.includes('no static bot token'));
161+
expect(tokenLines).toHaveLength(1);
162+
expect(infos).toHaveLength(1);
163+
164+
// Teardown with nothing registered is a no-op, not a throw.
165+
await plugin.destroy();
166+
});
167+
168+
it('with a token, the same harness registers the connector (control for the skip)', async () => {
169+
const registered: string[] = [];
170+
const services = new Map<string, unknown>([[
171+
'automation',
172+
{ registerConnector: (def: { name: string }) => { registered.push(def.name); }, unregisterConnector: () => {} },
173+
]]);
174+
const ctx = {
175+
registerService: (name: string, svc: unknown) => { services.set(name, svc); },
176+
getService: (name: string) => services.get(name),
177+
logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} },
178+
} as unknown as PluginContext;
179+
180+
const plugin = new ConnectorSlackPlugin({ token: 'xoxb-flow-token' });
181+
await plugin.init(ctx);
182+
await plugin.start(ctx);
183+
184+
expect(registered).toEqual(['slack']);
185+
});
186+
});

‎packages/connectors/connector-slack/src/connector-slack-plugin.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ export interface ConnectorRegistrySurface {
2020
unregisterConnector(name: string): void;
2121
}
2222

23-
export interface ConnectorSlackPluginOptions extends SlackConnectorOptions {}
23+
/**
24+
* The plugin's options: the connector's, with the static bot `token` optional.
25+
*
26+
* The token feeds only the flow `connector_action` connector registered in
27+
* start(). The `slack` client service init() registers takes each caller's own
28+
* token, so a deployment that only delivers Slack DMs with per-organization
29+
* tokens composes the plugin without one — the flow connector is then skipped.
30+
*/
31+
export interface ConnectorSlackPluginOptions extends Omit<SlackConnectorOptions, 'token'> {
32+
/**
33+
* Slack bot token (`xoxb-…`) for the flow connector. Omit it (or pass an
34+
* empty string) to register only the `slack` client service.
35+
*/
36+
token?: string;
37+
}
2438

2539
/**
2640
* ConnectorSlackPlugin — registers a Slack Web API connector on the automation
@@ -29,7 +43,9 @@ export interface ConnectorSlackPluginOptions extends SlackConnectorOptions {}
2943
* can dispatch to `slack.chat.postMessage` without the messaging stack.
3044
*
3145
* If no automation engine is present the plugin logs and skips — the connector
32-
* has nowhere to register, which is not an error.
46+
* has nowhere to register, which is not an error. Likewise without a static
47+
* `token`: the flow connector has no credential, so start() says so once and
48+
* registers only the `slack` client service (init()).
3349
*/
3450
export class ConnectorSlackPlugin implements Plugin {
3551
name = 'com.objectstack.connector.slack';
@@ -44,7 +60,7 @@ export class ConnectorSlackPlugin implements Plugin {
4460
private connectorName?: string;
4561
private automation?: ConnectorRegistrySurface;
4662

47-
constructor(options: ConnectorSlackPluginOptions) {
63+
constructor(options: ConnectorSlackPluginOptions = {}) {
4864
this.options = options;
4965
}
5066

@@ -57,6 +73,15 @@ export class ConnectorSlackPlugin implements Plugin {
5773
}
5874

5975
async start(ctx: PluginContext): Promise<void> {
76+
const { token, ...shared } = this.options;
77+
if (!token) {
78+
ctx.logger.info(
79+
'ConnectorSlackPlugin: no static bot token — flow Slack connector not registered; '
80+
+ "the 'slack' client service serves caller-held tokens only",
81+
);
82+
return;
83+
}
84+
6085
let automation: ConnectorRegistrySurface | undefined;
6186
try {
6287
automation = ctx.getService<ConnectorRegistrySurface>('automation');
@@ -69,7 +94,7 @@ export class ConnectorSlackPlugin implements Plugin {
6994
return;
7095
}
7196

72-
const { def, handlers } = createSlackConnector(this.options);
97+
const { def, handlers } = createSlackConnector({ ...shared, token });
7398
automation.registerConnector(def, handlers);
7499
this.automation = automation;
75100
this.connectorName = def.name;

0 commit comments

Comments
 (0)