@objectstack/connector-slack 7.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +22 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE +93 -0
- package/dist/index.d.mts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +166 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -0
- package/src/connector-slack-plugin.test.ts +78 -0
- package/src/connector-slack-plugin.ts +79 -0
- package/src/index.ts +28 -0
- package/src/slack-connector.test.ts +132 -0
- package/src/slack-connector.ts +210 -0
- package/tsconfig.json +10 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// src/slack-connector.ts
|
|
2
|
+
function createSlackConnector(opts) {
|
|
3
|
+
const name = opts.name ?? "slack";
|
|
4
|
+
const baseUrl = (opts.baseUrl ?? "https://slack.com/api").replace(/\/+$/, "");
|
|
5
|
+
const doFetch = opts.fetchImpl ?? fetch;
|
|
6
|
+
const def = {
|
|
7
|
+
name,
|
|
8
|
+
label: opts.label ?? "Slack",
|
|
9
|
+
type: "api",
|
|
10
|
+
description: "Slack Web API connector (static bot-token auth). Post and update messages, or call any Web API method.",
|
|
11
|
+
icon: "slack",
|
|
12
|
+
authentication: { type: "bearer", token: opts.token },
|
|
13
|
+
// Defaulted by ConnectorSchema; set explicitly so the literal satisfies
|
|
14
|
+
// the (post-parse) Connector output type.
|
|
15
|
+
status: "active",
|
|
16
|
+
enabled: true,
|
|
17
|
+
connectionTimeoutMs: 3e4,
|
|
18
|
+
requestTimeoutMs: 3e4,
|
|
19
|
+
actions: [
|
|
20
|
+
{
|
|
21
|
+
key: "chat.postMessage",
|
|
22
|
+
label: "Post Message",
|
|
23
|
+
description: "Post a message to a channel, DM, or thread (Slack chat.postMessage).",
|
|
24
|
+
inputSchema: {
|
|
25
|
+
type: "object",
|
|
26
|
+
required: ["channel"],
|
|
27
|
+
properties: {
|
|
28
|
+
channel: { type: "string", description: "Channel id, user id, or #name" },
|
|
29
|
+
text: { type: "string", description: "Message text" },
|
|
30
|
+
thread_ts: { type: "string", description: "Thread root ts to reply into" },
|
|
31
|
+
blocks: { type: "array", description: "Block Kit blocks" }
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
outputSchema: slackOutputSchema()
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
key: "chat.update",
|
|
38
|
+
label: "Update Message",
|
|
39
|
+
description: "Edit an existing message (Slack chat.update).",
|
|
40
|
+
inputSchema: {
|
|
41
|
+
type: "object",
|
|
42
|
+
required: ["channel", "ts"],
|
|
43
|
+
properties: {
|
|
44
|
+
channel: { type: "string", description: "Channel id" },
|
|
45
|
+
ts: { type: "string", description: "Timestamp of the message to update" },
|
|
46
|
+
text: { type: "string", description: "New message text" },
|
|
47
|
+
blocks: { type: "array", description: "New Block Kit blocks" }
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
outputSchema: slackOutputSchema()
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
key: "call",
|
|
54
|
+
label: "Call Web API Method",
|
|
55
|
+
description: "Escape hatch \u2014 call any Slack Web API method with arbitrary params.",
|
|
56
|
+
inputSchema: {
|
|
57
|
+
type: "object",
|
|
58
|
+
required: ["method"],
|
|
59
|
+
properties: {
|
|
60
|
+
method: { type: "string", description: "Web API method, e.g. conversations.list" },
|
|
61
|
+
params: { type: "object", description: "Method parameters (JSON body)" }
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
outputSchema: slackOutputSchema()
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
};
|
|
68
|
+
async function callSlack(method, params) {
|
|
69
|
+
const headers = {
|
|
70
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
71
|
+
...opts.defaultHeaders,
|
|
72
|
+
Authorization: `Bearer ${opts.token}`
|
|
73
|
+
};
|
|
74
|
+
const response = await doFetch(`${baseUrl}/${method}`, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers,
|
|
77
|
+
body: JSON.stringify(params)
|
|
78
|
+
});
|
|
79
|
+
const body = await response.json();
|
|
80
|
+
const ok = body.ok === true;
|
|
81
|
+
return {
|
|
82
|
+
ok,
|
|
83
|
+
status: response.status,
|
|
84
|
+
body,
|
|
85
|
+
error: ok ? void 0 : body.error
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
async function postMessage(input) {
|
|
89
|
+
return toRecord(await callSlack("chat.postMessage", input));
|
|
90
|
+
}
|
|
91
|
+
async function update(input) {
|
|
92
|
+
return toRecord(await callSlack("chat.update", input));
|
|
93
|
+
}
|
|
94
|
+
async function call(input) {
|
|
95
|
+
const method = String(input.method ?? "");
|
|
96
|
+
if (!method) throw new Error("slack 'call' action: 'method' is required");
|
|
97
|
+
const params = input.params ?? {};
|
|
98
|
+
return toRecord(await callSlack(method, params));
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
def,
|
|
102
|
+
handlers: {
|
|
103
|
+
"chat.postMessage": postMessage,
|
|
104
|
+
"chat.update": update,
|
|
105
|
+
call
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function toRecord(r) {
|
|
110
|
+
return { ok: r.ok, status: r.status, body: r.body, error: r.error };
|
|
111
|
+
}
|
|
112
|
+
function slackOutputSchema() {
|
|
113
|
+
return {
|
|
114
|
+
type: "object",
|
|
115
|
+
properties: {
|
|
116
|
+
ok: { type: "boolean", description: "Slack's logical success flag" },
|
|
117
|
+
status: { type: "number", description: "HTTP status" },
|
|
118
|
+
body: { type: "object", description: "Full Slack response payload" },
|
|
119
|
+
error: { type: "string", description: "Slack error code when ok is false" }
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/connector-slack-plugin.ts
|
|
125
|
+
var ConnectorSlackPlugin = class {
|
|
126
|
+
constructor(options) {
|
|
127
|
+
this.name = "com.objectstack.connector.slack";
|
|
128
|
+
this.version = "1.0.0";
|
|
129
|
+
this.type = "standard";
|
|
130
|
+
// Ensure the automation engine (and its connector registry) is started first.
|
|
131
|
+
this.dependencies = ["com.objectstack.service-automation"];
|
|
132
|
+
this.options = options;
|
|
133
|
+
}
|
|
134
|
+
async init(_ctx) {
|
|
135
|
+
}
|
|
136
|
+
async start(ctx) {
|
|
137
|
+
let automation;
|
|
138
|
+
try {
|
|
139
|
+
automation = ctx.getService("automation");
|
|
140
|
+
} catch {
|
|
141
|
+
automation = void 0;
|
|
142
|
+
}
|
|
143
|
+
if (!automation || typeof automation.registerConnector !== "function") {
|
|
144
|
+
ctx.logger.info("ConnectorSlackPlugin: no automation engine \u2014 Slack connector not registered");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const { def, handlers } = createSlackConnector(this.options);
|
|
148
|
+
automation.registerConnector(def, handlers);
|
|
149
|
+
this.automation = automation;
|
|
150
|
+
this.connectorName = def.name;
|
|
151
|
+
ctx.logger.info(`ConnectorSlackPlugin: Slack connector '${def.name}' registered`);
|
|
152
|
+
}
|
|
153
|
+
async stop(_ctx) {
|
|
154
|
+
if (this.automation && this.connectorName) {
|
|
155
|
+
try {
|
|
156
|
+
this.automation.unregisterConnector(this.connectorName);
|
|
157
|
+
} catch {
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
export {
|
|
163
|
+
ConnectorSlackPlugin,
|
|
164
|
+
createSlackConnector
|
|
165
|
+
};
|
|
166
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/slack-connector.ts","../src/connector-slack-plugin.ts"],"sourcesContent":["// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Connector } from '@objectstack/spec/integration';\n\n/**\n * Slack connector — a *concrete* connector (ADR-0018 §Addendum) and the second\n * reference implementation after `@objectstack/connector-rest`. It produces a\n * {@link Connector} definition plus handlers for a small set of Slack Web API\n * actions, which the baseline `connector_action` node dispatches to.\n *\n * Scope (ADR-0022 \"raw API call\" path): this is the *integration mechanism*\n * for talking to Slack's API — \"post this exact text to this channel\". It is\n * deliberately **not** the human-notification layer: there is no preference\n * matrix, inbox, outbox, or thread/session semantics here. Those belong to a\n * `MessagingChannel` (ADR-0012/0013), which may itself delegate its transport\n * to this connector.\n *\n * Open-source scope: **static** auth only — a Slack **bot token** (`xoxb-…`),\n * supplied by the caller and sent as a bearer credential. OAuth2 install/refresh,\n * credential vaulting, and multi-tenant connection lifecycle are the enterprise\n * tier (see `../cloud/docs/design/connector-tiering.md`) and are out of scope.\n *\n * Slack quirk: the Web API returns HTTP `200` even on logical failure, with the\n * real outcome in the JSON body's `ok` field (and `error` on failure). Handlers\n * therefore surface `ok` from the payload, not from the HTTP status, and never\n * throw on a logical failure — the flow author branches on `${node.ok}`.\n */\n\nexport interface SlackConnectorOptions {\n /** Connector machine name (snake_case). Defaults to `slack`. */\n name?: string;\n /** Human-readable label. Defaults to `Slack`. */\n label?: string;\n /** Slack bot token (`xoxb-…`), sent as `Authorization: Bearer <token>`. */\n token: string;\n /** Web API base URL. Defaults to `https://slack.com/api`. */\n baseUrl?: string;\n /** Headers merged into every request (request-level headers win). */\n defaultHeaders?: Record<string, string>;\n /** Injected for tests; defaults to the global `fetch`. */\n fetchImpl?: typeof fetch;\n}\n\n/** Input accepted by `chat.postMessage`. */\nexport interface SlackPostMessageInput {\n /** Channel id (`C…`), user id (`U…`), or channel name (`#general`). */\n channel: string;\n /** Message text (fallback when `blocks` are present). */\n text?: string;\n /** Thread root `ts` to reply into an existing thread. */\n thread_ts?: string;\n /** Block Kit blocks. */\n blocks?: unknown[];\n [key: string]: unknown;\n}\n\n/** Generic Slack Web API result envelope. */\nexport interface SlackResult {\n /** Slack's logical success flag (from the JSON body, not the HTTP status). */\n ok: boolean;\n /** HTTP status (almost always 200 for the Slack Web API). */\n status: number;\n /** Full parsed Slack payload. */\n body: Record<string, unknown>;\n /** Slack error code when `ok` is false (e.g. `channel_not_found`). */\n error?: string;\n}\n\n/** A connector definition paired with its action handlers, ready for registerConnector(). */\nexport interface SlackConnectorBundle {\n def: Connector;\n handlers: Record<\n string,\n (input: Record<string, unknown>, ctx: unknown) => Promise<Record<string, unknown>>\n >;\n}\n\nexport function createSlackConnector(opts: SlackConnectorOptions): SlackConnectorBundle {\n const name = opts.name ?? 'slack';\n const baseUrl = (opts.baseUrl ?? 'https://slack.com/api').replace(/\\/+$/, '');\n const doFetch = opts.fetchImpl ?? fetch;\n\n const def: Connector = {\n name,\n label: opts.label ?? 'Slack',\n type: 'api',\n description: 'Slack Web API connector (static bot-token auth). Post and update messages, or call any Web API method.',\n icon: 'slack',\n authentication: { type: 'bearer', token: opts.token },\n // Defaulted by ConnectorSchema; set explicitly so the literal satisfies\n // the (post-parse) Connector output type.\n status: 'active',\n enabled: true,\n connectionTimeoutMs: 30000,\n requestTimeoutMs: 30000,\n actions: [\n {\n key: 'chat.postMessage',\n label: 'Post Message',\n description: 'Post a message to a channel, DM, or thread (Slack chat.postMessage).',\n inputSchema: {\n type: 'object',\n required: ['channel'],\n properties: {\n channel: { type: 'string', description: 'Channel id, user id, or #name' },\n text: { type: 'string', description: 'Message text' },\n thread_ts: { type: 'string', description: 'Thread root ts to reply into' },\n blocks: { type: 'array', description: 'Block Kit blocks' },\n },\n },\n outputSchema: slackOutputSchema(),\n },\n {\n key: 'chat.update',\n label: 'Update Message',\n description: 'Edit an existing message (Slack chat.update).',\n inputSchema: {\n type: 'object',\n required: ['channel', 'ts'],\n properties: {\n channel: { type: 'string', description: 'Channel id' },\n ts: { type: 'string', description: 'Timestamp of the message to update' },\n text: { type: 'string', description: 'New message text' },\n blocks: { type: 'array', description: 'New Block Kit blocks' },\n },\n },\n outputSchema: slackOutputSchema(),\n },\n {\n key: 'call',\n label: 'Call Web API Method',\n description: 'Escape hatch — call any Slack Web API method with arbitrary params.',\n inputSchema: {\n type: 'object',\n required: ['method'],\n properties: {\n method: { type: 'string', description: 'Web API method, e.g. conversations.list' },\n params: { type: 'object', description: 'Method parameters (JSON body)' },\n },\n },\n outputSchema: slackOutputSchema(),\n },\n ],\n };\n\n /** POST a JSON body to a Slack Web API method and normalise the result. */\n async function callSlack(method: string, params: Record<string, unknown>): Promise<SlackResult> {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json; charset=utf-8',\n ...opts.defaultHeaders,\n Authorization: `Bearer ${opts.token}`,\n };\n\n const response = await doFetch(`${baseUrl}/${method}`, {\n method: 'POST',\n headers,\n body: JSON.stringify(params),\n });\n\n // The Slack Web API always answers with JSON; `ok` is the real outcome.\n const body = (await response.json()) as Record<string, unknown>;\n const ok = body.ok === true;\n return {\n ok,\n status: response.status,\n body,\n error: ok ? undefined : (body.error as string | undefined),\n };\n }\n\n async function postMessage(input: Record<string, unknown>): Promise<Record<string, unknown>> {\n return toRecord(await callSlack('chat.postMessage', input));\n }\n\n async function update(input: Record<string, unknown>): Promise<Record<string, unknown>> {\n return toRecord(await callSlack('chat.update', input));\n }\n\n async function call(input: Record<string, unknown>): Promise<Record<string, unknown>> {\n const method = String(input.method ?? '');\n if (!method) throw new Error(\"slack 'call' action: 'method' is required\");\n const params = (input.params as Record<string, unknown>) ?? {};\n return toRecord(await callSlack(method, params));\n }\n\n return {\n def,\n handlers: {\n 'chat.postMessage': postMessage,\n 'chat.update': update,\n call,\n },\n };\n}\n\nfunction toRecord(r: SlackResult): Record<string, unknown> {\n return { ok: r.ok, status: r.status, body: r.body, error: r.error };\n}\n\nfunction slackOutputSchema(): Record<string, unknown> {\n return {\n type: 'object',\n properties: {\n ok: { type: 'boolean', description: \"Slack's logical success flag\" },\n status: { type: 'number', description: 'HTTP status' },\n body: { type: 'object', description: 'Full Slack response payload' },\n error: { type: 'string', description: 'Slack error code when ok is false' },\n },\n };\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport type { Connector } from '@objectstack/spec/integration';\nimport { createSlackConnector, type SlackConnectorOptions } from './slack-connector.js';\n\n/**\n * Minimal surface of the automation engine this plugin depends on — the\n * connector registry from ADR-0018 §Addendum. Kept structural so the plugin\n * needs no runtime dependency on `@objectstack/service-automation`.\n */\nexport interface ConnectorRegistrySurface {\n registerConnector(\n def: Connector,\n handlers: Record<\n string,\n (input: Record<string, unknown>, ctx: unknown) => Promise<Record<string, unknown>>\n >,\n ): void;\n unregisterConnector(name: string): void;\n}\n\nexport interface ConnectorSlackPluginOptions extends SlackConnectorOptions {}\n\n/**\n * ConnectorSlackPlugin — registers a Slack Web API connector on the automation\n * engine. The second reference concrete connector (ADR-0018 §Addendum); it\n * enables the ADR-0022 \"raw API call\" path — a flow's `connector_action` step\n * can dispatch to `slack.chat.postMessage` without the messaging stack.\n *\n * If no automation engine is present the plugin logs and skips — the connector\n * has nowhere to register, which is not an error.\n */\nexport class ConnectorSlackPlugin implements Plugin {\n name = 'com.objectstack.connector.slack';\n version = '1.0.0';\n type = 'standard' as const;\n // Ensure the automation engine (and its connector registry) is started first.\n dependencies = ['com.objectstack.service-automation'];\n\n private readonly options: ConnectorSlackPluginOptions;\n private connectorName?: string;\n private automation?: ConnectorRegistrySurface;\n\n constructor(options: ConnectorSlackPluginOptions) {\n this.options = options;\n }\n\n async init(_ctx: PluginContext): Promise<void> {\n // No services to register; the connector is registered in start() once\n // the automation engine is available.\n }\n\n async start(ctx: PluginContext): Promise<void> {\n let automation: ConnectorRegistrySurface | undefined;\n try {\n automation = ctx.getService<ConnectorRegistrySurface>('automation');\n } catch {\n automation = undefined;\n }\n\n if (!automation || typeof automation.registerConnector !== 'function') {\n ctx.logger.info('ConnectorSlackPlugin: no automation engine — Slack connector not registered');\n return;\n }\n\n const { def, handlers } = createSlackConnector(this.options);\n automation.registerConnector(def, handlers);\n this.automation = automation;\n this.connectorName = def.name;\n ctx.logger.info(`ConnectorSlackPlugin: Slack connector '${def.name}' registered`);\n }\n\n async stop(_ctx: PluginContext): Promise<void> {\n if (this.automation && this.connectorName) {\n try { this.automation.unregisterConnector(this.connectorName); } catch { /* ignore */ }\n }\n }\n}\n"],"mappings":";AA6EO,SAAS,qBAAqB,MAAmD;AACpF,QAAM,OAAO,KAAK,QAAQ;AAC1B,QAAM,WAAW,KAAK,WAAW,yBAAyB,QAAQ,QAAQ,EAAE;AAC5E,QAAM,UAAU,KAAK,aAAa;AAElC,QAAM,MAAiB;AAAA,IACnB;AAAA,IACA,OAAO,KAAK,SAAS;AAAA,IACrB,MAAM;AAAA,IACN,aAAa;AAAA,IACb,MAAM;AAAA,IACN,gBAAgB,EAAE,MAAM,UAAU,OAAO,KAAK,MAAM;AAAA;AAAA;AAAA,IAGpD,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,qBAAqB;AAAA,IACrB,kBAAkB;AAAA,IAClB,SAAS;AAAA,MACL;AAAA,QACI,KAAK;AAAA,QACL,OAAO;AAAA,QACP,aAAa;AAAA,QACb,aAAa;AAAA,UACT,MAAM;AAAA,UACN,UAAU,CAAC,SAAS;AAAA,UACpB,YAAY;AAAA,YACR,SAAS,EAAE,MAAM,UAAU,aAAa,gCAAgC;AAAA,YACxE,MAAM,EAAE,MAAM,UAAU,aAAa,eAAe;AAAA,YACpD,WAAW,EAAE,MAAM,UAAU,aAAa,+BAA+B;AAAA,YACzE,QAAQ,EAAE,MAAM,SAAS,aAAa,mBAAmB;AAAA,UAC7D;AAAA,QACJ;AAAA,QACA,cAAc,kBAAkB;AAAA,MACpC;AAAA,MACA;AAAA,QACI,KAAK;AAAA,QACL,OAAO;AAAA,QACP,aAAa;AAAA,QACb,aAAa;AAAA,UACT,MAAM;AAAA,UACN,UAAU,CAAC,WAAW,IAAI;AAAA,UAC1B,YAAY;AAAA,YACR,SAAS,EAAE,MAAM,UAAU,aAAa,aAAa;AAAA,YACrD,IAAI,EAAE,MAAM,UAAU,aAAa,qCAAqC;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AAK,gBAAgB,IAAI;AACzB,QAAI,OAAO,KAAK,0CAA0C,IAAI,IAAI,cAAc;AAAA,EACpF;AAAA,EAEA,MAAM,KAAK,MAAoC;AAC3C,QAAI,KAAK,cAAc,KAAK,eAAe;AACvC,UAAI;AAAE,aAAK,WAAW,oBAAoB,KAAK,aAAa;AAAA,MAAG,QAAQ;AAAA,MAAe;AAAA,IAC1F;AAAA,EACJ;AACJ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@objectstack/connector-slack",
|
|
3
|
+
"version": "7.4.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Slack Web API connector for ObjectStack — registers `chat.postMessage` / `chat.update` / `call` actions on the automation engine's connector registry (ADR-0018 §Addendum, ADR-0022).",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@objectstack/core": "7.4.0",
|
|
17
|
+
"@objectstack/spec": "7.4.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^25.9.1",
|
|
21
|
+
"typescript": "^6.0.3",
|
|
22
|
+
"vitest": "^4.1.7",
|
|
23
|
+
"@objectstack/service-automation": "7.4.0"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"objectstack",
|
|
27
|
+
"connector",
|
|
28
|
+
"slack",
|
|
29
|
+
"integration",
|
|
30
|
+
"messaging"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup --config ../../../tsup.config.ts",
|
|
34
|
+
"test": "vitest run --passWithNoTests"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
import { LiteKernel } from '@objectstack/core';
|
|
5
|
+
import { AutomationServicePlugin, type AutomationEngine } from '@objectstack/service-automation';
|
|
6
|
+
import { ConnectorSlackPlugin } from './connector-slack-plugin.js';
|
|
7
|
+
|
|
8
|
+
/** A fetch stub mimicking the Slack Web API (HTTP 200 + ok in the body). */
|
|
9
|
+
function stubSlack() {
|
|
10
|
+
const calls: Array<{ url: string; init: RequestInit }> = [];
|
|
11
|
+
const impl = (async (url: string, init: RequestInit) => {
|
|
12
|
+
calls.push({ url, init });
|
|
13
|
+
return {
|
|
14
|
+
status: 200,
|
|
15
|
+
ok: true,
|
|
16
|
+
headers: { get: () => 'application/json' },
|
|
17
|
+
json: async () => ({ ok: true, ts: '1700000000.000200', channel: 'C42' }),
|
|
18
|
+
text: async () => '{"ok":true}',
|
|
19
|
+
};
|
|
20
|
+
}) as unknown as typeof fetch;
|
|
21
|
+
return { impl, calls };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('ConnectorSlackPlugin — end to end with the automation engine', () => {
|
|
25
|
+
it('registers the Slack connector so a connector_action flow can post a message', async () => {
|
|
26
|
+
const { impl, calls } = stubSlack();
|
|
27
|
+
|
|
28
|
+
const kernel = new LiteKernel();
|
|
29
|
+
kernel.use(new AutomationServicePlugin());
|
|
30
|
+
kernel.use(new ConnectorSlackPlugin({ token: 'xoxb-secret-token', fetchImpl: impl }));
|
|
31
|
+
await kernel.bootstrap();
|
|
32
|
+
|
|
33
|
+
const engine = kernel.getService<AutomationEngine>('automation');
|
|
34
|
+
|
|
35
|
+
// The baseline dispatch node and the plugin-contributed connector are both present.
|
|
36
|
+
expect(engine.getRegisteredNodeTypes()).toContain('connector_action');
|
|
37
|
+
expect(engine.getRegisteredConnectors()).toContain('slack');
|
|
38
|
+
|
|
39
|
+
engine.registerFlow('notify_channel', {
|
|
40
|
+
name: 'notify_channel',
|
|
41
|
+
label: 'Post to Slack',
|
|
42
|
+
type: 'autolaunched',
|
|
43
|
+
variables: [{ name: 'post.ok', type: 'boolean', isOutput: true }],
|
|
44
|
+
nodes: [
|
|
45
|
+
{ id: 'start', type: 'start', label: 'Start' },
|
|
46
|
+
{
|
|
47
|
+
id: 'post',
|
|
48
|
+
type: 'connector_action',
|
|
49
|
+
label: 'Post to #sales',
|
|
50
|
+
connectorConfig: {
|
|
51
|
+
connectorId: 'slack',
|
|
52
|
+
actionId: 'chat.postMessage',
|
|
53
|
+
input: { channel: 'C42', text: 'Deal closed 🎉' },
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{ id: 'end', type: 'end', label: 'End' },
|
|
57
|
+
],
|
|
58
|
+
edges: [
|
|
59
|
+
{ id: 'e1', source: 'start', target: 'post' },
|
|
60
|
+
{ id: 'e2', source: 'post', target: 'end' },
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const result = await engine.execute('notify_channel');
|
|
65
|
+
|
|
66
|
+
expect(result.success).toBe(true);
|
|
67
|
+
// The Slack connector handled the dispatch: one POST with bearer auth + JSON body.
|
|
68
|
+
expect(calls).toHaveLength(1);
|
|
69
|
+
expect(calls[0].url).toBe('https://slack.com/api/chat.postMessage');
|
|
70
|
+
expect(calls[0].init.method).toBe('POST');
|
|
71
|
+
expect((calls[0].init.headers as Record<string, string>).Authorization).toBe('Bearer xoxb-secret-token');
|
|
72
|
+
expect(calls[0].init.body).toBe('{"channel":"C42","text":"Deal closed 🎉"}');
|
|
73
|
+
// Slack's logical `ok` propagated back into the flow output.
|
|
74
|
+
expect(result.output).toEqual({ 'post.ok': true });
|
|
75
|
+
|
|
76
|
+
await kernel.shutdown();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import type { Plugin, PluginContext } from '@objectstack/core';
|
|
4
|
+
import type { Connector } from '@objectstack/spec/integration';
|
|
5
|
+
import { createSlackConnector, type SlackConnectorOptions } from './slack-connector.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Minimal surface of the automation engine this plugin depends on — the
|
|
9
|
+
* connector registry from ADR-0018 §Addendum. Kept structural so the plugin
|
|
10
|
+
* needs no runtime dependency on `@objectstack/service-automation`.
|
|
11
|
+
*/
|
|
12
|
+
export interface ConnectorRegistrySurface {
|
|
13
|
+
registerConnector(
|
|
14
|
+
def: Connector,
|
|
15
|
+
handlers: Record<
|
|
16
|
+
string,
|
|
17
|
+
(input: Record<string, unknown>, ctx: unknown) => Promise<Record<string, unknown>>
|
|
18
|
+
>,
|
|
19
|
+
): void;
|
|
20
|
+
unregisterConnector(name: string): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ConnectorSlackPluginOptions extends SlackConnectorOptions {}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* ConnectorSlackPlugin — registers a Slack Web API connector on the automation
|
|
27
|
+
* engine. The second reference concrete connector (ADR-0018 §Addendum); it
|
|
28
|
+
* enables the ADR-0022 "raw API call" path — a flow's `connector_action` step
|
|
29
|
+
* can dispatch to `slack.chat.postMessage` without the messaging stack.
|
|
30
|
+
*
|
|
31
|
+
* If no automation engine is present the plugin logs and skips — the connector
|
|
32
|
+
* has nowhere to register, which is not an error.
|
|
33
|
+
*/
|
|
34
|
+
export class ConnectorSlackPlugin implements Plugin {
|
|
35
|
+
name = 'com.objectstack.connector.slack';
|
|
36
|
+
version = '1.0.0';
|
|
37
|
+
type = 'standard' as const;
|
|
38
|
+
// Ensure the automation engine (and its connector registry) is started first.
|
|
39
|
+
dependencies = ['com.objectstack.service-automation'];
|
|
40
|
+
|
|
41
|
+
private readonly options: ConnectorSlackPluginOptions;
|
|
42
|
+
private connectorName?: string;
|
|
43
|
+
private automation?: ConnectorRegistrySurface;
|
|
44
|
+
|
|
45
|
+
constructor(options: ConnectorSlackPluginOptions) {
|
|
46
|
+
this.options = options;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async init(_ctx: PluginContext): Promise<void> {
|
|
50
|
+
// No services to register; the connector is registered in start() once
|
|
51
|
+
// the automation engine is available.
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async start(ctx: PluginContext): Promise<void> {
|
|
55
|
+
let automation: ConnectorRegistrySurface | undefined;
|
|
56
|
+
try {
|
|
57
|
+
automation = ctx.getService<ConnectorRegistrySurface>('automation');
|
|
58
|
+
} catch {
|
|
59
|
+
automation = undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!automation || typeof automation.registerConnector !== 'function') {
|
|
63
|
+
ctx.logger.info('ConnectorSlackPlugin: no automation engine — Slack connector not registered');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const { def, handlers } = createSlackConnector(this.options);
|
|
68
|
+
automation.registerConnector(def, handlers);
|
|
69
|
+
this.automation = automation;
|
|
70
|
+
this.connectorName = def.name;
|
|
71
|
+
ctx.logger.info(`ConnectorSlackPlugin: Slack connector '${def.name}' registered`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async stop(_ctx: PluginContext): Promise<void> {
|
|
75
|
+
if (this.automation && this.connectorName) {
|
|
76
|
+
try { this.automation.unregisterConnector(this.connectorName); } catch { /* ignore */ }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @objectstack/connector-slack
|
|
5
|
+
*
|
|
6
|
+
* Slack Web API connector — a concrete connector (ADR-0018 §Addendum) and the
|
|
7
|
+
* second reference implementation after `@objectstack/connector-rest`. The
|
|
8
|
+
* baseline automation engine ships the `connector_action` dispatch node + an
|
|
9
|
+
* empty connector registry; this plugin populates it with a `slack` connector
|
|
10
|
+
* exposing `chat.postMessage`, `chat.update`, and a generic `call` action.
|
|
11
|
+
*
|
|
12
|
+
* This is the integration mechanism (ADR-0022 "raw API call" path), not the
|
|
13
|
+
* human-notification layer. Static bot-token auth only; OAuth2 install/refresh,
|
|
14
|
+
* credential vaulting, and multi-tenant lifecycle are the enterprise tier.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
createSlackConnector,
|
|
19
|
+
type SlackConnectorOptions,
|
|
20
|
+
type SlackConnectorBundle,
|
|
21
|
+
type SlackPostMessageInput,
|
|
22
|
+
type SlackResult,
|
|
23
|
+
} from './slack-connector.js';
|
|
24
|
+
export {
|
|
25
|
+
ConnectorSlackPlugin,
|
|
26
|
+
type ConnectorSlackPluginOptions,
|
|
27
|
+
type ConnectorRegistrySurface,
|
|
28
|
+
} from './connector-slack-plugin.js';
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
import { createSlackConnector } from './slack-connector.js';
|
|
5
|
+
|
|
6
|
+
// ─── Helpers ─────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
interface CapturedCall {
|
|
9
|
+
url: string;
|
|
10
|
+
init: RequestInit;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A fetch stub mimicking the Slack Web API: always HTTP 200, with the logical
|
|
15
|
+
* outcome carried in the JSON body's `ok` field.
|
|
16
|
+
*/
|
|
17
|
+
function stubSlack(responseBody: Record<string, unknown> = { ok: true }) {
|
|
18
|
+
const calls: CapturedCall[] = [];
|
|
19
|
+
const impl = (async (url: string, init: RequestInit) => {
|
|
20
|
+
calls.push({ url, init });
|
|
21
|
+
return {
|
|
22
|
+
status: 200,
|
|
23
|
+
ok: true,
|
|
24
|
+
headers: { get: () => 'application/json' },
|
|
25
|
+
json: async () => responseBody,
|
|
26
|
+
text: async () => JSON.stringify(responseBody),
|
|
27
|
+
};
|
|
28
|
+
}) as unknown as typeof fetch;
|
|
29
|
+
return { impl, calls };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function headersOf(call: CapturedCall): Record<string, string> {
|
|
33
|
+
return (call.init.headers ?? {}) as Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ─── definition ──────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
describe('createSlackConnector — definition', () => {
|
|
39
|
+
it('declares a slack connector with the expected actions and bearer auth', () => {
|
|
40
|
+
const { def, handlers } = createSlackConnector({ token: 'xoxb-1' });
|
|
41
|
+
|
|
42
|
+
expect(def.name).toBe('slack');
|
|
43
|
+
expect(def.type).toBe('api');
|
|
44
|
+
expect(def.authentication).toEqual({ type: 'bearer', token: 'xoxb-1' });
|
|
45
|
+
|
|
46
|
+
const keys = (def.actions ?? []).map((a) => a.key);
|
|
47
|
+
expect(keys).toEqual(['chat.postMessage', 'chat.update', 'call']);
|
|
48
|
+
// Every declared action has a handler (the registry enforces this too).
|
|
49
|
+
for (const k of keys) expect(typeof handlers[k]).toBe('function');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// ─── chat.postMessage ────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
describe('createSlackConnector — chat.postMessage', () => {
|
|
56
|
+
it('POSTs JSON to the method URL with a bearer token and returns ok from the payload', async () => {
|
|
57
|
+
const { impl, calls } = stubSlack({ ok: true, ts: '1700000000.000100', channel: 'C123' });
|
|
58
|
+
const { handlers } = createSlackConnector({ token: 'xoxb-secret', fetchImpl: impl });
|
|
59
|
+
|
|
60
|
+
const out = await handlers['chat.postMessage'](
|
|
61
|
+
{ channel: 'C123', text: 'hello', thread_ts: '1699999999.0001' },
|
|
62
|
+
{},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(calls).toHaveLength(1);
|
|
66
|
+
expect(calls[0].url).toBe('https://slack.com/api/chat.postMessage');
|
|
67
|
+
expect(calls[0].init.method).toBe('POST');
|
|
68
|
+
expect(headersOf(calls[0]).Authorization).toBe('Bearer xoxb-secret');
|
|
69
|
+
expect(calls[0].init.body).toBe('{"channel":"C123","text":"hello","thread_ts":"1699999999.0001"}');
|
|
70
|
+
|
|
71
|
+
expect(out.ok).toBe(true);
|
|
72
|
+
expect(out.status).toBe(200);
|
|
73
|
+
expect(out.body).toEqual({ ok: true, ts: '1700000000.000100', channel: 'C123' });
|
|
74
|
+
expect(out.error).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('surfaces a Slack logical failure (HTTP 200, ok:false) without throwing', async () => {
|
|
78
|
+
const { impl } = stubSlack({ ok: false, error: 'channel_not_found' });
|
|
79
|
+
const { handlers } = createSlackConnector({ token: 'xoxb-1', fetchImpl: impl });
|
|
80
|
+
|
|
81
|
+
const out = await handlers['chat.postMessage']({ channel: 'nope', text: 'hi' }, {});
|
|
82
|
+
|
|
83
|
+
expect(out.ok).toBe(false);
|
|
84
|
+
expect(out.error).toBe('channel_not_found');
|
|
85
|
+
expect(out.status).toBe(200);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// ─── chat.update + call ───────────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
describe('createSlackConnector — chat.update & generic call', () => {
|
|
92
|
+
it('chat.update hits the chat.update method', async () => {
|
|
93
|
+
const { impl, calls } = stubSlack({ ok: true });
|
|
94
|
+
const { handlers } = createSlackConnector({ token: 'xoxb-1', fetchImpl: impl });
|
|
95
|
+
|
|
96
|
+
await handlers['chat.update']({ channel: 'C1', ts: '170.1', text: 'edited' }, {});
|
|
97
|
+
expect(calls[0].url).toBe('https://slack.com/api/chat.update');
|
|
98
|
+
expect(calls[0].init.body).toBe('{"channel":"C1","ts":"170.1","text":"edited"}');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('call dispatches to an arbitrary Web API method with params', async () => {
|
|
102
|
+
const { impl, calls } = stubSlack({ ok: true, channels: [] });
|
|
103
|
+
const { handlers } = createSlackConnector({ token: 'xoxb-1', fetchImpl: impl });
|
|
104
|
+
|
|
105
|
+
const out = await handlers.call({ method: 'conversations.list', params: { limit: 50 } }, {});
|
|
106
|
+
expect(calls[0].url).toBe('https://slack.com/api/conversations.list');
|
|
107
|
+
expect(calls[0].init.body).toBe('{"limit":50}');
|
|
108
|
+
expect(out.ok).toBe(true);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('call throws when method is missing', async () => {
|
|
112
|
+
const { impl } = stubSlack();
|
|
113
|
+
const { handlers } = createSlackConnector({ token: 'xoxb-1', fetchImpl: impl });
|
|
114
|
+
await expect(handlers.call({ params: {} }, {})).rejects.toThrow(/method.*required/);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('honours a custom baseUrl and merges defaultHeaders', async () => {
|
|
118
|
+
const { impl, calls } = stubSlack();
|
|
119
|
+
const { handlers } = createSlackConnector({
|
|
120
|
+
token: 'xoxb-1',
|
|
121
|
+
baseUrl: 'https://slack.example.test/api/',
|
|
122
|
+
defaultHeaders: { 'X-Trace': 'on' },
|
|
123
|
+
fetchImpl: impl,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
await handlers['chat.postMessage']({ channel: 'C1', text: 'x' }, {});
|
|
127
|
+
expect(calls[0].url).toBe('https://slack.example.test/api/chat.postMessage');
|
|
128
|
+
expect(headersOf(calls[0])['X-Trace']).toBe('on');
|
|
129
|
+
// Auth still wins over defaultHeaders.
|
|
130
|
+
expect(headersOf(calls[0]).Authorization).toBe('Bearer xoxb-1');
|
|
131
|
+
});
|
|
132
|
+
});
|