diff --git a/plugins/slack/README.md b/plugins/slack/README.md index 96cc4286..8372a576 100644 --- a/plugins/slack/README.md +++ b/plugins/slack/README.md @@ -23,7 +23,9 @@ on the pod. the code (the client secret never leaves the service), stores the returned user token in a secret setting, and presents it at `POST /v1/identify`. The service reads the token's own Slack user and binds it to this server's - stream. + stream. The token also carries the user scopes `channels:read` and + `groups:read`, so the settings page can list the channels *this developer* + can see — never what the bot can see. - **The wake.** A DM from that user arrives as `event: session`, `source: "slack"` on the same SSE stream the Linear plugin uses. The plugin maps the conversation (`channel:thread`) to one bb thread: the first message @@ -43,7 +45,13 @@ on the pod. |---|---| | Slack sign-in token | Filled in by Sign in with Slack. A secret; do not edit or paste by hand. | | Project | The bb project a Slack conversation starts its thread in when the channel map has no entry for it. Empty uses the personal project, or the first project when this server has none. | -| Channel map | A JSON array of `{"channel":"C…","project":"name"}` entries. A channel is a Slack channel id (`C…`/`G…`), never a name, so renaming a channel does not move its entry; the reserved key `dm` is a direct message with the app. A channel or DM with no entry uses the Project above. The settings page edits the same map as a list. | +| Channel map | A JSON array of `{"channel":"C…","project":"name"}` entries. A channel is a Slack channel id (`C…`/`G…`), never a name, so renaming a channel does not move its entry; the reserved key `dm` is a direct message with the app. A channel or DM with no entry uses the Project above. The settings page edits the same map as a list: the channel box is a typeahead over the channels the signed-in developer can see, showing `#name` and a lock for a private channel, and storing the id. A pasted `C…`/`G…` id and `dm` are still accepted. A channel the app's bot is not a member of is flagged "bb is not in this channel yet; invite it". | + +The channel typeahead calls Slack's `conversations.list` directly with the +stored user token, paginated and cached in memory for a few minutes per +server; filtering by name is local. A still-valid OpenID token from a sign-in +before this change cannot list channels, so the picker says "Sign in again to +search channels" and falls back to the id box. A DM typed while the assistant pane is showing a mapped channel uses that channel's project: the plugin remembers the latest diff --git a/plugins/slack/app.tsx b/plugins/slack/app.tsx index a2d2a708..1c9169f6 100644 --- a/plugins/slack/app.tsx +++ b/plugins/slack/app.tsx @@ -1,7 +1,9 @@ // The plugin's settings section: whether this server is signed in to Slack as // you, which bb project a Slack conversation starts its thread in by default, -// and the channel → project map. The token itself is never shown. -import { useCallback, useEffect, useState } from "react"; +// and the channel → project map. The channel box is a typeahead over the +// channels you can see, read from Slack with your own sign-in token; the id is +// what is stored. The token itself is never shown. +import { useCallback, useEffect, useMemo, useState } from "react"; import { definePluginApp, useBbNavigate, useRealtime, useRpc } from "@get-bb/plugin-sdk/app"; import type { rpcContract } from "./server"; import { Button } from "@/components/ui/button"; @@ -18,6 +20,22 @@ interface ChannelEntry { project: string; } +/** One channel the search RPC can return. */ +interface ChannelOption { + id: string; + name: string; + private: boolean; + extShared: boolean; +} + +interface ChannelSearch { + state: "ok" | "signin-required" | "unavailable"; + channels: ChannelOption[]; + botChannels: string[] | null; + truncated: boolean; + message?: string; +} + interface Status { signedIn: boolean; user: { id: string; name: string } | null; @@ -27,13 +45,203 @@ interface Status { hooks: { configured: boolean; connected: boolean; state: string }; } -/** The name shown beside a channel id when one is known. Only the reserved `dm` has one. */ -function channelLabel(channel: string): string | null { - return channel.trim().toLowerCase() === "dm" ? "Direct message" : null; +/** The reserved channel-map key for a direct message with the app. */ +const DM_CHANNEL = "dm"; +/** A Slack channel id: `C…` or `G…`, never a name. */ +const CHANNEL_ID = /^[CG][A-Z0-9]+$/i; + +const isDm = (channel: string): boolean => channel.trim().toLowerCase() === DM_CHANNEL; +const isChannelId = (channel: string): boolean => CHANNEL_ID.test(channel.trim()); + +/** Whether the map key is a channel id bb's bot is not a member of. */ +function needsInvite(channel: string, botChannels: string[] | null): boolean { + return botChannels !== null && isChannelId(channel) && !botChannels.includes(channel.trim()); +} + +/** The one-line note under a row: private, Slack Connect, or not invited yet. */ +function channelNote(channel: string, options: ChannelOption[], botChannels: string[] | null): string | null { + if (isDm(channel) || channel.trim() === "") return null; + if (needsInvite(channel, botChannels)) return "bb is not in this channel yet; invite it"; + const known = options.find((c) => c.id === channel.trim()); + if (known === null || known === undefined) return null; + if (known.extShared) return "Slack Connect channel"; + if (known.private) return "Private channel"; + return null; +} + +/** The meta text beside an option in the dropdown. */ +function optionMeta(option: ChannelOption, botChannels: string[] | null): string { + const parts: string[] = []; + if (option.private) parts.push("private"); + if (option.extShared) parts.push("Slack Connect"); + if (needsInvite(option.id, botChannels)) parts.push("bb is not in this channel yet; invite it"); + return parts.join(" · "); +} + +/** One clickable row of the picker's dropdown. */ +function PickerRow({ label, meta, selected, onClick }: { label: string; meta: string; selected: boolean; onClick: () => void }) { + return ( + + ); +} + +/** + * The channel box. With a searchable token it is a typeahead over the + * developer's channels, showing `#name` and a lock for a private channel, and + * storing the id; a pasted `C…`/`G…` id and `dm` are still accepted. Without + * one it is the plain id box the picker falls back to. + */ +function ChannelPicker({ + value, + options, + botChannels, + canSearch, + disabled, + onChange, +}: { + value: string; + options: ChannelOption[]; + botChannels: string[] | null; + canSearch: boolean; + disabled: boolean; + onChange: (channel: string) => void; +}) { + const [open, setOpen] = useState(false); + const [query, setQuery] = useState(""); + const selected = options.find((c) => c.id === value.trim()) ?? null; + const label = isDm(value) ? "Direct message" : selected !== null ? `#${selected.name}` : value; + const close = () => { + setOpen(false); + setQuery(""); + }; + + /** Store what was typed: an exact id, an exact name, a pasted id, or `dm`. */ + const accept = (text: string) => { + const typed = text.trim(); + if (typed === "") return close(); + if (typed.toLowerCase() === DM_CHANNEL) { + onChange(DM_CHANNEL); + return close(); + } + const byId = options.find((c) => c.id === typed); + if (byId !== undefined) { + onChange(byId.id); + return close(); + } + const byName = options.find((c) => c.name.toLowerCase() === typed.toLowerCase()); + if (byName !== undefined) { + onChange(byName.id); + return close(); + } + if (isChannelId(typed)) { + onChange(typed); + return close(); + } + close(); + }; + + const wanted = query.trim().toLowerCase(); + const allMatches = useMemo( + () => (wanted === "" ? options : options.filter((c) => c.name.toLowerCase().includes(wanted) || c.id.toLowerCase().includes(wanted))), + [options, wanted], + ); + const matches = allMatches.slice(0, 50); + const capped = allMatches.length > matches.length; + const rawId = isChannelId(query) && !options.some((c) => c.id === query.trim()) ? query.trim() : null; + + if (!canSearch) { + return ( + onChange(e.target.value)} + /> + ); + } + + return ( +
+ { + setOpen(true); + setQuery(""); + }} + onChange={(e) => { + setQuery(e.target.value); + setOpen(true); + }} + onKeyDown={(e) => { + if (e.key === "Enter") { + e.preventDefault(); + accept(query); + } else if (e.key === "Escape") { + e.preventDefault(); + close(); + } + }} + onBlur={() => { + if (open) accept(query); + }} + /> + {open ? ( +
+
+ {wanted === "" || "direct message".includes(wanted) ? ( + { onChange(DM_CHANNEL); close(); }} /> + ) : null} + {matches.map((option) => ( + { + onChange(option.id); + close(); + }} + /> + ))} + {rawId !== null ? ( + { onChange(rawId); close(); }} /> + ) : null} + {capped ?

Refine your search to see more.

: null} + {matches.length === 0 && wanted !== "" && rawId === null ?

No matching channels.

: null} +
+
+ ) : null} +
+ ); } /** The channel → project map: a row per channel, saved together. */ -function ChannelMapEditor({ status, busy, save }: { status: Status; busy: boolean; save: (entries: ChannelEntry[]) => void }) { +function ChannelMapEditor({ + status, + search, + busy, + save, +}: { + status: Status; + search: ChannelSearch | null; + busy: boolean; + save: (entries: ChannelEntry[]) => void; +}) { const [rows, setRows] = useState(status.channels); useEffect(() => setRows(status.channels), [status.channels]); const projectNames = status.projects.map((p) => p.name); @@ -42,43 +250,63 @@ function ChannelMapEditor({ status, busy, save }: { status: Status; busy: boolea const change = (index: number, patch: Partial) => setRows((all) => all.map((row, i) => (i === index ? { ...row, ...patch } : row))); + const options = search?.channels ?? []; + const botChannels = search?.botChannels ?? null; + const canSearch = search?.state === "ok"; + return (

- A mention in a channel starts its thread in the project beside it. A channel is a Slack channel id (C… or G…), never a name, so + A mention in a channel starts its thread in the project beside it. The channel is a Slack channel id (C… or G…), never a name, so renaming a channel does not break its entry; the reserved dm is a direct message with the app. A channel or DM with no entry uses the project above.

+ {search !== null && search.state !== "ok" ? ( +

+ {search.state === "signin-required" + ? status.signedIn + ? "Sign in again to search channels; paste a channel id instead." + : "Sign in with Slack to search channels; paste a channel id instead." + : `Could not search channels right now${search.message !== undefined ? ` (${search.message})` : ""}; paste a channel id instead.`} +

+ ) : null} + {search !== null && search.state === "ok" && search.truncated ? ( +

Slack has more channels than could be listed here; some may be missing from the search.

+ ) : null} {rows.length === 0 ?

No channel entries yet.

: null} - {rows.map((row, index) => ( -
- - - -
- ))} + {rows.map((row, index) => { + const note = channelNote(row.channel, options, botChannels); + return ( +
+
+ change(index, { channel })} + /> + + +
+ {note !== null ? {note} : null} +
+ ); + })}