Skip to content

Commit

Permalink
Feat/discord (Chainlit#986)
Browse files Browse the repository at this point in the history
* add slack platform
* add discord platform
  • Loading branch information
willydouhard authored May 13, 2024
1 parent 2cd9a0f commit d47045f
Show file tree
Hide file tree
Showing 27 changed files with 887 additions and 103 deletions.
10 changes: 3 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

Nothing unreleased!

## [1.1.0rc1] - 2024-05-06

### Changed

- bumped literalai package version to 0.0.600

## [1.1.0rc0] - 2024-05-06
## [1.1.0] - 2024-05-13

### Added

- You can know serve your Chainlit app through Slack
- You can know serve your Chainlit app through Discord
- `cl.on_audio_chunk` decorator to process incoming the user incoming audio stream
- `cl.on_audio_end` decorator to react to the end of the user audio stream
- The `cl.Audio` element now has an `auto_play` property
Expand Down
6 changes: 4 additions & 2 deletions backend/chainlit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from importlib import util
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, Literal

import tomli
from chainlit.logger import logger
Expand Down Expand Up @@ -132,9 +132,10 @@
# Be careful: If this is a relative path, it should not start with a slash.
# custom_build = "./public/build"
# Override default MUI light theme. (Check theme.ts)
[UI.theme]
#layout = "wide"
#font_family = "Inter, sans-serif"
# Override default MUI light theme. (Check theme.ts)
[UI.theme.light]
#background = "#FAFAFA"
#paper = "#FFFFFF"
Expand Down Expand Up @@ -194,6 +195,7 @@ class Palette(DataClassJsonMixin):
@dataclass()
class Theme(DataClassJsonMixin):
font_family: Optional[str] = None
layout: Optional[Literal["default", "wide"]] = "default"
light: Optional[Palette] = None
dark: Optional[Palette] = None

Expand Down
26 changes: 19 additions & 7 deletions backend/chainlit/context.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import asyncio
import uuid
from contextvars import ContextVar
from typing import TYPE_CHECKING, Dict, Optional, Union, List
from typing import TYPE_CHECKING, Dict, List, Optional, Union

from chainlit.session import HTTPSession, WebsocketSession
from chainlit.session import ClientType, HTTPSession, WebsocketSession
from lazify import LazyProxy

if TYPE_CHECKING:
from chainlit.emitter import BaseChainlitEmitter
from chainlit.user import PersistedUser, User
from chainlit.step import Step
from chainlit.user import PersistedUser, User


class ChainlitContextException(Exception):
Expand All @@ -28,13 +28,20 @@ def current_step(self):
if self.active_steps:
return self.active_steps[-1]

def __init__(self, session: Union["HTTPSession", "WebsocketSession"]):
def __init__(
self,
session: Union["HTTPSession", "WebsocketSession"],
emitter: Optional["BaseChainlitEmitter"] = None,
):
from chainlit.emitter import BaseChainlitEmitter, ChainlitEmitter

self.loop = asyncio.get_running_loop()
self.session = session
self.active_steps = []
if isinstance(self.session, HTTPSession):

if emitter:
self.emitter = emitter
elif isinstance(self.session, HTTPSession):
self.emitter = BaseChainlitEmitter(self.session)
elif isinstance(self.session, WebsocketSession):
self.emitter = ChainlitEmitter(self.session)
Expand All @@ -56,15 +63,20 @@ def init_ws_context(session_or_sid: Union[WebsocketSession, str]) -> ChainlitCon


def init_http_context(
thread_id: Optional[str] = None,
user: Optional[Union["User", "PersistedUser"]] = None,
auth_token: Optional[str] = None,
user_env: Optional[Dict[str, str]] = None,
client_type: ClientType = "webapp",
) -> ChainlitContext:
session_id = str(uuid.uuid4())
thread_id = thread_id or str(uuid.uuid4())
session = HTTPSession(
id=str(uuid.uuid4()),
id=session_id,
thread_id=thread_id,
token=auth_token,
user=user,
client_type="app",
client_type=client_type,
user_env=user_env,
)
context = ChainlitContext(session)
Expand Down
5 changes: 4 additions & 1 deletion backend/chainlit/data/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
async def is_thread_author(username: str, thread_id: str):
data_layer = get_data_layer()
if not data_layer:
raise HTTPException(status_code=401, detail="Unauthorized")
raise HTTPException(status_code=400, detail="Data layer not initialized")

thread_author = await data_layer.get_thread_author(thread_id)

if not thread_author:
raise HTTPException(status_code=404, detail="Thread not found")

if thread_author != username:
raise HTTPException(status_code=401, detail="Unauthorized")
Expand Down
6 changes: 6 additions & 0 deletions backend/chainlit/discord/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try:
import discord
except ModuleNotFoundError:
raise ValueError(
"The discord package is required to integrate Chainlit with a Slack app. Run `pip install discord --upgrade`"
)
Loading

0 comments on commit d47045f

Please sign in to comment.