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

fix: send message button state reset on stop #4341

Merged
merged 2 commits into from
Dec 26, 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
15 changes: 15 additions & 0 deletions web/helpers/atoms/Thread.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@
* Get the active thread state
*/
export const activeThreadStateAtom = atom<ThreadState | undefined>((get) => {
const threadId = get(activeThreadIdAtom)
if (!threadId) {
console.debug('Active thread id is undefined')
return undefined

Check warning on line 93 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

90-93 lines are not covered with tests
}

return get(threadStatesAtom)[threadId]

Check warning on line 96 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

96 line is not covered with tests
})

/**
Expand All @@ -107,7 +107,7 @@
return undefined
}

return get(threadModelParamsAtom)[threadId]

Check warning on line 110 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

110 line is not covered with tests
}
)
/// End Active Thread Atom
Expand All @@ -130,19 +130,19 @@
*/
export const createNewThreadAtom = atom(null, (get, set, newThread: Thread) => {
// create thread state for this new thread
const currentState = { ...get(threadStatesAtom) }

Check warning on line 133 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

133 line is not covered with tests

const threadState: ThreadState = {

Check warning on line 135 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

135 line is not covered with tests
hasMore: false,
waitingForResponse: false,
lastMessage: undefined,
}
currentState[newThread.id] = threadState
set(threadStatesAtom, currentState)

Check warning on line 141 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

140-141 lines are not covered with tests

// add the new thread on top of the thread list to the state
const threads = get(threadsAtom)
set(threadsAtom, [newThread, ...threads])

Check warning on line 145 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

144-145 lines are not covered with tests
})

/**
Expand All @@ -163,16 +163,31 @@
export const updateThreadWaitingForResponseAtom = atom(
null,
(get, set, threadId: string, waitingForResponse: boolean) => {
const currentState = { ...get(threadStatesAtom) }
currentState[threadId] = {

Check warning on line 167 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

166-167 lines are not covered with tests
...currentState[threadId],
waitingForResponse,
error: undefined,
}
set(threadStatesAtom, currentState)

Check warning on line 172 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

172 line is not covered with tests
}
)

/**
* Reset the thread waiting for response state
*/
export const resetThreadWaitingForResponseAtom = atom(null, (get, set) => {
const currentState = { ...get(threadStatesAtom) }
Object.keys(currentState).forEach((threadId) => {
currentState[threadId] = {

Check warning on line 182 in web/helpers/atoms/Thread.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

180-182 lines are not covered with tests
...currentState[threadId],
waitingForResponse: false,
error: undefined,
}
})
set(threadStatesAtom, currentState)
})

/**
* Update the thread last message
*/
Expand Down
19 changes: 18 additions & 1 deletion web/hooks/useActiveModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { LAST_USED_MODEL_ID } from './useRecommendedModel'
import { vulkanEnabledAtom } from '@/helpers/atoms/AppConfig.atom'
import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import {
isGeneratingResponseAtom,
resetThreadWaitingForResponseAtom,
} from '@/helpers/atoms/Thread.atom'

export const activeModelAtom = atom<Model | undefined>(undefined)
export const loadModelErrorAtom = atom<string | undefined>(undefined)
Expand All @@ -34,6 +38,10 @@ export function useActiveModel() {
const pendingModelLoad = useRef(false)
const isVulkanEnabled = useAtomValue(vulkanEnabledAtom)
const activeAssistant = useAtomValue(activeAssistantAtom)
const setGeneratingResponse = useSetAtom(isGeneratingResponseAtom)
const resetThreadWaitingForResponseState = useSetAtom(
resetThreadWaitingForResponseAtom
)

const downloadedModelsRef = useRef<Model[]>([])

Expand Down Expand Up @@ -139,6 +147,8 @@ export function useActiveModel() {
return

const engine = EngineManager.instance().get(stoppingModel.engine)
setGeneratingResponse(false)
resetThreadWaitingForResponseState()
return engine
?.unloadModel(stoppingModel)
.catch((e) => console.error(e))
Expand All @@ -148,7 +158,14 @@ export function useActiveModel() {
pendingModelLoad.current = false
})
},
[activeModel, setStateModel, setActiveModel, stateModel]
[
activeModel,
setStateModel,
setActiveModel,
stateModel,
setGeneratingResponse,
resetThreadWaitingForResponseState,
]
)

const stopInference = useCallback(async () => {
Expand Down
1 change: 1 addition & 0 deletions web/hooks/useDeleteThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function useDeleteThread() {
if (thread) {
const updatedThread = {
...thread,
title: 'New Thread',
metadata: {
...thread.metadata,
title: 'New Thread',
Expand Down
4 changes: 1 addition & 3 deletions web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useEffect, useRef, useState } from 'react'

import { InferenceEngine, MessageStatus } from '@janhq/core'

Check warning on line 4 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-macos

'MessageStatus' is defined but never used

Check warning on line 4 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'MessageStatus' is defined but never used

Check warning on line 4 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'MessageStatus' is defined but never used

Check warning on line 4 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'MessageStatus' is defined but never used

import { TextArea, Button, Tooltip, useClickOutside, Badge } from '@janhq/joi'
import { useAtom, useAtomValue } from 'jotai'
Expand Down Expand Up @@ -50,7 +50,7 @@
const ChatInput = () => {
const activeThread = useAtomValue(activeThreadAtom)
const { stateModel } = useActiveModel()
const messages = useAtomValue(getCurrentChatMessagesAtom)

Check warning on line 53 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-macos

'messages' is assigned a value but never used

Check warning on line 53 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'messages' is assigned a value but never used

Check warning on line 53 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'messages' is assigned a value but never used

Check warning on line 53 in web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'messages' is assigned a value but never used
const spellCheck = useAtomValue(spellCheckAtom)

const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom)
Expand Down Expand Up @@ -302,9 +302,7 @@
</div>
)}

{messages[messages.length - 1]?.status !== MessageStatus.Pending &&
!isGeneratingResponse &&
!isStreamingResponse ? (
{!isGeneratingResponse && !isStreamingResponse ? (
<>
{currentPrompt.length !== 0 && (
<Button
Expand Down
Loading