Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
Signed-off-by: San Nguyen <vinhsannguyen91@gmail.com>
  • Loading branch information
sandangel committed Jan 20, 2025
1 parent 32d7fd8 commit 80bd98a
Show file tree
Hide file tree
Showing 77 changed files with 1,133 additions and 1,147 deletions.
1 change: 1 addition & 0 deletions backend/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout poetry
18 changes: 18 additions & 0 deletions backend/chainlit/auth/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
_cookie_secure = _cookie_samesite == "none"

_state_cookie_lifetime = 3 * 60 # 3m
# TODO: Prefix with __Host- to use in conjunction with the partitioned cookie as described here
# [CHIPS](https://github.com/privacycg/CHIPS/blob/main/README.md)
_auth_cookie_name = "access_token"
_state_cookie_name = "oauth_state"

Expand Down Expand Up @@ -89,6 +91,14 @@ def set_auth_cookie(response: Response, token: str):
samesite=_cookie_samesite,
max_age=config.project.user_session_timeout,
)
if _cookie_secure:
# Set partitioned cookie to avoid cookie rejection errors
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#partitioned
# TODO: Use FastAPI native methods once it supports partitioned cookie
# https://github.com/fastapi/fastapi/discussions/11285
response.headers["Set-Cookie"] = (
response.headers["Set-Cookie"] + "; Partitioned"
)


def clear_auth_cookie(response: Response):
Expand All @@ -107,6 +117,14 @@ def set_oauth_state_cookie(response: Response, token: str):
secure=_cookie_secure,
max_age=_state_cookie_lifetime,
)
if _cookie_secure:
# Set partitioned cookie to avoid cookie rejection errors
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#partitioned
# TODO: Use FastAPI native methods once it supports partitioned cookie
# https://github.com/fastapi/fastapi/discussions/11285
response.headers["Set-Cookie"] = (
response.headers["Set-Cookie"] + "; Partitioned"
)


def validate_oauth_state_cookie(request: Request, state: str):
Expand Down
1 change: 1 addition & 0 deletions backend/chainlit/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def chainlit_run(
os.environ["OPENAI_API_KEY"] = "sk-FAKE-OPENAI-API-KEY"
# This is required for authentication tests
os.environ["CHAINLIT_AUTH_SECRET"] = "SUPER_SECRET" # nosec B105
os.environ["CHAINLIT_COOKIE_SAMESITE"] = "none"
else:
trace_event("chainlit run")

Expand Down
2 changes: 1 addition & 1 deletion backend/chainlit/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Element:
for_id: Optional[str] = None
# The language, if relevant
language: Optional[str] = None
# Mime type, infered based on content if not provided
# Mime type, inferred based on content if not provided
mime: Optional[str] = None

def __post_init__(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion backend/chainlit/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing_extensions import TypeAlias

from chainlit.auth import get_current_user, require_login
from chainlit.auth.cookie import _auth_cookie_name
from chainlit.chat_context import chat_context
from chainlit.config import config
from chainlit.context import init_ws_context
Expand Down Expand Up @@ -83,7 +84,7 @@ def load_user_env(user_env):
def _get_token_from_cookie(environ: WSGIEnvironment) -> Optional[str]:
if cookie_header := environ.get("HTTP_COOKIE", None):
cookies = cookie_parser(cookie_header)
return cookies.get("access_token", None)
return cookies.get(_auth_cookie_name, None)

return None

Expand Down
420 changes: 218 additions & 202 deletions backend/poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ literalai = "0.1.103"
dataclasses_json = "^0.6.7"
fastapi = ">=0.115.3,<0.116"
starlette = "^0.41.2"
uvicorn = "^0.25.0"
uvicorn = ">=0.25.0"
python-socketio = "^5.11.0"
aiofiles = "^23.1.0"
syncer = "^2.0.3"
Expand All @@ -59,7 +59,7 @@ uptrace = "^1.22.0"
watchfiles = "^0.20.0"
filetype = "^1.2.0"
lazify = "^0.4.0"
packaging = "^23.1"
packaging = ">=23.1"
python-multipart = "^0.0.18"
pyjwt = "^2.8.0"

Expand Down
41 changes: 17 additions & 24 deletions cypress/e2e/action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,52 @@


@cl.action_callback("test action")
async def on_test_action():
async def on_action():
await cl.Message(content="Executed test action!").send()


@cl.action_callback("removable action")
async def on_removable_action(action: cl.Action):
async def on_action(action: cl.Action):

Check failure on line 10 in cypress/e2e/action/main.py

View workflow job for this annotation

GitHub Actions / lint-backend / lint-backend

Ruff (F811)

cypress/e2e/action/main.py:10:11: F811 Redefinition of unused `on_action` from line 5
await cl.Message(content="Executed removable action!").send()
await action.remove()


@cl.action_callback("multiple actions")
async def on_multiple_actions(action: cl.Action):
async def on_action(action: cl.Action):

Check failure on line 16 in cypress/e2e/action/main.py

View workflow job for this annotation

GitHub Actions / lint-backend / lint-backend

Ruff (F811)

cypress/e2e/action/main.py:16:11: F811 Redefinition of unused `on_action` from line 10
await cl.Message(content=f"Action(id={action.id}) has been removed!").send()
await action.remove()


@cl.action_callback("all actions removed")
async def on_all_actions_removed(_: cl.Action):
await cl.Message(content="All actions have been removed!").send()
async def on_action(_: cl.Action):

Check failure on line 22 in cypress/e2e/action/main.py

View workflow job for this annotation

GitHub Actions / lint-backend / lint-backend

Ruff (F811)

cypress/e2e/action/main.py:22:11: F811 Redefinition of unused `on_action` from line 16
await cl.Message(content=f"All actions have been removed!").send()

Check failure on line 23 in cypress/e2e/action/main.py

View workflow job for this annotation

GitHub Actions / lint-backend / lint-backend

Ruff (F541)

cypress/e2e/action/main.py:23:30: F541 f-string without any placeholders
to_remove = cl.user_session.get("to_remove") # type: cl.Message
await to_remove.remove_actions()


@cl.on_chat_start
async def main():
actions = [
cl.Action(id="test-action", name="test action", payload={"value": "test"}),
cl.Action(id="test-action", name="test action", value="test"),
cl.Action(id="removable-action", name="removable action", value="test"),
cl.Action(
id="removable-action", name="removable action", payload={"value": "test"}
),
cl.Action(
id="label-action",
name="label action",
payload={"value": "test"},
label="Test Label",
id="label-action", name="label action", value="test", label="Test Label"
),
cl.Action(
id="multiple-action-one",
name="multiple actions",
payload={"value": "multiple action one"},
value="multiple action one",
label="multiple action one",
collapsed=True,
),
cl.Action(
id="multiple-action-two",
name="multiple actions",
payload={"value": "multiple action two"},
value="multiple action two",
label="multiple action two",
collapsed=True,
),
cl.Action(
id="all-actions-removed",
name="all actions removed",
payload={"value": "test"},
),
cl.Action(id="all-actions-removed", name="all actions removed", value="test"),
]
message = cl.Message("Hello, this is a test message!", actions=actions)
cl.user_session.set("to_remove", message)
Expand All @@ -66,17 +59,17 @@ async def main():
cl.Action(
id="first-action",
name="first_action",
payload={"value": "first-action"},
value="first-action",
label="First action",
),
cl.Action(
id="second-action",
name="second_action",
payload={"value": "second-action"},
value="second-action",
label="Second action",
),
],
).send()

if result is not None:
await cl.Message(f"Thanks for pressing: {result['payload']['value']}").send()
if result != None:

Check failure on line 74 in cypress/e2e/action/main.py

View workflow job for this annotation

GitHub Actions / lint-backend / lint-backend

Ruff (E711)

cypress/e2e/action/main.py:74:18: E711 Comparison to `None` should be `cond is not None`
await cl.Message(f"Thanks for pressing: {result['value']}").send()
16 changes: 16 additions & 0 deletions cypress/e2e/action/spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,51 @@ describe('Action', () => {
cy.get('.step').eq(3).should('contain', 'Executed test action!');
cy.get("[id='test-action']").should('exist');

cy.wait(100);

// Click on "removable action"
cy.get("[id='removable-action']").should('exist');
cy.get("[id='removable-action']").click();
cy.get('.step').should('have.length', 5);
cy.get('.step').eq(4).should('contain', 'Executed removable action!');
cy.get("[id='removable-action']").should('not.exist');

cy.wait(100);

// Click on "multiple action one" in the action drawer, should remove the correct action button
cy.get("[id='actions-drawer-button']").should('exist');
cy.get("[id='actions-drawer-button']").click();
cy.get('.step').should('have.length', 5);

cy.wait(100);

cy.get("[id='multiple-action-one']").should('exist');
cy.get("[id='multiple-action-one']").click();
cy.get('.step')
.eq(5)
.should('contain', 'Action(id=multiple-action-one) has been removed!');
cy.get("[id='multiple-action-one']").should('not.exist');

cy.wait(100);

// Click on "multiple action two", should remove the correct action button
cy.get('.step').should('have.length', 6);
cy.get("[id='actions-drawer-button']").click();
cy.get("[id='multiple-action-two']").should('exist');
cy.get("[id='multiple-action-two']").click();
cy.get('.step')
.eq(6)
.should('contain', 'Action(id=multiple-action-two) has been removed!');
cy.get("[id='multiple-action-two']").should('not.exist');

cy.wait(100);

// Click on "all actions removed", should remove all buttons
cy.get("[id='all-actions-removed']").should('exist');
cy.get("[id='all-actions-removed']").click();
cy.get('.step').eq(7).should('contain', 'All actions have been removed!');
cy.get("[id='all-actions-removed']").should('not.exist');
cy.get("[id='test-action']").should('not.exist');
cy.get("[id='actions-drawer-button']").should('not.exist');
});
});
2 changes: 1 addition & 1 deletion cypress/e2e/ask_user/spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Ask User', () => {
it('should send a new message containing the user input', () => {
cy.get('.step').should('have.length', 1);
submitMessage('Jeeves');

cy.wait(2000);
cy.get('.step').should('have.length', 3);

cy.get('.step').eq(2).should('contain', 'Jeeves');
Expand Down
19 changes: 19 additions & 0 deletions cypress/e2e/author_rename/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import chainlit as cl


@cl.author_rename
def rename(orig_author: str):
rename_dict = {"LLMMathChain": "Albert Einstein", "Chatbot": "Assistant"}
return rename_dict.get(orig_author, orig_author)


@cl.step
def LLMMathChain():
return "2+2=4"


@cl.on_chat_start
async def main():
await cl.Message(author="LLMMathChain", content="2+2=4").send()
LLMMathChain()
await cl.Message(content="The response is 4").send()
12 changes: 12 additions & 0 deletions cypress/e2e/author_rename/spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { runTestServer } from '../../support/testUtils';

describe('Author rename', () => {
before(() => {
runTestServer();
});

it('should be able to rename authors', () => {
cy.get('.step').eq(0).should('contain', 'Albert Einstein');
cy.get('.step').eq(1).should('contain', 'Assistant');
});
});
32 changes: 32 additions & 0 deletions cypress/e2e/avatar/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import chainlit as cl


@cl.on_chat_start
async def start():
await cl.Avatar(
name="Tool 1",
url="https://avatars.githubusercontent.com/u/128686189?s=400&u=a1d1553023f8ea0921fba0debbe92a8c5f840dd9&v=4",
).send()

await cl.Avatar(name="Cat", path="./public/cat.jpeg").send()
await cl.Avatar(name="Cat 2", url="/public/cat.jpeg").send()

await cl.Message(
content="This message should not have an avatar!", author="Tool 0"
).send()

await cl.Message(
content="Tool 1! This message should have an avatar!", author="Tool 1"
).send()

await cl.Message(
content="This message should not have an avatar!", author="Tool 2"
).send()

await cl.Message(
content="This message should have a cat avatar!", author="Cat"
).send()

await cl.Message(
content="This message should have a cat avatar!", author="Cat 2"
).send()
Binary file added cypress/e2e/avatar/public/cat.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions cypress/e2e/avatar/spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { runTestServer } from '../../support/testUtils';

describe('Avatar', () => {
before(() => {
runTestServer();
});

it('should be able to display avatars', () => {
cy.get('.step').should('have.length', 5);

cy.get('.step').eq(0).find('img').should('have.length', 0);
cy.get('.step').eq(1).find('img').should('have.length', 1);
cy.get('.step').eq(2).find('img').should('have.length', 0);
cy.get('.step').eq(3).find('img').should('have.length', 1);
cy.get('.step').eq(4).find('img').should('have.length', 1);

cy.get('.element-link').should('have.length', 0);
});
});
Loading

0 comments on commit 80bd98a

Please sign in to comment.