Skip to content

Commit

Permalink
Connectors batch get (#6078)
Browse files Browse the repository at this point in the history
* connectors: batch get API

* connectors API

* public-urls batch get

* clean-up:

* lint
  • Loading branch information
spolu authored Jul 5, 2024
1 parent 20e43c9 commit 8b9de26
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 19 deletions.
44 changes: 44 additions & 0 deletions connectors/src/api/get_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
ConnectorType,
WithConnectorsAPIErrorReponse,
} from "@dust-tt/types";
import { isConnectorProvider } from "@dust-tt/types";
import type { Request, Response } from "express";

import { GithubDiscussion, GithubIssue } from "@connectors/lib/models/github";
Expand Down Expand Up @@ -72,3 +73,46 @@ const _getConnector = async (
};

export const getConnectorAPIHandler = withLogging(_getConnector);

type GetConnectorsResponseBody = WithConnectorsAPIErrorReponse<ConnectorType[]>;

const _getConnectors = async (
req: Request<Record<string, string>, GetConnectorsResponseBody, undefined>,
res: Response<GetConnectorsResponseBody>
) => {
if (
typeof req.query.provider !== "string" ||
!isConnectorProvider(req.query.provider)
) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "unknown_connector_provider",
message: `Unknown connector provider ${req.params.provider}`,
},
});
}

if (typeof req.query.connector_id === "string") {
req.query.connector_id = [req.query.connector_id];
}

if (!Array.isArray(req.query.connector_id)) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: `Expecting connector_id to be passed as query parameters`,
},
});
}

const connectors = await ConnectorResource.fetchByIds(
req.query.provider,
req.query.connector_id as string[]
);

return res.status(200).json(connectors.map((c) => c.toJSON()));
};

export const getConnectorsAPIHandler = withLogging(_getConnectors);
6 changes: 5 additions & 1 deletion connectors/src/api_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { adminAPIHandler } from "@connectors/api/admin";
import { patchConnectorConfigurationAPIHandler } from "@connectors/api/configuration";
import { createConnectorAPIHandler } from "@connectors/api/create_connector";
import { deleteConnectorAPIHandler } from "@connectors/api/delete_connector";
import { getConnectorAPIHandler } from "@connectors/api/get_connector";
import {
getConnectorAPIHandler,
getConnectorsAPIHandler,
} from "@connectors/api/get_connector";
import { getConnectorPermissionsAPIHandler } from "@connectors/api/get_connector_permissions";
import { getContentNodesParentsAPIHandler } from "@connectors/api/get_content_node_parents";
import { getContentNodesAPIHandler } from "@connectors/api/get_content_nodes";
Expand Down Expand Up @@ -95,6 +98,7 @@ 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", getConnectorsAPIHandler);
app.post("/connectors/sync/:connector_id", syncConnectorAPIHandler);
app.get(
"/connectors/:connector_id/permissions",
Expand Down
8 changes: 6 additions & 2 deletions connectors/src/resources/connector_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ export class ConnectorResource extends BaseResource<ConnectorModel> {
return c;
}

static async fetchByIds(type: ConnectorProvider, ids: ModelId[]) {
static async fetchByIds(type: ConnectorProvider, ids: (ModelId | string)[]) {
const parsedIds = ids.map((id) =>
typeof id === "string" ? parseInt(id, 10) : id
);

const blobs = await ConnectorResource.model.findAll({
where: {
type,
id: ids,
id: parsedIds,
},
});

Expand Down
44 changes: 28 additions & 16 deletions front/pages/w/[wId]/builder/data-sources/public-urls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,36 @@ export const getServerSideProps = withDefaultUserAuthRequirements<{
providerFilter: "webcrawler",
});

const connectorIds = allDataSources
.filter(
(ds) => ds.connectorProvider === "webcrawler" && ds.connectorId !== null
)
.map((ds) => ds.connectorId) as string[];

const connectorsAPI = new ConnectorsAPI(logger);
const dataSources = await Promise.all(
allDataSources
.filter((ds) => ds.connectorProvider === "webcrawler")
.map(async (ds): Promise<DataSourceWithConnector> => {
if (!ds.connectorId) {
throw new Error("Connector ID is missing");
}
const connectorRes = await connectorsAPI.getConnector(ds.connectorId);
if (connectorRes.isErr()) {
throw new Error("Connector not found");
}
return {
...ds,
connector: connectorRes.value,
};
})

const connectorsRes = await connectorsAPI.getConnectors(
"webcrawler",
connectorIds
);
if (connectorsRes.isErr()) {
throw new Error("Failed to fetch connectors");
}

const dataSources = allDataSources
.filter((ds) => ds.connectorProvider === "webcrawler")
.map((ds) => {
const connector = connectorsRes.value.find(
(c) => c.id === ds.connectorId
);
if (!connector) {
throw new Error("Connector not found");
}
return {
...ds,
connector,
};
});

return {
props: {
Expand Down
22 changes: 22 additions & 0 deletions types/src/front/lib/connectors_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,28 @@ export class ConnectorsAPI {
return this._resultFromResponse(res);
}

async getConnectors(
provider: ConnectorProvider,
connectorIds: string[]
): Promise<ConnectorsAPIResponse<ConnectorType[]>> {
if (connectorIds.length === 0) {
return new Ok([]);
}
const res = await this._fetchWithError(
`${CONNECTORS_API}/connectors?provider=${encodeURIComponent(
provider
)}&${connectorIds
.map((id) => `connector_id=${encodeURIComponent(id)}`)
.join("&")}`,
{
method: "GET",
headers: this.getDefaultHeaders(),
}
);

return this._resultFromResponse(res);
}

async setConnectorConfig(
connectorId: string,
configKey: string,
Expand Down

0 comments on commit 8b9de26

Please sign in to comment.