forked from jupyterlab/jupyter-ai
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customizing user icon to disable the AI agent
- Loading branch information
Showing
6 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { DefaultIconRenderer, UsersItem } from '@jupyter/collaboration'; | ||
import { | ||
CollaborativeChatModel, | ||
IChatChanges, | ||
YChat | ||
} from 'jupyterlab-collaborative-chat'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
|
||
/** | ||
* The user icon renderer. | ||
*/ | ||
export const userIconRenderer = ( | ||
props: UsersItem.IIconRendererProps | ||
): JSX.Element => { | ||
const { user } = props; | ||
|
||
if (user.userData.name === 'Jupyternaut') { | ||
return <AgentIconRenderer {...props} />; | ||
} | ||
|
||
return <DefaultIconRenderer user={user} />; | ||
}; | ||
|
||
/** | ||
* The user icon renderer for an AI agent. | ||
* It modify the metadata of the ydocument to enable/disable the AI agent. | ||
*/ | ||
const AgentIconRenderer = ( | ||
props: UsersItem.IIconRendererProps | ||
): JSX.Element => { | ||
const { user, model } = props; | ||
const [iconClass, setIconClass] = useState<string>(''); | ||
const sharedModel = useRef<YChat>( | ||
(model as CollaborativeChatModel).sharedModel | ||
); | ||
|
||
useEffect(() => { | ||
// Update the icon class. | ||
const updateStatus = () => { | ||
const agents = | ||
(sharedModel.current.getMetadata('agents') as string[]) || []; | ||
setIconClass( | ||
agents.includes(user.userData.username) ? '' : 'disconnected' | ||
); | ||
}; | ||
|
||
const onChange = (_: YChat, changes: IChatChanges) => { | ||
if (changes.metadataChanges) { | ||
updateStatus(); | ||
} | ||
}; | ||
|
||
sharedModel.current.changed.connect(onChange); | ||
updateStatus(); | ||
return () => { | ||
sharedModel.current.changed.disconnect(updateStatus); | ||
}; | ||
}, [model]); | ||
|
||
const onclick = () => { | ||
const agents = | ||
(sharedModel.current.getMetadata('agents') as string[]) || []; | ||
const index = agents.indexOf(user.userData.username); | ||
if (index > -1) { | ||
agents.splice(index, 1); | ||
} else { | ||
agents.push(user.userData.username); | ||
} | ||
sharedModel.current.setMetadata('agents', agents); | ||
}; | ||
|
||
return ( | ||
<DefaultIconRenderer user={user} onClick={onclick} className={iconClass} /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.jp-toolbar-users-item .jp-MenuBar-imageIcon.disconnected { | ||
opacity: 0.5; | ||
} | ||
|
||
.jp-toolbar-users-item .jp-MenuBar-imageIcon.disconnected:before { | ||
position: absolute; | ||
left: 11px; | ||
content: ''; | ||
height: 28px; | ||
width: 2px; | ||
background-color: #333; | ||
transform: rotate(45deg); | ||
} |