-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assistant builder Slackbot channel based routing (#1999)
* WIP assistant builder Slackbot channel based routing * remove disabled * honor channel-based routing * fix bugs * permissions filter * ui improvment * wrap in tx * fix comment
- Loading branch information
1 parent
a9c8f9f
commit 40847de
Showing
10 changed files
with
809 additions
and
114 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { Request, Response } from "express"; | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as t from "io-ts"; | ||
import * as reporter from "io-ts-reporters"; | ||
import { Op } from "sequelize"; | ||
|
||
import { APIErrorWithStatusCode } from "@connectors/lib/error"; | ||
import { sequelize_conn, SlackChannel } from "@connectors/lib/models"; | ||
import { apiError, withLogging } from "@connectors/logger/withlogging"; | ||
|
||
const PatchSlackChannelsLinkedWithAgentReqBodySchema = t.type({ | ||
agent_configuration_id: t.string, | ||
slack_channel_ids: t.array(t.string), | ||
connector_id: t.string, | ||
}); | ||
|
||
type PatchSlackChannelsLinkedWithAgentReqBody = t.TypeOf< | ||
typeof PatchSlackChannelsLinkedWithAgentReqBodySchema | ||
>; | ||
|
||
type PatchSlackChannelsLinkedWithAgentResBody = | ||
| { success: true } | ||
| APIErrorWithStatusCode; | ||
|
||
const _patchSlackChannelsLinkedWithAgentHandler = async ( | ||
req: Request< | ||
Record<string, string>, | ||
PatchSlackChannelsLinkedWithAgentResBody, | ||
PatchSlackChannelsLinkedWithAgentReqBody | ||
>, | ||
res: Response<PatchSlackChannelsLinkedWithAgentResBody> | ||
) => { | ||
const bodyValidation = PatchSlackChannelsLinkedWithAgentReqBodySchema.decode( | ||
req.body | ||
); | ||
|
||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
status_code: 400, | ||
}); | ||
} | ||
|
||
const { | ||
connector_id: connectorId, | ||
agent_configuration_id: agentConfigurationId, | ||
slack_channel_ids: slackChannelIds, | ||
} = bodyValidation.right; | ||
|
||
const slackChannels = await SlackChannel.findAll({ | ||
where: { | ||
slackChannelId: slackChannelIds, | ||
connectorId, | ||
}, | ||
}); | ||
|
||
const foundSlackChannelIds = new Set( | ||
slackChannels.map((c) => c.slackChannelId) | ||
); | ||
|
||
const missingSlackChannelIds = Array.from( | ||
new Set(slackChannelIds.filter((id) => !foundSlackChannelIds.has(id))) | ||
); | ||
|
||
if (missingSlackChannelIds.length) { | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "not_found", | ||
message: `Slack channel(s) not found: ${missingSlackChannelIds.join( | ||
", " | ||
)}`, | ||
}, | ||
status_code: 404, | ||
}); | ||
} | ||
|
||
await sequelize_conn.transaction(async (t) => { | ||
await SlackChannel.update( | ||
{ agentConfigurationId: null }, | ||
{ | ||
where: { | ||
connectorId, | ||
agentConfigurationId, | ||
}, | ||
transaction: t, | ||
} | ||
); | ||
await Promise.all( | ||
slackChannels.map((slackChannel) => | ||
slackChannel.update({ agentConfigurationId }, { transaction: t }) | ||
) | ||
); | ||
}); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
}); | ||
}; | ||
|
||
export const patchSlackChannelsLinkedWithAgentHandler = withLogging( | ||
_patchSlackChannelsLinkedWithAgentHandler | ||
); | ||
|
||
type GetSlackChannelsLinkedWithAgentResBody = | ||
| { | ||
slackChannels: { | ||
slackChannelId: string; | ||
slackChannelName: string; | ||
agentConfigurationId: string; | ||
}[]; | ||
} | ||
| APIErrorWithStatusCode; | ||
|
||
const _getSlackChannelsLinkedWithAgentHandler = async ( | ||
req: Request< | ||
Record<string, string>, | ||
{ slackChannelIds: string[] } | APIErrorWithStatusCode, | ||
undefined | ||
>, | ||
res: Response<GetSlackChannelsLinkedWithAgentResBody> | ||
) => { | ||
const { connector_id: connectorId } = req.query; | ||
|
||
if (!connectorId || typeof connectorId !== "string") { | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Missing required parameters: connector_id`, | ||
}, | ||
status_code: 400, | ||
}); | ||
} | ||
|
||
const slackChannels = await SlackChannel.findAll({ | ||
where: { | ||
connectorId, | ||
agentConfigurationId: { | ||
[Op.not]: null, | ||
}, | ||
}, | ||
}); | ||
|
||
res.status(200).json({ | ||
slackChannels: slackChannels.map((c) => ({ | ||
slackChannelId: c.slackChannelId, | ||
slackChannelName: c.slackChannelName, | ||
// We know that agentConfigurationId is not null because of the where clause above | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
agentConfigurationId: c.agentConfigurationId!, | ||
})), | ||
}); | ||
}; | ||
|
||
export const getSlackChannelsLinkedWithAgentHandler = withLogging( | ||
_getSlackChannelsLinkedWithAgentHandler | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.