Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix: implement a recursive version of getChannels (#8470)

Łukasz Jernaś35c8adaccc86
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.ts
index e8d7eebb27..ab84aab4a2 100644
--- a/packages/shared/src/env.ts
+++ b/packages/shared/src/env.ts
@@ -199,6 +199,14 @@ const EnvSchema = z.object({
SLACK_CLIENT_ID: z.string().optional(),
SLACK_CLIENT_SECRET: z.string().optional(),
SLACK_STATE_SECRET: z.string().optional(),
+ SLACK_FETCH_LIMIT: z
+ .number()
+ .positive()
+ .optional()
+ .default(1_000)
+ .describe(
+ "How many records should be fetched from Slack, before we give up",
+ ),
HTTPS_PROXY: z.string().optional(),
LANGFUSE_SERVER_SIDE_IO_CHAR_LIMIT: z.coerce
diff --git a/packages/shared/src/server/services/SlackService.ts b/packages/shared/src/server/services/SlackService.ts
index a45f1f125a..2b8165e93c 100644
--- a/packages/shared/src/server/services/SlackService.ts
+++ b/packages/shared/src/server/services/SlackService.ts
@@ -88,7 +88,7 @@ export function parseSlackInstallationMetadata(
*/
export class SlackService {
private static instance: SlackService | null = null;
- private installer: InstallProvider;
+ private readonly installer: InstallProvider;
private constructor() {
this.installer = new InstallProvider({
@@ -296,14 +296,20 @@ export class SlackService {
}
/**
- * Get channels accessible to the bot
+ * Recursively fetch all channels accessible to the bot
+ * Uses cursor-based pagination defined by Slack API https://api.slack.com/apis/pagination
*/
- async getChannels(client: WebClient): Promise<SlackChannel[]> {
+ private async getChannelsRecursive(
+ client: WebClient,
+ cursor?: string,
+ fetchedRecords: number = 0,
+ ): Promise<SlackChannel[]> {
try {
const result = await client.conversations.list({
exclude_archived: true,
types: "public_channel",
limit: 200,
+ cursor: cursor,
});
if (!result.ok) {
@@ -319,6 +325,41 @@ export class SlackService {
}),
);
+ const nextCursor = result.response_metadata?.next_cursor;
+ if (
+ nextCursor &&
+ fetchedRecords + channels.length < env.SLACK_FETCH_LIMIT
+ ) {
+ try {
+ const nextPageChannels = await this.getChannelsRecursive(
+ client,
+ nextCursor,
+ fetchedRecords + channels.length,
+ );
+ return [...channels, ...nextPageChannels];
+ } catch (error) {
+ logger.error(
+ `Failed to retrieve next page of channels, returning only already fetched`,
+ error,
+ );
+ }
+ }
+ return channels;
+ } catch (error) {
+ logger.error("Failed to fetch channels recursively", { error, cursor });
+ throw new Error(
+ `Failed to fetch channels: ${error instanceof Error ? error.message : "Unknown error"}`,
+ );
+ }
+ }
+
+ /**
+ * Get channels accessible to the bot
+ */
+ async getChannels(client: WebClient): Promise<SlackChannel[]> {
+ try {
+ const channels = await this.getChannelsRecursive(client);
+
logger.debug("Retrieved channels from Slack", {
channelCount: channels.length,
});