diff --git a/connectors/src/api/admin.ts b/connectors/src/api/admin.ts index 54d0f65ab8d3..b0e3895efa09 100644 --- a/connectors/src/api/admin.ts +++ b/connectors/src/api/admin.ts @@ -21,6 +21,10 @@ const whitelistedCommands = [ command: "find-url", }, { majorCommand: "slack", command: "whitelist-bot" }, + { + majorCommand: "connectors", + command: "set-error", + }, ]; const _adminAPIHandler = async ( diff --git a/front/lib/api/poke/plugins/data_sources/index.ts b/front/lib/api/poke/plugins/data_sources/index.ts index e6a311fd97f9..65a8bd66cde5 100644 --- a/front/lib/api/poke/plugins/data_sources/index.ts +++ b/front/lib/api/poke/plugins/data_sources/index.ts @@ -1,2 +1,3 @@ export * from "./garbage_collect_google_drive_document"; +export * from "./mark_connector_as_error"; export * from "./operations"; diff --git a/front/lib/api/poke/plugins/data_sources/mark_connector_as_error.ts b/front/lib/api/poke/plugins/data_sources/mark_connector_as_error.ts new file mode 100644 index 000000000000..8dc08e54a16c --- /dev/null +++ b/front/lib/api/poke/plugins/data_sources/mark_connector_as_error.ts @@ -0,0 +1,78 @@ +import type { AdminCommandType } from "@dust-tt/types"; +import { CONNECTORS_ERROR_TYPES, ConnectorsAPI, Err, Ok } from "@dust-tt/types"; + +import config from "@app/lib/api/config"; +import { createPlugin } from "@app/lib/api/poke/types"; +import { isManaged, isWebsite } from "@app/lib/data_sources"; +import { DataSourceResource } from "@app/lib/resources/data_source_resource"; +import logger from "@app/logger/logger"; + +export const markConnectorAsErrorPlugin = createPlugin( + { + id: "mark-connector-as-error", + name: "Mark connector as error", + description: "Mark a connector as errored with a specific error type", + resourceTypes: ["data_sources"], + args: { + errorType: { + type: "enum", + label: "Error Type", + description: "Select error type to set", + values: CONNECTORS_ERROR_TYPES, + }, + }, + }, + async (auth, dataSourceId, args) => { + if (!dataSourceId) { + return new Err(new Error("Data source not found.")); + } + + const dataSource = await DataSourceResource.fetchById(auth, dataSourceId); + if (!dataSource) { + return new Err(new Error("Data source not found.")); + } + + if (!isManaged(dataSource) && !isWebsite(dataSource)) { + return new Err(new Error("Data source is not managed or website.")); + } + + const { connectorId } = dataSource; + if (!connectorId) { + return new Err(new Error("No connector on datasource.")); + } + + const { errorType } = args; + const connectorsAPI = new ConnectorsAPI( + config.getConnectorsAPIConfig(), + logger + ); + + // First set the error. + const setErrorCommand: AdminCommandType = { + majorCommand: "connectors", + command: "set-error", + args: { + connectorId: connectorId.toString(), + error: errorType, + wId: auth.getNonNullableWorkspace().sId, + dsId: dataSource.sId, + }, + }; + + const setErrorRes = await connectorsAPI.admin(setErrorCommand); + if (setErrorRes.isErr()) { + return new Err(new Error(setErrorRes.error.message)); + } + + // Then pause it. + const pauseRes = await connectorsAPI.pauseConnector(connectorId.toString()); + if (pauseRes.isErr()) { + return new Err(new Error(pauseRes.error.message)); + } + + return new Ok({ + display: "text", + value: `Connector ${connectorId} marked as ${errorType} and paused.`, + }); + } +); diff --git a/front/lib/api/poke/plugins/data_sources/operations.ts b/front/lib/api/poke/plugins/data_sources/operations.ts index c2f67c2137c5..6a12ea6ad762 100644 --- a/front/lib/api/poke/plugins/data_sources/operations.ts +++ b/front/lib/api/poke/plugins/data_sources/operations.ts @@ -52,13 +52,11 @@ export const connectorOperationsPlugin = createPlugin( } const dataSource = await DataSourceResource.fetchById(auth, dataSourceId); - if (!dataSource) { return new Err(new Error("Data source not found.")); } const { connectorId } = dataSource; - if (!connectorId) { return new Err(new Error("No connector on datasource.")); } diff --git a/types/src/front/lib/connectors_api.ts b/types/src/front/lib/connectors_api.ts index 17167a9178c9..35af0abc3a4a 100644 --- a/types/src/front/lib/connectors_api.ts +++ b/types/src/front/lib/connectors_api.ts @@ -14,7 +14,7 @@ import { Err, Ok, Result } from "../../shared/result"; export type ConnectorsAPIResponse = Result; export type ConnectorSyncStatus = "succeeded" | "failed"; -const CONNECTORS_ERROR_TYPES = [ +export const CONNECTORS_ERROR_TYPES = [ "oauth_token_revoked", "third_party_internal_error", "webcrawling_error",