File size: 2,684 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { useCallback } from 'react';
import type { TStartupConfig, TSubmission } from 'librechat-data-provider';
import { useUpdateEphemeralAgent, useApplyNewAgentTemplate } from '~/store/agents';
import { getModelSpec, applyModelSpecEphemeralAgent } from '~/utils';

/**
 * Hook that applies a model spec from a preset to an ephemeral agent.
 * This is used when initializing a new conversation with a preset that has a spec.
 */
export function useApplyModelSpecEffects() {
  const updateEphemeralAgent = useUpdateEphemeralAgent();
  const applyModelSpecEffects = useCallback(
    ({
      convoId,
      specName,
      startupConfig,
    }: {
      convoId: string | null;
      specName?: string | null;
      startupConfig?: TStartupConfig;
    }) => {
      if (specName == null || !specName) {
        return;
      }

      const modelSpec = getModelSpec({
        specName,
        startupConfig,
      });

      applyModelSpecEphemeralAgent({
        convoId,
        modelSpec,
        updateEphemeralAgent,
      });
    },
    [updateEphemeralAgent],
  );

  return applyModelSpecEffects;
}

export function useApplyAgentTemplate() {
  const applyAgentTemplate = useApplyNewAgentTemplate();
  /**
   * Helper function to apply agent template with model spec merged into ephemeral agent
   */
  const applyAgentTemplateWithSpec = useCallback(
    ({
      targetId,
      sourceId,
      ephemeralAgent,
      specName,
      startupConfig,
    }: {
      targetId: string;
      sourceId?: TSubmission['conversation']['conversationId'] | null;
      ephemeralAgent: TSubmission['ephemeralAgent'];
      specName?: string | null;
      startupConfig?: TStartupConfig;
    }) => {
      if (!specName) {
        applyAgentTemplate(targetId, sourceId, ephemeralAgent);
        return;
      }

      const modelSpec = getModelSpec({
        specName,
        startupConfig,
      });

      if (!modelSpec) {
        applyAgentTemplate(targetId, sourceId, ephemeralAgent);
        return;
      }

      // Merge model spec fields into ephemeral agent
      const mergedAgent = {
        ...ephemeralAgent,
        mcp: [...(ephemeralAgent?.mcp ?? []), ...(modelSpec.mcpServers ?? [])],
        web_search: ephemeralAgent?.web_search ?? modelSpec.webSearch ?? false,
        file_search: ephemeralAgent?.file_search ?? modelSpec.fileSearch ?? false,
        execute_code: ephemeralAgent?.execute_code ?? modelSpec.executeCode ?? false,
      };

      // Deduplicate MCP servers
      mergedAgent.mcp = [...new Set(mergedAgent.mcp)];

      applyAgentTemplate(targetId, sourceId, mergedAgent);
    },
    [applyAgentTemplate],
  );

  return applyAgentTemplateWithSpec;
}