@capybara-hq/plugin-slack 0.1.0-rc.4 → 0.1.0-rc.5

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
3
  "name": "slack",
4
- "version": "0.1.0-rc.4",
4
+ "version": "0.1.0-rc.5",
5
5
  "publisher": "Capybara",
6
6
  "description": "Slack (channel@1 bot over Socket Mode with Block Kit cards, identity-source@1 Sign in with Slack, directory@1)",
7
7
  "displayName": {
@@ -19,67 +19,19 @@
19
19
  "setupGuides": [
20
20
  {
21
21
  "id": "slack-app",
22
- "place": "channel-connection",
23
- "title": {
24
- "en": "Create the Slack app",
25
- "zh": "创建 Slack 应用"
26
- },
27
- "steps": [
28
- {
29
- "en": "Open https://api.slack.com/apps, choose Create New App → From a manifest, pick your workspace and paste the app manifest below. It registers the bot scopes, the events and the slash commands `/new`, `/env` and `/capybara` the plugin needs.",
30
- "zh": "打开 https://api.slack.com/apps,选择 Create New App → From a manifest,选中工作区,粘贴下面的应用 manifest。它会登记插件需要的机器人权限、事件以及斜杠命令 `/new`、`/env`、`/capybara`。"
31
- },
32
- {
33
- "en": "Under Basic Information → App-Level Tokens choose Generate Token and Scopes, add the scope `connections:write` and copy the `xapp-…` token into the App-level token field. Socket Mode needs no public URL.",
34
- "zh": "在 Basic Information → App-Level Tokens 选择 Generate Token and Scopes,添加权限 `connections:write`,把 `xapp-…` 令牌填进「应用令牌」。Socket Mode 不需要公网地址。",
35
- "mode": "long_connection"
36
- },
37
- {
38
- "en": "Slack checks the Request URL {{webhookUrl}} when you create the app or save Event Subscriptions: the connection must exist first, and Capybara answers the check. If Slack reports it unverified, save the channel connection, then retry under Event Subscriptions.",
39
- "zh": "创建应用或保存 Event Subscriptions 时,Slack 会校验 Request URL {{webhookUrl}}:渠道连接要先存在,由 Capybara 应答校验。若 Slack 提示未通过,先保存渠道连接,再到 Event Subscriptions 重试。",
40
- "mode": "webhook"
41
- },
42
- {
43
- "en": "Choose Install App → Install to Workspace and copy the Bot User OAuth Token (`xoxb-…`) into the Bot token field.",
44
- "zh": "选择 Install App → Install to Workspace,把 Bot User OAuth Token(`xoxb-…`)填进「机器人令牌」。"
45
- },
46
- {
47
- "en": "Copy Basic Information → App Credentials → Signing Secret into the Signing secret field: every webhook request is verified with it, and without it every request is refused.",
48
- "zh": "把 Basic Information → App Credentials → Signing Secret 填进「签名密钥」:每个 webhook 请求都用它校验,不填则全部拒绝。",
49
- "mode": "webhook"
50
- },
51
- {
52
- "en": "The manifest's redirect URL {{signInCallbackUrl}} is for Sign in with Slack; it needs `http.publicUrl` set on the deployment. Without it, remove the redirect URL or ask your super admin to set it.",
53
- "zh": "manifest 里的重定向地址 {{signInCallbackUrl}} 用于 Slack 登录,需要部署设置 `http.publicUrl`。未设置时,删掉该重定向地址,或请超级管理员设置。"
54
- }
55
- ],
56
- "files": [
57
- {
58
- "path": "./guides/app-manifest.socket.yaml.tmpl",
59
- "title": {
60
- "en": "App manifest (Socket Mode)",
61
- "zh": "应用 manifest(Socket Mode)"
62
- },
63
- "language": "yaml",
64
- "mode": "long_connection"
65
- },
66
- {
67
- "path": "./guides/app-manifest.webhook.yaml.tmpl",
68
- "title": {
69
- "en": "App manifest (webhook)",
70
- "zh": "应用 manifest(Webhook)"
71
- },
72
- "language": "yaml",
73
- "mode": "webhook"
74
- }
75
- ]
22
+ "place": "channel-connection"
23
+ },
24
+ {
25
+ "id": "slack-connector",
26
+ "place": "connector-template",
27
+ "template": "slack"
76
28
  }
77
29
  ],
78
30
  "extension": {
79
31
  "entry": "./dist/index.mjs",
80
32
  "manifest": {
81
33
  "name": "slack",
82
- "version": "0.1.0-rc.4",
34
+ "version": "0.1.0-rc.5",
83
35
  "sdk": "^0.1.0-rc.0",
84
36
  "provides": [
85
37
  "channel@1",
@@ -92,6 +44,11 @@
92
44
  "network": [
93
45
  "slack.com",
94
46
  "wss-primary.slack.com"
47
+ ],
48
+ "setupChecks": [
49
+ "bot-token",
50
+ "bot-scopes",
51
+ "app-token"
95
52
  ]
96
53
  },
97
54
  "config": {
package/README.md CHANGED
@@ -18,44 +18,39 @@ The Slack docs cited here were consulted on 2026-10-20.
18
18
 
19
19
  ## Create the Slack app
20
20
 
21
- The plugin ships a **setup guide** (`plugin.json` `setupGuides`, id `slack-app`): when an org admin
22
- creates a Slack channel connection (Channels → New channel) or opens one, the web app shows the steps
23
- below and the app manifest rendered for that connection, with a copy button and a switch between
24
- Socket Mode and webhook mode. The manifest is filled in with the connection's name, the Sign in with
25
- Slack callback (`<http.publicUrl>/api/v1/auth/sso/slack/callback`) and, in webhook mode, the
26
- connection's webhook URL. When a plugin upgrade changes the manifest, the channel page says so and
27
- asks you to update the Slack app. The templates are in [`guides/`](./guides):
28
- [`app-manifest.socket.yaml.tmpl`](./guides/app-manifest.socket.yaml.tmpl) (Socket Mode) and
29
- [`app-manifest.webhook.yaml.tmpl`](./guides/app-manifest.webhook.yaml.tmpl) (webhook mode); outside
30
- Capybara, replace each `{{…}}` placeholder by hand.
31
-
32
- 1. Open <https://api.slack.com/apps> → **Create New App** → **From a manifest**, pick the workspace, and paste the
33
- manifest from the setup guide (format: [App manifest reference](https://docs.slack.dev/reference/app-manifest)).
34
- It turns on Socket Mode (or, in webhook mode, sets the Request URLs) and Interactivity, subscribes to the events `message.im`, `app_mention`, `message.channels`,
35
- `message.groups`, `team_join` and `user_change`, registers the slash commands `/new`, `/env` and
36
- `/capybara` (see [Commands](#commands)), and requests these bot scopes:
37
-
38
- | scope | Purpose |
39
- |---|---|
40
- | `chat:write` | Send and update replies and cards; post the visible echo of a slash command run in a channel |
41
- | `commands` | Receive the slash commands `/new`, `/env`, `/capybara` |
42
- | `im:write` | `conversations.open`: tell the bot's own DM from a DM between two people where a slash command was run |
43
- | `im:history` | Receive DM messages |
44
- | `app_mentions:read` | Receive messages that @-mention the bot in channels |
45
- | `channels:history` / `groups:history` | Receive thread replies in public / private channels |
46
- | `users:read` | Look up the speaker's display name, avatar, time zone and language (`users.info`); people-change events; directory `users.list` |
47
- | `users:read.email` | Read emails during directory sync (the bot itself does not use it) |
48
-
49
- 2. **Basic Information → App-Level Tokens → Generate Token and Scopes**: add the scope `connections:write`
50
- to get the app-level token `xapp-…` (Socket Mode uses it to call `apps.connections.open`).
51
- 3. **Install App → Install to Workspace** to get the bot token `xoxb-…`.
52
- 4. In the Capybara org settings, enable the `slack` plugin (its org-level fields are optional and only for
53
- [directory sync](#directory-directory1): leave them empty if you only chat with the bot), then **create a new channel
54
- connection** under Channels: pick Slack as the plugin, fill in the app-level token (`appToken`, `xapp-…`) and
55
- the bot token (`botToken`, `xoxb-…`), and bind an agent. Both are stored in the secret store, and the API only returns "set".
56
- The app's name in the manifest is the connection's name; Slack caps it at 35 characters, so shorten it in
57
- the app settings if Slack refuses it (the bot user's name stays `capybara`).
58
- 5. In Slack, DM the bot, or add the bot to a channel and @-mention it. The first conversation gets a binding card; click the button to confirm the binding on the web page.
21
+ The steps live in the product: the plugin ships the setup guide `slack-app`
22
+ ([`guides/slack-app.md`](./guides/slack-app.md), Chinese
23
+ [`guides/slack-app.zh.md`](./guides/slack-app.zh.md)). When an org admin creates a Slack channel
24
+ connection (Channels → New channel) or opens one, the web app shows the guide for the connection's
25
+ inbound mode (Socket Mode or webhook) with the app manifest filled in for that connection (its name,
26
+ the Sign in with Slack callback and, in webhook mode, its webhook URL) and a copy button. When a
27
+ plugin upgrade changes the manifest, the channel page says so and asks you to update the Slack app.
28
+ To preview the guide outside Capybara, run
29
+ `capybara-plugin guide slack-app --mode long_connection` (or `--mode webhook`, `--locale zh`) in
30
+ this repository.
31
+
32
+ The guide carries live checks, run when the page is viewed (cached for five minutes) or on
33
+ **Check again**:
34
+
35
+ | Check | What it does |
36
+ |---|---|
37
+ | `bot-token` | `auth.test` with the connection's bot token: refused (`invalid_auth`, `token_revoked`, …), wrong token type or an org-wide install is failing with the fix |
38
+ | `bot-scopes` | Compares the scopes Slack reports for the bot token (`x-oauth-scopes`) with the manifest's bot scopes and names the missing ones |
39
+ | `app-token` | `apps.connections.open` with the app-level token (Socket Mode only; the socket is never opened) |
40
+
41
+ Each check makes one read-only call, passes the kernel's abort signal on and never retries; rate
42
+ limits and Slack outages answer "unknown", never "failing". The guide also uses the kernel checks
43
+ `capybara:public-url`, `capybara:webhook-public` and `capybara:webhook-received`.
44
+
45
+ Notes for operators:
46
+
47
+ - The org must enable the `slack` plugin before it can create a connection (its org-level fields are
48
+ optional and only for [directory sync](#directory-directory1)). The tokens are stored in the
49
+ secret store, and the API only returns "set".
50
+ - The app's name in the manifest is the connection's name; Slack caps it at 35 characters, so shorten it in
51
+ the app settings if Slack refuses it (the bot user's name stays `capybara`).
52
+ - In Slack, DM the bot, or add the bot to a channel and @-mention it. The first conversation gets a
53
+ binding card; click the button to confirm the binding on the web page.
59
54
 
60
55
  Note: apps that use Socket Mode cannot currently be listed in the public Slack Marketplace, and one app can hold at most 10 concurrent
61
56
  Socket Mode connections ([Using Socket Mode](https://docs.slack.dev/apis/events-api/using-socket-mode)). At any moment Capybara has only one
@@ -160,44 +155,27 @@ on the caller's own user token; there is no shared org token. Design and decisio
160
155
  agents change the policies on the tools page; tools Slack adds later default by their annotations (`ask`
161
156
  without `readOnlyHint`).
162
157
 
163
- ### Create the OAuth app (Slack admin)
164
-
165
- The recommended setup is a separate internal app, "Capybara connector" (user scopes only, MCP on, no bot):
166
-
167
- 1. Open <https://api.slack.com/apps> → **Create New App** → **From a manifest** in the company workspace and
168
- paste [`app-manifest.connector.yaml`](./app-manifest.connector.yaml), with its redirect URL replaced by
169
- `<http.publicUrl>/api/v1/oauth/callback` (for example
170
- `https://capybara.example.com/api/v1/oauth/callback`). The deployment must set `http.publicUrl`. The
171
- manifest turns on MCP (`is_mcp_enabled`) and requests the user scopes the template asks for:
172
- `search:read.public`, `search:read.private`, `search:read.mpim`, `search:read.im`, `search:read.files`,
173
- `search:read.users`, `channels:history`, `groups:history`, `mpim:history`, `im:history`,
174
- `channels:read`, `groups:read`, `mpim:read`, `im:read`, `channels:write`, `groups:write`, `mpim:write`,
175
- `im:write`, `chat:write`, `canvases:read`, `canvases:write`, `lists:read`, `lists:write`, `files:read`,
176
- `files:write`, `reactions:read`, `reactions:write`, `emoji:read`, `users:read`, `users:read.email`.
177
- 2. Leave **Manage Distribution** off: Slack's MCP server serves only internal apps and Marketplace apps,
178
- and an internal app cannot be authorized from other workspaces.
179
- 3. Do **not** enable PKCE in the app settings: it turns the app into a public client for good. Capybara
180
- stays a confidential client (it still sends a PKCE challenge, which Slack's metadata advertises).
181
- 4. Keep token rotation off (the manifest does). Without rotation, user tokens do not expire.
182
- 5. Approve or install the app as your workspace requires (with "Only allow pre-approved apps", a workspace
183
- admin approves it; on Enterprise Grid, an org admin).
184
- 6. Copy **Basic Information → App Credentials → Client ID** and **Client Secret**.
185
-
186
- Reusing the channel app instead is possible as long as token rotation stays off (rotation is app-wide and
187
- would expire the bot's static `xoxb` token): add the user scopes, turn on MCP and add the redirect URL. Slack
188
- prefix-matches redirect URLs, so one entry `<http.publicUrl>/api/v1/` covers both this callback and the Sign
189
- in with Slack callback that the [setup guide](#create-the-slack-app) puts in the bot app's manifest.
190
-
191
- ### Turn it on (org admin) and connect (members)
192
-
193
- 1. Org settings → plugins → Slack: switch on the Slack connector template and enter the client
194
- (`clientId`, `clientSecret`) from step 6. The secret is stored encrypted and reads back only as "set";
195
- rotate it later through the connector's org credentials. Each org enters its own client (the same app may
196
- serve several orgs).
197
- 2. Members open **My connections** (`/account/connections`), click **Connect** on Slack and approve on
198
- Slack's consent screen. Until they do, a Slack tool call shows the standard "connection required" card.
199
- 3. **Disconnect** deletes the stored token in Capybara but does not revoke it at Slack: remove the
200
- authorization in Slack too (the app's page → **Remove app**, or a workspace admin under **Manage apps**).
158
+ ### Set it up
159
+
160
+ The connector template carries the setup guide `slack-connector`
161
+ ([`guides/slack-connector.md`](./guides/slack-connector.md), Chinese
162
+ [`guides/slack-connector.zh.md`](./guides/slack-connector.zh.md)), shown on the template's pages:
163
+ org admins see how to create the internal Slack app (the manifest with this deployment's OAuth
164
+ callback filled in), keep it internal and enter its client id and secret; members see how to connect
165
+ their own Slack account. Preview it with
166
+ `capybara-plugin guide slack-connector --audience admin` (or `--audience member`). Its rows are
167
+ kernel checks: `capybara:public-url`, `capybara:oauth-client`, `capybara:oauth-token-endpoint`
168
+ (the stored client against Slack's token endpoint; the secret stays in the kernel),
169
+ `capybara:connector-tools` and, for members, `capybara:connector-connected`.
170
+
171
+ Notes for operators:
172
+
173
+ - The deployment must set `http.publicUrl`: the OAuth callback is
174
+ `<http.publicUrl>/api/v1/oauth/callback`.
175
+ - Each org enters its own client (the same Slack app may serve several orgs); rotate the secret later
176
+ through the connector's org credentials.
177
+ - Capybara stays a confidential client: it still sends a PKCE challenge (Slack's metadata advertises
178
+ it), but PKCE must not be enabled in the app settings.
201
179
 
202
180
  ### Decided risks
203
181
 
package/dist/index.mjs CHANGED
@@ -25872,9 +25872,9 @@ var require_cache_handler = __commonJS({
25872
25872
  function appendConnectionHeaderTokens(headersToRemove, connectionHeader) {
25873
25873
  const values = Array.isArray(connectionHeader) ? connectionHeader : [connectionHeader];
25874
25874
  for (let i = 0; i < values.length; i++) {
25875
- const tokens = values[i].split(",");
25876
- for (let j = 0; j < tokens.length; j++) {
25877
- headersToRemove.push(trimOWS(tokens[j]).toLowerCase());
25875
+ const tokens2 = values[i].split(",");
25876
+ for (let j = 0; j < tokens2.length; j++) {
25877
+ headersToRemove.push(trimOWS(tokens2[j]).toLowerCase());
25878
25878
  }
25879
25879
  }
25880
25880
  }
@@ -39190,26 +39190,355 @@ function createSlackIdentitySource() {
39190
39190
  };
39191
39191
  }
39192
39192
 
39193
+ // src/setup-checks.ts
39194
+ var REQUIRED_BOT_SCOPES = [
39195
+ "chat:write",
39196
+ "commands",
39197
+ "im:write",
39198
+ "im:history",
39199
+ "app_mentions:read",
39200
+ "channels:history",
39201
+ "groups:history",
39202
+ "users:read",
39203
+ "users:read.email"
39204
+ ];
39205
+ var ERROR_CODE_RE = /^[a-z][a-z0-9_.]{0,63}$/;
39206
+ async function slackCall(apiUrl, method, token, signal) {
39207
+ let res;
39208
+ try {
39209
+ res = await fetch(new URL(method, apiUrl), {
39210
+ method: "POST",
39211
+ headers: {
39212
+ "content-type": "application/x-www-form-urlencoded",
39213
+ authorization: `Bearer ${token}`
39214
+ },
39215
+ body: "",
39216
+ redirect: "manual",
39217
+ signal
39218
+ });
39219
+ } catch {
39220
+ return { kind: "network" };
39221
+ }
39222
+ if (res.status !== 200) {
39223
+ await res.body?.cancel().catch(() => void 0);
39224
+ return { kind: "http", status: res.status };
39225
+ }
39226
+ let body;
39227
+ try {
39228
+ body = await res.json();
39229
+ } catch {
39230
+ return { kind: "network" };
39231
+ }
39232
+ if (typeof body !== "object" || body === null) return { kind: "error", code: "invalid_response" };
39233
+ const b = body;
39234
+ if (b.ok === true) return { kind: "ok", body: b, headers: res.headers };
39235
+ const code = typeof b.error === "string" && ERROR_CODE_RE.test(b.error) ? b.error : "unknown_error";
39236
+ return { kind: "error", code };
39237
+ }
39238
+ var unknown = (reason) => ({ status: "unknown", reason });
39239
+ var failing = (reason, fix) => ({ status: "failing", reason, fix });
39240
+ var TRANSIENT = /* @__PURE__ */ new Set([
39241
+ "ratelimited",
39242
+ "rate_limited",
39243
+ "internal_error",
39244
+ "fatal_error",
39245
+ "service_unavailable",
39246
+ "request_timeout"
39247
+ ]);
39248
+ var REFUSED = /* @__PURE__ */ new Set([
39249
+ "invalid_auth",
39250
+ "not_authed",
39251
+ "token_revoked",
39252
+ "token_expired",
39253
+ "account_inactive",
39254
+ "no_permission",
39255
+ "ekm_access_denied"
39256
+ ]);
39257
+ function notAnswered(r) {
39258
+ if (r.kind === "network")
39259
+ return unknown({
39260
+ en: "Could not reach Slack (network error or timeout); check again later.",
39261
+ zh: "无法连接 Slack(网络错误或超时),请稍后再检查。"
39262
+ });
39263
+ if (r.kind === "http") {
39264
+ if (r.status === 429)
39265
+ return unknown({
39266
+ en: "Slack is rate limiting requests; check again in a minute.",
39267
+ zh: "Slack 正在限流,请一分钟后再检查。"
39268
+ });
39269
+ return unknown({
39270
+ en: `Slack answered HTTP ${r.status}; check again later.`,
39271
+ zh: `Slack 返回 HTTP ${r.status},请稍后再检查。`
39272
+ });
39273
+ }
39274
+ if (r.code === "ratelimited" || r.code === "rate_limited")
39275
+ return unknown({
39276
+ en: "Slack is rate limiting requests; check again in a minute.",
39277
+ zh: "Slack 正在限流,请一分钟后再检查。"
39278
+ });
39279
+ if (TRANSIENT.has(r.code))
39280
+ return unknown({
39281
+ en: `Slack is temporarily unavailable (${r.code}); check again later.`,
39282
+ zh: `Slack 暂时不可用(${r.code}),请稍后再检查。`
39283
+ });
39284
+ return unknown({
39285
+ en: `Slack answered ${r.code}; the check could not tell whether the setup is right.`,
39286
+ zh: `Slack 返回 ${r.code},无法判断设置是否正确。`
39287
+ });
39288
+ }
39289
+ var BOT_TOKEN_FIX = {
39290
+ en: "Under Install App, reinstall the app to the workspace and copy the new Bot User OAuth Token (xoxb-…) into the Bot token field.",
39291
+ zh: "在 Install App 中把应用重新安装到工作区,再把新的 Bot User OAuth Token(xoxb-…)填进「机器人令牌」。"
39292
+ };
39293
+ var MANIFEST_FIX = {
39294
+ en: "Open the app at https://api.slack.com/apps, paste the manifest from this guide under App Manifest, save, then reinstall the app under Install App.",
39295
+ zh: "在 https://api.slack.com/apps 打开该应用,在 App Manifest 粘贴本指南中的 manifest 并保存,然后在 Install App 中重新安装应用。"
39296
+ };
39297
+ function botTokenRefused(code) {
39298
+ if (REFUSED.has(code))
39299
+ return failing(
39300
+ {
39301
+ en: `Slack refused the bot token (${code}); it may have been revoked or the app uninstalled.`,
39302
+ zh: `Slack 拒绝了机器人令牌(${code}),令牌可能已被撤销或应用已被卸载。`
39303
+ },
39304
+ BOT_TOKEN_FIX
39305
+ );
39306
+ if (code === "not_allowed_token_type")
39307
+ return failing(
39308
+ {
39309
+ en: "The Bot token field holds a token of the wrong type (not_allowed_token_type).",
39310
+ zh: "「机器人令牌」中的令牌类型不对(not_allowed_token_type)。"
39311
+ },
39312
+ {
39313
+ en: "Copy the Bot User OAuth Token (xoxb-…) from Install App, not a user or app-level token.",
39314
+ zh: "从 Install App 复制 Bot User OAuth Token(xoxb-…),不要用用户令牌或应用级令牌。"
39315
+ }
39316
+ );
39317
+ if (code === "missing_scope")
39318
+ return failing(
39319
+ {
39320
+ en: "Slack refused the call for a missing scope (missing_scope).",
39321
+ zh: "Slack 因缺少权限拒绝了调用(missing_scope)。"
39322
+ },
39323
+ MANIFEST_FIX
39324
+ );
39325
+ return void 0;
39326
+ }
39327
+ function plain2(s, max = 80) {
39328
+ if (typeof s !== "string") return void 0;
39329
+ const t = s.replace(/[\p{C}\p{Zl}\p{Zp}]/gu, "").trim().slice(0, max);
39330
+ return t === "" ? void 0 : t;
39331
+ }
39332
+ function tokens(args) {
39333
+ if (args.target.kind !== "channel-connection") return void 0;
39334
+ const c = args.target.config;
39335
+ if (typeof c !== "object" || c === null) return {};
39336
+ const x = c;
39337
+ return {
39338
+ ...typeof x.botToken === "string" && x.botToken !== "" && { botToken: x.botToken },
39339
+ ...typeof x.appToken === "string" && x.appToken !== "" && { appToken: x.appToken }
39340
+ };
39341
+ }
39342
+ var NOT_A_CONNECTION = unknown({
39343
+ en: "This check applies to Slack channel connections only.",
39344
+ zh: "此检查只适用于 Slack 渠道连接。"
39345
+ });
39346
+ var NO_BOT_TOKEN = failing(
39347
+ { en: "No bot token is set on this connection.", zh: "这个渠道连接还没有填写机器人令牌。" },
39348
+ {
39349
+ en: "Install the app under Install App and copy the Bot User OAuth Token (xoxb-…) into the Bot token field.",
39350
+ zh: "在 Install App 安装应用,把 Bot User OAuth Token(xoxb-…)填进「机器人令牌」。"
39351
+ }
39352
+ );
39353
+ function botTokenCheck(opts) {
39354
+ return {
39355
+ place: "channel-connection",
39356
+ async run(args) {
39357
+ const t = tokens(args);
39358
+ if (t === void 0) return NOT_A_CONNECTION;
39359
+ if (t.botToken === void 0) return NO_BOT_TOKEN;
39360
+ const r = await slackCall(opts.apiUrl(), "auth.test", t.botToken, args.signal);
39361
+ if (r.kind !== "ok") {
39362
+ return r.kind === "error" && botTokenRefused(r.code) || notAnswered(r);
39363
+ }
39364
+ const problem = workspaceTokenProblem(r.body);
39365
+ if (problem !== void 0)
39366
+ return failing(
39367
+ {
39368
+ en: `${problem}.`,
39369
+ zh: "这个机器人令牌不是工作区令牌(来自组织级安装或企业 ID)。"
39370
+ },
39371
+ {
39372
+ en: "Install the app to one workspace (not org-wide) and copy that workspace's Bot User OAuth Token.",
39373
+ zh: "把应用安装到单个工作区(不要组织级安装),再复制该工作区的 Bot User OAuth Token。"
39374
+ }
39375
+ );
39376
+ if (typeof r.body.bot_id !== "string" || r.body.bot_id === "")
39377
+ return failing(
39378
+ {
39379
+ en: "Slack accepted the token, but it is not a bot token.",
39380
+ zh: "Slack 接受了该令牌,但它不是机器人令牌。"
39381
+ },
39382
+ {
39383
+ en: "Copy the Bot User OAuth Token (xoxb-…) from Install App into the Bot token field.",
39384
+ zh: "从 Install App 复制 Bot User OAuth Token(xoxb-…)填进「机器人令牌」。"
39385
+ }
39386
+ );
39387
+ const teamId = plain2(r.body.team_id, 32) ?? "";
39388
+ const team = plain2(r.body.team);
39389
+ const where = team !== void 0 ? `${team} (${teamId})` : teamId;
39390
+ return {
39391
+ status: "ok",
39392
+ reason: {
39393
+ en: `Slack accepted the bot token for workspace ${where}.`,
39394
+ zh: `Slack 接受了工作区 ${where} 的机器人令牌。`
39395
+ }
39396
+ };
39397
+ }
39398
+ };
39399
+ }
39400
+ function botScopesCheck(opts) {
39401
+ return {
39402
+ place: "channel-connection",
39403
+ async run(args) {
39404
+ const t = tokens(args);
39405
+ if (t === void 0) return NOT_A_CONNECTION;
39406
+ if (t.botToken === void 0)
39407
+ return unknown({
39408
+ en: "No bot token is set yet, so the scopes cannot be read.",
39409
+ zh: "还没有填写机器人令牌,无法读取权限。"
39410
+ });
39411
+ const r = await slackCall(opts.apiUrl(), "auth.test", t.botToken, args.signal);
39412
+ if (r.kind !== "ok") {
39413
+ if (r.kind === "error" && (REFUSED.has(r.code) || r.code === "not_allowed_token_type"))
39414
+ return unknown({
39415
+ en: `Slack refused the bot token (${r.code}), so the scopes cannot be read; see the bot token row.`,
39416
+ zh: `Slack 拒绝了机器人令牌(${r.code}),无法读取权限;请看「机器人令牌」一行。`
39417
+ });
39418
+ return notAnswered(r);
39419
+ }
39420
+ const header = r.headers.get("x-oauth-scopes");
39421
+ if (header === null)
39422
+ return unknown({
39423
+ en: "Slack did not report the token's scopes; check again later.",
39424
+ zh: "Slack 没有返回令牌的权限,请稍后再检查。"
39425
+ });
39426
+ const granted = new Set(
39427
+ header.split(",").map((s) => s.trim()).filter((s) => s !== "")
39428
+ );
39429
+ const missing = REQUIRED_BOT_SCOPES.filter((s) => !granted.has(s));
39430
+ if (missing.length > 0)
39431
+ return failing(
39432
+ {
39433
+ en: `The app is missing the bot scopes ${missing.join(", ")}.`,
39434
+ zh: `应用缺少机器人权限 ${missing.join("、")}。`
39435
+ },
39436
+ MANIFEST_FIX
39437
+ );
39438
+ return {
39439
+ status: "ok",
39440
+ reason: {
39441
+ en: `The bot token has all ${REQUIRED_BOT_SCOPES.length} scopes the manifest asks for.`,
39442
+ zh: `机器人令牌具备 manifest 要求的全部 ${REQUIRED_BOT_SCOPES.length} 项权限。`
39443
+ }
39444
+ };
39445
+ }
39446
+ };
39447
+ }
39448
+ var APP_TOKEN_FIX = {
39449
+ en: "Under Basic Information → App-Level Tokens choose Generate Token and Scopes, add the scope connections:write and copy the xapp-… token into the App-level token field.",
39450
+ zh: "在 Basic Information → App-Level Tokens 选择 Generate Token and Scopes,添加权限 connections:write,把 xapp-… 令牌填进「应用令牌」。"
39451
+ };
39452
+ function appTokenCheck(opts) {
39453
+ return {
39454
+ place: "channel-connection",
39455
+ async run(args) {
39456
+ const t = tokens(args);
39457
+ if (t === void 0) return NOT_A_CONNECTION;
39458
+ if (args.mode !== null && args.mode !== "long_connection")
39459
+ return unknown({
39460
+ en: "Webhook mode does not use an app-level token.",
39461
+ zh: "Webhook 模式不使用应用级令牌。"
39462
+ });
39463
+ if (t.appToken === void 0)
39464
+ return failing(
39465
+ {
39466
+ en: "No app-level token is set; Socket Mode needs one.",
39467
+ zh: "还没有填写应用令牌,Socket Mode 需要它。"
39468
+ },
39469
+ APP_TOKEN_FIX
39470
+ );
39471
+ const r = await slackCall(opts.apiUrl(), "apps.connections.open", t.appToken, args.signal);
39472
+ if (r.kind === "ok")
39473
+ return {
39474
+ status: "ok",
39475
+ reason: {
39476
+ en: "Slack accepted the app-level token and offered a Socket Mode connection.",
39477
+ zh: "Slack 接受了应用令牌,并提供了 Socket Mode 连接。"
39478
+ }
39479
+ };
39480
+ if (r.kind === "error") {
39481
+ if (REFUSED.has(r.code))
39482
+ return failing(
39483
+ {
39484
+ en: `Slack refused the app-level token (${r.code}); it may have been revoked.`,
39485
+ zh: `Slack 拒绝了应用令牌(${r.code}),令牌可能已被撤销。`
39486
+ },
39487
+ APP_TOKEN_FIX
39488
+ );
39489
+ if (r.code === "not_allowed_token_type")
39490
+ return failing(
39491
+ {
39492
+ en: "The App-level token field holds a token of the wrong type (not_allowed_token_type).",
39493
+ zh: "「应用令牌」中的令牌类型不对(not_allowed_token_type)。"
39494
+ },
39495
+ APP_TOKEN_FIX
39496
+ );
39497
+ if (r.code === "missing_scope")
39498
+ return failing(
39499
+ {
39500
+ en: "The app-level token lacks the scope connections:write (missing_scope).",
39501
+ zh: "应用令牌缺少权限 connections:write(missing_scope)。"
39502
+ },
39503
+ APP_TOKEN_FIX
39504
+ );
39505
+ }
39506
+ return notAnswered(r);
39507
+ }
39508
+ };
39509
+ }
39510
+
39193
39511
  // src/index.ts
39194
39512
  var index_default = defineExtension({
39195
39513
  name: "slack",
39196
- version: "0.1.0-rc.4",
39514
+ version: "0.1.0-rc.5",
39197
39515
  sdk: "^0.1.0-rc.0",
39198
39516
  provides: [CHANNEL_EXTENSION_POINT, IDENTITY_SOURCE_EXTENSION_POINT, DIRECTORY_EXTENSION_POINT],
39199
- permissions: { network: ["slack.com", "wss-primary.slack.com"] },
39517
+ permissions: {
39518
+ network: ["slack.com", "wss-primary.slack.com"],
39519
+ setupChecks: ["bot-token", "bot-scopes", "app-token"]
39520
+ },
39200
39521
  config: { deployment: SlackDeployment, org: SlackDirectoryConfig },
39201
39522
  setup(ctx) {
39202
39523
  const { apiUrl } = SlackDeployment.parse(ctx.config ?? {});
39203
39524
  ctx.provide(CHANNEL_EXTENSION_POINT, createSlackChannel({ apiUrl, log: ctx.log, kv: ctx.kv }));
39204
39525
  ctx.provide(IDENTITY_SOURCE_EXTENSION_POINT, createSlackIdentitySource());
39205
39526
  ctx.provide(DIRECTORY_EXTENSION_POINT, createSlackDirectory({ apiUrl }));
39527
+ const checks = { apiUrl: () => SlackDeployment.parse(ctx.config ?? {}).apiUrl };
39528
+ ctx.setup.check("bot-token", botTokenCheck(checks));
39529
+ ctx.setup.check("bot-scopes", botScopesCheck(checks));
39530
+ ctx.setup.check("app-token", appTokenCheck(checks));
39206
39531
  }
39207
39532
  });
39208
39533
  export {
39209
39534
  MIN_EDIT_INTERVAL_MS,
39535
+ REQUIRED_BOT_SCOPES,
39210
39536
  SlackConnection,
39211
39537
  SlackDeployment,
39212
39538
  SlackDirectoryConfig,
39539
+ appTokenCheck,
39540
+ botScopesCheck,
39541
+ botTokenCheck,
39213
39542
  createSlackChannel,
39214
39543
  createSlackDirectory,
39215
39544
  createSlackIdentitySource,