| | import { Constants } from 'librechat-data-provider'; |
| | import { atomFamily, useRecoilCallback } from 'recoil'; |
| | import type { TEphemeralAgent } from 'librechat-data-provider'; |
| | import { logger } from '~/utils'; |
| |
|
| | export const ephemeralAgentByConvoId = atomFamily<TEphemeralAgent | null, string>({ |
| | key: 'ephemeralAgentByConvoId', |
| | default: null, |
| | effects: [ |
| | ({ onSet, node }) => { |
| | onSet(async (newValue) => { |
| | const conversationId = node.key.split('__')[1]?.replaceAll('"', ''); |
| | logger.log('agents', 'Setting ephemeral agent:', { conversationId, newValue }); |
| | }); |
| | }, |
| | ] as const, |
| | }); |
| |
|
| | export function useUpdateEphemeralAgent() { |
| | const updateEphemeralAgent = useRecoilCallback( |
| | ({ set }) => |
| | (convoId: string, agent: TEphemeralAgent | null) => { |
| | set(ephemeralAgentByConvoId(convoId), agent); |
| | }, |
| | [], |
| | ); |
| |
|
| | return updateEphemeralAgent; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function useApplyNewAgentTemplate() { |
| | const applyTemplate = useRecoilCallback( |
| | ({ snapshot, set }) => |
| | async ( |
| | targetId: string, |
| | _sourceId: string | null = Constants.NEW_CONVO, |
| | ephemeralAgentState?: TEphemeralAgent | null, |
| | ) => { |
| | const sourceId = _sourceId || Constants.NEW_CONVO; |
| | logger.log('agents', `Attempting to apply template from "${sourceId}" to "${targetId}"`); |
| |
|
| | if (targetId === sourceId) { |
| | logger.warn('agents', `Attempted to apply template to itself ("${sourceId}"). Skipping.`); |
| | return; |
| | } |
| |
|
| | try { |
| | |
| | |
| | const agentTemplate = |
| | ephemeralAgentState ?? (await snapshot.getPromise(ephemeralAgentByConvoId(sourceId))); |
| |
|
| | |
| | if (agentTemplate) { |
| | logger.log('agents', `Applying agent template to "${targetId}":`, agentTemplate); |
| | |
| | set(ephemeralAgentByConvoId(targetId), agentTemplate); |
| | } else { |
| | |
| | logger.warn( |
| | 'agents', |
| | `Agent template from "${sourceId}" is null or unset. Setting agent for "${targetId}" to null.`, |
| | ); |
| | |
| | set(ephemeralAgentByConvoId(targetId), null); |
| | |
| | |
| | } |
| | } catch (error) { |
| | logger.error( |
| | 'agents', |
| | `Error applying agent template from "${sourceId}" to "${targetId}":`, |
| | error, |
| | ); |
| | set(ephemeralAgentByConvoId(targetId), null); |
| | } |
| | }, |
| | [], |
| | ); |
| |
|
| | return applyTemplate; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | export function useGetEphemeralAgent() { |
| | const getEphemeralAgent = useRecoilCallback( |
| | ({ snapshot }) => |
| | (conversationId: string): TEphemeralAgent | null => { |
| | logger.log('agents', `[useGetEphemeralAgent] Getting loadable for ID: ${conversationId}`); |
| | const agentLoadable = snapshot.getLoadable(ephemeralAgentByConvoId(conversationId)); |
| | return agentLoadable.contents as TEphemeralAgent | null; |
| | }, |
| | [], |
| | ); |
| |
|
| | return getEphemeralAgent; |
| | } |
| |
|