StrategyList.tsx
7.69 KB
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
import { useEffect, useState } from 'react';
import { Table, Button, Card, Space, Tag, Modal, message, Input } from 'antd';
import { PlusOutlined, EditOutlined, StopOutlined, CheckCircleOutlined, ReloadOutlined, DeleteOutlined } from '@ant-design/icons';
import { useStrategyStore } from '../../stores/strategyStore';
import { useProviderStore } from '../../stores/providerStore';
import { useAccountStore } from '../../stores/accountStore';
import StrategyForm from './StrategyForm';
import type { Strategy } from '../../types';
const { Search } = Input;
const STRATEGY_COLORS: Record<string, string> = {
retry: 'orange',
degrade: 'purple',
timeout: 'red',
};
function StrategyTag({ label, enabled, value }: { label: string; enabled: boolean; value?: string }) {
return (
<Tag
color={enabled ? STRATEGY_COLORS[label] || 'default' : 'default'}
className="strategy-tag"
>
<span className={`strategy-tag-dot ${enabled ? 'on' : 'off'}`} />
{label === 'retry' ? '重试' : label === 'degrade' ? '降级' : '超时'}
{enabled && value && (
<span className="strategy-tag-value">{value}</span>
)}
</Tag>
);
}
export default function StrategyList() {
const { strategies, total, page, pageSize, loading, fetchStrategies, toggleStrategyEnabled, deleteStrategy }
= useStrategyStore();
const { providers, fetchProviders } = useProviderStore();
const { accounts, fetchAccounts } = useAccountStore();
const [modalOpen, setModalOpen] = useState(false);
const [editingStrategy, setEditingStrategy] = useState<Strategy | null>(null);
useEffect(() => {
fetchStrategies();
fetchProviders();
fetchAccounts();
}, [fetchStrategies, fetchProviders, fetchAccounts]);
const getAccountName = (accountId: string) => {
return accounts.find((a) => a.id === accountId)?.account_name || accountId;
};
const getProviderName = (accountId: string) => {
const acct = accounts.find((a) => a.id === accountId);
if (!acct) return '';
return providers.find((p) => p.id === acct.provider_id)?.display_name || '';
};
const handleEdit = (record: Strategy) => {
setEditingStrategy(record);
setModalOpen(true);
};
const handleCreate = () => {
setEditingStrategy(null);
setModalOpen(true);
};
const handleToggleStatus = async (record: Strategy) => {
const newStatus = !record.enabled;
const actionText = newStatus ? '启用' : '停用';
Modal.confirm({
title: `${actionText}确认`,
content: `确定要${actionText}策略「${record.strategy_name}」吗?`,
okText: actionText,
cancelText: '取消',
onOk: async () => {
try {
await toggleStrategyEnabled(record.id, newStatus);
message.success(`${actionText}成功`);
} catch {
message.error(`${actionText}失败`);
}
},
});
};
const handleDelete = (record: Strategy) => {
Modal.confirm({
title: '删除确认',
content: `确定要删除策略「${record.strategy_name}」吗?此操作不可恢复。`,
okText: '删除',
okType: 'danger',
cancelText: '取消',
onOk: async () => {
try {
await deleteStrategy(record.id);
message.success('删除成功');
} catch {
message.error('删除失败');
}
},
});
};
const handleSearch = (value: string) => {
fetchStrategies({ keyword: value || undefined });
};
const renderStatus = (val: boolean) => (
<Tag className="status-tag" color={val ? 'success' : 'default'}>
<span className={`status-dot ${val ? 'active' : 'disabled'}`} />
{val ? '启用' : '停用'}
</Tag>
);
const renderStrategyRow = (_: string, record: Strategy) => (
<Space size={4} wrap>
<StrategyTag
label="retry"
enabled={record.retry_enabled}
value={record.retry_enabled ? `${record.retry_max_attempts}次` : undefined}
/>
<StrategyTag
label="degrade"
enabled={record.degrade_enabled}
/>
<StrategyTag
label="timeout"
enabled={record.timeout_enabled}
value={record.timeout_enabled ? `${(record.timeout_connect_ms ?? 0) / 1000}s` : undefined}
/>
{record.max_concurrency && (
<Tag color="cyan" className="strategy-tag" title="并发限制">
<span className="strategy-tag-dot on" />
并发{record.max_concurrency}
</Tag>
)}
</Space>
);
const columns = [
{
title: '策略名称',
dataIndex: 'strategy_name',
key: 'strategy_name',
width: 140,
render: (val: string, record: Strategy) => (
<Space>
<span style={{ fontWeight: 500 }}>{val}</span>
</Space>
),
},
{
title: '关联账号',
dataIndex: 'account_id',
key: 'account_id',
width: 160,
render: (_: string, record: Strategy) => (
<Space direction="column" size={2} style={{ gap: 0 }}>
<span style={{ fontSize: 13 }}>{getAccountName(record.account_id)}</span>
{getProviderName(record.account_id) && (
<Tag style={{ fontSize: 11, margin: 0 }}>{getProviderName(record.account_id)}</Tag>
)}
</Space>
),
},
{
title: '策略配置',
key: 'strategy_config',
render: renderStrategyRow,
},
{
title: '状态',
dataIndex: 'enabled',
key: 'enabled',
width: 90,
render: renderStatus,
},
{
title: '操作',
key: 'action',
width: 180,
render: (_: unknown, record: Strategy) => (
<Space>
<Button type="link" size="small" icon={<EditOutlined />} onClick={() => handleEdit(record)} className="action-btn">
编辑
</Button>
<Button
type="link"
size="small"
icon={record.enabled ? <StopOutlined /> : <CheckCircleOutlined />}
onClick={() => handleToggleStatus(record)}
className="action-btn"
>
{record.enabled ? '停用' : '启用'}
</Button>
<Button type="link" size="small" danger icon={<DeleteOutlined />} onClick={() => handleDelete(record)} className="action-btn">
删除
</Button>
</Space>
),
},
];
return (
<>
<div className="page-header">
<div className="page-header-left">
<h4 className="page-header-title">调用策略管理</h4>
<span className="page-header-desc">为模型账号配置调用策略(重试 / 降级 / 超时 / 并发)</span>
</div>
<div className="page-header-actions">
<Search
placeholder="搜索策略..."
allowClear
onSearch={handleSearch}
style={{ width: 240 }}
className="page-search"
/>
<Button icon={<ReloadOutlined />} onClick={() => fetchStrategies()} className="action-btn" />
<Button type="primary" icon={<PlusOutlined />} onClick={handleCreate} className="action-btn">
新增策略
</Button>
</div>
</div>
<Card className="page-card" styles={{ body: { padding: 0 } }}>
<Table
dataSource={strategies}
columns={columns}
rowKey="id"
loading={loading}
className="page-table"
pagination={{
current: page,
pageSize,
total,
showSizeChanger: true,
showTotal: (t) => `共 ${t} 条`,
onChange: (p, ps) => fetchStrategies({ page: p, page_size: ps }),
}}
/>
</Card>
<StrategyForm
open={modalOpen}
strategy={editingStrategy}
onClose={() => setModalOpen(false)}
/>
</>
);
}