Skip to content

Commit

Permalink
general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Feb 5, 2025
1 parent d3ffbbd commit b893a66
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion backend/onyx/db/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SLACK_BOT_PERSONA_PREFIX = "__slack_bot_persona__"
DEFAULT_PERSONA_SLACKCHANNEL_NAME = "DEFAULT"
DEFAULT_PERSONA_SLACK_CHANNEL_NAME = "DEFAULT_SLACK_CHANNEL"
4 changes: 2 additions & 2 deletions backend/onyx/db/slack_channel_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from onyx.configs.chat_configs import MAX_CHUNKS_FED_TO_CHAT
from onyx.context.search.enums import RecencyBiasSetting
from onyx.db.constants import DEFAULT_PERSONA_SLACKCHANNEL_NAME
from onyx.db.constants import DEFAULT_PERSONA_SLACK_CHANNEL_NAME
from onyx.db.constants import SLACK_BOT_PERSONA_PREFIX
from onyx.db.models import ChannelConfig
from onyx.db.models import Persona
Expand All @@ -24,7 +24,7 @@


def _build_persona_name(channel_name: str | None) -> str:
return f"{SLACK_BOT_PERSONA_PREFIX}{channel_name if channel_name else DEFAULT_PERSONA_SLACKCHANNEL_NAME}"
return f"{SLACK_BOT_PERSONA_PREFIX}{channel_name if channel_name else DEFAULT_PERSONA_SLACK_CHANNEL_NAME}"


def _cleanup_relationships(db_session: Session, persona_id: int) -> None:
Expand Down
4 changes: 0 additions & 4 deletions backend/onyx/server/manage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,3 @@ class AllUsersResponse(BaseModel):
class SlackChannel(BaseModel):
id: str
name: str


class SlackChannelsResponse(BaseModel):
channels: dict[str, SlackChannel]
13 changes: 5 additions & 8 deletions backend/onyx/server/manage/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from onyx.server.manage.models import SlackChannel
from onyx.server.manage.models import SlackChannelConfig
from onyx.server.manage.models import SlackChannelConfigCreationRequest
from onyx.server.manage.models import SlackChannelsResponse
from onyx.server.manage.validate_tokens import validate_app_token
from onyx.server.manage.validate_tokens import validate_bot_token
from onyx.utils.telemetry import create_milestone_and_report
Expand Down Expand Up @@ -340,13 +339,13 @@ def list_bot_configs(


@router.get(
"/admin/slack-app/bots/{bot_id}/channels_from_slack_api",
"/admin/slack-app/bots/{bot_id}/channels",
)
def get_all_channels_from_slack_api(
bot_id: int,
db_session: Session = Depends(get_session),
_: User | None = Depends(current_admin_user),
) -> SlackChannelsResponse:
) -> list[SlackChannel]:
tokens = fetch_slack_bot_tokens(db_session, bot_id)
if not tokens or "bot_token" not in tokens:
raise HTTPException(
Expand All @@ -357,7 +356,7 @@ def get_all_channels_from_slack_api(
client = WebClient(token=bot_token)

try:
channels = {}
channels = []
cursor = None
while True:
response = client.conversations_list(
Expand All @@ -367,15 +366,13 @@ def get_all_channels_from_slack_api(
cursor=cursor,
)
for channel in response["channels"]:
channels[channel["name"]] = SlackChannel(
id=channel["id"], name=channel["name"]
)
channels.append(SlackChannel(id=channel["id"], name=channel["name"]))

cursor = response.get("response_metadata", {}).get("next_cursor")
if not cursor:
break

return SlackChannelsResponse(channels=channels)
return channels
except SlackApiError as e:
raise HTTPException(
status_code=500, detail=f"Error fetching channels from Slack API: {str(e)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ export function SlackChannelConfigFormFields({
}, [documentSets]);

const { data: channelOptions, isLoading } = useSWR(
`/api/manage/admin/slack-app/bots/${slack_bot_id}/channels_from_slack_api`,
`/api/manage/admin/slack-app/bots/${slack_bot_id}/channels`,
async (url: string) => {
const channels = await fetchSlackChannels(slack_bot_id);
return Object.entries(channels).map(([name, id]) => ({
name,
value: id as string,
return channels.map((channel: any) => ({
name: channel.name,
value: channel.id,
}));
}
);
Expand Down
15 changes: 6 additions & 9 deletions web/src/app/admin/bots/[bot-id]/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,12 @@ export function isPersonaASlackBotPersona(persona: Persona) {
}

export const fetchSlackChannels = async (botId: number) => {
return fetch(
`/api/manage/admin/slack-app/bots/${botId}/channels_from_slack_api`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
}
).then((response) => {
return fetch(`/api/manage/admin/slack-app/bots/${botId}/channels`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch Slack channels");
}
Expand Down
5 changes: 5 additions & 0 deletions web/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ export interface SlackChannelConfig {
is_default: boolean;
}

export interface SlackChannelDescriptor {
id: string;
name: string;
}

export interface SlackBot {
id: number;
name: string;
Expand Down

0 comments on commit b893a66

Please sign in to comment.