feat: enable copy instruction (#3401)
This commit is contained in:
parent
62c3a7c302
commit
6a3b438558
38
web/containers/CopyInstruction/index.tsx
Normal file
38
web/containers/CopyInstruction/index.tsx
Normal file
@ -0,0 +1,38 @@
|
||||
import { ChangeEvent, useCallback } from 'react'
|
||||
|
||||
import { Switch } from '@janhq/joi'
|
||||
import { useAtom } from 'jotai'
|
||||
import { atomWithStorage } from 'jotai/utils'
|
||||
|
||||
const COPY_OVER_INSTRUCTION_ENABLED = 'copy_over_instruction_enabled'
|
||||
|
||||
export const copyOverInstructionEnabledAtom = atomWithStorage(
|
||||
COPY_OVER_INSTRUCTION_ENABLED,
|
||||
false
|
||||
)
|
||||
|
||||
const CopyOverInstruction: React.FC = () => {
|
||||
const [copyOverInstructionEnabled, setCopyOverInstructionEnabled] = useAtom(
|
||||
copyOverInstructionEnabledAtom
|
||||
)
|
||||
|
||||
const onSwitchToggled = useCallback(
|
||||
(e: ChangeEvent<HTMLInputElement>) => {
|
||||
setCopyOverInstructionEnabled(e.target.checked)
|
||||
},
|
||||
[setCopyOverInstructionEnabled]
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="my-2 flex w-full flex-row items-center justify-center gap-x-2">
|
||||
<h6 className="flex-1 font-medium">Save instructions for new threads</h6>
|
||||
<Switch
|
||||
checked={copyOverInstructionEnabled}
|
||||
onChange={onSwitchToggled}
|
||||
className="flex-shrink-0"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CopyOverInstruction
|
||||
@ -12,6 +12,7 @@ import {
|
||||
} from '@janhq/core'
|
||||
import { atom, useAtomValue, useSetAtom } from 'jotai'
|
||||
|
||||
import { copyOverInstructionEnabledAtom } from '@/containers/CopyInstruction'
|
||||
import { fileUploadAtom } from '@/containers/Providers/Jotai'
|
||||
|
||||
import { generateThreadId } from '@/utils/thread'
|
||||
@ -31,6 +32,7 @@ import {
|
||||
updateThreadAtom,
|
||||
setThreadModelParamsAtom,
|
||||
isGeneratingResponseAtom,
|
||||
activeThreadAtom,
|
||||
} from '@/helpers/atoms/Thread.atom'
|
||||
|
||||
const createNewThreadAtom = atom(null, (get, set, newThread: Thread) => {
|
||||
@ -57,6 +59,10 @@ export const useCreateNewThread = () => {
|
||||
const setFileUpload = useSetAtom(fileUploadAtom)
|
||||
const setSelectedModel = useSetAtom(selectedModelAtom)
|
||||
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
|
||||
const copyOverInstructionEnabled = useAtomValue(
|
||||
copyOverInstructionEnabledAtom
|
||||
)
|
||||
const activeThread = useAtomValue(activeThreadAtom)
|
||||
|
||||
const experimentalEnabled = useAtomValue(experimentalFeatureEnabledAtom)
|
||||
const setIsGeneratingResponse = useSetAtom(isGeneratingResponseAtom)
|
||||
@ -105,6 +111,10 @@ export const useCreateNewThread = () => {
|
||||
: {}
|
||||
|
||||
const createdAt = Date.now()
|
||||
let instructions: string | undefined = undefined
|
||||
if (copyOverInstructionEnabled) {
|
||||
instructions = activeThread?.assistants[0]?.instructions ?? undefined
|
||||
}
|
||||
const assistantInfo: ThreadAssistantInfo = {
|
||||
assistant_id: assistant.id,
|
||||
assistant_name: assistant.name,
|
||||
@ -116,7 +126,7 @@ export const useCreateNewThread = () => {
|
||||
{ ...defaultModel?.parameters, ...overriddenParameters } ?? {},
|
||||
engine: defaultModel?.engine,
|
||||
},
|
||||
instructions: assistant.instructions,
|
||||
instructions,
|
||||
}
|
||||
|
||||
const threadId = generateThreadId(assistant.id)
|
||||
|
||||
@ -14,6 +14,7 @@ import { useAtom, useAtomValue, useSetAtom } from 'jotai'
|
||||
|
||||
import { Settings2Icon } from 'lucide-react'
|
||||
|
||||
import CopyOverInstruction from '@/containers/CopyInstruction'
|
||||
import EngineSetting from '@/containers/EngineSetting'
|
||||
import ModelDropdown from '@/containers/ModelDropdown'
|
||||
|
||||
@ -212,6 +213,7 @@ const ThreadRightPanel = () => {
|
||||
onChange={onAssistantInstructionChanged}
|
||||
/>
|
||||
</div>
|
||||
{experimentalFeature && <CopyOverInstruction />}
|
||||
<div>
|
||||
<label
|
||||
id="assistant-instructions"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user