-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserLLMProviderDetail.tsx
338 lines (314 loc) · 10.8 KB
/
UserLLMProviderDetail.tsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import React, { useState, useEffect } from 'react';
import {
Card,
Typography,
Button,
Space,
Table,
Tag,
Modal,
message,
Popconfirm,
Tooltip,
Descriptions,
Divider
} from 'antd';
import {
EditOutlined,
DeleteOutlined,
PlusOutlined,
CheckCircleOutlined,
StopOutlined,
StarOutlined,
StarFilled,
} from '@ant-design/icons';
import { LLMModel, LLMProvider } from '../../models/llm';
import LLMModelForm from './LLMModelForm';
import {
fetchUserProviderModels,
createUserProviderModel,
updateUserProviderModel,
deleteUserProviderModel
} from '../../services/llmService';
const { Title } = Typography;
interface UserLLMProviderDetailProps {
userId: string;
provider: LLMProvider;
onBackToList: () => void;
onProviderUpdated: () => void;
}
const UserLLMProviderDetail: React.FC<UserLLMProviderDetailProps> = ({
userId,
provider,
onBackToList,
}) => {
const [models, setModels] = useState<LLMModel[]>([]);
const [loading, setLoading] = useState(false);
const [isAddModelModalVisible, setIsAddModelModalVisible] = useState(false);
const [isEditModelModalVisible, setIsEditModelModalVisible] = useState(false);
const [editingModel, setEditingModel] = useState<LLMModel | null>(null);
const [actionLoading, setActionLoading] = useState(false);
const fetchModels = React.useCallback(async () => {
if (!provider?.id) return
if (!userId) return
setLoading(true);
try {
const data = await fetchUserProviderModels(userId, provider.id);
setModels(data || []);
} catch (error: unknown) {
console.error('Error fetching models:', error);
message.error('Failed to load models');
} finally {
setLoading(false);
}
}, [provider?.id, userId]);
useEffect(() => {
fetchModels();
}, [fetchModels]);
const handleAddModel = async (values: any) => {
setActionLoading(true);
try {
await createUserProviderModel(userId, provider.id, values);
message.success('Model added successfully');
setIsAddModelModalVisible(false);
fetchModels();
} catch (error: unknown) {
console.error('Error adding model:', error);
message.error('Failed to add model');
} finally {
setActionLoading(false);
}
};
const handleEditModel = async (values: any) => {
if (!editingModel) return;
setActionLoading(true);
try {
await updateUserProviderModel(userId, provider.id, editingModel.id, values);
message.success('Model updated successfully');
setIsEditModelModalVisible(false);
fetchModels();
} catch (error: unknown) {
console.error('Error updating model:', error);
message.error('Failed to update model');
} finally {
setActionLoading(false);
}
};
const handleDeleteModel = async (modelId: string) => {
setActionLoading(true);
try {
await deleteUserProviderModel(userId, provider.id, modelId);
message.success('Model deleted successfully');
fetchModels();
} catch (error: unknown) {
console.error('Error deleting model:', error);
message.error('Failed to delete model');
} finally {
setActionLoading(false);
}
};
const getModelTypeTag = (type: string) => {
let color = '';
let label = type;
switch (type) {
case 'chat':
color = 'green';
label = 'Chat';
break;
case 'text':
color = 'blue';
label = 'Text';
break;
case 'embedding':
color = 'purple';
label = 'Embedding';
break;
case 'image':
color = 'orange';
label = 'Image';
break;
default:
color = 'default';
break;
}
return <Tag color={color}>{label}</Tag>;
};
const columns = [
{
title: 'Model',
key: 'name',
render: (record: LLMModel) => (
<Space direction="vertical" size={0}>
<Space>
{record.isDefault && (
<Tooltip title="Default Model for this type">
<StarFilled style={{ color: '#faad14' }} />
</Tooltip>
)}
<Typography.Text strong>
{record.displayName || record.name}
</Typography.Text>
</Space>
<Typography.Text type="secondary" style={{ fontSize: '12px' }}>
{record.name}
</Typography.Text>
</Space>
),
},
{
title: 'Type',
dataIndex: 'modelType',
key: 'modelType',
render: getModelTypeTag,
},
{
title: 'Context Window',
dataIndex: 'contextWindow',
key: 'contextWindow',
render: (value: number) => value ? `${value.toLocaleString()} tokens` : '-',
},
{
title: 'Status',
key: 'status',
render: (record: LLMModel) => (
<Tag color={record.isActive ? 'success' : 'error'} icon={record.isActive ? <CheckCircleOutlined /> : <StopOutlined />}>
{record.isActive ? 'Active' : 'Inactive'}
</Tag>
),
},
{
title: 'Actions',
key: 'actions',
render: (record: LLMModel) => (
<Space size="small">
<Tooltip title="Edit Model">
<Button
type="text"
icon={<EditOutlined />}
onClick={() => {
setEditingModel(record);
setIsEditModelModalVisible(true);
}}
/>
</Tooltip>
{!record.isDefault && (
<Tooltip title="Set as Default for this type">
<Button
type="text"
icon={<StarOutlined />}
onClick={() => handleSetDefault(record)}
disabled={actionLoading}
/>
</Tooltip>
)}
<Popconfirm
title="Delete this model?"
description="This action cannot be undone."
onConfirm={() => handleDeleteModel(record.id)}
okText="Delete"
cancelText="Cancel"
okButtonProps={{ danger: true }}
>
<Button type="text" danger icon={<DeleteOutlined />} />
</Popconfirm>
</Space>
),
},
];
const handleSetDefault = async (model: LLMModel) => {
setActionLoading(true);
try {
await updateUserProviderModel(userId, provider.id, model.id, {
isDefault: true
});
message.success('Default model updated');
fetchModels();
} catch (error: unknown) {
console.error('Error setting default model:', error);
message.error('Failed to update default model');
} finally {
setActionLoading(false);
}
};
return (
<div>
<div style={{ marginBottom: 16 }}>
<Button onClick={onBackToList} style={{ marginRight: 16 }}>
Back to Providers
</Button>
<Title level={3}>{provider.name} Models</Title>
</div>
<Card>
<Descriptions title="Provider Details" bordered column={1}>
<Descriptions.Item label="Type">
{provider.providerType.toUpperCase()}
</Descriptions.Item>
<Descriptions.Item label="Endpoint URL">
{provider.endpointUrl}
</Descriptions.Item>
<Descriptions.Item label="Status">
<Tag color={provider.isActive ? 'success' : 'error'}>
{provider.isActive ? 'Active' : 'Inactive'}
</Tag>
</Descriptions.Item>
<Descriptions.Item label="Description">
{provider.description || 'No description'}
</Descriptions.Item>
</Descriptions>
</Card>
<Divider />
<Card
title={<Title level={4}>Models</Title>}
extra={
<Button
type="primary"
icon={<PlusOutlined />}
onClick={() => setIsAddModelModalVisible(true)}
>
Add Model
</Button>
}
>
<Table
dataSource={models}
columns={columns}
rowKey="id"
loading={loading}
pagination={false}
/>
</Card>
{/* Add Model Modal */}
<Modal
title="Add New Model"
open={isAddModelModalVisible}
onCancel={() => setIsAddModelModalVisible(false)}
footer={null}
width={700}
>
<LLMModelForm
providerId={provider.id}
onSubmit={handleAddModel}
isLoading={actionLoading}
/>
</Modal>
{/* Edit Model Modal */}
{editingModel && (
<Modal
title="Edit Model"
open={isEditModelModalVisible}
onCancel={() => setIsEditModelModalVisible(false)}
footer={null}
width={700}
>
<LLMModelForm
initialValues={editingModel}
providerId={provider.id}
onSubmit={handleEditModel}
isLoading={actionLoading}
/>
</Modal>
)}
</div>
);
};
export default UserLLMProviderDetail;