feat: enable copy instruction (#3401)

This commit is contained in:
Faisal Amir 2024-08-19 15:40:41 +07:00 committed by GitHub
parent 62c3a7c302
commit 6a3b438558
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 1 deletions

View 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

View File

@ -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)

View File

@ -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"