@copilotkit/channels-slack 0.1.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/LICENSE +21 -0
- package/README.md +353 -0
- package/dist/__tests__/assistant.test.d.ts +2 -0
- package/dist/__tests__/assistant.test.d.ts.map +1 -0
- package/dist/__tests__/assistant.test.js +153 -0
- package/dist/__tests__/auto-close-streaming.test.d.ts +2 -0
- package/dist/__tests__/auto-close-streaming.test.d.ts.map +1 -0
- package/dist/__tests__/auto-close-streaming.test.js +139 -0
- package/dist/__tests__/built-ins.test.d.ts +2 -0
- package/dist/__tests__/built-ins.test.d.ts.map +1 -0
- package/dist/__tests__/built-ins.test.js +67 -0
- package/dist/__tests__/chunked-message-stream.test.d.ts +2 -0
- package/dist/__tests__/chunked-message-stream.test.d.ts.map +1 -0
- package/dist/__tests__/chunked-message-stream.test.js +226 -0
- package/dist/__tests__/conversation-store.test.d.ts +2 -0
- package/dist/__tests__/conversation-store.test.d.ts.map +1 -0
- package/dist/__tests__/conversation-store.test.js +209 -0
- package/dist/__tests__/download-files.test.d.ts +2 -0
- package/dist/__tests__/download-files.test.d.ts.map +1 -0
- package/dist/__tests__/download-files.test.js +142 -0
- package/dist/__tests__/ephemeral.test.d.ts +2 -0
- package/dist/__tests__/ephemeral.test.d.ts.map +1 -0
- package/dist/__tests__/ephemeral.test.js +85 -0
- package/dist/__tests__/event-renderer.test.d.ts +2 -0
- package/dist/__tests__/event-renderer.test.d.ts.map +1 -0
- package/dist/__tests__/event-renderer.test.js +521 -0
- package/dist/__tests__/markdown-to-mrkdwn.test.d.ts +2 -0
- package/dist/__tests__/markdown-to-mrkdwn.test.d.ts.map +1 -0
- package/dist/__tests__/markdown-to-mrkdwn.test.js +80 -0
- package/dist/__tests__/message-stream.test.d.ts +2 -0
- package/dist/__tests__/message-stream.test.d.ts.map +1 -0
- package/dist/__tests__/message-stream.test.js +116 -0
- package/dist/__tests__/native-renderer.test.d.ts +2 -0
- package/dist/__tests__/native-renderer.test.d.ts.map +1 -0
- package/dist/__tests__/native-renderer.test.js +236 -0
- package/dist/__tests__/native-stream.test.d.ts +2 -0
- package/dist/__tests__/native-stream.test.d.ts.map +1 -0
- package/dist/__tests__/native-stream.test.js +235 -0
- package/dist/__tests__/reactions.test.d.ts +2 -0
- package/dist/__tests__/reactions.test.d.ts.map +1 -0
- package/dist/__tests__/reactions.test.js +193 -0
- package/dist/__tests__/slack-listener.test.d.ts +2 -0
- package/dist/__tests__/slack-listener.test.d.ts.map +1 -0
- package/dist/__tests__/slack-listener.test.js +559 -0
- package/dist/__tests__/views.test.d.ts +2 -0
- package/dist/__tests__/views.test.d.ts.map +1 -0
- package/dist/__tests__/views.test.js +143 -0
- package/dist/adapter.d.ts +188 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +859 -0
- package/dist/adapter.test.d.ts +2 -0
- package/dist/adapter.test.d.ts.map +1 -0
- package/dist/adapter.test.js +368 -0
- package/dist/assistant.d.ts +19 -0
- package/dist/assistant.d.ts.map +1 -0
- package/dist/assistant.js +124 -0
- package/dist/auto-close-streaming.d.ts +62 -0
- package/dist/auto-close-streaming.d.ts.map +1 -0
- package/dist/auto-close-streaming.js +273 -0
- package/dist/built-in-context.d.ts +22 -0
- package/dist/built-in-context.d.ts.map +1 -0
- package/dist/built-in-context.js +65 -0
- package/dist/built-in-tools.d.ts +23 -0
- package/dist/built-in-tools.d.ts.map +1 -0
- package/dist/built-in-tools.js +45 -0
- package/dist/chunked-message-stream.d.ts +67 -0
- package/dist/chunked-message-stream.d.ts.map +1 -0
- package/dist/chunked-message-stream.js +134 -0
- package/dist/codec.d.ts +18 -0
- package/dist/codec.d.ts.map +1 -0
- package/dist/codec.js +18 -0
- package/dist/codec.test.d.ts +2 -0
- package/dist/codec.test.d.ts.map +1 -0
- package/dist/codec.test.js +19 -0
- package/dist/conversation-store.d.ts +92 -0
- package/dist/conversation-store.d.ts.map +1 -0
- package/dist/conversation-store.js +196 -0
- package/dist/download-files.d.ts +59 -0
- package/dist/download-files.d.ts.map +1 -0
- package/dist/download-files.js +116 -0
- package/dist/event-renderer.d.ts +98 -0
- package/dist/event-renderer.d.ts.map +1 -0
- package/dist/event-renderer.js +493 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/ingress-normalize.d.ts +75 -0
- package/dist/ingress-normalize.d.ts.map +1 -0
- package/dist/ingress-normalize.js +153 -0
- package/dist/ingress-normalize.test.d.ts +2 -0
- package/dist/ingress-normalize.test.d.ts.map +1 -0
- package/dist/ingress-normalize.test.js +187 -0
- package/dist/interaction.d.ts +30 -0
- package/dist/interaction.d.ts.map +1 -0
- package/dist/interaction.js +219 -0
- package/dist/interaction.test.d.ts +2 -0
- package/dist/interaction.test.d.ts.map +1 -0
- package/dist/interaction.test.js +137 -0
- package/dist/markdown-to-mrkdwn.d.ts +21 -0
- package/dist/markdown-to-mrkdwn.d.ts.map +1 -0
- package/dist/markdown-to-mrkdwn.js +99 -0
- package/dist/message-stream.d.ts +44 -0
- package/dist/message-stream.d.ts.map +1 -0
- package/dist/message-stream.js +70 -0
- package/dist/native-stream.d.ts +131 -0
- package/dist/native-stream.d.ts.map +1 -0
- package/dist/native-stream.js +208 -0
- package/dist/render/block-kit.d.ts +35 -0
- package/dist/render/block-kit.d.ts.map +1 -0
- package/dist/render/block-kit.js +399 -0
- package/dist/render/block-kit.test.d.ts +2 -0
- package/dist/render/block-kit.test.d.ts.map +1 -0
- package/dist/render/block-kit.test.js +289 -0
- package/dist/render/budget.d.ts +24 -0
- package/dist/render/budget.d.ts.map +1 -0
- package/dist/render/budget.js +30 -0
- package/dist/render/budget.test.d.ts +2 -0
- package/dist/render/budget.test.d.ts.map +1 -0
- package/dist/render/budget.test.js +49 -0
- package/dist/render/modal.d.ts +13 -0
- package/dist/render/modal.d.ts.map +1 -0
- package/dist/render/modal.js +108 -0
- package/dist/render/transport.d.ts +42 -0
- package/dist/render/transport.d.ts.map +1 -0
- package/dist/render/transport.js +1 -0
- package/dist/render.d.ts +23 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +20 -0
- package/dist/sanitizing-http-agent.d.ts +28 -0
- package/dist/sanitizing-http-agent.d.ts.map +1 -0
- package/dist/sanitizing-http-agent.js +51 -0
- package/dist/slack-listener.d.ts +73 -0
- package/dist/slack-listener.d.ts.map +1 -0
- package/dist/slack-listener.js +126 -0
- package/dist/slack-listener.test.d.ts +2 -0
- package/dist/slack-listener.test.d.ts.map +1 -0
- package/dist/slack-listener.test.js +115 -0
- package/dist/types.d.ts +153 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +20 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Atai Barkai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
# @copilotkit/channels-slack
|
|
2
|
+
|
|
3
|
+
The **Slack `PlatformAdapter`** for [`@copilotkit/channels`](../channels). It connects a
|
|
4
|
+
Slack workspace to any AG-UI agent: ingress via Bolt (Socket Mode), egress as
|
|
5
|
+
Block Kit rendered from the `@copilotkit/channels-ui` JSX vocabulary, plus text
|
|
6
|
+
streaming, opaque-id interactions, and HITL.
|
|
7
|
+
|
|
8
|
+
You write your UI as JSX once (`@copilotkit/channels-ui`) and drive the bot with
|
|
9
|
+
`@copilotkit/channels`; this package is the only one that talks to Slack.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add @copilotkit/channels-slack @copilotkit/channels @copilotkit/channels-ui
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { createBot } from "@copilotkit/channels";
|
|
21
|
+
import {
|
|
22
|
+
slack,
|
|
23
|
+
defaultSlackTools,
|
|
24
|
+
defaultSlackContext,
|
|
25
|
+
} from "@copilotkit/channels-slack";
|
|
26
|
+
|
|
27
|
+
const bot = createBot({
|
|
28
|
+
adapters: [
|
|
29
|
+
slack({
|
|
30
|
+
botToken: process.env.SLACK_BOT_TOKEN!, // xoxb-…
|
|
31
|
+
appToken: process.env.SLACK_APP_TOKEN!, // xapp-… (Socket Mode)
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
agent: (threadId) => makeAgent(threadId),
|
|
35
|
+
tools: [...defaultSlackTools, ...appTools], // lookup_slack_user + your tools
|
|
36
|
+
context: [...defaultSlackContext, ...appContext], // tagging/mrkdwn/thread guidance
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
bot.onMention(({ thread }) => thread.runAgent());
|
|
40
|
+
|
|
41
|
+
await bot.start();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`slack(opts)` returns a `SlackAdapter`. By default it runs in **Socket Mode**
|
|
45
|
+
(`socketMode: true`) — outbound WebSocket only, no public URL needed. HTTP
|
|
46
|
+
mode (`socketMode: false`) needs `signingSecret` and a `port`. The Slack
|
|
47
|
+
listener pre-filters ingress to the turns the bot should answer. By default,
|
|
48
|
+
DMs are conversational, app mentions respond in-thread, and plain replies in
|
|
49
|
+
channel/private-channel threads require another app mention.
|
|
50
|
+
|
|
51
|
+
### Required env
|
|
52
|
+
|
|
53
|
+
| Var | Token | Purpose |
|
|
54
|
+
| ----------------- | ------- | -------------------------------- |
|
|
55
|
+
| `SLACK_BOT_TOKEN` | `xoxb-` | Bot token for the Web API. |
|
|
56
|
+
| `SLACK_APP_TOKEN` | `xapp-` | App-level token for Socket Mode. |
|
|
57
|
+
|
|
58
|
+
## Response routing
|
|
59
|
+
|
|
60
|
+
Use `respondTo` to choose which Slack message events become `onMention` turns:
|
|
61
|
+
|
|
62
|
+
| Surface | Default behavior | Option |
|
|
63
|
+
| --------------------------------------- | ----------------------- | ----------------------------------------------------- |
|
|
64
|
+
| Direct messages (`message.im`) | Respond | `respondTo.directMessages` |
|
|
65
|
+
| App mentions (`app_mention`) | Respond in-thread | `respondTo.appMentions` / `appMentions.reply` |
|
|
66
|
+
| Plain channel/private-channel replies | Ignore unless mentioned | `respondTo.threadReplies: "afterBotReply"` for legacy |
|
|
67
|
+
| Assistant pane | Separate default-on API | `assistant`; not controlled by `respondTo` |
|
|
68
|
+
| Slash commands, reactions, interactions | Explicit trigger paths | Not controlled by `respondTo` |
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
// Default routing made explicit.
|
|
72
|
+
slack({
|
|
73
|
+
botToken,
|
|
74
|
+
appToken,
|
|
75
|
+
respondTo: {
|
|
76
|
+
directMessages: true,
|
|
77
|
+
appMentions: { reply: "thread" },
|
|
78
|
+
threadReplies: "mentionsOnly",
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
// Legacy owned-thread continuation.
|
|
85
|
+
slack({
|
|
86
|
+
botToken,
|
|
87
|
+
appToken,
|
|
88
|
+
respondTo: {
|
|
89
|
+
threadReplies: "afterBotReply",
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For the default mention-only thread behavior, subscribe to `app_mention` and
|
|
95
|
+
`message.im` events. Add `message.channels` and `message.groups` only when you
|
|
96
|
+
enable `respondTo.threadReplies: "afterBotReply"` and want Slack to deliver
|
|
97
|
+
plain channel/private-channel thread replies.
|
|
98
|
+
|
|
99
|
+
## What it provides
|
|
100
|
+
|
|
101
|
+
### JSX → Block Kit rendering
|
|
102
|
+
|
|
103
|
+
`renderSlackMessage(ir)` / `renderBlockKit(ir)` translate the
|
|
104
|
+
`@copilotkit/channels-ui` vocabulary to Block Kit: `Message → blocks`,
|
|
105
|
+
`Header → header`, `Section → section (mrkdwn)`, `Markdown → markdownToMrkdwn`,
|
|
106
|
+
`Field(s) → section.fields`, `Context → context`, `Actions → actions`,
|
|
107
|
+
`Button → button (action_id = minted opaque id)`, `Select → static_select`,
|
|
108
|
+
`Input → plain_text_input`, `Image → image`, `Divider → divider`.
|
|
109
|
+
|
|
110
|
+
### Per-element budget
|
|
111
|
+
|
|
112
|
+
Slack caps every element. The renderer degrades by truncate-with-overflow /
|
|
113
|
+
clamp — it never silently drops content. Limits live in `SLACK_LIMITS`:
|
|
114
|
+
|
|
115
|
+
| Limit | Value | Element |
|
|
116
|
+
| ------------------ | ----- | -------------------------- |
|
|
117
|
+
| `blocksPerMessage` | 50 | blocks per message |
|
|
118
|
+
| `sectionText` | 3000 | section body chars |
|
|
119
|
+
| `headerText` | 150 | header chars |
|
|
120
|
+
| `fieldsPerSection` | 10 | fields per section |
|
|
121
|
+
| `fieldText` | 2000 | field chars |
|
|
122
|
+
| `actionsElements` | 25 | controls per actions row |
|
|
123
|
+
| `contextElements` | 10 | elements per context block |
|
|
124
|
+
| `buttonText` | 75 | button label chars |
|
|
125
|
+
| `actionId` | 255 | `action_id` chars |
|
|
126
|
+
| `buttonValue` | 2000 | button value chars |
|
|
127
|
+
| `selectOptions` | 100 | options per select |
|
|
128
|
+
|
|
129
|
+
### Colored cards
|
|
130
|
+
|
|
131
|
+
`<Message accent="#RRGGBB">` renders as a Slack attachment with a colored
|
|
132
|
+
left bar (Block Kit blocks have no native accent, so accented messages are
|
|
133
|
+
posted as `attachments: [{ color, blocks }]`).
|
|
134
|
+
|
|
135
|
+
### Streaming
|
|
136
|
+
|
|
137
|
+
By default, replies stream via Slack's **native streaming API**
|
|
138
|
+
(`chat.startStream` / `appendStream` / `stopStream`) wherever the reply target
|
|
139
|
+
is a thread — a true streaming UI rendering **raw markdown** (so real tables and
|
|
140
|
+
fenced code render natively). A whole turn streams into **one** message: text
|
|
141
|
+
from every step accumulates into a single bubble (Slack documents only a 12k
|
|
142
|
+
char limit _per append_, with no cumulative cap, so there is no multi-message
|
|
143
|
+
splitting), and tool calls surface as native in-message **`task_update`**
|
|
144
|
+
chunks (a "timeline" of `Using …` → `Used …` steps) instead of separate status
|
|
145
|
+
messages. Workspaces where structured chunks aren't available degrade
|
|
146
|
+
automatically to `:wrench:` status rows.
|
|
147
|
+
|
|
148
|
+
Flat DMs (no thread) and any workspace where the streaming API is unavailable
|
|
149
|
+
fall back automatically to the shipped `chat.update` transport (throttled edits,
|
|
150
|
+
multi-message chunking, mid-stream bracket auto-close, Markdown → mrkdwn
|
|
151
|
+
translation). Pass `streaming: "legacy"` to force the `chat.update` transport
|
|
152
|
+
everywhere. The fallback is transparent — **opting in can never break a bot**:
|
|
153
|
+
the first `startStream` failure marks the workspace legacy and redoes the stream
|
|
154
|
+
the old way.
|
|
155
|
+
|
|
156
|
+
### Feedback buttons (opt-in)
|
|
157
|
+
|
|
158
|
+
Pass `feedback` to attach Slack's native AI feedback row (👍/👎,
|
|
159
|
+
`context_actions` + `feedback_buttons`) to each finalized streamed reply. Clicks
|
|
160
|
+
are routed straight to your handler — they never reach the engine's interaction
|
|
161
|
+
dispatch. Without `feedback`, no buttons are shown.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
slack({
|
|
165
|
+
botToken,
|
|
166
|
+
appToken,
|
|
167
|
+
feedback: {
|
|
168
|
+
onFeedback: ({ sentiment, user, channel, messageTs }) => {
|
|
169
|
+
recordFeedback({ sentiment, user, channel, messageTs }); // your telemetry
|
|
170
|
+
},
|
|
171
|
+
// positiveLabel / negativeLabel are optional
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The row is attached at `chat.stopStream` (the only streaming call that accepts
|
|
177
|
+
`blocks`), so it appears on the native path only — the legacy `chat.update`
|
|
178
|
+
fallback omits it.
|
|
179
|
+
|
|
180
|
+
### Native "is thinking…" status (everywhere)
|
|
181
|
+
|
|
182
|
+
While the agent runs, the bot shows Slack's **native** loading status
|
|
183
|
+
(`assistant.threads.setStatus`: "is thinking…") on every thread-anchored reply —
|
|
184
|
+
channel @-mentions, threads it owns, DMs, and the assistant pane. Slack now
|
|
185
|
+
accepts this method with the ordinary **`chat:write`** scope (no `assistant:write`
|
|
186
|
+
needed just for the loading state), so it works for channel-based apps too. The
|
|
187
|
+
status auto-clears when the reply streams in. Tool progress is surfaced per
|
|
188
|
+
surface: the pane uses live composer status ("is using \`tool\`…"); elsewhere it
|
|
189
|
+
uses the native `task_update` timeline (or `:wrench:` rows on older workspaces).
|
|
190
|
+
Set `assistant: false` to opt out of the status (and pane) entirely.
|
|
191
|
+
|
|
192
|
+
### Assistant pane (agent-native, default-on)
|
|
193
|
+
|
|
194
|
+
When the Slack app has the **Agents & AI Apps** toggle (an `assistant_view`
|
|
195
|
+
manifest block + the `assistant:write` scope and `assistant_thread_*` events),
|
|
196
|
+
the adapter activates Slack's assistant pane with **zero config**:
|
|
197
|
+
|
|
198
|
+
- Opening the pane posts a greeting + tappable prompt chips, and each pane
|
|
199
|
+
conversation is its own thread (replies stay in-thread).
|
|
200
|
+
- While the agent runs, native composer status is shown (see above), with
|
|
201
|
+
"is using \`tool\`…" per tool call.
|
|
202
|
+
- The pane thread is auto-titled from the first message.
|
|
203
|
+
|
|
204
|
+
Customize via the `assistant` option, or set `assistant: false` to disable pane
|
|
205
|
+
handling entirely. Apps **without** the toggle behave exactly as before — the
|
|
206
|
+
pane machinery lies dormant.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
slack({
|
|
210
|
+
botToken,
|
|
211
|
+
appToken,
|
|
212
|
+
assistant: {
|
|
213
|
+
greeting: "Hi! I can triage issues, search docs, and more.",
|
|
214
|
+
suggestedPrompts: [
|
|
215
|
+
{ title: "Triage my open issues", message: "Triage my open issues" },
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Dynamic behavior when a user opens the pane (layers on top of the defaults):
|
|
221
|
+
bot.onThreadStarted(async ({ thread, user }) => {
|
|
222
|
+
await thread.setSuggestedPrompts(promptsFor(user));
|
|
223
|
+
// await thread.setTitle(...) is also available
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Interactions (ack-first)
|
|
228
|
+
|
|
229
|
+
Every Slack `block_actions` click is acked immediately (within the **≤3s**
|
|
230
|
+
deadline, `ackDeadlineMs = 3000`), then `decodeInteraction` extracts the
|
|
231
|
+
opaque minted id (`ck:…`), any tiny `bind()` value, and the message ref, and
|
|
232
|
+
hands an `InteractionEvent` to the engine. The token carries only the opaque
|
|
233
|
+
id — no props or secrets. Unrelated clicks decode to events the bot
|
|
234
|
+
harmlessly ignores.
|
|
235
|
+
|
|
236
|
+
### Human-in-the-loop
|
|
237
|
+
|
|
238
|
+
Use `thread.awaitChoice(<Picker .../>)` to post an interactive message and
|
|
239
|
+
block until a click resolves it; the resolved value is the clicked control's
|
|
240
|
+
value. Agent interrupts (`on_interrupt`) are captured by the run renderer and
|
|
241
|
+
dispatched to your `onInterrupt` handler, which posts a picker; the click
|
|
242
|
+
resumes the agent via `thread.resume(value)`.
|
|
243
|
+
|
|
244
|
+
### Sender-profile resolution & file download
|
|
245
|
+
|
|
246
|
+
The adapter resolves each turn's Slack user id to a richer `PlatformUser`
|
|
247
|
+
(`{ id, name?, email? }`), cached per id. Inbound files can be downloaded and
|
|
248
|
+
delivered to the agent as multimodal content parts (`buildFileContentParts`);
|
|
249
|
+
a tool can post a file back out via `thread.postFile(...)`.
|
|
250
|
+
|
|
251
|
+
### Built-ins
|
|
252
|
+
|
|
253
|
+
- `defaultSlackTools` — ships `lookup_slack_user` so the agent can resolve a
|
|
254
|
+
name/handle/email to a `<@USERID>` mention. Spread into `tools`.
|
|
255
|
+
- `defaultSlackContext` — tagging procedure, Markdown-vs-mrkdwn guidance, and
|
|
256
|
+
the Slack thread/DM conversation model. Spread into `context`.
|
|
257
|
+
|
|
258
|
+
## Tool context
|
|
259
|
+
|
|
260
|
+
There is no Slack-specific tool context. Tools receive the single shared
|
|
261
|
+
`BotToolContext` from `@copilotkit/channels` (`{ thread, message?, user?, signal?,
|
|
262
|
+
platform }`) and reach Slack power only through capability-gated `thread`
|
|
263
|
+
methods, which this adapter backs:
|
|
264
|
+
|
|
265
|
+
- `thread.getMessages()` — the current thread's messages (via
|
|
266
|
+
`conversations.replies`), each a `ThreadMessage` (`{ user?, text, ts?,
|
|
267
|
+
isBot? }`).
|
|
268
|
+
- `thread.lookupUser(query)` — resolve a name/handle/email to a `PlatformUser`.
|
|
269
|
+
- `thread.postFile({ bytes, filename, title?, altText? })` — upload a file
|
|
270
|
+
back into the thread (`files.uploadV2`).
|
|
271
|
+
|
|
272
|
+
This keeps tools portable: define them with `defineBotTool({...})` and they
|
|
273
|
+
work against any adapter that advertises the same capabilities.
|
|
274
|
+
|
|
275
|
+
## Running the demo
|
|
276
|
+
|
|
277
|
+
This package is the **library**. A runnable end-to-end demo wiring all of the
|
|
278
|
+
above against a real workspace lives in
|
|
279
|
+
[`examples/slack`](../../examples/slack).
|
|
280
|
+
|
|
281
|
+
## Slash commands
|
|
282
|
+
|
|
283
|
+
The adapter forwards every slash command Slack delivers to the engine, which
|
|
284
|
+
routes it to the matching `bot.onCommand` handler (and ignores unregistered
|
|
285
|
+
ones). Register handlers on the engine — see
|
|
286
|
+
[`@copilotkit/channels`](../channels/README.md):
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
bot.onCommand({
|
|
290
|
+
name: "triage",
|
|
291
|
+
description: "Summarize the thread and propose issues.",
|
|
292
|
+
async handler({ thread, text, user }) {
|
|
293
|
+
await thread.runAgent({ prompt: `Triage: ${text}` });
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
**You must also declare each command in the Slack app config** ("Slash
|
|
299
|
+
Commands" / app manifest) with the same name — Slack won't deliver an
|
|
300
|
+
unregistered command, even over Socket Mode. Args arrive as free text
|
|
301
|
+
(`ctx.text`); the optional `options` schema is for surfaces with native
|
|
302
|
+
structured args (e.g. Discord) and is unused on Slack. The adapter does not
|
|
303
|
+
implement `registerCommands`, so the engine skips it (Slack matches commands
|
|
304
|
+
dynamically rather than registering them up front).
|
|
305
|
+
|
|
306
|
+
## OAuth bot scopes
|
|
307
|
+
|
|
308
|
+
The following bot token scopes are required or relevant depending on the
|
|
309
|
+
features your app uses:
|
|
310
|
+
|
|
311
|
+
| Scope | Required for |
|
|
312
|
+
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
313
|
+
| `chat:write` | Posting messages, streaming, ephemeral messages (`chat.postEphemeral`), and opening modals (`views.open`) — all share this single scope. |
|
|
314
|
+
| `reactions:read` | Reading reactions; subscribe to `reaction_added` / `reaction_removed` events in the app manifest to receive them. |
|
|
315
|
+
| `reactions:write` | Adding or removing reactions via `reactions.add` / `reactions.remove`. |
|
|
316
|
+
| `assistant:write` | Native streaming `task_update` tool-timeline chunks and the assistant pane. (The "is thinking…" status works with `chat:write` alone.) |
|
|
317
|
+
| `files:write` | Uploading files via `thread.postFile()`. |
|
|
318
|
+
| `users:read` | Resolving Slack user profiles (name, email) via `users.info`. |
|
|
319
|
+
| `users:read.email` | Resolving user email addresses. |
|
|
320
|
+
| `channels:history` | Reading channel thread messages via `conversations.replies`. |
|
|
321
|
+
| `groups:history` | Reading private-channel thread messages via `conversations.replies`. |
|
|
322
|
+
| `im:history` | Reading DM thread messages via `conversations.replies`. |
|
|
323
|
+
| `mpim:history` | Reading group-DM thread messages via `conversations.replies`. |
|
|
324
|
+
|
|
325
|
+
### Notes
|
|
326
|
+
|
|
327
|
+
- **Modals** (`views.open`, `view_submission`, `view_closed`): handled via
|
|
328
|
+
`chat:write` — no additional scope is needed.
|
|
329
|
+
- **Ephemeral messages** (`chat.postEphemeral`): covered by `chat:write`.
|
|
330
|
+
- **Reactions** (`reactions:read` / `reactions:write`): these scopes alone
|
|
331
|
+
are not enough — you must also subscribe to the `reaction_added` and
|
|
332
|
+
`reaction_removed` events in the Slack app manifest so that Slack delivers
|
|
333
|
+
the events to your bot.
|
|
334
|
+
|
|
335
|
+
## What's NOT in v1
|
|
336
|
+
|
|
337
|
+
- OAuth / multi-workspace install (single bot token only)
|
|
338
|
+
- Durable (Redis/DB) `ActionStore` — in-memory only; actions expire on
|
|
339
|
+
restart
|
|
340
|
+
- Proactive posting (bot replies only to turns it's part of)
|
|
341
|
+
|
|
342
|
+
## Exports
|
|
343
|
+
|
|
344
|
+
`slack`, `SlackAdapter`, `SlackAdapterOptions`, `SlackAssistantOptions`,
|
|
345
|
+
`SlackRespondToOptions`;
|
|
346
|
+
`createRunRenderer`; `decodeInteraction`, `conversationKeyOf`; `renderBlockKit`,
|
|
347
|
+
`renderSlackMessage`, `SLACK_LIMITS`; `defaultSlackTools`,
|
|
348
|
+
`lookupSlackUserTool`, `defaultSlackContext` (+ the individual context
|
|
349
|
+
entries); `markdownToMrkdwn`; and the
|
|
350
|
+
preserved mechanics (`SlackConversationStore`, `MessageStream`,
|
|
351
|
+
`ChunkedMessageStream`, `NativeMessageStream`, `attachSlackListener`,
|
|
352
|
+
`attachAssistant`, `SanitizingHttpAgent`, `buildFileContentParts`,
|
|
353
|
+
`autoCloseOpenMarkdown`, and supporting types).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assistant.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/assistant.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
let capturedConfig;
|
|
3
|
+
vi.mock("@slack/bolt", () => ({
|
|
4
|
+
Assistant: class {
|
|
5
|
+
config;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
capturedConfig = config;
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
}));
|
|
12
|
+
import { attachAssistant } from "../assistant.js";
|
|
13
|
+
function setup(opts = {}) {
|
|
14
|
+
capturedConfig = undefined;
|
|
15
|
+
const onTurn = vi.fn(async () => { });
|
|
16
|
+
const onThreadStarted = vi.fn(async () => { });
|
|
17
|
+
const sink = {
|
|
18
|
+
onTurn,
|
|
19
|
+
onInteraction: vi.fn(),
|
|
20
|
+
onCommand: vi.fn(),
|
|
21
|
+
onThreadStarted,
|
|
22
|
+
onReaction: vi.fn(),
|
|
23
|
+
onModalSubmit: vi.fn(async () => { }),
|
|
24
|
+
onModalClose: vi.fn(),
|
|
25
|
+
};
|
|
26
|
+
const resolveUser = vi.fn(async (id) => ({ id, name: `name-${id}` }));
|
|
27
|
+
// The Bolt App is only used for `app.assistant(...)`, which our mocked
|
|
28
|
+
// Assistant ignores — capture happens in the constructor above.
|
|
29
|
+
const app = { assistant: vi.fn() };
|
|
30
|
+
const handle = attachAssistant({ app, sink, opts, resolveUser });
|
|
31
|
+
return { handle, onTurn, onThreadStarted, resolveUser };
|
|
32
|
+
}
|
|
33
|
+
const threadStartedEvent = {
|
|
34
|
+
event: {
|
|
35
|
+
assistant_thread: {
|
|
36
|
+
user_id: "U1",
|
|
37
|
+
channel_id: "D1",
|
|
38
|
+
thread_ts: "100.0",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
describe("attachAssistant — threadStarted", () => {
|
|
43
|
+
beforeEach(() => setup());
|
|
44
|
+
it("applies static defaults (greeting + prompts) BEFORE emitting onThreadStarted", async () => {
|
|
45
|
+
const { onThreadStarted } = setup({
|
|
46
|
+
greeting: "hello!",
|
|
47
|
+
suggestedPrompts: [{ title: "Triage", message: "Triage my issues" }],
|
|
48
|
+
});
|
|
49
|
+
const say = vi.fn(async () => { });
|
|
50
|
+
const setSuggestedPrompts = vi.fn(async () => { });
|
|
51
|
+
await capturedConfig.threadStarted({
|
|
52
|
+
...threadStartedEvent,
|
|
53
|
+
say,
|
|
54
|
+
setSuggestedPrompts,
|
|
55
|
+
});
|
|
56
|
+
expect(say).toHaveBeenCalledWith("hello!");
|
|
57
|
+
expect(setSuggestedPrompts).toHaveBeenCalledWith({
|
|
58
|
+
prompts: [{ title: "Triage", message: "Triage my issues" }],
|
|
59
|
+
});
|
|
60
|
+
expect(onThreadStarted).toHaveBeenCalledTimes(1);
|
|
61
|
+
// Ordering rule: defaults first, then the engine hook layers on top.
|
|
62
|
+
expect(say.mock.invocationCallOrder[0]).toBeLessThan(onThreadStarted.mock.invocationCallOrder[0]);
|
|
63
|
+
expect(setSuggestedPrompts.mock.invocationCallOrder[0]).toBeLessThan(onThreadStarted.mock.invocationCallOrder[0]);
|
|
64
|
+
});
|
|
65
|
+
it("emits onThreadStarted with a thread-scoped key and recipientUserId", async () => {
|
|
66
|
+
const { onThreadStarted } = setup();
|
|
67
|
+
await capturedConfig.threadStarted({
|
|
68
|
+
...threadStartedEvent,
|
|
69
|
+
say: vi.fn(),
|
|
70
|
+
setSuggestedPrompts: vi.fn(),
|
|
71
|
+
});
|
|
72
|
+
expect(onThreadStarted).toHaveBeenCalledWith(expect.objectContaining({
|
|
73
|
+
conversationKey: "D1::100.0",
|
|
74
|
+
replyTarget: {
|
|
75
|
+
channel: "D1",
|
|
76
|
+
threadTs: "100.0",
|
|
77
|
+
recipientUserId: "U1",
|
|
78
|
+
},
|
|
79
|
+
user: { id: "U1", name: "name-U1" },
|
|
80
|
+
platform: "slack",
|
|
81
|
+
}));
|
|
82
|
+
});
|
|
83
|
+
it("records the pane thread for the listener guard", async () => {
|
|
84
|
+
const { handle } = setup();
|
|
85
|
+
expect(handle.isAssistantThread("D1", "100.0")).toBe(false);
|
|
86
|
+
await capturedConfig.threadStarted({
|
|
87
|
+
...threadStartedEvent,
|
|
88
|
+
say: vi.fn(),
|
|
89
|
+
setSuggestedPrompts: vi.fn(),
|
|
90
|
+
});
|
|
91
|
+
expect(handle.isAssistantThread("D1", "100.0")).toBe(true);
|
|
92
|
+
expect(handle.isAssistantThread("D1", "999.0")).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
it("does not post a greeting or prompts when none are configured", async () => {
|
|
95
|
+
const { onThreadStarted } = setup();
|
|
96
|
+
const say = vi.fn(async () => { });
|
|
97
|
+
const setSuggestedPrompts = vi.fn(async () => { });
|
|
98
|
+
await capturedConfig.threadStarted({
|
|
99
|
+
...threadStartedEvent,
|
|
100
|
+
say,
|
|
101
|
+
setSuggestedPrompts,
|
|
102
|
+
});
|
|
103
|
+
expect(say).not.toHaveBeenCalled();
|
|
104
|
+
expect(setSuggestedPrompts).not.toHaveBeenCalled();
|
|
105
|
+
expect(onThreadStarted).toHaveBeenCalledTimes(1);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
describe("attachAssistant — userMessage", () => {
|
|
109
|
+
const msg = {
|
|
110
|
+
message: { channel: "D1", thread_ts: "100.0", text: "hello", user: "U1" },
|
|
111
|
+
};
|
|
112
|
+
it("delivers exactly one turn scoped to the pane thread, with recipientUserId", async () => {
|
|
113
|
+
const { onTurn } = setup();
|
|
114
|
+
await capturedConfig.userMessage({ ...msg, setTitle: vi.fn() });
|
|
115
|
+
expect(onTurn).toHaveBeenCalledTimes(1);
|
|
116
|
+
expect(onTurn).toHaveBeenCalledWith(expect.objectContaining({
|
|
117
|
+
conversationKey: "D1::100.0",
|
|
118
|
+
replyTarget: {
|
|
119
|
+
channel: "D1",
|
|
120
|
+
threadTs: "100.0",
|
|
121
|
+
recipientUserId: "U1",
|
|
122
|
+
},
|
|
123
|
+
userText: "hello",
|
|
124
|
+
platform: "slack",
|
|
125
|
+
}));
|
|
126
|
+
});
|
|
127
|
+
it("auto-titles from the FIRST user message only", async () => {
|
|
128
|
+
const { onTurn } = setup(); // title defaults to "auto"
|
|
129
|
+
const setTitle = vi.fn(async () => { });
|
|
130
|
+
await capturedConfig.userMessage({ ...msg, setTitle });
|
|
131
|
+
await capturedConfig.userMessage({
|
|
132
|
+
message: { ...msg.message, text: "second message" },
|
|
133
|
+
setTitle,
|
|
134
|
+
});
|
|
135
|
+
expect(setTitle).toHaveBeenCalledTimes(1);
|
|
136
|
+
expect(setTitle).toHaveBeenCalledWith("hello");
|
|
137
|
+
expect(onTurn).toHaveBeenCalledTimes(2);
|
|
138
|
+
});
|
|
139
|
+
it("never titles when title is disabled", async () => {
|
|
140
|
+
setup({ title: false });
|
|
141
|
+
const setTitle = vi.fn(async () => { });
|
|
142
|
+
await capturedConfig.userMessage({ ...msg, setTitle });
|
|
143
|
+
expect(setTitle).not.toHaveBeenCalled();
|
|
144
|
+
});
|
|
145
|
+
it("ignores a message with no thread (not a pane message)", async () => {
|
|
146
|
+
const { onTurn } = setup();
|
|
147
|
+
await capturedConfig.userMessage({
|
|
148
|
+
message: { channel: "D1", text: "no thread", user: "U1" },
|
|
149
|
+
setTitle: vi.fn(),
|
|
150
|
+
});
|
|
151
|
+
expect(onTurn).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto-close-streaming.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/auto-close-streaming.test.ts"],"names":[],"mappings":""}
|