From 0ce770295b4606445ddd5002ba1bc4d6b024d85d Mon Sep 17 00:00:00 2001 From: aychang95 Date: Tue, 7 Nov 2023 22:09:12 -0500 Subject: [PATCH 1/5] Ensure initials appear in collaborative mode --- packages/jupyter-ai/jupyter_ai/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/handlers.py b/packages/jupyter-ai/jupyter_ai/handlers.py index 38fc9552d..3450a8823 100644 --- a/packages/jupyter-ai/jupyter_ai/handlers.py +++ b/packages/jupyter-ai/jupyter_ai/handlers.py @@ -127,8 +127,8 @@ def get_chat_user(self) -> ChatUser: chat_user_kwargs = { # set in case IdentityProvider doesn't return initials, e.g. # JupyterHub (#302) - "initials": initials, **asdict(self.current_user), + "initials": initials, } return ChatUser(**chat_user_kwargs) From 1fc580c6417dc58cdfc30752be8b190968fddfa2 Mon Sep 17 00:00:00 2001 From: aychang95 Date: Wed, 8 Nov 2023 15:28:20 -0500 Subject: [PATCH 2/5] Set default initials and use instead of null current_user attr --- packages/jupyter-ai/jupyter_ai/handlers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/handlers.py b/packages/jupyter-ai/jupyter_ai/handlers.py index 3450a8823..f7a65a924 100644 --- a/packages/jupyter-ai/jupyter_ai/handlers.py +++ b/packages/jupyter-ai/jupyter_ai/handlers.py @@ -121,14 +121,14 @@ def get_chat_user(self) -> ChatUser: if collaborative: names = self.current_user.name.split(" ", maxsplit=2) - initials = "".join( + # set in case IdentityProvider doesn't return initials, e.g. + # JupyterHub (#302) + default_initials = "".join( [(name.capitalize()[0] if len(name) > 0 else "") for name in names] ) chat_user_kwargs = { - # set in case IdentityProvider doesn't return initials, e.g. - # JupyterHub (#302) **asdict(self.current_user), - "initials": initials, + "initials": getattr(self.current_user, 'initials', default_initials), } return ChatUser(**chat_user_kwargs) From d62916af09c74e18e4bfcb7e069a9c2dcbcedfd4 Mon Sep 17 00:00:00 2001 From: aychang95 Date: Wed, 8 Nov 2023 15:37:22 -0500 Subject: [PATCH 3/5] Black format to avoid pre-commit failure --- packages/jupyter-ai/jupyter_ai/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/handlers.py b/packages/jupyter-ai/jupyter_ai/handlers.py index 9f6eee965..d7194c233 100644 --- a/packages/jupyter-ai/jupyter_ai/handlers.py +++ b/packages/jupyter-ai/jupyter_ai/handlers.py @@ -128,7 +128,7 @@ def get_chat_user(self) -> ChatUser: ) chat_user_kwargs = { **asdict(self.current_user), - "initials": getattr(self.current_user, 'initials', default_initials), + "initials": getattr(self.current_user, "initials", default_initials), } return ChatUser(**chat_user_kwargs) From ec00e2b6f8765d58a4b161043fa8a9094be89417 Mon Sep 17 00:00:00 2001 From: aychang95 Date: Wed, 8 Nov 2023 16:27:24 -0500 Subject: [PATCH 4/5] Getattr does not return default because initials attr still exists even though it's None --- packages/jupyter-ai/jupyter_ai/handlers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/handlers.py b/packages/jupyter-ai/jupyter_ai/handlers.py index d7194c233..c9ac2c0bc 100644 --- a/packages/jupyter-ai/jupyter_ai/handlers.py +++ b/packages/jupyter-ai/jupyter_ai/handlers.py @@ -126,9 +126,12 @@ def get_chat_user(self) -> ChatUser: default_initials = "".join( [(name.capitalize()[0] if len(name) > 0 else "") for name in names] ) + current_user_initials = getattr(self.current_user, "initials", None) chat_user_kwargs = { **asdict(self.current_user), - "initials": getattr(self.current_user, "initials", default_initials), + "initials": current_user_initials + if current_user_initials + else default_initials, } return ChatUser(**chat_user_kwargs) From 6a083f3d19f3430313a0cc2ebe64579328e98d56 Mon Sep 17 00:00:00 2001 From: Andrew Chang Date: Tue, 14 Nov 2023 10:14:42 +0200 Subject: [PATCH 5/5] optimize and set to one initial var --- packages/jupyter-ai/jupyter_ai/handlers.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/handlers.py b/packages/jupyter-ai/jupyter_ai/handlers.py index c9ac2c0bc..289df81dc 100644 --- a/packages/jupyter-ai/jupyter_ai/handlers.py +++ b/packages/jupyter-ai/jupyter_ai/handlers.py @@ -121,18 +121,19 @@ def get_chat_user(self) -> ChatUser: if collaborative: names = self.current_user.name.split(" ", maxsplit=2) - # set in case IdentityProvider doesn't return initials, e.g. - # JupyterHub (#302) - default_initials = "".join( - [(name.capitalize()[0] if len(name) > 0 else "") for name in names] - ) - current_user_initials = getattr(self.current_user, "initials", None) + initials = getattr(self.current_user, "initials", None) + if not initials: + # compute default initials in case IdentityProvider doesn't + # return initials, e.g. JupyterHub (#302) + names = self.current_user.name.split(" ", maxsplit=2) + initials = "".join( + [(name.capitalize()[0] if len(name) > 0 else "") for name in names] + ) chat_user_kwargs = { **asdict(self.current_user), - "initials": current_user_initials - if current_user_initials - else default_initials, + "initials": initials, } + return ChatUser(**chat_user_kwargs) login = getpass.getuser()