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
38 changes: 24 additions & 14 deletions plugins/slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ on the pod.
`/api/v1/plugins/slack/http/signin/callback`; the router's tsjwt sends it to
the browser owner's own server. The plugin asks bb-gate-hooks to exchange
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
user token in plugin storage, and presents it at `POST /v1/identify`. The
service reads the token's own Slack user and binds it to this server's
stream. The token also carries the user scopes `channels:read` and
`groups:read`, so the settings page can list the channels *this developer*
Expand All @@ -41,30 +41,40 @@ on the pod.

## Settings

| Setting | What it is |
The plugin declares no bb settings: the Slack section under
Settings → Plugins → Slack manages everything, and the sign-in token and the
routing map live in the plugin's own storage (`bb.storage.kv`), not in the
settings table. The token is never sent to the settings page or the status RPC.

| In the Slack section | What it is |
|---|---|
| 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: 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". |
| Sign in with Slack / Sign out | The developer's own Slack sign-in. The token it returns proves who they are and reads the channel list they can see. |
| Routing map | A pinned first row, **Everything else**, then a row per channel. A mention in a mapped channel starts its thread in that channel's project; everything else, DMs included, starts in the project on the first row. The channel is a Slack channel id (`C…`/`G…`), never a name, so renaming a channel does not move its entry. The editor refuses to save without a fallback. A fallback or row naming a project this server does not have is skipped at run time — the conversation falls to the personal project, or the first one — and the page warns beside the row, since one map is meant to work on every server. |

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
`assistant_thread_context_changed` channel per DM conversation and consults it
before the `dm` entry. Context is read only when a thread is started; an
existing Slack thread keeps its bb thread.
search channels" and falls back to the id box. A channel the app's bot is not a
member of is flagged "bb is not in this channel yet; invite it".

Words from anyone but the owner never run a turn. A channel mention by someone
else is held under a short id and the owner releases it by mentioning the app
with `confirm <id>`; only then does the text reach a bb thread.

**Sign out** forgets the token and reconnects the stream, so the service drops
the identity bound to the old connection.
**Sign out** deletes the token from plugin storage and reconnects the stream,
so the service drops the identity bound to the old connection.

### Upgrade

The first start after this version moves the old `token`, `project` and
`channels` settings into plugin storage. The old `channels` array becomes the
map's channel rows, and the fallback is the old `dm` entry if there was one,
else the old `project` setting; when both existed and differed, `dm` won. The
old `dm` entry is dropped from the rows. Only the old token setting is cleared;
the old `project` and `channels` settings are left in place, so rolling back to
the previous version asks the developer to sign in again and then applies the
old project and channels settings.

## By hand (a Slack admin)

Expand Down
96 changes: 48 additions & 48 deletions plugins/slack/app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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 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.
// you, and the routing map that decides which bb project a Slack conversation
// starts its thread in. 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";
Expand Down Expand Up @@ -39,18 +39,15 @@ interface ChannelSearch {
interface Status {
signedIn: boolean;
user: { id: string; name: string } | null;
project: { id: string; name: string } | null;
fallback: string;
projects: Project[];
channels: ChannelEntry[];
hooks: { configured: boolean; connected: boolean; state: string };
}

/** 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. */
Expand All @@ -60,7 +57,7 @@ function needsInvite(channel: string, botChannels: string[] | null): boolean {

/** 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 (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;
Expand Down Expand Up @@ -99,8 +96,8 @@ function PickerRow({ label, meta, selected, onClick }: { label: string; meta: st
/**
* 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.
* storing the id; a pasted `C…`/`G…` id is still accepted. Without one it is
* the plain id box the picker falls back to.
*/
function ChannelPicker({
value,
Expand All @@ -120,20 +117,16 @@ function ChannelPicker({
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 label = 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`. */
/** Store what was typed: an exact id, an exact name, or a pasted id. */
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);
Expand Down Expand Up @@ -164,7 +157,7 @@ function ChannelPicker({
return (
<input
className="h-8 w-44 rounded-md border border-input bg-transparent px-2 text-sm"
placeholder="C0123 or dm"
placeholder="C0123"
value={value}
disabled={disabled}
onChange={(e) => onChange(e.target.value)}
Expand Down Expand Up @@ -203,9 +196,6 @@ function ChannelPicker({
{open ? (
<div className="absolute z-10 mt-1 w-72 rounded-md border border-input bg-popover p-1 text-sm shadow-md">
<div className="max-h-56 overflow-auto">
{wanted === "" || "direct message".includes(wanted) ? (
<PickerRow label="Direct message" meta="dm" selected={isDm(value)} onClick={() => { onChange(DM_CHANNEL); close(); }} />
) : null}
{matches.map((option) => (
<PickerRow
key={option.id}
Expand All @@ -230,7 +220,7 @@ function ChannelPicker({
);
}

/** The channel → project map: a row per channel, saved together. */
/** The routing map: a pinned fallback row, then a row per channel, saved together. */
function ChannelMapEditor({
status,
search,
Expand All @@ -240,25 +230,32 @@ function ChannelMapEditor({
status: Status;
search: ChannelSearch | null;
busy: boolean;
save: (entries: ChannelEntry[]) => void;
save: (map: { fallback: string; channels: ChannelEntry[] }) => void;
}) {
const [fallback, setFallback] = useState(status.fallback);
const [rows, setRows] = useState<ChannelEntry[]>(status.channels);
useEffect(() => setRows(status.channels), [status.channels]);
useEffect(() => {
setFallback(status.fallback);
setRows(status.channels);
}, [status.fallback, status.channels]);
const projectNames = status.projects.map((p) => p.name);
const valid = rows.filter((row) => row.channel.trim() !== "" && row.project.trim() !== "");
const dirty = JSON.stringify(valid) !== JSON.stringify(status.channels);
const dirty = fallback.trim() !== status.fallback.trim() || JSON.stringify(valid) !== JSON.stringify(status.channels);
const change = (index: number, patch: Partial<ChannelEntry>) =>
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";
const trimmed = fallback.trim();
const onServer = (name: string) => projectNames.some((candidate) => candidate.toLowerCase() === name.toLowerCase());
const missing = trimmed !== "" && !onServer(trimmed);

return (
<div className="flex flex-col gap-2">
<p className="text-muted-foreground">
A mention in a channel starts its thread in the project beside it. The channel is a Slack channel id (<code>C…</code> or <code>G…</code>), never a name, so
renaming a channel does not break its entry; the reserved <code>dm</code> is a direct message with the app. A channel or DM with no entry uses the project above.
A mention in a channel starts its thread in the project beside it; everything else, DMs included, starts in the project on the first row. The channel is a Slack
channel id (<code>C…</code> or <code>G…</code>), never a name, so renaming a channel does not break its entry.
</p>
{search !== null && search.state !== "ok" ? (
<p className="text-muted-foreground">
Expand All @@ -272,6 +269,27 @@ function ChannelMapEditor({
{search !== null && search.state === "ok" && search.truncated ? (
<p className="text-muted-foreground">Slack has more channels than could be listed here; some may be missing from the search.</p>
) : null}
<div className="flex flex-col gap-0.5">
<div className="flex items-center gap-2">
<span className="w-44 shrink-0 text-muted-foreground">Everything else</span>
<select
className="h-8 flex-1 rounded-md border border-input bg-transparent px-2 text-sm"
value={fallback}
disabled={busy}
onChange={(e) => setFallback(e.target.value)}
>
<option value="">Choose a project…</option>
{missing ? <option value={fallback}>{fallback} (not on this server)</option> : null}
{projectNames.map((name) => (
<option key={name} value={name}>
{name}
</option>
))}
</select>
</div>
{missing ? <span className="text-xs text-destructive">This project is not on this server; conversations fall back to the personal project, or the first one.</span> : null}
{trimmed === "" ? <span className="text-xs text-muted-foreground">Choose a project for conversations that are not in a mapped channel.</span> : null}
</div>
{rows.length === 0 ? <p className="text-muted-foreground">No channel entries yet.</p> : null}
{rows.map((row, index) => {
const note = channelNote(row.channel, options, botChannels);
Expand All @@ -292,7 +310,7 @@ function ChannelMapEditor({
disabled={busy}
onChange={(e) => change(index, { project: e.target.value })}
>
{!projectNames.includes(row.project) ? <option value={row.project}>{row.project} (not on this server)</option> : null}
{!onServer(row.project) ? <option value={row.project}>{row.project} (not on this server)</option> : null}
{projectNames.map((name) => (
<option key={name} value={name}>
{name}
Expand All @@ -316,7 +334,7 @@ function ChannelMapEditor({
>
Add channel
</Button>
<Button size="sm" disabled={busy || !dirty} onClick={() => save(valid)}>
<Button size="sm" disabled={busy || trimmed === "" || !dirty} onClick={() => save({ fallback: trimmed, channels: valid })}>
Save map
</Button>
</div>
Expand Down Expand Up @@ -409,25 +427,7 @@ function SlackSection() {
)}
</div>
{status.projects.length > 0 ? (
<label className="flex items-center justify-between gap-4">
<span className="text-muted-foreground">Project a Slack conversation starts its bb thread in</span>
<select
className="h-8 rounded-md border border-input bg-transparent px-2 text-sm"
disabled={busy}
value={status.project?.id ?? ""}
onChange={(e) => void act(() => rpc.call("setProject", { projectId: e.target.value }))}
>
{status.projects.map((p) => (
<option key={p.id} value={p.id}>
{p.name}
{p.kind === "personal" ? " (personal)" : ""}
</option>
))}
</select>
</label>
) : null}
{status.projects.length > 0 ? (
<ChannelMapEditor status={status} search={search} busy={busy} save={(entries) => void act(() => rpc.call("setChannels", { entries }))} />
<ChannelMapEditor status={status} search={search} busy={busy} save={(map) => void act(() => rpc.call("setMap", map))} />
) : null}
{!status.hooks.connected && status.hooks.configured ? <p className="text-muted-foreground">Connecting to bb-gate-hooks…</p> : null}
{error !== null ? <p className="text-destructive">{error}</p> : null}
Expand Down
Loading
Loading