File size: 2,305 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
import {
  isAgentsEndpoint,
  tQueryParamsSchema,
  isAssistantsEndpoint,
} from 'librechat-data-provider';
import type { TConversation, TPreset } from 'librechat-data-provider';
import { isEphemeralAgent } from '~/common';

const allowedParams = Object.keys(tQueryParamsSchema.shape);
export default function createChatSearchParams(
  input: TConversation | TPreset | Record<string, string> | null,
): URLSearchParams {
  if (input == null) {
    return new URLSearchParams();
  }

  const params = new URLSearchParams();

  if (input && typeof input === 'object' && !('endpoint' in input) && !('model' in input)) {
    Object.entries(input as Record<string, string>).forEach(([key, value]) => {
      if (value != null && allowedParams.includes(key)) {
        params.set(key, value);
      }
    });
    return params;
  }

  const conversation = input as TConversation | TPreset;
  const endpoint = conversation.endpoint;
  if (conversation.spec) {
    return new URLSearchParams({ spec: conversation.spec });
  }
  if (
    isAgentsEndpoint(endpoint) &&
    conversation.agent_id &&
    !isEphemeralAgent(conversation.agent_id)
  ) {
    return new URLSearchParams({ agent_id: String(conversation.agent_id) });
  } else if (isAssistantsEndpoint(endpoint) && conversation.assistant_id) {
    return new URLSearchParams({ assistant_id: String(conversation.assistant_id) });
  } else if (isAgentsEndpoint(endpoint) && !conversation.agent_id) {
    return params;
  } else if (isAssistantsEndpoint(endpoint) && !conversation.assistant_id) {
    return params;
  }

  if (endpoint) {
    params.set('endpoint', endpoint);
  }
  if (conversation.model) {
    params.set('model', conversation.model);
  }

  const paramMap: Record<string, any> = {};
  allowedParams.forEach((key) => {
    if (key === 'agent_id' && isEphemeralAgent(conversation.agent_id)) {
      return;
    }
    if (key !== 'endpoint' && key !== 'model') {
      paramMap[key] = (conversation as any)[key];
    }
  });

  return Object.entries(paramMap).reduce((params, [key, value]) => {
    if (value != null) {
      if (Array.isArray(value)) {
        params.set(key, key === 'stop' ? value.join(',') : JSON.stringify(value));
      } else {
        params.set(key, String(value));
      }
    }

    return params;
  }, params);
}