summaryrefslogtreecommitdiff
path: root/extension/react-app/src/components/ModelSelect.tsx
blob: faa64f69f5ae7e6abd4be5f18a113c18cb8a626c (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import styled from "styled-components";
import {
  defaultBorderRadius,
  lightGray,
  secondaryDark,
  vscBackground,
  vscForeground,
} from ".";
import { useContext } from "react";
import { GUIClientContext } from "../App";
import { RootStore } from "../redux/store";
import { useDispatch, useSelector } from "react-redux";
import { ArrowLeftIcon, PlusIcon } from "@heroicons/react/24/outline";
import { setDialogMessage, setShowDialog } from "../redux/slices/uiStateSlice";
import { useNavigate } from "react-router-dom";

const MODEL_INFO: { title: string; class: string; args: any }[] = [
  {
    title: "Ollama",
    class: "Ollama",
    args: {
      model: "codellama",
    },
  },
  {
    title: "llama.cpp",
    class: "LlamaCpp",
    args: {},
  }
];

const GridDiv = styled.div`
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  border: 0.5px solid ${lightGray};
  border-radius: ${defaultBorderRadius};
  overflow: hidden;
`;

const Select = styled.select`
  border: none;
  max-width: 50vw; //changed the width
  background-color: ${vscBackground};
  color: ${vscForeground};
  padding: 6px;
  max-height: 35vh;
  overflow: scroll;
  cursor: pointer;

  &:focus {
    outline: none;
  }
  &:hover {
    background-color: ${secondaryDark};
  }
`;

const StyledPlusIcon = styled(PlusIcon)`
  cursor: pointer;
  margin: 0px;
  padding-left: 4px;
  padding-right: 4px;
  height: 100%;

  &:hover {
    background-color: ${secondaryDark};
  }
  border-left: 0.5px solid ${lightGray};
`;

const NewProviderDiv = styled.div`
  cursor: pointer;
  padding: 8px;
  padding-left: 16px;
  padding-right: 16px;
  border-top: 0.5px solid ${lightGray};

  &:hover {
    background-color: ${secondaryDark};
  }
`;

function modelSelectTitle(model: any): string {
  if (model?.title) return model?.title;
  if (model?.model !== undefined && model?.model.trim() !== "") {
    if (model?.class_name) {
      return `${model?.class_name} - ${model?.model}`;
    }
    return model?.model;
  }
  return model?.class_name;
}

function ModelSelect(props: {}) {
  const dispatch = useDispatch();
  const client = useContext(GUIClientContext);
  const defaultModel = useSelector(
    (state: RootStore) => (state.serverState.config as any)?.models?.default
  );
  const savedModels = useSelector(
    (state: RootStore) => (state.serverState.config as any)?.models?.saved
  );

  const navigate = useNavigate();

  return (
    <GridDiv>
      <Select
        value={JSON.stringify({
          t: "default",
          idx: -1,
        })}
        defaultValue={0}
        onChange={(e) => {
          const value = JSON.parse(e.target.value);
          if (value.t === "saved") {
            client?.setModelForRoleFromIndex("*", value.idx);
          }
        }}
      >
        {!defaultModel && !savedModels && (
          <option
            value={JSON.stringify({
              t: "default",
              idx: -1,
            })}
          >
            Ollama
          </option>
        )}
        {defaultModel && (
          <option
            value={JSON.stringify({
              t: "default",
              idx: -1,
            })}
          >
            {modelSelectTitle(defaultModel)}
          </option>
        )}
        {savedModels?.map((model: any, idx: number) => {
          return (
            <option
              value={JSON.stringify({
                t: "saved",
                idx,
              })}
            >
              {modelSelectTitle(model)}
            </option>
          );
        })}
      </Select>

      <StyledPlusIcon
        width="1.3em"
        height="1.3em"
        onClick={() => {
          navigate("/models");
        }}
      />
    </GridDiv>
  );
}

export default ModelSelect;