diff --git a/src/assistant/AssistantProvider.jsx b/src/assistant/AssistantProvider.jsx index 715e897bf7..b15808bf46 100644 --- a/src/assistant/AssistantProvider.jsx +++ b/src/assistant/AssistantProvider.jsx @@ -1,7 +1,9 @@ import React, { useMemo, useContext, useState, useCallback } from 'react' import { useClient } from 'cozy-client' -import useRealtime from 'cozy-ui/transpiled/react/hooks/useRealtime' +import useRealtime from 'cozy-realtime/dist/useRealtime' + +import { CHAT_EVENTS_DOCTYPE, CHAT_CONVERSATIONS_DOCTYPE } from './queries' export const AssistantContext = React.createContext() @@ -14,17 +16,44 @@ export const useAssistant = () => { return context } +const pushMessagesIdInState = (res, setState) => { + const messagesId = res.messages.map(message => message.id) + setState(v => ({ + ...v, + messagesId + })) +} + +const isMessageForThisConversation = (res, messagesId) => + messagesId.includes(res._id) + const AssistantProvider = ({ children }) => { const client = useClient() const [assistantState, setAssistantState] = useState({ message: '', status: 'idle', + messagesId: [], conversationId: undefined }) useRealtime(client, { - 'io.cozy.ai.chat.events': { + [CHAT_CONVERSATIONS_DOCTYPE]: { + created: res => { + pushMessagesIdInState(res, setAssistantState) + }, + updated: res => { + pushMessagesIdInState(res, setAssistantState) + } + } + }) + + useRealtime(client, { + [CHAT_EVENTS_DOCTYPE]: { created: res => { + if (!isMessageForThisConversation(res, assistantState.messagesId)) { + return + } + if (res.object === 'done') { if (assistantState.status !== 'idle') { // to be sure the last response is inside io.cozy.ai.chat.conversations @@ -86,6 +115,7 @@ const AssistantProvider = ({ children }) => { setAssistantState({ message: '', status: 'idle', + messagesId: [], conversationId: undefined }), [] diff --git a/src/assistant/queries.js b/src/assistant/queries.js index 63b732774a..570f4ed15a 100644 --- a/src/assistant/queries.js +++ b/src/assistant/queries.js @@ -1,7 +1,8 @@ import { Q, fetchPolicies } from 'cozy-client' const CONTACTS_DOCTYPE = 'io.cozy.contacts' -const CHAT_CONVERSATIONS_DOCTYPE = 'io.cozy.ai.chat.conversations' +export const CHAT_CONVERSATIONS_DOCTYPE = 'io.cozy.ai.chat.conversations' +export const CHAT_EVENTS_DOCTYPE = 'io.cozy.ai.chat.events' const defaultFetchPolicy = fetchPolicies.olderThan(86_400_000) // 24 hours