
Slack Agent Template
A Slack Agent template built with Workflow DevKit's DurableAgent, AI SDK tools, Bolt for JavaScript (TypeScript), and the Nitro server framework.
Features
- Workflow DevKit — Make any TypeScript function durable. Build AI agents that can suspend, resume, and maintain state with ease. Reliability-as-code with automatic retries and observability built in
- AI SDK — The AI Toolkit for TypeScript. Define type-safe tools with schema validation and switch between AI providers by changing a single line of code
- Vercel AI Gateway — One endpoint, all your models. Access hundreds of AI models through a centralized interface with intelligent failovers and no rate limits
- Slack Assistant — Integrates with Slack's Assistant API for threaded conversations with real-time streaming responses
- Human-in-the-Loop — Built-in approval workflows that pause agent execution until a user approves sensitive actions like joining channels
- Built-in Tools — Pre-configured tools for reading channels, threads, joining channels (with approval), and searching
Prerequisites
Before getting started, make sure you have a development workspace where you have permissions to install apps. You can use a developer sandbox or create a workspace.
Getting Started
Clone and install dependencies
git clone https://github.com/vercel-partner-solutions/slack-agent-template && cd slack-agent-template && pnpm install
Create a Slack App
- Open https://api.slack.com/apps/new and choose "From an app manifest"
- Choose the workspace you want to use
- Copy the contents of
manifest.jsoninto the text box that says "Paste your manifest code here" (JSON tab) and click Next - Review the configuration and click Create
- On the Install App tab, click Install to <Workspace_Name>
- You will be redirected to the App Configuration dashboard
- Copy the Bot User OAuth Token into your environment as
SLACK_BOT_TOKEN - On the Basic Information tab, copy your Signing Secret into your environment as
SLACK_SIGNING_SECRET
Environment Setup
- Add your
AI_GATEWAY_API_KEYto your.envfile. You can get one here - Add your
NGROK_AUTH_TOKENto your.envfile. You can get one here - In the terminal run
slack app link - If prompted
update the manifest source to remoteselectyes - Copy your App ID from the app you just created
- Select
Localwhen prompted - Open
.slack/config.jsonand update your manifest source tolocal
{"manifest": {"source": "local"},"project_id": "<project-id-added-by-slack-cli>"}
- Start your local server using
slack run. If prompted, select the workspace you'd like to grant access to- Select
yesif asked "Update app settings with changes to the local manifest?"
- Select
- Open your Slack workspace and add your new Slack Agent to a channel. Your Slack Agent should respond whenever it's tagged in a message or sent a DM
Deploy to Vercel
- Create a new Slack app for production following the steps from above
- Create a new Vercel project here and select this repo
- Copy the Bot User OAuth Token into your Vercel environment variables as
SLACK_BOT_TOKEN - On the Basic Information tab, copy your Signing Secret into your Vercel environment variables as
SLACK_SIGNING_SECRET - When your deployment has finished, open your App Manifest from the Slack App Dashboard
- Update the manifest so all the
request_urlandurlfields usehttps://<your-app-domain>/api/slack/events - Click save and you will be prompted to verify the URL
- Open your Slack workspace and add your new Slack Agent to a channel. Your Slack Agent should respond whenever it's tagged in a message or sent a DM
- Note: Make sure you add the production app, not the local app we setup earlier
- Your app will now automatically build and deploy whenever you commit to your repo. More information here
Project Structure
manifest.json
manifest.json is a configuration for Slack apps. With a manifest, you can create an app with a pre-defined configuration, or adjust the configuration of an existing app.
/server/app.ts
/server/app.ts is the entry point of the application. This file is kept minimal and primarily serves to route inbound requests.
/server/lib/ai
Contains the AI agent implementation:
-
agent.ts— Creates theDurableAgentfrom Workflow with system instructions and available tools. The agent automatically handles tool calling loops until it has enough context to respond. -
tools.ts— Tool definitions using AI SDK'stoolfunction:getChannelMessages— Fetches recent messages from a Slack channelgetThreadMessages— Fetches messages from a specific threadjoinChannel— Joins a public Slack channel (with Human-in-the-Loop approval)searchChannels— Searches for channels by name, topic, or purpose
/server/listeners
Every incoming request is routed to a "listener". Inside this directory, we group each listener based on the Slack Platform feature used:
/listeners/assistant— Handles Slack Assistant events (thread started, user message, context changed)/listeners/actions— Handles interactive component actions (buttons, menus) including HITL approval handlers/listeners/shortcuts— Handles incoming Shortcuts requests/listeners/views— Handles View submissions/listeners/events— Handles Slack events like app mentions and home tab opens
/server/api
This is your Nitro server API directory. Contains events.post.ts which matches the request URL defined in your manifest.json. Nitro uses file-based routing for incoming requests. Learn more here.
Agent Architecture
Chat Workflow
The core agent loop is implemented as a durable workflow using Workflow DevKit. When a user sends a message, the workflow orchestrates the agent's response with automatic retry handling and streaming support.
┌─────────────────────────────────────────────────────────────────┐│ Chat Workflow │├─────────────────────────────────────────────────────────────────┤│ ││ User Message ──▶ assistantUserMessage listener ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ start(chatWorkflow)│ ││ │ with messages + │ ││ │ context │ ││ └─────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ createSlackAgent() │ ││ │ with tools │ ││ └─────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ agent.stream() │──▶ Tool calls ││ │ generates response │ (may loop) ││ └─────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ Stream chunks to │ ││ │ Slack via │ ││ │ chatStream() │ ││ └─────────────────────┘ ││ │ ││ ▼ ││ User sees response ││ │└─────────────────────────────────────────────────────────────────┘
Key files:
/server/lib/ai/workflows/chat.ts— The durable workflow definition using"use workflow"directive/server/lib/ai/agent.ts— Creates theDurableAgentwith system prompt and tools/server/listeners/assistant/assistantUserMessage.ts— Listener that starts the workflow and streams responses
How it works:
- User sends a message to the Slack Assistant
- The
assistantUserMessagelistener collects thread context and starts the workflow chatWorkflowcreates the agent and callsagent.stream()with the messages- The agent processes the request, calling tools as needed (each tool uses
"use step"for durability) - Response chunks are streamed back to Slack in real-time via
chatStream()
Human-in-the-Loop (HITL) Workflow
This template demonstrates a production-ready Human-in-the-Loop pattern using Workflow DevKit's defineHook primitive. When the agent needs to perform sensitive actions (like joining a channel), it pauses execution and waits for user approval.
┌─────────────────────────────────────────────────────────────────┐│ HITL Flow │├─────────────────────────────────────────────────────────────────┤│ ││ User Request ──▶ Agent ──▶ joinChannel Tool ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ Send Slack message │ ││ │ with Approve/Reject│ ││ │ buttons │ ││ └─────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ Workflow PAUSES │ ││ │ (no compute used) │◀── await hook ││ └─────────────────────┘ ││ │ ││ User clicks button ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ Action handler │ ││ │ calls hook.resume()│ ││ └─────────────────────┘ ││ │ ││ ▼ ││ ┌─────────────────────┐ ││ │ Workflow RESUMES │ ││ │ with approval data │ ││ └─────────────────────┘ ││ │ ││ ▼ ││ Agent responds ││ │└─────────────────────────────────────────────────────────────────┘
Key files:
/server/lib/ai/workflows/hooks.ts— Hook definitions for HITL workflows (e.g.,channelJoinApprovalHook)/server/lib/ai/tools.ts— Tool definitions includingjoinChannelwhich uses the approval hook/server/lib/slack/blocks.ts— Slack Block Kit UI for approval buttons/server/listeners/actions/channel-join-approval.ts— Action handler that resumes the workflow
How it works:
- The
joinChanneltool is called by the agent - A Slack message with Approve/Reject buttons is posted to the thread
channelJoinApprovalHook.create()creates a hook instance and the workflow pauses atawait hook- When the user clicks a button, the action handler calls
hook.resume()with the decision - The workflow resumes and the agent either joins the channel or acknowledges the rejection
This pattern can be extended for any action requiring human approval (e.g., sending messages, modifying data, external API calls).
Customizing the Agent
Modifying Instructions
Edit the system prompt in /server/lib/ai/agent.ts to change how your agent behaves, responds, and uses tools.
Adding New Tools
- Add a new tool definition in
/server/lib/ai/tools.tsusing AI SDK'stoolfunction:
import { tool } from "ai";import { z } from "zod";import type { SlackAgentContextInput } from "~/lib/ai/context";const myNewTool = tool({description: "Description of what this tool does",inputSchema: z.object({param: z.string().describe("Parameter description"),}),execute: async ({ param }, { experimental_context }) => {"use step"; // Required for Workflow's durable execution// Dynamic imports inside step to avoid bundling Node.js modules in workflowconst { WebClient } = await import("@slack/web-api");const ctx = experimental_context as SlackAgentContextInput;const client = new WebClient(ctx.token);// Tool implementationreturn { result: "..." };},});
- Add it to the
slackToolsexport in/server/lib/ai/tools.ts - Update the agent instructions in
/server/lib/ai/agent.tsto describe when to use the new tool
Learn more about building agents with the AI SDK in the Agents documentation.
Adding Human-in-the-Loop to Tools
To add approval workflows to your own tools:
- Add a hook definition to
/server/lib/ai/workflows/hooks.ts:
import { defineHook } from "workflow";import { z } from "zod";export const myApprovalHook = defineHook({schema: z.object({approved: z.boolean(),// Add any additional data you need}),});
- In your tool's execute function (without
"use step"), create and await the hook:
execute: async ({ param }, { toolCallId, experimental_context }) => {const ctx = experimental_context as SlackAgentContextInput;// Send approval UI to user (in a step)await sendApprovalMessage(ctx, toolCallId);// Create hook and wait for approval (in workflow context)const hook = myApprovalHook.create({ token: toolCallId });const { approved } = await hook;if (!approved) {return { success: false, message: "User declined" };}// Perform the action (in a step)return await performAction(ctx);};
- Create an action handler that calls
hook.resume()when the user responds
Learn more about hooks in the Workflow DevKit documentation.
