Skip to content

Commit

Permalink
Add getter for the model from the widget, and export the chat icon
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Mar 18, 2024
1 parent 9cc64e2 commit 8afcf83
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
24 changes: 12 additions & 12 deletions src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { IChatMessage, IMessage } from '../types';
import { IThemeManager } from '@jupyterlab/apputils';

type ChatBodyProps = {
chatModel: IChatModel;
model: IChatModel;
rmRegistry: IRenderMimeRegistry;
};

function ChatBody({
chatModel,
model,
rmRegistry: renderMimeRegistry
}: ChatBodyProps): JSX.Element {
const [messages, setMessages] = useState<IChatMessage[]>([]);
Expand All @@ -30,17 +30,17 @@ function ChatBody({
*/
useEffect(() => {
async function fetchHistory() {
if (!chatModel.getHistory) {
if (!model.getHistory) {
return;
}
chatModel
model
.getHistory()
.then(history => setMessages(history.messages))
.catch(e => console.error(e));
}

fetchHistory();
}, [chatModel]);
}, [model]);

/**
* Effect: listen to chat messages
Expand All @@ -55,19 +55,19 @@ function ChatBody({
}
}

chatModel.incomingMessage.connect(handleChatEvents);
model.incomingMessage.connect(handleChatEvents);
return function cleanup() {
chatModel.incomingMessage.disconnect(handleChatEvents);
model.incomingMessage.disconnect(handleChatEvents);
};
}, [chatModel]);
}, [model]);

// no need to append to messageGroups imperatively here. all of that is
// handled by the listeners registered in the effect hooks above.
const onSend = async () => {
setInput('');

// send message to backend
chatModel.addMessage({ body: input });
model.addMessage({ body: input });
};

return (
Expand All @@ -86,7 +86,7 @@ function ChatBody({
paddingBottom: 0,
borderTop: '1px solid var(--jp-border-color1)'
}}
sendWithShiftEnter={chatModel.config.sendWithShiftEnter ?? false}
sendWithShiftEnter={model.config.sendWithShiftEnter ?? false}
/>
</>
);
Expand Down Expand Up @@ -130,7 +130,7 @@ export function Chat(props: Chat.IOptions): JSX.Element {
</Box>
{/* body */}
{view === Chat.ChatView.Chat && (
<ChatBody chatModel={props.chatModel} rmRegistry={props.rmRegistry} />
<ChatBody model={props.model} rmRegistry={props.rmRegistry} />
)}
{view === Chat.ChatView.Settings && props.settingsPanel && (
<props.settingsPanel />
Expand All @@ -151,7 +151,7 @@ export namespace Chat {
/**
* The chat model.
*/
chatModel: IChatModel;
model: IChatModel;
/**
* The theme manager.
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './handlers/websocket-handler';
export * from './icons';
export * from './model';
export * from './types';
export * from './widgets/chat-error';
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/chat-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function buildChatSidebar(
): ReactWidget {
const ChatWidget = ReactWidget.create(
<Chat
chatModel={chatModel}
model={chatModel}
themeManager={themeManager}
rmRegistry={rmRegistry}
/>
Expand Down
13 changes: 10 additions & 3 deletions src/widgets/chat-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@ export class ChatWidget extends ReactWidget {
this.title.icon = chatIcon;
this.title.caption = 'Jupyter Chat'; // TODO: i18n

this._chatModel = options.chatModel;
this._model = options.model;
this._themeManager = options.themeManager;
this._rmRegistry = options.rmRegistry;
}

/**
* Gte the model of the widget.
*/
get model(): IChatModel {
return this._model;
}

render() {
return (
<Chat
chatModel={this._chatModel}
model={this._model}
themeManager={this._themeManager}
rmRegistry={this._rmRegistry}
/>
);
}

private _chatModel: IChatModel;
private readonly _model: IChatModel;
private _themeManager: IThemeManager | null;
private _rmRegistry: IRenderMimeRegistry;
}
Expand Down

0 comments on commit 8afcf83

Please sign in to comment.