Skip to content

Commit

Permalink
Added threadId to Feedback (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayaankvad committed May 18, 2024
1 parent 6295895 commit aa1f722
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
19 changes: 11 additions & 8 deletions backend/chainlit/discord/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import re
import uuid
from io import BytesIO
from typing import Dict, List, Optional, Union, TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, List, Optional, Union

if TYPE_CHECKING:
from discord.abc import MessageableChannel

import discord

import filetype
import httpx
from chainlit.config import config
Expand All @@ -19,11 +18,10 @@
from chainlit.emitter import BaseChainlitEmitter
from chainlit.logger import logger
from chainlit.message import Message, StepDict
from chainlit.telemetry import trace
from chainlit.types import Feedback
from chainlit.user import PersistedUser, User
from chainlit.user_session import user_session
from chainlit.telemetry import trace

from discord.ui import Button, View


Expand All @@ -36,7 +34,9 @@ def __init__(self, step_id: str):
async def thumbs_down(self, interaction: discord.Interaction, button: Button):
if data_layer := get_data_layer():
try:
await data_layer.upsert_feedback(Feedback(forId=self.step_id, value=0))
thread_id = context_var.get().session.thread_id
feedback = Feedback(forId=self.step_id, threadId=thread_id, value=0)
await data_layer.upsert_feedback(feedback)
except Exception as e:
logger.error(f"Error upserting feedback: {e}")
if interaction.message:
Expand All @@ -47,7 +47,9 @@ async def thumbs_down(self, interaction: discord.Interaction, button: Button):
async def thumbs_up(self, interaction: discord.Interaction, button: Button):
if data_layer := get_data_layer():
try:
await data_layer.upsert_feedback(Feedback(forId=self.step_id, value=1))
thread_id = context_var.get().session.thread_id
feedback = Feedback(forId=self.step_id, threadId=thread_id, value=1)
await data_layer.upsert_feedback(feedback)
except Exception as e:
logger.error(f"Error upserting feedback: {e}")
if interaction.message:
Expand Down Expand Up @@ -123,6 +125,7 @@ async def update_step(self, step_dict: StepDict):

client = discord.Client(intents=intents)


@trace
def init_discord_context(
session: HTTPSession,
Expand All @@ -145,7 +148,7 @@ def init_discord_context(
async def get_user(discord_user: Union[discord.User, discord.Member]):
if discord_user.id in users_by_discord_id:
return users_by_discord_id[discord_user.id]

metadata = {
"name": discord_user.name,
"id": discord_user.id,
Expand All @@ -161,7 +164,7 @@ async def get_user(discord_user: Union[discord.User, discord.Member]):
users_by_discord_id[discord_user.id] = persisted_user
except Exception as e:
logger.error(f"Error creating user: {e}")

return users_by_discord_id[discord_user.id]


Expand Down
17 changes: 11 additions & 6 deletions backend/chainlit/slack/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from chainlit.data import get_data_layer
from chainlit.element import Element, ElementDict
from chainlit.emitter import BaseChainlitEmitter
from chainlit.logger import logger
from chainlit.message import Message, StepDict
from chainlit.telemetry import trace
from chainlit.types import Feedback
from chainlit.user import PersistedUser, User
from chainlit.user_session import user_session
from chainlit.logger import logger
from chainlit.telemetry import trace
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
from slack_bolt.async_app import AsyncApp

Expand Down Expand Up @@ -124,6 +124,7 @@ async def update_step(self, step_dict: StepDict):
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
)


@trace
def init_slack_context(
session: HTTPSession,
Expand Down Expand Up @@ -166,7 +167,7 @@ def clean_content(message: str):
async def get_user(slack_user_id: str):
if slack_user_id in users_by_slack_id:
return users_by_slack_id[slack_user_id]

slack_user = await slack_app.client.users_info(user=slack_user_id)
slack_user_profile = slack_user["user"]["profile"]

Expand Down Expand Up @@ -303,7 +304,7 @@ async def process_slack_message(
thread_id=thread_id,
name=thread_name or msg.content,
metadata=ctx.session.to_persistable(),
user_id=user_id
user_id=user_id,
)
except Exception as e:
logger.error(f"Error updating thread: {e}")
Expand Down Expand Up @@ -335,7 +336,9 @@ async def thumb_down(ack, context, body):
step_id = body["actions"][0]["value"]

if data_layer := get_data_layer():
await data_layer.upsert_feedback(Feedback(forId=step_id, value=0))
thread_id = context_var.get().session.thread_id
feedback = Feedback(forId=step_id, threadId=thread_id, value=0)
await data_layer.upsert_feedback(feedback)

text = body["message"]["text"]
blocks = body["message"]["blocks"]
Expand All @@ -360,7 +363,9 @@ async def thumb_up(ack, context, body):
step_id = body["actions"][0]["value"]

if data_layer := get_data_layer():
await data_layer.upsert_feedback(Feedback(forId=step_id, value=1))
thread_id = context_var.get().session.thread_id
feedback = Feedback(forId=step_id, threadId=thread_id, value=1)
await data_layer.upsert_feedback(feedback)

text = body["message"]["text"]
blocks = body["message"]["blocks"]
Expand Down
1 change: 1 addition & 0 deletions backend/chainlit/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class FeedbackDict(TypedDict):
@dataclass
class Feedback:
forId: str
threadId: Optional[str]
value: Literal[0, 1]
id: Optional[str] = None
comment: Optional[str] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const FeedbackButtons = ({ message }: Props) => {
{
...(message.feedback || {}),
forId: message.id,
threadId: message.threadId,
value: feedback,
comment
}
Expand Down
1 change: 1 addition & 0 deletions libs/react-client/src/types/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface IFeedback {
id?: string;
forId?: string;
threadId?: string;
comment?: string;
value: number;
}

0 comments on commit aa1f722

Please sign in to comment.