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): Show instant message sorted by position #2236

Merged
merged 2 commits into from
Oct 31, 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
24 changes: 7 additions & 17 deletions src/assistant/AssistantProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { useMemo, useContext, useState, useCallback } from 'react'
import set from 'lodash/set'

import { useClient } from 'cozy-client'
import useRealtime from 'cozy-realtime/dist/useRealtime'

import { CHAT_EVENTS_DOCTYPE, CHAT_CONVERSATIONS_DOCTYPE } from './queries'
import { pushMessagesIdInState, isMessageForThisConversation } from './helpers'

export const AssistantContext = React.createContext()

export const makeConversationId = () =>
`${Date.now()}-${Math.floor(Math.random() * 90000) + 10000}`

export const useAssistant = () => {
const context = useContext(AssistantContext)

Expand All @@ -19,21 +18,10 @@ 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: '',
message: {},
status: 'idle',
messagesId: []
})
Expand Down Expand Up @@ -70,9 +58,11 @@ const AssistantProvider = ({ children }) => {
}

if (res.object === 'delta') {
const message = set(assistantState.message, res.position, res.content)

setAssistantState(v => ({
...v,
message: v.message + res.content,
message,
status: 'writing'
}))
}
Expand All @@ -83,7 +73,7 @@ const AssistantProvider = ({ children }) => {
const clearAssistant = useCallback(
() =>
setAssistantState({
message: '',
message: {},
status: 'idle',
messagesId: []
}),
Expand Down
3 changes: 2 additions & 1 deletion src/assistant/Conversations/ChatConversation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useQuery, isQueryLoading } from 'cozy-client'

import { buildChatConversationQueryById } from '../queries'
import { useAssistant } from '../AssistantProvider'
import { getInstantMessage } from '../helpers'
import ChatUserItem from './ChatUserItem'
import ChatAssistantItem from './ChatAssistantItem'
import ChatRealtimeAnswer from './ChatRealtimeAnswer'
Expand Down Expand Up @@ -67,7 +68,7 @@ const ChatConversation = ({ conversation, myself }) => {
{showLastConv && (
<ChatRealtimeAnswer
isLoading={assistantState.status === 'pending'}
label={assistantState.message}
label={getInstantMessage(assistantState)}
/>
)}
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/assistant/Search/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import flag from 'cozy-flags'
import { useBreakpoints } from 'cozy-ui/transpiled/react/providers/Breakpoints'

import { useSearch } from './SearchProvider'
import { makeConversationId, useAssistant } from '../AssistantProvider'
import { useAssistant } from '../AssistantProvider'
import { makeConversationId } from '../helpers'
import SearchBarMobile from './SearchBarMobile'
import SearchBarDesktop from './SearchBarDesktop'

Expand Down
3 changes: 2 additions & 1 deletion src/assistant/Views/SearchDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import flag from 'cozy-flags'
import { FixedDialog } from 'cozy-ui/transpiled/react/CozyDialogs'

import SearchProvider from '../Search/SearchProvider'
import { makeConversationId, useAssistant } from '../AssistantProvider'
import { useAssistant } from '../AssistantProvider'
import { makeConversationId } from '../helpers'
import ResultMenuContent from '../ResultMenu/ResultMenuContent'
import { useSearch } from '../Search/SearchProvider'
import SearchBar from '../Search/SearchBar'
Expand Down
19 changes: 19 additions & 0 deletions src/assistant/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const getInstantMessage = assistantState =>
Object.keys(assistantState.message)
.sort((a, b) => a - b)
.map(key => assistantState.message[key])
.join('')

export const makeConversationId = () =>
`${Date.now()}-${Math.floor(Math.random() * 90000) + 10000}`

export const pushMessagesIdInState = (res, setState) => {
const messagesId = res.messages.map(message => message.id)
setState(v => ({
...v,
messagesId
}))
}

export const isMessageForThisConversation = (res, messagesId) =>
messagesId.includes(res._id)
Loading