Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Assistant): Ignore instant messages if not for the actual conversation #2222

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 32 additions & 2 deletions src/assistant/AssistantProvider.jsx
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -86,6 +115,7 @@ const AssistantProvider = ({ children }) => {
setAssistantState({
message: '',
status: 'idle',
messagesId: [],
conversationId: undefined
}),
[]
Expand Down
3 changes: 2 additions & 1 deletion src/assistant/queries.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 13 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6027,7 +6027,7 @@ cozy-client@^48.25.0:
sift "^6.0.0"
url-search-params-polyfill "^8.0.0"

[email protected], cozy-device-helper@^2.1.0, cozy-device-helper@^2.7.0:
[email protected], 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==
Expand All @@ -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"

[email protected]:
version "1.83.8"
resolved "https://registry.yarnpkg.com/cozy-doctypes/-/cozy-doctypes-1.83.8.tgz#99ec864059034bd032f6f01e322b57fea130a5d3"
Expand Down Expand Up @@ -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"

[email protected]:
version "1.10.0"
Expand Down
Loading