Skip to content

Commit e9c6d4e

Browse files
lethemanhlethemanh
authored andcommitted
feat: Insert assistant ID into conversation ✨
1 parent d4c8fbc commit e9c6d4e

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

packages/cozy-search/src/components/Conversations/ConversationComposer.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ConversationComposer = () => {
1818
const { isMobile } = useBreakpoints()
1919
const composerRuntime = useComposerRuntime()
2020
const isRunning = useThread(state => state.isRunning)
21+
const isThreadEmpty = useThread(state => state.messages.length === 0)
2122
const { setOpenedKnowledgePanel } = useAssistant()
2223

2324
const value = useComposer(state => state.text)
@@ -59,7 +60,9 @@ const ConversationComposer = () => {
5960
/>
6061

6162
<div className="u-flex u-flex-items-center u-flex-justify-between u-mt-1">
62-
{flag('cozy.create-assistant.enabled') && <AssistantSelection />}
63+
{flag('cozy.create-assistant.enabled') && (
64+
<AssistantSelection disabled={!isThreadEmpty} />
65+
)}
6366
{flag('cozy.source-knowledge.enabled') && (
6467
<TwakeKnowledgeSelector
6568
onSelectTwakeKnowledge={setOpenedKnowledgePanel}

packages/cozy-search/src/components/Conversations/ConversationListItem.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import React from 'react'
33
import { useI18n } from 'twake-i18n'
44

55
import flag from 'cozy-flags'
6-
import Icon from 'cozy-ui/transpiled/react/Icon'
7-
import AssistantIcon from 'cozy-ui/transpiled/react/Icons/Assistant'
86
import ListItem from 'cozy-ui/transpiled/react/ListItem'
97
import ListItemText from 'cozy-ui/transpiled/react/ListItemText'
108
import Typography from 'cozy-ui/transpiled/react/Typography'
119

1210
import ConversationActions from './ConversationActions'
1311
import styles from './styles.styl'
12+
import AssistantAvatar from '../Assistant/AssistantAvatar'
1413
import {
1514
formatConversationDate,
1615
getDescriptionOfConversation,
@@ -62,7 +61,11 @@ const ConversationListItem = ({
6261
variant="h6"
6362
className="u-flex u-flex-items-center u-coolGrey"
6463
>
65-
<Icon icon={AssistantIcon} className="u-mr-half" />
64+
<AssistantAvatar
65+
assistant={conversation.assistant}
66+
className="u-mr-half"
67+
isSmall
68+
/>
6669
{formatConversationDate(
6770
conversation.cozyMetadata?.updatedAt,
6871
t,

packages/cozy-search/src/components/Conversations/ConversationListItemWider.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import React from 'react'
33
import { useI18n } from 'twake-i18n'
44

55
import flag from 'cozy-flags'
6-
import Icon from 'cozy-ui/transpiled/react/Icon'
7-
import ImageIcon from 'cozy-ui/transpiled/react/Icons/Image'
86
import ListItem from 'cozy-ui/transpiled/react/ListItem'
97
import ListItemIcon from 'cozy-ui/transpiled/react/ListItemIcon'
108
import ListItemText from 'cozy-ui/transpiled/react/ListItemText'
119
import Typography from 'cozy-ui/transpiled/react/Typography'
1210

1311
import ConversationActions from './ConversationActions'
1412
import styles from './styles.styl'
13+
import AssistantAvatar from '../Assistant/AssistantAvatar'
1514
import {
1615
formatConversationDate,
1716
getDescriptionOfConversation,
@@ -39,7 +38,11 @@ const ConversationListItemWider = ({
3938
selected={selected}
4039
>
4140
<ListItemIcon>
42-
<Icon icon={ImageIcon} />
41+
<AssistantAvatar
42+
assistant={conversation.assistant}
43+
className="u-mr-half"
44+
isSmall
45+
/>
4346
</ListItemIcon>
4447
<ListItemText
4548
primary={

packages/cozy-search/src/components/CozyAssistantRuntimeProvider.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
CHAT_CONVERSATIONS_DOCTYPE,
3535
buildChatConversationQueryById
3636
} from './queries'
37+
import { useAssistant } from './AssistantProvider'
38+
import { DEFAULT_ASSISTANT } from './constants'
3739

3840
interface ConversationMessage {
3941
id: string
@@ -45,6 +47,7 @@ interface ConversationMessage {
4547
interface Conversation {
4648
_id: string
4749
messages?: ConversationMessage[]
50+
assistant_id?: string
4851
}
4952

5053
interface CozyAssistantRuntimeProviderProps {
@@ -73,6 +76,7 @@ const ConversationLoader = ({
7376
}: CozyAssistantRuntimeProviderProps & {
7477
conversationId: string
7578
}): JSX.Element | null => {
79+
const { setSelectedAssistantId } = useAssistant()
7680
const conversationQuery = buildChatConversationQueryById(conversationId)
7781
const queryResult = useQuery(
7882
conversationQuery.definition,
@@ -86,6 +90,10 @@ const ConversationLoader = ({
8690
[conversation?.messages]
8791
)
8892

93+
useEffect(() => {
94+
setSelectedAssistantId(conversation?.assistant_id || DEFAULT_ASSISTANT.id)
95+
}, [conversation?.assistant_id, setSelectedAssistantId])
96+
8997
if (isLoading) {
9098
return (
9199
<div className="u-flex u-flex-items-center u-flex-justify-center u-h-100 u-w-100">
@@ -119,6 +127,11 @@ const CozyAssistantRuntimeProviderInner = ({
119127
const messagesIdRef = useRef<string[]>([])
120128
const cancelledMessageIdsRef = useRef<Set<string>>(new Set())
121129
const currentStreamingMessageIdRef = useRef<string | null>(null)
130+
const { selectedAssistantId } = useAssistant()
131+
132+
useEffect(() => {
133+
cancelledMessageIdsRef.current.clear()
134+
}, [conversationId])
122135

123136
useEffect(() => {
124137
messagesIdRef.current = initialMessages
@@ -223,11 +236,12 @@ const CozyAssistantRuntimeProviderInner = ({
223236
typeof createCozyRealtimeChatAdapter
224237
>[0]['client'],
225238
conversationId,
226-
streamBridge: streamBridgeRef.current
239+
streamBridge: streamBridgeRef.current,
240+
assistantId: selectedAssistantId
227241
},
228242
t
229243
),
230-
[client, conversationId, t]
244+
[client, conversationId, selectedAssistantId, t]
231245
)
232246

233247
const runtime = useLocalRuntime(adapter, {

packages/cozy-search/src/components/adapters/CozyRealtimeChatAdapter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface CozyRealtimeChatAdapterOptions {
3131
client: CozyClient
3232
conversationId: string
3333
streamBridge: StreamBridge
34+
assistantId?: string
3435
}
3536

3637
/**
@@ -66,7 +67,7 @@ export const createCozyRealtimeChatAdapter = (
6667
messages,
6768
abortSignal
6869
}: ChatModelRunOptions): AsyncGenerator<ChatModelRunResult> {
69-
const { client, conversationId, streamBridge } = options
70+
const { client, conversationId, streamBridge, assistantId } = options
7071

7172
const userQuery = findUserQuery(messages)
7273
if (!userQuery) {
@@ -81,7 +82,7 @@ export const createCozyRealtimeChatAdapter = (
8182
await client.stackClient.fetchJSON(
8283
'POST',
8384
`/ai/chat/conversations/${conversationId}`,
84-
{ q: userQuery }
85+
{ q: userQuery, assistantId }
8586
)
8687

8788
let fullText = ''

packages/cozy-search/src/components/queries.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const buildChatConversationsQuery = () => {
7171
.where({})
7272
.indexFields(['cozyMetadata.updatedAt'])
7373
.sortBy([{ 'cozyMetadata.updatedAt': 'desc' }])
74+
.include(['assistant'])
7475
.limitBy(50),
7576
options: {
7677
as: CHAT_CONVERSATIONS_DOCTYPE + '/recent',

0 commit comments

Comments
 (0)