Skip to content

Commit

Permalink
fix(frontend): fix widget new chat callback
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 committed Jan 23, 2025
1 parent 0d64bee commit 97cf600
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
10 changes: 9 additions & 1 deletion frontend/app/src/components/chat/chat-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ export class ChatController<State extends AppChatStreamState = AppChatStreamStat
const newCreated = !this.chat;
this.chat = { ...this.chat, ...chat };
this.emit(newCreated ? 'created' : 'updated', this.chat);

if (process.env.NEXT_PUBLIC_IS_WIDGET) {
if (newCreated) {
window.dispatchEvent(new CustomEvent('tidbainewchat', {
detail: { id: chat.id },
}));
}
}
}

upsertMessage (message: ChatMessage): void {
Expand Down Expand Up @@ -240,7 +248,7 @@ export class ChatController<State extends AppChatStreamState = AppChatStreamStat
break;
case 'tool_result':
this._processToolResultPart(ongoingMessageController, part);
break
break;
case 'finish_step':

case 'finish_message':
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/src/components/chat/chat-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({ onChatCreated, children }: { children: ReactNode, /** @deprecated */onChatCreated?: (id: string, chat: Chat, controller: ChatController) => void }) {
const bootstrapStatusRef = useLatestRef(useBootstrapStatus());
const [chats, setChats] = useState(() => new Map<string, ChatController>);

Expand Down
8 changes: 1 addition & 7 deletions frontend/packages/widget-react/src/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,7 @@ export const Widget = forwardRef<WidgetInstance, WidgetProps>(({ container, trig
<PortalProvider container={container}>
<BootstrapStatusProvider bootstrapStatus={bootstrapStatus}>
<ExperimentalFeaturesProvider features={experimentalFeatures}>
<ChatsProvider
onChatCreated={id => {
window.dispatchEvent(new CustomEvent('tidbainewchat', {
detail: { id },
}));
}}
>
<ChatsProvider>
<Dialog open={open} onOpenChange={(open) => {
setOpen(open);
if (!open) {
Expand Down

0 comments on commit 97cf600

Please sign in to comment.