diff --git a/connectors/src/api/bot_enabled.ts b/connectors/src/api/bot_enabled.ts deleted file mode 100644 index ab03aad8840b..000000000000 --- a/connectors/src/api/bot_enabled.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { Request, Response } from "express"; - -import { - GET_BOT_ENABLED_BY_TYPE, - TOGGLE_BOT_BY_TYPE, -} from "@connectors/connectors"; -import { Connector } from "@connectors/lib/models"; -import { apiError, withLogging } from "@connectors/logger/withlogging"; - -type GetBotEnabledRes = { - botEnabled: boolean; -}; - -type ToggleBotReqBody = { - botEnabled: boolean; -}; - -const _getBotEnabled = async ( - req: Request<{ connector_id: string }, GetBotEnabledRes, undefined>, - res: Response -) => { - if (!req.params.connector_id) { - return apiError(req, res, { - status_code: 400, - api_error: { - type: "invalid_request_error", - message: "Missing required parameters. Required: connector_id", - }, - }); - } - const connector = await Connector.findByPk(req.params.connector_id); - if (!connector) { - return apiError(req, res, { - status_code: 404, - api_error: { - type: "connector_not_found", - message: "Connector not found", - }, - }); - } - if (connector.type !== "slack") { - return apiError(req, res, { - status_code: 400, - api_error: { - type: "invalid_request_error", - message: "Connector is not a slack connector", - }, - }); - } - - const botEnabledRes = await GET_BOT_ENABLED_BY_TYPE[connector.type]( - connector.id - ); - - if (botEnabledRes.isErr()) { - return apiError(req, res, { - status_code: 500, - api_error: { - type: "internal_server_error", - message: `An error occurred while getting the bot status: ${botEnabledRes.error}`, - }, - }); - } - return res.status(200).json({ - botEnabled: botEnabledRes.value, - }); -}; - -const _setBotEnabled = async ( - req: Request<{ connector_id: string }, ToggleBotReqBody>, - res: Response -) => { - if (!req.params.connector_id) { - return apiError(req, res, { - status_code: 400, - api_error: { - type: "invalid_request_error", - message: "Missing required parameters. Required: connector_id", - }, - }); - } - const connector = await Connector.findByPk(req.params.connector_id); - if (!connector) { - return apiError(req, res, { - status_code: 404, - api_error: { - type: "connector_not_found", - message: "Connector not found", - }, - }); - } - if (!req.body || typeof req.body.botEnabled !== "boolean") { - return apiError(req, res, { - status_code: 400, - api_error: { - type: "invalid_request_error", - message: "Missing required parameters. Required: botEnabled", - }, - }); - } - - const toggleRes = await TOGGLE_BOT_BY_TYPE[connector.type]( - connector.id, - req.body.botEnabled - ); - - if (toggleRes.isErr()) { - return apiError(req, res, { - status_code: 500, - api_error: { - type: "internal_server_error", - message: `An error occurred while toggling the bot: ${toggleRes.error.message}`, - }, - }); - } - return res.status(200).json({ - botEnabled: req.body.botEnabled, - }); -}; - -export const getBotEnabledAPIHandler = withLogging(_getBotEnabled); -export const setBotEnabledAPIHandler = withLogging(_setBotEnabled); diff --git a/connectors/src/api_server.ts b/connectors/src/api_server.ts index 5dbdff04a2cf..17b7f8675b8f 100644 --- a/connectors/src/api_server.ts +++ b/connectors/src/api_server.ts @@ -1,10 +1,6 @@ import bodyParser from "body-parser"; import express from "express"; -import { - getBotEnabledAPIHandler, - setBotEnabledAPIHandler, -} from "@connectors/api/bot_enabled"; import { createConnectorAPIHandler } from "@connectors/api/create_connector"; import { deleteConnectorAPIHandler } from "@connectors/api/delete_connector"; import { getConnectorAPIHandler } from "@connectors/api/get_connector"; @@ -65,8 +61,6 @@ export function startServer(port: number) { app.post("/connectors/resume/:connector_id", resumeConnectorAPIHandler); app.delete("/connectors/delete/:connector_id", deleteConnectorAPIHandler); app.get("/connectors/:connector_id", getConnectorAPIHandler); - app.get("/connectors/:connector_id/bot_enabled", getBotEnabledAPIHandler); - app.post("/connectors/:connector_id/bot_enabled", setBotEnabledAPIHandler); app.post("/connectors/sync/:connector_id", syncConnectorAPIHandler); app.get( "/connectors/:connector_id/permissions", diff --git a/connectors/src/connectors/index.ts b/connectors/src/connectors/index.ts index acd4f8c13369..14d7248b0fb0 100644 --- a/connectors/src/connectors/index.ts +++ b/connectors/src/connectors/index.ts @@ -1,4 +1,4 @@ -import { ConnectorProvider, ModelId } from "@dust-tt/types"; +import { ConnectorProvider } from "@dust-tt/types"; import { cleanupGithubConnector, @@ -34,8 +34,6 @@ import { updateIntercomConnector, } from "@connectors/connectors/intercom"; import { - BotEnabledGetter, - BotToggler, ConnectorBatchResourceTitleRetriever, ConnectorCleaner, ConnectorConfigGetter, @@ -65,17 +63,15 @@ import { import { cleanupSlackConnector, createSlackConnector, + getSlackConfig, retrieveSlackChannelsTitles, retrieveSlackConnectorPermissions, + setSlackConfig, setSlackConnectorPermissions, updateSlackConnector, } from "@connectors/connectors/slack"; -import { - getBotEnabled, - toggleSlackbot, -} from "@connectors/connectors/slack/bot"; import { launchSlackSyncWorkflow } from "@connectors/connectors/slack/temporal/client"; -import { Err, Ok, Result } from "@connectors/lib/result"; +import { Err, Ok } from "@connectors/lib/result"; import logger from "@connectors/logger/logger"; import { @@ -165,45 +161,6 @@ export const RESUME_CONNECTOR_BY_TYPE: Record< }, }; -const toggleBotNotImplemented = async ( - connectorId: ModelId -): Promise> => { - return new Err( - new Error(`Toggling bot for connector ${connectorId} is not implemented.`) - ); -}; - -export const TOGGLE_BOT_BY_TYPE: Record = { - slack: toggleSlackbot, - notion: toggleBotNotImplemented, - github: toggleBotNotImplemented, - google_drive: toggleBotNotImplemented, - intercom: toggleBotNotImplemented, - webcrawler: toggleBotNotImplemented, -}; - -const getBotEnabledNotImplemented = async ( - connectorId: ModelId -): Promise> => { - return new Err( - new Error( - `Getting botEnabled for connector ${connectorId} is not implemented.` - ) - ); -}; - -export const GET_BOT_ENABLED_BY_TYPE: Record< - ConnectorProvider, - BotEnabledGetter -> = { - slack: getBotEnabled, - notion: getBotEnabledNotImplemented, - github: getBotEnabledNotImplemented, - google_drive: getBotEnabledNotImplemented, - intercom: getBotEnabledNotImplemented, - webcrawler: getBotEnabledNotImplemented, -}; - export const SYNC_CONNECTOR_BY_TYPE: Record = { slack: launchSlackSyncWorkflow, @@ -285,9 +242,7 @@ export const SET_CONNECTOR_CONFIG_BY_TYPE: Record< ConnectorProvider, ConnectorConfigSetter > = { - slack: () => { - throw new Error("Not implemented"); - }, + slack: setSlackConfig, notion: async () => { throw new Error("Not implemented"); }, @@ -307,9 +262,7 @@ export const GET_CONNECTOR_CONFIG_BY_TYPE: Record< ConnectorProvider, ConnectorConfigGetter > = { - slack: () => { - throw new Error("Not implemented"); - }, + slack: getSlackConfig, notion: async () => { throw new Error("Not implemented"); }, diff --git a/connectors/src/connectors/interface.ts b/connectors/src/connectors/interface.ts index 37fdd27e74dc..5e8b40c267d1 100644 --- a/connectors/src/connectors/interface.ts +++ b/connectors/src/connectors/interface.ts @@ -39,15 +39,6 @@ export type ConnectorResumer = ( connectorId: string ) => Promise>; -export type BotToggler = ( - connectorId: ModelId, - botEnabled: boolean -) => Promise>; - -export type BotEnabledGetter = ( - connectorId: ModelId -) => Promise>; - export type SyncConnector = ( connectorId: string, fromTs: number | null diff --git a/connectors/src/connectors/slack/index.ts b/connectors/src/connectors/slack/index.ts index 60e42b0a469c..b21806cb70d7 100644 --- a/connectors/src/connectors/slack/index.ts +++ b/connectors/src/connectors/slack/index.ts @@ -2,8 +2,15 @@ import { ModelId } from "@dust-tt/types"; import { WebClient } from "@slack/web-api"; import PQueue from "p-queue"; -import { ConnectorPermissionRetriever } from "@connectors/connectors/interface"; +import { + ConnectorConfigGetter, + ConnectorPermissionRetriever, +} from "@connectors/connectors/interface"; import { getChannels } from "@connectors/connectors/slack//temporal/activities"; +import { + getBotEnabled, + toggleSlackbot, +} from "@connectors/connectors/slack/bot"; import { joinChannel } from "@connectors/connectors/slack/lib/channels"; import { getSlackAccessToken, @@ -629,3 +636,51 @@ export async function retrieveSlackChannelsTitles( return new Ok(titles); } + +export const getSlackConfig: ConnectorConfigGetter = async function ( + connectorId: ModelId, + configKey: string +) { + const connector = await Connector.findOne({ + where: { id: connectorId }, + }); + if (!connector) { + return new Err(new Error(`Connector not found with id ${connectorId}`)); + } + + switch (configKey) { + case "botEnabled": { + const botEnabledRes = await getBotEnabled(connectorId); + if (botEnabledRes.isErr()) { + return botEnabledRes; + } + return new Ok(botEnabledRes.value.toString()); + } + default: + return new Err(new Error(`Invalid config key ${configKey}`)); + } +}; + +export async function setSlackConfig( + connectorId: ModelId, + configKey: string, + configValue: string +) { + const connector = await Connector.findOne({ + where: { id: connectorId }, + }); + if (!connector) { + return new Err(new Error(`Connector not found with id ${connectorId}`)); + } + + switch (configKey) { + case "botEnabled": { + const res = await toggleSlackbot(connectorId, configValue === "true"); + return res; + } + + default: { + return new Err(new Error(`Invalid config key ${configKey}`)); + } + } +} diff --git a/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts b/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts index ee137b774458..aefc429dece0 100644 --- a/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts +++ b/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts @@ -45,7 +45,7 @@ async function handler( }, }); } - const { botEnabled } = req.body; + const { botEnabled }: { botEnabled: boolean } = req.body; const dataSource = await DataSource.findOne({ where: { @@ -75,9 +75,10 @@ async function handler( } const connectorsAPI = new ConnectorsAPI(logger); - const connectorRes = await connectorsAPI.setBotEnabled( + const connectorRes = await connectorsAPI.setConnectorConfig( dataSource.connectorId, - botEnabled + "botEnabled", + botEnabled ? "true" : "false" ); if (connectorRes.isErr()) { diff --git a/front/pages/api/w/[wId]/data_sources/[name]/managed/bot_enabled.ts b/front/pages/api/w/[wId]/data_sources/[name]/managed/bot_enabled.ts index d6a51365dc68..6e8f8ff12133 100644 --- a/front/pages/api/w/[wId]/data_sources/[name]/managed/bot_enabled.ts +++ b/front/pages/api/w/[wId]/data_sources/[name]/managed/bot_enabled.ts @@ -75,8 +75,9 @@ async function handler( switch (req.method) { case "GET": - const botEnabledRes = await connectorsAPI.getBotEnabled( - dataSource.connectorId + const botEnabledRes = await connectorsAPI.getConnectorConfig( + dataSource.connectorId, + "botEnabled" ); if (botEnabledRes.isErr()) { @@ -89,7 +90,9 @@ async function handler( }); } - res.status(200).json(botEnabledRes.value); + res + .status(200) + .json({ botEnabled: botEnabledRes.value.configValue === "true" }); return; case "POST": @@ -116,9 +119,10 @@ async function handler( }); } - const setBotEnabledRes = await connectorsAPI.setBotEnabled( + const setBotEnabledRes = await connectorsAPI.setConnectorConfig( dataSource.connectorId, - bodyValidation.right.botEnabled + "botEnabled", + bodyValidation.right.botEnabled ? "true" : "false" ); if (setBotEnabledRes.isErr()) { diff --git a/front/pages/poke/[wId]/index.tsx b/front/pages/poke/[wId]/index.tsx index 2a88c12edb66..99aa43c88da5 100644 --- a/front/pages/poke/[wId]/index.tsx +++ b/front/pages/poke/[wId]/index.tsx @@ -133,11 +133,14 @@ export const getServerSideProps: GetServerSideProps<{ let slackbotEnabled = false; if (slackConnectorId) { - const botEnabledRes = await connectorsAPI.getBotEnabled(slackConnectorId); + const botEnabledRes = await connectorsAPI.getConnectorConfig( + slackConnectorId, + "botEnabled" + ); if (botEnabledRes.isErr()) { throw botEnabledRes.error; } - slackbotEnabled = botEnabledRes.value.botEnabled; + slackbotEnabled = botEnabledRes.value.configValue === "true"; } // Get Gdrive PDF enabled status const gdriveConnectorId = dataSources.find( diff --git a/types/src/front/lib/connectors_api.ts b/types/src/front/lib/connectors_api.ts index c435075e2c5b..07d5786d874b 100644 --- a/types/src/front/lib/connectors_api.ts +++ b/types/src/front/lib/connectors_api.ts @@ -243,46 +243,6 @@ export class ConnectorsAPI { return this._resultFromResponse(res); } - async getBotEnabled(connectorId: string): Promise< - ConnectorsAPIResponse<{ - botEnabled: boolean; - }> - > { - const res = await fetch( - `${CONNECTORS_API}/connectors/${connectorId}/bot_enabled`, - { - method: "GET", - headers: this.getDefaultHeaders(), - } - ); - - return this._resultFromResponse(res); - } - - async setBotEnabled( - connectorId: string, - botEnabled: boolean - ): Promise< - ConnectorsAPIResponse<{ - botEnabled: boolean; - }> - > { - const headers = this.getDefaultHeaders(); - - const res = await fetch( - `${CONNECTORS_API}/connectors/${connectorId}/bot_enabled`, - { - method: "POST", - headers, - body: JSON.stringify({ - botEnabled, - }), - } - ); - - return this._resultFromResponse(res); - } - async setConnectorConfig( connectorId: string, configKey: string,