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 1 commit
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
15 changes: 12 additions & 3 deletions src/assistant/AssistantProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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'
Expand All @@ -7,6 +8,12 @@ import { CHAT_EVENTS_DOCTYPE, CHAT_CONVERSATIONS_DOCTYPE } from './queries'

export const AssistantContext = React.createContext()

export const getInstantMessage = assistantState =>
Object.keys(assistantState.message)
.sort((a, b) => a - b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.sort() est suffisant pour trier dans cet ordre

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non sur les number ça va faire 1 puis 11 puis 111 alors qu'avec la fonction actuelle ça va faire 1 puis 2 puis 3 ... puis 11

.map(key => assistantState.message[key])
.join('')

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

Expand All @@ -33,7 +40,7 @@ const isMessageForThisConversation = (res, messagesId) =>
const AssistantProvider = ({ children }) => {
const client = useClient()
const [assistantState, setAssistantState] = useState({
message: '',
message: {},
status: 'idle',
messagesId: []
})
Expand Down Expand Up @@ -70,9 +77,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 +92,7 @@ const AssistantProvider = ({ children }) => {
const clearAssistant = useCallback(
() =>
setAssistantState({
message: '',
message: {},
status: 'idle',
messagesId: []
}),
Expand Down
4 changes: 2 additions & 2 deletions src/assistant/Conversations/ChatConversation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useRef, useEffect } from 'react'
import { useQuery, isQueryLoading } from 'cozy-client'

import { buildChatConversationQueryById } from '../queries'
import { useAssistant } from '../AssistantProvider'
import { useAssistant, getInstantMessage } from '../AssistantProvider'
import ChatUserItem from './ChatUserItem'
import ChatAssistantItem from './ChatAssistantItem'
import ChatRealtimeAnswer from './ChatRealtimeAnswer'
Expand Down Expand Up @@ -67,7 +67,7 @@ const ChatConversation = ({ conversation, myself }) => {
{showLastConv && (
<ChatRealtimeAnswer
isLoading={assistantState.status === 'pending'}
label={assistantState.message}
label={getInstantMessage(assistantState)}
/>
)}
</div>
Expand Down