Skip to content

Commit 404f286

Browse files
Slack tool (#821)
* TLK-1725 - Slack tool initial commit * TLK-1725 - Slack tool * TLK-1725 - Slack tool * TLK-1725 - Slack tool lint * TLK-1725 - Slack tool pyright * TLK-1725 - Slack tool pyright * TLK-1725 - Slack tool pyright * TLK-1725 - Slack tool pyright * TLK-1725 - Slack tool - pyright * TLK-1725 - Slack tool * TLK-1725 - Slack tool small improvements * TLK-1725 - Slack tool * TLK-1725 - Slack tool review fixes * TLK-1725 - Slack tool review fixes * TLK-1725 - Slack tool prettier * TLK-1725 - Slack tool * TLK-1725 - Slack tool * TLK-1725 - Slack tool
1 parent 642f2ad commit 404f286

File tree

26 files changed

+715
-45
lines changed

26 files changed

+715
-45
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Toolkit is a deployable all-in-one RAG application that enables users to quickly
1313
- [How to add tools](/docs/custom_tool_guides/tool_guide.md)
1414
- [How to add auth to your tools](/docs/custom_tool_guides/tool_auth_guide.md)
1515
- [How to setup Google Drive](/docs/custom_tool_guides/google_drive.md)
16+
- [How to setup Slack Tool](/docs/custom_tool_guides/slack.md)
1617
- [How to setup Google Text-to-Speech](/docs/text_to_speech.md)
1718
- [How to add authentication](/docs/auth_guide.md)
1819
- [How to deploy toolkit services](/docs/service_deployments.md)

docs/custom_tool_guides/slack.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Slack Tool Setup
2+
3+
To set up the Slack tool you will need a Slack application. Follow the steps below to set it up:
4+
5+
## 1. Create a Slack App
6+
7+
Head to the [Slack API](https://api.slack.com/apps) and create a new app.
8+
After creating the app, you will see the `App Credentials` section. Copy the `Client ID` and `Client Secret` values.
9+
That will be used for the environment variables specified above.
10+
11+
## 2. Set up OAuth & Permissions
12+
OAuth flow is required to authenticate users with Slack.
13+
To enable it please set the following redirect URL to your app's settings:
14+
```bash
15+
https://<your_backend_url>/v1/tool/auth
16+
```
17+
Please note that for the local development you will need to enable HTTPS.
18+
See the [Setup HTTPS for Local Development](#5-setup-https-for-local-development) section for more details.
19+
If you are using a local https setup, redirect url should be
20+
```
21+
https://localhost:8000/v1/tool/auth
22+
```
23+
Also, you can set up a proxy, such as [ngrok](https://ngrok.com/docs/getting-started/), to expose your local server to the internet.
24+
25+
The Slack tool uses User Token Scopes to access the user's Slack workspace.
26+
The required and the default permission scope is `search:read`.
27+
Set it in the `OAuth & Permissions` section of your Slack app settings.
28+
29+
To work with the Slack Tool Advanced token security via token rotation is required.
30+
To enable it, go to the `OAuth & Permissions` section of your Slack app settings and click 'Opt in' button in the 'Advanced token security via token rotation' section.
31+
32+
More information about the OAuth flow can be found [here](https://api.slack.com/authentication/oauth-v2).
33+
34+
## 3. Set Up Environment Variables
35+
36+
Then set the following environment variables. You can either set the below values in your `secrets.yaml` file:
37+
```bash
38+
slack:
39+
client_id: <your_client_id from step 1>
40+
client_secret: <your_client_secret from step 1>
41+
```
42+
or update your `.env` configuration to contain:
43+
```bash
44+
SLACK_CLIENT_ID=<your_client_id from step 1>
45+
SLACK_CLIENT_SECRET=<your_client_secret from step 1>
46+
```
47+
48+
## 4. Enable the Slack Tool in the Frontend
49+
50+
To enable the Slack tool in the frontend, you will need to modify the `src/community/config/tools.py` file. Add the `TOOL_SLACK_ID` to the `AGENT_SETTINGS_TOOLS` list.
51+
52+
```typescript
53+
export const AGENT_SETTINGS_TOOLS = [
54+
TOOL_HYBRID_WEB_SEARCH_ID,
55+
TOOL_PYTHON_INTERPRETER_ID,
56+
TOOL_WEB_SCRAPE_ID,
57+
TOOL_SLACK_ID,
58+
];
59+
```
60+
61+
To enable the Slack tool in the frontend for Base Agent, you will need to modify the `src/community/config/tools.py` file. Remove the `TOOL_SLACK_ID` from the `BASE_AGENT_EXCLUDED_TOOLS` list.
62+
By default, the Slack Tool is disabled for the Base Agent. Also if you need to exclude some tool from the Base Agent just add it to the `BASE_AGENT_EXCLUDED_TOOLS` list.
63+
```typescript
64+
export const BASE_AGENT_EXCLUDED_TOOLS = [];
65+
```
66+
67+
## 5. Setup HTTPS for Local Development
68+
69+
To enable HTTPS for local development, the self-signed certificate needs to be generated.
70+
Run the following command in the project root directory to generate the certificate and key:
71+
72+
```bash
73+
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
74+
```
75+
76+
Then, update the backend Docker configuration(src/backend/Dockerfile) to use the generated certificate.
77+
Just change next lines in the Dockerfile:
78+
```Dockerfile
79+
COPY pyproject.toml poetry.lock cert.pem key.pem ./
80+
```
81+
and
82+
```Dockerfile
83+
CMD uvicorn backend.main:app --reload --host 0.0.0.0 --port ${PORT} --timeout-keep-alive 300 --ssl-keyfile /workspace/key.pem --ssl-certfile /workspace/cert.pem
84+
```
85+
Change NEXT_PUBLIC_API_HOSTNAME environment variable in the .env `https` protocol:
86+
```bash
87+
NEXT_PUBLIC_API_HOSTNAME=https://localhost:8000
88+
```
89+
90+
or in the configurations.yaml file:
91+
92+
```yaml
93+
auth:
94+
backend_hostname: https://localhost:8000
95+
```
96+
97+
To run the Frontend with HTTPS, update the `start` script in the `package.json` file:
98+
99+
```json
100+
"scripts": {
101+
"dev": "next dev --port 4000 --experimental-https",
102+
..........
103+
}
104+
```
105+
106+
Add the following line to the 'docker-compose.yml' file to the frontend environment variables:
107+
108+
```yaml
109+
NEXT_PUBLIC_API_HOSTNAME=https://localhost:8000
110+
```
111+
112+
and change the API_HOSTNAME to
113+
114+
```yaml
115+
API_HOSTNAME: https://localhost:8000
116+
```
117+
also change the src/interfaces/assistants_web/.env.development file env variables to use https.
118+
119+
## 6. Run the Backend and Frontend
120+
121+
run next command to start the backend and frontend:
122+
123+
```bash
124+
make dev
125+
```
126+
127+
## 7. Troubleshooting
128+
129+
If you encounter any issues with OAuth, please check the following [link](https://api.slack.com/authentication/oauth-v2#errors)
130+
For example, if you see the invalid_team_for_non_distributed_app error,
131+
please ensure the app is distributed or try logging in with the workspace owner's account.

poetry.lock

Lines changed: 21 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ llama-index = "^0.11.10"
5757
llama-index-llms-cohere = "^0.3.0"
5858
llama-index-embeddings-cohere = "^0.2.1"
5959
google-cloud-texttospeech = "^2.18.0"
60+
slack-sdk = "^3.33.1"
6061

6162
[tool.poetry.group.dev]
6263
optional = true

src/backend/config/configuration.template.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ tools:
3434
- tavily_web_search
3535
python_interpreter:
3636
url: http://terrarium:8080
37+
slack:
38+
user_scopes:
39+
- search:read
3740
feature_flags:
3841
# Experimental features
3942
use_agents_view: false

src/backend/config/secrets.template.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ tools:
3131
google_web_search:
3232
api_key:
3333
cse_id:
34+
slack:
35+
client_id:
36+
client_secret:
3437
auth:
3538
secret_key:
3639
google_oauth:

src/backend/config/settings.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ class GDriveSettings(BaseSettings, BaseModel):
146146
)
147147

148148

149+
class SlackSettings(BaseSettings, BaseModel):
150+
model_config = SETTINGS_CONFIG
151+
client_id: Optional[str] = Field(
152+
default=None,
153+
validation_alias=AliasChoices("SLACK_CLIENT_ID", "client_id"),
154+
)
155+
client_secret: Optional[str] = Field(
156+
default=None,
157+
validation_alias=AliasChoices("SLACK_CLIENT_SECRET", "client_secret"),
158+
)
159+
user_scopes: Optional[str] = Field(
160+
default=None,
161+
validation_alias=AliasChoices(
162+
"SLACK_USER_SCOPES", "scopes"
163+
),
164+
)
165+
166+
149167
class TavilyWebSearchSettings(BaseSettings, BaseModel):
150168
model_config = SETTINGS_CONFIG
151169
api_key: Optional[str] = Field(
@@ -195,10 +213,13 @@ class ToolSettings(BaseSettings, BaseModel):
195213
brave_web_search: Optional[BraveWebSearchSettings] = Field(
196214
default=BraveWebSearchSettings()
197215
)
198-
199216
hybrid_web_search: Optional[HybridWebSearchSettings] = Field(
200217
default=HybridWebSearchSettings()
201218
)
219+
slack: Optional[SlackSettings] = Field(
220+
default=SlackSettings()
221+
)
222+
202223

203224

204225
class DatabaseSettings(BaseSettings, BaseModel):

src/backend/config/tools.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
PythonInterpreter,
1515
ReadFileTool,
1616
SearchFileTool,
17+
SlackAuth,
18+
SlackTool,
1719
TavilyWebSearch,
1820
WebScrapeTool,
1921
)
@@ -43,6 +45,7 @@ class ToolName(StrEnum):
4345
Google_Web_Search = GoogleWebSearch.NAME
4446
Brave_Web_Search = BraveWebSearch.NAME
4547
Hybrid_Web_Search = HybridWebSearch.NAME
48+
Slack = SlackTool.NAME
4649

4750

4851
ALL_TOOLS = {
@@ -239,6 +242,23 @@ class ToolName(StrEnum):
239242
category=Category.WebSearch,
240243
description="Returns a list of relevant document snippets for a textual query retrieved from the internet using a mix of any existing Web Search tools.",
241244
),
245+
ToolName.Slack: ManagedTool(
246+
display_name="Slack",
247+
implementation=SlackTool,
248+
parameter_definitions={
249+
"query": {
250+
"description": "Query to search slack.",
251+
"type": "str",
252+
"required": True,
253+
}
254+
},
255+
is_visible=True,
256+
is_available=SlackTool.is_available(),
257+
auth_implementation=SlackAuth,
258+
error_message="SlackTool not available, please enable it in the SlackTool class.",
259+
category=Category.DataLoader,
260+
description="Returns a list of relevant document snippets from slack.",
261+
),
242262
}
243263

244264

src/backend/tools/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from backend.tools.hybrid_search import HybridWebSearch
77
from backend.tools.lang_chain import LangChainVectorDBRetriever, LangChainWikiRetriever
88
from backend.tools.python_interpreter import PythonInterpreter
9+
from backend.tools.slack import SlackAuth, SlackTool
910
from backend.tools.tavily_search import TavilyWebSearch
1011
from backend.tools.web_scrape import WebScrapeTool
1112

@@ -23,4 +24,6 @@
2324
"BraveWebSearch",
2425
"GoogleWebSearch",
2526
"HybridWebSearch",
27+
"SlackTool",
28+
"SlackAuth"
2629
]

src/backend/tools/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def is_auth_required(self, session: DBSessionDep, user_id: str) -> bool:
9191
return False
9292

9393
# Refresh failed, delete existing Auth
94-
tool_auth_crud.delete_tool_auth(session, self.TOOL_ID, user_id)
94+
tool_auth_crud.delete_tool_auth(session, user_id, self.TOOL_ID)
9595
return True
9696

9797
# Check access_token is retrievable
@@ -100,7 +100,7 @@ def is_auth_required(self, session: DBSessionDep, user_id: str) -> bool:
100100
auth.refresh_token
101101
except Exception():
102102
# Retrieval failed, delete existing Auth
103-
tool_auth_crud.delete_tool_auth(session, self.TOOL_ID, user_id)
103+
tool_auth_crud.delete_tool_auth(session, user_id, self.TOOL_ID)
104104
return True
105105

106106
# ToolAuth retrieved and is not expired

0 commit comments

Comments
 (0)