Skip to content

Commit

Permalink
add rendermime registry requrement, activation to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-i committed Jan 4, 2024
1 parent 0bfc939 commit 15d6fc8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/jupyter-ai/src/components/chat-messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { MarkdownComponent } from './markdown-component';
import { useCollaboratorsContext } from '../contexts/collaborators-context';

type ChatMessagesProps = {
rendermime: IRenderMimeRegistry;
rmRegistry: IRenderMimeRegistry;
messages: AiService.ChatMessage[];
};

Expand Down Expand Up @@ -144,7 +144,7 @@ export function ChatMessages(props: ChatMessagesProps): JSX.Element {
sx={{ marginBottom: 3 }}
/>
<MarkdownComponent
rendermime={props.rendermime}
rmRegistry={props.rmRegistry}
markdownString={message.body}
/>
</Box>
Expand Down
10 changes: 5 additions & 5 deletions packages/jupyter-ai/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import { ScrollContainer } from './scroll-container';
type ChatBodyProps = {
chatHandler: ChatHandler;
setChatView: (view: ChatView) => void;
renderMimeRegistry: IRenderMimeRegistry;
rmRegistry: IRenderMimeRegistry;
};

function ChatBody({
chatHandler,
setChatView: chatViewHandler,
renderMimeRegistry
rmRegistry: renderMimeRegistry
}: ChatBodyProps): JSX.Element {
const [messages, setMessages] = useState<AiService.ChatMessage[]>([]);
const [showWelcomeMessage, setShowWelcomeMessage] = useState<boolean>(false);
Expand Down Expand Up @@ -149,7 +149,7 @@ function ChatBody({
return (
<>
<ScrollContainer sx={{ flexGrow: 1 }}>
<ChatMessages messages={messages} rendermime={renderMimeRegistry} />
<ChatMessages messages={messages} rmRegistry={renderMimeRegistry} />
</ScrollContainer>
<ChatInput
value={input}
Expand Down Expand Up @@ -181,7 +181,7 @@ export type ChatProps = {
selectionWatcher: SelectionWatcher;
chatHandler: ChatHandler;
globalAwareness: Awareness | null;
renderMimeRegistry: IRenderMimeRegistry;
rmRegistry: IRenderMimeRegistry;
chatView?: ChatView;
};

Expand Down Expand Up @@ -231,7 +231,7 @@ export function Chat(props: ChatProps): JSX.Element {
<ChatBody
chatHandler={props.chatHandler}
setChatView={setView}
renderMimeRegistry={props.renderMimeRegistry}
rmRegistry={props.rmRegistry}
/>
)}
{view === ChatView.Settings && <ChatSettings />}
Expand Down
20 changes: 11 additions & 9 deletions packages/jupyter-ai/src/components/markdown-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ import { Widget } from '@lumino/widgets';
import { LuminoComponent } from './lumino-component';

type MarkdownWidgetProps = {
rendermime: IRenderMimeRegistry;
markdownString: string;
rmRegistry: IRenderMimeRegistry;
};

type MarkdownComponentProps = {
markdownString: string;
rendermime: IRenderMimeRegistry;
rmRegistry: IRenderMimeRegistry;
};

class MarkdownWidget extends Widget {
private rendermime: IRenderMimeRegistry;
private rmRegistry: IRenderMimeRegistry;
private markdownString: string;

constructor(props: MarkdownWidgetProps) {
super();
this.rendermime = props.rendermime;
this.rmRegistry = props.rmRegistry;
this.markdownString = props.markdownString;
this.initializeMarkdownRendering();
}

async initializeMarkdownRendering(): Promise<void> {
const mimeType = 'text/markdown';
const model = this.rendermime.createModel({
const model = this.rmRegistry.createModel({
data: { [mimeType]: this.markdownString }
});
const renderer = this.rendermime.createRenderer(mimeType);
const renderer = this.rmRegistry.createRenderer(mimeType);
await renderer.renderModel(model);
this.node.appendChild(renderer.node);
}
Expand All @@ -38,18 +38,20 @@ class MarkdownWidget extends Widget {
export function MarkdownComponent(
props: MarkdownComponentProps
): React.ReactElement | null {
const { markdownString, rendermime } = props;
const widgetRef = useRef<MarkdownWidget | null>(null);

useEffect(() => {
if (!widgetRef.current) {
widgetRef.current = new MarkdownWidget({ rendermime, markdownString });
widgetRef.current = new MarkdownWidget({
markdownString: props.markdownString,
rmRegistry: props.rmRegistry
});
}

return () => {
widgetRef.current?.dispose();
};
}, []); // Empty dependency array if props are not expected to change
}, []);

return widgetRef.current ? (
<LuminoComponent widget={widgetRef.current} />
Expand Down
7 changes: 4 additions & 3 deletions packages/jupyter-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export type DocumentTracker = IWidgetTracker<IDocumentWidget>;
const plugin: JupyterFrontEndPlugin<void> = {
id: 'jupyter_ai:plugin',
autoStart: true,
requires: [IRenderMimeRegistry],
optional: [IGlobalAwareness, ILayoutRestorer],
activate: async (
app: JupyterFrontEnd,
rmRegistry: IRenderMimeRegistry,
globalAwareness: Awareness | null,
restorer: ILayoutRestorer | null,
renderMimeRegistry: IRenderMimeRegistry
restorer: ILayoutRestorer | null
) => {
/**
* Initialize selection watcher singleton
Expand All @@ -46,7 +47,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
selectionWatcher,
chatHandler,
globalAwareness,
renderMimeRegistry
rmRegistry
);
} catch (e) {
chatWidget = buildErrorWidget();
Expand Down
4 changes: 2 additions & 2 deletions packages/jupyter-ai/src/widgets/chat-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export function buildChatSidebar(
selectionWatcher: SelectionWatcher,
chatHandler: ChatHandler,
globalAwareness: Awareness | null,
renderMimeRegistry: IRenderMimeRegistry
rmRegistry: IRenderMimeRegistry
): ReactWidget {
const ChatWidget = ReactWidget.create(
<Chat
selectionWatcher={selectionWatcher}
chatHandler={chatHandler}
globalAwareness={globalAwareness}
renderMimeRegistry={renderMimeRegistry}
rmRegistry={rmRegistry}
/>
);
ChatWidget.id = 'jupyter-ai::chat';
Expand Down

0 comments on commit 15d6fc8

Please sign in to comment.