jan/web/helpers/atoms/Thread.atom.ts
NamH ec9b5bf682
fix: update new api from cortex to support 0.5.0 (#3221)
* fix: update new api from cortex to support 0.5.0

Signed-off-by: James <namnh0122@gmail.com>

* fix stop button for streaming

Signed-off-by: James <namnh0122@gmail.com>

* fix stop inference for nonstreaming

Signed-off-by: James <namnh0122@gmail.com>

* chore: remove umami prevent tracking call to vercel

Signed-off-by: James <namnh0122@gmail.com>

* add warning modal when running more than 2 model concurrently

Signed-off-by: James <namnh0122@gmail.com>

* fix: skip summarize if abort

Signed-off-by: James <namnh0122@gmail.com>

* 0.5.0-3

* add inference error popup

Signed-off-by: James <namnh0122@gmail.com>

* add back import local model

Signed-off-by: James <namnh0122@gmail.com>

* fix: max token issue (#3225)

Signed-off-by: James <namnh0122@gmail.com>

* format status

Signed-off-by: James <namnh0122@gmail.com>

* fix migration missing instructions

Signed-off-by: James <namnh0122@gmail.com>

* fix: wait for cortex process overlay should be on top (#3224)

* fix: wait for cortex process overlay should be on top

* chore: update cortex.js

* Cortex 0.5.0-5

* add import model to my model screen

Signed-off-by: James <namnh0122@gmail.com>

* fix: should migrate symlink models (#3226)

* fix import on windows (#3229)

Signed-off-by: James <namnh0122@gmail.com>

* fix yarn lint

Signed-off-by: James <namnh0122@gmail.com>

* fix: clean up port before start jan (#3232)

Signed-off-by: James <namnh0122@gmail.com>

---------

Signed-off-by: James <namnh0122@gmail.com>
Co-authored-by: Van Pham <64197333+Van-QA@users.noreply.github.com>
Co-authored-by: Louis <louis@jan.ai>
2024-08-02 09:37:04 +07:00

118 lines
3.0 KiB
TypeScript

import { ModelRuntimeParams, ModelSettingParams, Thread } from '@janhq/core'
import { atom } from 'jotai'
import {
downloadedModelsAtom,
getSelectedModelAtom,
updateSelectedModelAtom,
} from './Model.atom'
const threadIdShouldAnimateTitle = atom<string[]>([])
export const getThreadIdsShouldAnimateTitleAtom = atom((get) =>
get(threadIdShouldAnimateTitle)
)
export const addThreadIdShouldAnimateTitleAtom = atom(
null,
(_get, set, threadId: string) => {
set(threadIdShouldAnimateTitle, (current) => [...current, threadId])
}
)
/**
* Stores the current active thread id.
*/
const activeThreadIdAtom = atom<string | undefined>(undefined)
export const getActiveThreadIdAtom = atom((get) => get(activeThreadIdAtom))
export const setActiveThreadIdAtom = atom(
null,
(get, set, threadId: string | undefined) => {
const thread = get(threadsAtom).find((t) => t.id === threadId)
if (!thread) {
console.error(`Thread ${threadId} not found in state`)
return
}
set(activeThreadIdAtom, threadId)
const modelId = thread.assistants[0]?.model
if (!modelId) {
console.error(`No model id ${modelId} found in thread`, thread)
return
}
const activeModelId = get(getSelectedModelAtom)?.model
if (activeModelId === modelId) {
console.debug('Model already selected:', modelId)
return
}
const model = get(downloadedModelsAtom).find((m) => m.model === modelId)
if (!model) {
console.warn(`Model ${modelId} removed or deleted`)
return
}
console.debug('Set selected model:', model)
set(updateSelectedModelAtom, model)
}
)
export const isLoadingModelAtom = atom<boolean | undefined>(undefined)
export const isGeneratingResponseAtom = atom<boolean>(false)
/**
* Stores all threads for the current user
*/
export const threadsAtom = atom<Thread[]>([])
export const deleteThreadAtom = atom(null, (_get, set, threadId: string) => {
set(threadsAtom, (threads) => {
// set active thread to the latest
const allThreads = threads.filter((c) => c.id !== threadId)
if (allThreads.length > 0) {
const latestThread = allThreads[0]
set(activeThreadIdAtom, latestThread.id)
}
return allThreads
})
})
export const activeThreadAtom = atom<Thread | undefined>((get) =>
get(threadsAtom).find((c) => c.id === get(getActiveThreadIdAtom))
)
export const updateThreadTitleAtom = atom(
null,
(_get, set, threadId: string, title: string) => {
set(
threadsAtom,
(threads) =>
threads.map((t) =>
t.id === threadId ? { ...t, title } : t
) as Thread[]
)
}
)
/**
* Store model params at thread level settings
*/
export const threadModelParamsAtom = atom<Record<string, ModelParams>>({})
export type ModelParams = ModelRuntimeParams | ModelSettingParams
export const setThreadModelParamsAtom = atom(
null,
(get, set, threadId: string, params: ModelParams) => {
const currentState = { ...get(threadModelParamsAtom) }
currentState[threadId] = params
set(threadModelParamsAtom, currentState)
}
)