Skip to content
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
3 changes: 2 additions & 1 deletion plugins/inference-plugin/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let currentModelFile = null;
*/
interface InitModelResponse {
error?: any;
modelFile?: string;
}

/**
Expand All @@ -51,7 +52,7 @@ function initModel(modelFile: string): Promise<InitModelResponse> {
.then(validateModelStatus)
.catch((err) => {
log.error("error: " + JSON.stringify(err));
return { error: err };
return { error: err, modelFile };
})
);
}
Expand Down
5 changes: 3 additions & 2 deletions web/hooks/useActiveModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function useActiveModel() {
return
}

setActiveModel(undefined)

setStateModel({ state: 'start', loading: true, model: modelId })

const model = downloadedModels.find((e) => e.id === modelId)
Expand All @@ -52,15 +54,14 @@ export function useActiveModel() {
console.debug('Init model: ', modelId)
const path = join('models', model.name, modelId)
const res = await initModel(path)
if (res?.error && (!activeModel?.id || modelId === activeModel?.id)) {
if (res && res.error && res.modelFile === stateModel.model) {
const errorMessage = `${res.error}`
alert(errorMessage)
setStateModel(() => ({
state: 'start',
loading: false,
model: modelId,
}))
setActiveModel(undefined)
} else {
console.debug(
`Init model ${modelId} successfully!, take ${
Expand Down
1 change: 1 addition & 0 deletions web/hooks/useSendChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function useSendChatMessage() {
...newMessage,
messages: newMessage.messages?.slice(0, -1).concat([summaryMsg]),
})
.catch(console.error)
if (
currentConvo &&
currentConvo.id === newMessage.threadId &&
Expand Down
26 changes: 14 additions & 12 deletions web/screens/Chat/ChatInstruction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ const ChatInstruction = () => {
}
return (
<div className="mx-auto mb-20 flex flex-col space-y-2">
<p>
What does this Assistant do? How does it behave? What should it avoid
doing?
</p>
{!isSettingInstruction && (
<Button
themes={'outline'}
className="w-32"
onClick={() => setIsSettingInstruction(true)}
>
Give Instruction
</Button>
{!isSettingInstruction && activeConvoId && (
<>
<p>
What does this Assistant do? How does it behave? What should it
avoid doing?
</p>
<Button
themes={'outline'}
className="w-32"
onClick={() => setIsSettingInstruction(true)}
>
Give Instruction
</Button>
</>
)}
{isSettingInstruction && (
<div className="space-y-4">
Expand Down
12 changes: 1 addition & 11 deletions web/screens/Chat/HistoryList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { twMerge } from 'tailwind-merge'

import { useActiveModel } from '@/hooks/useActiveModel'
import { useCreateConversation } from '@/hooks/useCreateConversation'
import { useGetDownloadedModels } from '@/hooks/useGetDownloadedModels'
import useGetUserConversations from '@/hooks/useGetUserConversations'

import { displayDate } from '@/utils/datetime'
Expand All @@ -27,11 +26,10 @@ export default function HistoryList() {
const conversations = useAtomValue(userConversationsAtom)
const threadStates = useAtomValue(conversationStatesAtom)
const { getUserConversations } = useGetUserConversations()
const { activeModel, startModel } = useActiveModel()
const { activeModel } = useActiveModel()
const { requestCreateConvo } = useCreateConversation()
const activeConvoId = useAtomValue(getActiveConvoIdAtom)
const setActiveConvoId = useSetAtom(setActiveConvoIdAtom)
const { downloadedModels } = useGetDownloadedModels()

useEffect(() => {
getUserConversations()
Expand All @@ -48,14 +46,6 @@ export default function HistoryList() {
console.debug('modelId is undefined')
return
}
const model = downloadedModels.find((e) => e.id === convo.modelId)
if (convo == null) {
console.debug('modelId is undefined')
return
}
if (model != null) {
startModel(model.id)
}
if (activeConvoId !== convo.id) {
setActiveConvoId(convo.id)
}
Expand Down
39 changes: 31 additions & 8 deletions web/screens/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import { twMerge } from 'tailwind-merge'

import { currentPromptAtom } from '@/containers/Providers/Jotai'

import { FeatureToggleContext } from '@/context/FeatureToggle'
import ShortCut from '@/containers/Shortcut'

import { toaster } from '@/containers/Toast'

import { FeatureToggleContext } from '@/context/FeatureToggle'

import { MainViewState } from '@/constants/screens'

import { useActiveModel } from '@/hooks/useActiveModel'
Expand Down Expand Up @@ -64,6 +67,12 @@ const ChatScreen = () => {
const { experimentalFeatureEnabed } = useContext(FeatureToggleContext)

const textareaRef = useRef<HTMLTextAreaElement>(null)
const { startModel } = useActiveModel()
const modelRef = useRef(activeModel)

useEffect(() => {
modelRef.current = activeModel
}, [activeModel])

useEffect(() => {
getUserConversations()
Expand All @@ -81,6 +90,24 @@ const ChatScreen = () => {
}, [currentConvo, downloadedModels])

const handleSendMessage = async () => {
if (!activeModel || activeModel.id !== currentConvo?.modelId) {
const model = downloadedModels.find((e) => e.id === currentConvo?.modelId)

// Model is available to start
if (model != null) {
toaster({
title: 'Message queued.',
description: 'It will be sent once the model is done loading.',
})
startModel(model.id).then(() => {
setTimeout(() => {
if (modelRef?.current?.id === currentConvo?.modelId)
sendChatMessage()
}, 300)
})
}
return
}
if (activeConversationId) {
sendChatMessage()
} else {
Expand Down Expand Up @@ -206,20 +233,16 @@ const ChatScreen = () => {
ref={textareaRef}
onKeyDown={(e) => handleKeyDown(e)}
placeholder="Type your message ..."
disabled={
!activeModel ||
stateModel.loading ||
activeModel.id !== currentConvo?.modelId
}
disabled={stateModel.loading || !currentConvo}
value={currentPrompt}
onChange={(e) => {
handleMessageChange(e)
}}
/>
<Button
size="lg"
disabled={!activeModel || disabled || stateModel.loading}
themes={!activeModel ? 'secondary' : 'primary'}
disabled={disabled || stateModel.loading || !currentConvo}
themes={'primary'}
onClick={handleSendMessage}
>
Send
Expand Down