Skip to content

Commit

Permalink
fix: thread chat cache
Browse files Browse the repository at this point in the history
  • Loading branch information
levanion committed Mar 18, 2024
1 parent b5cf8d8 commit c3bc959
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions apps/ui/src/hooks/useApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const useApollo = () => {
return new ApolloClient({
link: apolloLink,
cache: new InMemoryCache(),
connectToDevTools: true,

defaultOptions: {
watchQuery: {
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/modals/AIChatModal/components/ChatV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ChatV2 = ({ chatSessionId }: { chatSessionId?: string }) => {

const agentId = urlParams.get('agent')
const teamId = urlParams.get('team')
const chatId = urlParams.get('chat') || chatSessionId
const chatId = urlParams.get('chat') || urlParams.get('session') || chatSessionId

const { thinking, setThinking, socket } = useChatState()

Expand Down
4 changes: 3 additions & 1 deletion apps/ui/src/modals/AIChatModal/hooks/useChatSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const useChatSocket = ({ userId, createdChatId }: ChatSocketProps) => {

const agentId = urlParams.get('agent')
const teamId = urlParams.get('team')
const chatId = urlParams.get('chat') || createdChatId || ''
const chatId = urlParams.get('chat') || urlParams.get('session') || createdChatId || ''

const groupId = getSessionId({
user,
Expand All @@ -46,6 +46,7 @@ const useChatSocket = ({ userId, createdChatId }: ChatSocketProps) => {

const response = await fetch(url)
const data = await response.json()

return data.url
}, [user?.id])

Expand All @@ -68,6 +69,7 @@ const useChatSocket = ({ userId, createdChatId }: ChatSocketProps) => {
onUserTypingEvent(e)
}
if (data.type === 'CHAT_MESSAGE_ADDED') {
console.log('socket', data)
upsertChatMessageInCache(data.chat_message, {
agentId: data.agent_id,
teamId: data.team_id,
Expand Down
1 change: 1 addition & 0 deletions apps/ui/src/modals/AIChatModal/hooks/useUpdateChatCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const useUpdateChatCache = () => {
isNil,
)
}

apolloClient.cache.updateQuery(
{ query: CHAT_MESSAGES_GQL, variables: queryVariables },
data => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const AgentSessionsTable = ({ agentId }: { agentId: string }) => {
/>
{sessionId && (
<ChatContextProvider>
<ChatV2 chatSessionId={sessionId} />
<ChatV2 />
</ChatContextProvider>
)}
</StyledChatWrapper>
Expand Down
33 changes: 18 additions & 15 deletions apps/ui/src/services/chat/useChatMessagesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ type UseChatMessagesService = {
chatId?: Nullable<string>
}

export const useChatMessagesService = ({
agentId,
teamId,
chatId,
}: UseChatMessagesService) => {
const { data, error, loading, refetch } = useQuery(CHAT_MESSAGES_GQL, {
// Omit undefined variables to exclude in query params
variables: omitBy(
export const useChatMessagesService = ({ agentId, teamId, chatId }: UseChatMessagesService) => {
let queryVariables = omitBy(
{
agent_id: agentId,
team_id: teamId,
chat_id: chatId,
},
isNil,
)
if (chatId) {
queryVariables = omitBy(
{
agent_id: agentId,
team_id: teamId,
chat_id: chatId,
},
isNil,
),
)
}

const { data, error, loading, refetch } = useQuery(CHAT_MESSAGES_GQL, {
// Omit undefined variables to exclude in query params
variables: queryVariables,
})

return {
Expand All @@ -37,10 +43,7 @@ export const useChatMessagesService = ({
}
}

export const useChatMessagesHistoryService = ({
agentId,
teamId,
}: UseChatMessagesService) => {
export const useChatMessagesHistoryService = ({ agentId, teamId }: UseChatMessagesService) => {
const { data, error, loading, refetch } = useQuery(CHAT_MESSAGES_HISTORY_GQL, {
// Omit undefined variables to exclude in query params
variables: omitBy(
Expand Down

0 comments on commit c3bc959

Please sign in to comment.