Skip to content

Commit

Permalink
feat(frontend): support js new chat api in widget (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 authored Jan 22, 2025
1 parent f7fedfe commit 3cda949
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions frontend/app/src/components/chat/chat-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { AppChatStreamState, StackVMState } from '@/components/chat/chat-st
import { useGtagFn } from '@/components/gtag-provider';
import { useBootstrapStatus } from '@/components/system/BootstrapStatusProvider';
import { useLatestRef } from '@/components/use-latest-ref';
import { createContext, type ReactNode, useContext, useEffect, useState } from 'react';
import { createContext, type ReactNode, type Ref, useContext, useEffect, useImperativeHandle, useState } from 'react';

export interface ChatsProviderValues {
chats: Map<string, ChatController>;
Expand All @@ -30,7 +30,7 @@ const ChatsContext = createContext<ChatsProviderValues>({

const ChatControllerContext = createContext<ChatController | null>(null);

export function ChatsProvider ({ onChatCreated, children }: { children: ReactNode, onChatCreated?: (id: string, chat: Chat, controller: ChatController) => void }) {
export function ChatsProvider ({ newChatRef, onChatCreated, children }: { children: ReactNode,newChatRef?: Ref<ChatsProviderValues['newChat'] | undefined>, onChatCreated?: (id: string, chat: Chat, controller: ChatController) => void }) {
const bootstrapStatusRef = useLatestRef(useBootstrapStatus());
const [chats, setChats] = useState(() => new Map<string, ChatController>);

Expand All @@ -56,6 +56,8 @@ export function ChatsProvider ({ onChatCreated, children }: { children: ReactNod
});
};

useImperativeHandle(newChatRef, () => newChat);

return (
<ChatsContext.Provider value={{
chats,
Expand Down
15 changes: 11 additions & 4 deletions frontend/packages/widget-react/src/Widget.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BootstrapStatus } from '@/api/system';
import { ManualScrollVoter } from '@/components/auto-scroll';
import { AutoScroll } from '@/components/auto-scroll/auto-scroll';
import { ChatsProvider } from '@/components/chat/chat-hooks';
import { ChatsProvider, type ChatsProviderValues } from '@/components/chat/chat-hooks';
import { Conversation } from '@/components/chat/conversation';
import { useGtagFn } from '@/components/gtag-provider';
import { PortalProvider } from '@/components/portal-provider';
Expand Down Expand Up @@ -108,6 +108,8 @@ export const Widget = forwardRef<WidgetInstance, WidgetProps>(({ container, trig
}
}, [trigger]);

const newChatRef = useRef<ChatsProviderValues['newChat'] | undefined>(undefined);

useImperativeHandle(ref, () => ({
get open () {
return openRef.current;
Expand All @@ -125,13 +127,20 @@ export const Widget = forwardRef<WidgetInstance, WidgetProps>(({ container, trig
setDark(d);
},
get initialized (): true { return true; },
newChat (content: string) {
newChatRef.current?.(undefined, [], {
content,
chat_engine: chatEngine,
});
},
}), []);

return (
<PortalProvider container={container}>
<BootstrapStatusProvider bootstrapStatus={bootstrapStatus}>
<ExperimentalFeaturesProvider features={experimentalFeatures}>
<ChatsProvider
newChatRef={newChatRef}
onChatCreated={id => {
window.dispatchEvent(new CustomEvent('tidbainewchat', {
detail: { id },
Expand Down Expand Up @@ -171,9 +180,7 @@ export const Widget = forwardRef<WidgetInstance, WidgetProps>(({ container, trig
Ask AI
</span>
</DialogTitle>
<DialogDescription className="sr-only">
.
</DialogDescription>
<DialogDescription className="sr-only" />
</DialogHeader>
<AutoScroll target={scrollTarget} edgePixels={12}>
<ManualScrollVoter />
Expand Down

0 comments on commit 3cda949

Please sign in to comment.