@gitterm/slack-bot 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/README.md ADDED
@@ -0,0 +1,274 @@
1
+ # @gitterm/slack-bot
2
+
3
+ A Slack bot that puts a coding agent on your repository. Mention it in a channel and an OpenCode
4
+ agent works on the request in a [GitTerm](https://gitterm.dev) sandbox, asks back with buttons
5
+ when it needs a decision, and replies in the thread.
6
+
7
+ You run one small process anywhere (your laptop, a VPS, Railway, a container). It connects to
8
+ Slack over Socket Mode, so it needs no public URL.
9
+
10
+ ## Set up in five minutes
11
+
12
+ 1. **Create the bot under Bots in the GitTerm dashboard.** Pick a model, the repository, GitHub
13
+ access, and optionally tools, where it answers, and who can use it. GitTerm saves these
14
+ settings (never your tokens) and gives you the bot's API token, shown once, in a ready `.env`
15
+ that holds only secrets. Come back to the bot's page any time to change its settings; the bot
16
+ loads them each time it starts.
17
+
18
+ 2. **Create the Slack app** with the page's _Create the Slack app_ button: Slack opens with the
19
+ app already configured. (Or paste `npx @gitterm/slack-bot manifest "Acme Agent"` at
20
+ [api.slack.com/apps](https://api.slack.com/apps) → _From a manifest_.) Install it to your
21
+ workspace, copy the _Bot User OAuth Token_ (`xoxb-…`) into `SLACK_BOT_TOKEN`, and under
22
+ _Basic Information → App-Level Tokens_ create one with `connections:write` for
23
+ `SLACK_APP_TOKEN` (`xapp-…`).
24
+
25
+ 3. **Start it** next to the `.env`, from the terminal or from your own code:
26
+
27
+ ```sh
28
+ npx @gitterm/slack-bot
29
+ ```
30
+
31
+ ```ts
32
+ import { createSlackBot } from "@gitterm/slack-bot";
33
+
34
+ await createSlackBot({
35
+ repo: "https://github.com/acme/app",
36
+ model: "anthropic/claude-sonnet-5-5",
37
+ }).start(); // tokens from the environment, or botToken / appToken / gitterm: { token }
38
+ ```
39
+
40
+ The bot checks everything before it connects (token, model credential, GitHub access, tools,
41
+ compute) and prints what to fix if something is missing.
42
+
43
+ 4. **Invite the bot** to a channel (`/invite @Acme Agent`) and mention it:
44
+ `@Acme Agent why does the login page flash on load?`
45
+
46
+ ## How it works
47
+
48
+ - **One sandbox per repository.** The first message creates a GitTerm workspace for the
49
+ repository. It pauses when idle and wakes up with the next message, so the checkout, installed
50
+ dependencies, and branches stay where the agent left them, and you pay only while it works.
51
+ - **One session per thread.** A mention in a channel starts a session; mentioning the bot again in
52
+ the thread continues it with full context. Anyone in the thread can follow up, and messages
53
+ posted between mentions are passed along as context. Threads work in parallel; messages within
54
+ one thread wait their turn.
55
+ - **Native progress.** While it works, Slack shows _“Acme Agent is working on it…”_ under the
56
+ thread, the same indicator Slack's AI apps use, with the elapsed time. Workspaces that refuse it
57
+ get a status message the bot keeps up to date instead.
58
+ - **Private DMs.** Message the bot directly, no mention needed. Each DM gets its own sandbox, so
59
+ the agent there can't see sessions from channels or other people's DMs. Apps created before
60
+ this need the manifest's `im:history` scope, `message.im` event, and Messages tab; reinstall
61
+ after updating it.
62
+ - **Pick a repository per thread.** With `repos`, people name one in a thread's first message
63
+ (`@Acme Agent in acme/api, why is login slow?`); a channel without a repository asks which one.
64
+ The thread stays on that repository.
65
+ - **Credited pull requests.** The agent opens pull requests that start with "Requested by <name> in Slack", without linking the conversation (the repository may be public). The request gets 👀 when picked up, then ✅ or ❌.
66
+ - **It asks back.** Agent questions appear as a card with a button per option (checkboxes for
67
+ multiple choice, a dialog for a written answer). Tool approvals show _Allow once_, _Always
68
+ allow_, and _Deny_. The person who asked can also just reply in the thread with an option
69
+ number, `yes`, or `no`.
70
+ - **Images reach the agent.** PNG, JPEG, GIF, and WebP files up to 5 MB, posted with the mention
71
+ or earlier in the thread, are attached to the run. Other files are only named.
72
+ - **Restarts are safe.** Runs live in GitTerm, not in the bot. A restarted bot reattaches to
73
+ running work and posts the result. The state file only holds thread → run ids, so losing it
74
+ costs thread continuity, never sandboxes (they are found by their tags).
75
+ - **Open the sandbox yourself.** Each repository's sandbox is the GitTerm workspace named
76
+ `slack-bot-<repository>`. Its dashboard page has the server URL and password, so you can
77
+ attach the OpenCode TUI or desktop app and see every thread's session. Prompts you send there
78
+ are not posted back to Slack.
79
+
80
+ ### Commands
81
+
82
+ Mention the bot with one word:
83
+
84
+ | Command | What it does |
85
+ | -------- | -------------------------------------------------------------------------- |
86
+ | `stop` | Cancel the work in this thread. |
87
+ | `status` | Show the repository, sandbox state, model, tools, and who may use the bot. |
88
+ | `reset` | Terminate the sandbox; the next message creates a fresh one. |
89
+ | `help` | Explain how to use the bot. |
90
+
91
+ Use `reset` after changing the bot's instructions, connections, or workspace settings: they apply
92
+ when a sandbox is created.
93
+
94
+ ## Configuration
95
+
96
+ Settings saved for the bot under Bots in the dashboard load when it starts, found by its API
97
+ token. Every setting below overrides them, from code or from the terminal: in code, pass it to
98
+ `createSlackBot(await withSavedConfig({ ... }))`; on the command line, set the environment
99
+ variable (a `.env` file in the working directory is loaded) or the flag. Without saved settings,
100
+ these are the whole configuration.
101
+
102
+ | Option | Environment variable / flag | Meaning |
103
+ | --------------------------------- | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
104
+ | `botToken` | `SLACK_BOT_TOKEN` | Bot token (`xoxb-…`). |
105
+ | `appToken` | `SLACK_APP_TOKEN` | App-level token with `connections:write` (`xapp-…`). |
106
+ | `app` | — | A Bolt app you already run; see [below](#add-it-to-a-bot-you-already-run). |
107
+ | `gitterm` | `GITTERM_API_TOKEN`, `GITTERM_SERVER_URL` | `{ token, serverUrl }` or a client. Falls back to the `gitterm login` config. |
108
+ | `repo` | `GITTERM_BOT_REPO`, `--repo` | Repository for every channel; `url#branch` picks a branch. |
109
+ | `channels` | `GITTERM_BOT_CHANNELS` (`C0123=https://github.com/acme/api,…`) | Per-channel repositories. Without `repo`, the bot answers only in these channels. |
110
+ | `repos` | `GITTERM_BOT_REPOS` | More repositories people can name in a thread's first message (`in acme/api`). |
111
+ | `connections` | `GITTERM_BOT_CONNECTIONS` (`Linear,Sentry`) | Tools besides GitHub, by name as shown under Integrations, integration key (`executor`), or id. GitHub access for the repository is attached automatically. |
112
+ | `model` | `GITTERM_BOT_MODEL`, `--model`, `GITTERM_BOT_MODEL_CREDENTIAL`, `GITTERM_BOT_MODEL_API_KEY` | `provider/model` for every run; only that provider's credential reaches the sandbox. See [Models](#models). |
113
+ | `instructions` | `GITTERM_BOT_INSTRUCTIONS`, `GITTERM_BOT_INSTRUCTIONS_FILE`, `--instructions-file` | What the agent should know and how it should behave; see [Instructions](#instructions). |
114
+ | `env` | `GITTERM_BOT_SANDBOX_ENV_FILE` (a dotenv file) | Environment variables for new sandboxes, e.g. test credentials. |
115
+ | `setup` | `GITTERM_BOT_SETUP` | Commands run in the checkout before the agent starts, e.g. `pnpm install`. |
116
+ | `allowedUsers` | `GITTERM_BOT_ALLOWED_USERS` | Only these user ids may use the bot. Default: everyone in the channel. |
117
+ | `allowGuests` | `GITTERM_BOT_ALLOW_GUESTS=true` | Let guests and people from other organisations in shared channels use the bot. Default off. |
118
+ | `workspace` | `GITTERM_BOT_PROVIDER` (provider only) | Any `workspaces.create()` setting for new sandboxes: provider, image, setup, OpenCode config. |
119
+ | `workspace.repositoryCredentials` | `GITTERM_BOT_GITHUB_TOKEN` | Your own GitHub token instead of a GitTerm GitHub connection. |
120
+ | `stateFile` | `GITTERM_BOT_STATE_FILE` | Where thread sessions are kept. Default `.gitterm-bot/slack.json`. |
121
+ | `runTimeoutMs` | `GITTERM_BOT_RUN_TIMEOUT_MINUTES` | Give up on a request after this long, including time waiting for answers. Default 60 min. |
122
+ | `inputTimeoutMs` | — | How long a question or approval waits for an answer. Default 30 min. |
123
+ | `logger` | — | Where the bot logs. Default `console`. |
124
+
125
+ GitHub access is automatic: GitTerm attaches the GitHub connection that covers the repository's
126
+ owner, or the deployment's shared one. To use your own token instead, set
127
+ `GITTERM_BOT_GITHUB_TOKEN` (classic with the `repo` scope, or fine-grained with Contents and
128
+ Pull requests read and write); GitTerm doesn't save it. Tools are attached only when you name them, because the
129
+ bot acts with your accounts on behalf of everyone in the channel. `workspace` takes any
130
+ [`@gitterm/sdk`](https://www.npmjs.com/package/@gitterm/sdk) `workspaces.create()` option except
131
+ the repository, name, agent, connections, and tags, which the bot owns.
132
+
133
+ ### Instructions
134
+
135
+ Tell the agent about your team and how to behave. The text is added after the bot's own rules and
136
+ loaded into every session:
137
+
138
+ ```ts
139
+ import { readFile } from "node:fs/promises";
140
+
141
+ createSlackBot({
142
+ repo: "https://github.com/acme/app",
143
+ instructions: await readFile("bot-instructions.md", "utf8"),
144
+ });
145
+ ```
146
+
147
+ ```sh
148
+ npx @gitterm/slack-bot --repo https://github.com/acme/app --instructions-file bot-instructions.md
149
+ ```
150
+
151
+ Instructions are written into a sandbox when it is created; after changing them, mention the bot
152
+ with `reset`. Project knowledge that belongs with the code (how to run tests, where things live)
153
+ is better in an `AGENTS.md` in the repository, which OpenCode reads on its own.
154
+
155
+ ### Models
156
+
157
+ Pick the model and the bot hands the sandbox that one provider's credential, nothing else. A
158
+ sandbox anyone in the channel can drive should not hold every model account on your dashboard.
159
+
160
+ ```ts
161
+ model: "anthropic/claude-sonnet-5-5" // the provider's default saved credential
162
+ model: { id: "anthropic/claude-sonnet-5-5", credential: "team" } // a saved credential by label
163
+ model: { id: "anthropic/claude-sonnet-5-5", apiKey: process.env.KEY } // a key for this bot only, never saved
164
+ ```
165
+
166
+ On the command line: `GITTERM_BOT_MODEL`, plus `GITTERM_BOT_MODEL_CREDENTIAL` or
167
+ `GITTERM_BOT_MODEL_API_KEY`. Models that need no key (such as OpenCode's free models) get no
168
+ credential. Without a model, the sandbox gets every saved dashboard credential and the dashboard's
169
+ default model. Credentials are set when a sandbox is created; after switching to another
170
+ provider, mention the bot with `reset`.
171
+
172
+ ## Running from code
173
+
174
+ `createSlackBot()` returns `{ start(), stop() }`. `start()` resolves once the bot is connected;
175
+ `stop()` disconnects it. Runs keep going in GitTerm while the bot is down, and the next
176
+ `start()` picks them up.
177
+
178
+ ```ts
179
+ import { createSlackBot } from "@gitterm/slack-bot";
180
+
181
+ const bot = createSlackBot({
182
+ repo: "https://github.com/acme/app#main",
183
+ channels: { C0API: "https://github.com/acme/api" },
184
+ model: "anthropic/claude-sonnet-5-5",
185
+ instructions: "Run `pnpm test` before opening a pull request.",
186
+ workspace: {
187
+ provider: { type: "railway" },
188
+ setup: { beforeAgent: ["pnpm install"] },
189
+ opencode: { config: { permission: { bash: "allow", edit: "allow" } } },
190
+ },
191
+ logger: myLogger, // anything with info, warn, and error
192
+ });
193
+
194
+ await bot.start();
195
+ process.once("SIGTERM", () => void bot.stop());
196
+ ```
197
+
198
+ ### Add it to a bot you already run
199
+
200
+ Pass your Bolt app and the agent answers its mentions alongside your own listeners. You keep
201
+ starting and stopping the app, with whatever receiver it uses (Socket Mode or HTTP). Your app
202
+ needs the scopes and events from `npx @gitterm/slack-bot manifest`; make sure none of your own
203
+ listeners also answer `app_mention`.
204
+
205
+ ```ts
206
+ import { App } from "@slack/bolt";
207
+ import { createSlackBot } from "@gitterm/slack-bot";
208
+
209
+ const app = new App({
210
+ token: process.env.SLACK_BOT_TOKEN,
211
+ signingSecret: process.env.SLACK_SIGNING_SECRET,
212
+ });
213
+ app.command("/deploy", async ({ ack }) => {
214
+ await ack("Deploying…");
215
+ });
216
+
217
+ await createSlackBot({ app, repo: "https://github.com/acme/app" }).start();
218
+ await app.start(3000);
219
+ ```
220
+
221
+ An app that authorizes per workspace also needs `botToken`, which the agent uses for its own API
222
+ calls and file downloads.
223
+
224
+ ### Slack and Discord in one process
225
+
226
+ ```ts
227
+ import { createDiscordBot } from "@gitterm/discord-bot";
228
+ import { createSlackBot } from "@gitterm/slack-bot";
229
+
230
+ const settings = {
231
+ repo: "https://github.com/acme/app",
232
+ gitterm: { token: process.env.GITTERM_API_TOKEN },
233
+ };
234
+ await Promise.all([createSlackBot(settings).start(), createDiscordBot(settings).start()]);
235
+ ```
236
+
237
+ Each platform gets its own sandbox and its own state file.
238
+
239
+ ## Hosting
240
+
241
+ The image runs either bot and picks the platform from the token in the `.env`. In the folder
242
+ with the `.env`:
243
+
244
+ ```sh
245
+ docker run -d --name gitterm-slack-bot --restart unless-stopped \
246
+ -v "$PWD/.env:/data/.env:ro" -v gitterm-slack-bot:/data \
247
+ ghcr.io/opeoginni/gitterm-bot
248
+ ```
249
+
250
+ The `.env` is mounted rather than passed with `--env-file`, which doesn't read dotenv quoting.
251
+ Thread sessions are kept in the `/data` volume. Without Docker, any machine with Node 22.12+
252
+ works: run `npx @gitterm/slack-bot` and keep `.gitterm-bot/` on persistent storage.
253
+
254
+ Run one bot process per Slack app. Two processes with the same tokens would both answer.
255
+
256
+ ## Security
257
+
258
+ - **Who can use it:** full members of the workspace in channels the bot was invited to, or only
259
+ the `allowedUsers` you list. Guests and people from other organisations in shared channels are
260
+ refused (and cannot press approval buttons) unless you set `allowGuests`.
261
+ - **What the agent can reach:** whatever the attached GitTerm connections allow. Scope the GitHub
262
+ connection and MCP credentials accordingly, and use OpenCode `permission` rules (via
263
+ `workspace.opencode.config`) to deny commands outright.
264
+ - **What the bot stores:** GitTerm workspace and run ids, written with mode `0600`. No tokens.
265
+ - Agent replies never mention `@channel` or `@here`, and only images are forwarded to the agent.
266
+
267
+ ## Limits
268
+
269
+ - The agent is OpenCode. T3 Code workspaces have no run API for bots to drive.
270
+ - A question left unanswered past the workspace's idle timeout lets the sandbox pause, which
271
+ cancels the run.
272
+ - Direct messages are not supported; talk to the bot in channels.
273
+
274
+ See [`@gitterm/bot`](https://www.npmjs.com/package/@gitterm/bot) to put the same agent behind another chat platform.
@@ -0,0 +1,19 @@
1
+ import type { ChatAdapter } from "@gitterm/bot";
2
+ import { App } from "@slack/bolt";
3
+ export type SlackAdapterOptions = {
4
+ /** Bot token (`xoxb-…`). Defaults to `SLACK_BOT_TOKEN`. */
5
+ botToken?: string;
6
+ /** App-level token with `connections:write` (`xapp-…`) for Socket Mode. Defaults to `SLACK_APP_TOKEN`. */
7
+ appToken?: string;
8
+ /**
9
+ * A Bolt app you already run, to add the agent to an existing bot. The agent registers its
10
+ * listeners on it; you start and stop the app yourself, with any receiver. Make sure your own
11
+ * listeners do not also answer the same mentions.
12
+ */
13
+ app?: App;
14
+ };
15
+ /**
16
+ * Slack over Socket Mode: no public URL needed. The bot answers mentions in channels it was
17
+ * invited to; every thread is one agent session.
18
+ */
19
+ export declare function createSlackAdapter(options?: SlackAdapterOptions): ChatAdapter;
@@ -0,0 +1,58 @@
1
+ import { type ChatPrompt } from "@gitterm/bot";
2
+ /** `action_id`s and `callback_id`s of the interactive parts; adapter.ts routes them. */
3
+ export declare const ACTION: {
4
+ readonly option: RegExp;
5
+ readonly toggle: "gitterm_toggle";
6
+ readonly submit: "gitterm_submit";
7
+ readonly custom: "gitterm_custom";
8
+ readonly permission: RegExp;
9
+ };
10
+ export declare const CUSTOM_MODAL = "gitterm_custom_modal";
11
+ export declare const CUSTOM_INPUT = "gitterm_custom_text";
12
+ type Block = {
13
+ type: string;
14
+ block_id?: string;
15
+ [key: string]: unknown;
16
+ };
17
+ export declare const promptIdOf: (id: string) => string | null;
18
+ /** GitHub-flavoured Markdown to Slack mrkdwn, leaving code blocks alone. */
19
+ export declare function toMrkdwn(markdown: string): string;
20
+ /** The agent's answer as messages of section blocks; the last one carries the footer. */
21
+ export declare function answerMessages(markdown: string, footer: string): Array<{
22
+ text: string;
23
+ blocks: Block[];
24
+ }>;
25
+ /** The open prompt as a message attachment (the colour bar), with notification text. */
26
+ export declare function promptAttachment(prompt: ChatPrompt): {
27
+ color: string;
28
+ fallback: string;
29
+ blocks: Block[];
30
+ };
31
+ /** The prompt once answered or abandoned: controls removed, outcome shown. */
32
+ export declare function settledAttachment(prompt: ChatPrompt, outcome: string): {
33
+ color: string;
34
+ fallback: string;
35
+ blocks: Block[];
36
+ };
37
+ /** Dialog behind "Write your own answer"; `private_metadata` routes the submission back. */
38
+ export declare function customAnswerView(prompt: Extract<ChatPrompt, {
39
+ kind: "question";
40
+ }>): {
41
+ type: "modal";
42
+ callback_id: string;
43
+ private_metadata: string;
44
+ title: {
45
+ type: "plain_text";
46
+ text: string;
47
+ };
48
+ submit: {
49
+ type: "plain_text";
50
+ text: string;
51
+ };
52
+ close: {
53
+ type: "plain_text";
54
+ text: string;
55
+ };
56
+ blocks: Block[];
57
+ };
58
+ export {};
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};