From 681dd522640c24be71e49aba1652d9dc15240fd3 Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Tue, 22 Oct 2024 14:53:38 +0200 Subject: [PATCH 1/2] feat: Upgrade cozy-realtime --- package.json | 2 +- yarn.lock | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index fd57fb98dd..e3a575eb5a 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "cozy-keys-lib": "^6.1.1", "cozy-logger": "1.10.4", "cozy-minilog": "^3.3.1", - "cozy-realtime": "4.6.0", + "cozy-realtime": "^5.0.4", "cozy-sharing": "^16.0.0", "cozy-stack-client": "^48.16.0", "cozy-tsconfig": "1.2.0", diff --git a/yarn.lock b/yarn.lock index 0f60b703b8..3d32d48e86 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6027,7 +6027,7 @@ cozy-client@^48.25.0: sift "^6.0.0" url-search-params-polyfill "^8.0.0" -cozy-device-helper@2.7.0, cozy-device-helper@^2.1.0, cozy-device-helper@^2.7.0: +cozy-device-helper@2.7.0, cozy-device-helper@^2.1.0: version "2.7.0" resolved "https://registry.yarnpkg.com/cozy-device-helper/-/cozy-device-helper-2.7.0.tgz#573749997f18e5a1f11f720faec8c9bf2406beeb" integrity sha512-jMzW7s4IDuMivbsP8fo1IWW1z5l0wJ0u440E0fQmdsi+Zm/L9GXFKthLuuceYqXlH6c/APUCfpozJqjFMMHU1A== @@ -6041,6 +6041,13 @@ cozy-device-helper@^3.1.0: dependencies: lodash "^4.17.19" +cozy-device-helper@^3.1.2: + version "3.1.2" + resolved "https://registry.yarnpkg.com/cozy-device-helper/-/cozy-device-helper-3.1.2.tgz#1a276a5c116d7b15968ddbcb33aff1d05b1e7a68" + integrity sha512-/eERI5+2/PoS0+JpExCvxhsy6vRSz1hhGKfUwpkVIT68tSMD6IjX/tZpXdU2YZ/dm9dsDusZZAVzpqtMRnX7EA== + dependencies: + lodash "^4.17.19" + cozy-doctypes@1.83.8: version "1.83.8" resolved "https://registry.yarnpkg.com/cozy-doctypes/-/cozy-doctypes-1.83.8.tgz#99ec864059034bd032f6f01e322b57fea130a5d3" @@ -6169,13 +6176,13 @@ cozy-minilog@^3.3.1: dependencies: microee "0.0.6" -cozy-realtime@4.6.0: - version "4.6.0" - resolved "https://registry.yarnpkg.com/cozy-realtime/-/cozy-realtime-4.6.0.tgz#2600d091bef95c197ec5ebe0308df34507c81516" - integrity sha512-LE+zpgFFS3NgbR0aF1e2vyZXiVLHDB9dqT+tA41JO6RLHTZu6GSpZrn0oM2GcNS13/0vGXBJY6tKKFBmyK82ag== +cozy-realtime@^5.0.4: + version "5.0.4" + resolved "https://registry.yarnpkg.com/cozy-realtime/-/cozy-realtime-5.0.4.tgz#17ebf7d82239a3df3b8ff588fc2356b55fbc48ff" + integrity sha512-8Kch3YWf2fdNv5LPah8umR7Ukqj2HmZLpwId1aYxO+MY767cTJXlX5fYKcUAPVm1RCOZmp83A8JDBcKCfsf2qA== dependencies: "@cozy/minilog" "^1.0.0" - cozy-device-helper "^2.7.0" + cozy-device-helper "^3.1.2" cozy-release@1.10.0: version "1.10.0" From 1f1f3f5bc324bb937f39fed7271a12293e4cdc44 Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Tue, 22 Oct 2024 14:54:39 +0200 Subject: [PATCH 2/2] feat(Assistant): Ignore instant messages if not for the actual conversation --- src/assistant/AssistantProvider.jsx | 34 +++++++++++++++++++++++++++-- src/assistant/queries.js | 3 ++- 2 files changed, 34 insertions(+), 3 deletions(-) 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