zhou zhou
2 天以前 1724f77c35022b643c28dd3e5547679a5edc2d49
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
import React, { useState } from "react";
import {
    List,
    SearchInput,
    TopToolbar,
    SelectColumnsButton,
    EditButton,
    FilterButton,
    TextInput,
    DateInput,
    SelectInput,
    useNotify,
    useRefresh,
    useListContext,
    Pagination,
    DeleteButton,
} from 'react-admin';
import { Box, Button, Chip, Grid, Stack, Typography } from '@mui/material';
import EmptyData from "@/page/components/EmptyData";
import MyCreateButton from "@/page/components/MyCreateButton";
import MyExportButton from '@/page/components/MyExportButton';
import { OPERATE_MODE, DEFAULT_PAGE_SIZE } from '@/config/setting';
import AiRouteCreate from "./AiRouteCreate";
import request from "@/utils/request";
import { AiConsoleLayout, AiConsolePanel, aiCardSx } from "@/page/components/AiConsoleLayout";
 
const routeChoices = [
    { id: 'general_chat', name: '通用对话' },
    { id: 'system_diagnose', name: '系统诊断' },
];
 
const filters = [
    <SearchInput source="condition" alwaysOn />,
    <DateInput label='common.time.after' source="timeStart" alwaysOn />,
    <DateInput label='common.time.before' source="timeEnd" alwaysOn />,
    <SelectInput source="routeCode" label="路由编码" choices={routeChoices} />,
    <TextInput source="modelCode" label="模型编码" />,
    <SelectInput label="common.field.status" source="status" choices={[
        { id: '1', name: 'common.enums.statusTrue' },
        { id: '0', name: 'common.enums.statusFalse' },
    ]} />,
];
 
const RouteCardActions = ({ record }) => {
    const refresh = useRefresh();
    const notify = useNotify();
 
    const handleToggle = async () => {
        try {
            await request.post('/ai/route/toggle', { id: record.id, status: record.status === 1 ? 0 : 1 });
            notify('common.response.success');
            refresh();
        } catch (error) {
            notify(error.message || '操作失败', { type: 'error' });
        }
    };
 
    const handleReset = async () => {
        try {
            await request.post('/ai/route/reset', { id: record.id });
            notify('common.response.success');
            refresh();
        } catch (error) {
            notify(error.message || '操作失败', { type: 'error' });
        }
    };
 
    return (
        <Stack direction="row" spacing={1} flexWrap="wrap">
            <EditButton record={record} sx={{ px: 1.25, py: 0.5, minWidth: 64 }} />
            <DeleteButton record={record} sx={{ px: 1.25, py: 0.5, minWidth: 64 }} mutationMode={OPERATE_MODE} />
            <Button size="small" variant="outlined" onClick={handleToggle}>{record.status === 1 ? '停用' : '启用'}</Button>
            <Button size="small" variant="outlined" onClick={handleReset}>重置</Button>
        </Stack>
    );
};
 
const RouteBoard = () => {
    const { data, isLoading } = useListContext();
    const records = data || [];
    const enabledCount = records.filter((item) => item.status === 1).length;
    const coolingCount = records.filter((item) => item.cooldownUntil).length;
    const failSwitchCount = records.filter((item) => (item.failCount || 0) > 0).length;
    const successCount = records.reduce((sum, item) => sum + (item.successCount || 0), 0);
 
    if (!isLoading && !records.length) {
        return <EmptyData />;
    }
 
    return (
        <AiConsoleLayout
            title="AI模型路由"
            subtitle="参考 zy-wcs-master 的 LLM 控制台信息层次,突出路由状态、冷却与成功失败计数,但保留现有系统的按钮、筛选和表单风格。"
            stats={[
                { label: '总路由', value: records.length },
                { label: '启用', value: enabledCount },
                { label: '冷却中', value: coolingCount },
                { label: '累计成功', value: successCount, helper: `已有失败记录 ${failSwitchCount} 条` },
            ]}
        >
            <AiConsolePanel
                title="路由卡片"
                subtitle="每张卡片代表一条模型候选,优先级越小越先命中;启停、重置会立即作用于后续请求。"
                minHeight={460}
            >
                <Grid container spacing={2}>
                    {records.map((record) => (
                        <Grid item xs={12} md={6} xl={4} key={record.id}>
                            <Box sx={aiCardSx(record.status === 1)}>
                                <Stack direction="row" justifyContent="space-between" alignItems="flex-start" spacing={1}>
                                    <Box>
                                        <Typography variant="subtitle1" sx={{ fontWeight: 700, color: '#284059' }}>
                                            {record.modelCode}
                                        </Typography>
                                        <Typography variant="caption" sx={{ color: '#8093a8' }}>
                                            {record.routeCode} · 优先级 {record.priority}
                                        </Typography>
                                    </Box>
                                    <Stack direction="row" spacing={0.75} flexWrap="wrap" justifyContent="flex-end">
                                        <Chip size="small" color={record.status === 1 ? 'success' : 'default'} label={record.status === 1 ? '启用' : '停用'} />
                                        {record.cooldownUntil ? <Chip size="small" color="warning" label="冷却中" /> : null}
                                    </Stack>
                                </Stack>
                                <Grid container spacing={1.25} sx={{ mt: 1 }}>
                                    <Grid item xs={6}>
                                        <Typography variant="caption" sx={{ color: '#70839a' }}>失败次数</Typography>
                                        <Typography variant="h6" sx={{ color: '#2f455c', mt: 0.25 }}>{record.failCount || 0}</Typography>
                                    </Grid>
                                    <Grid item xs={6}>
                                        <Typography variant="caption" sx={{ color: '#70839a' }}>成功次数</Typography>
                                        <Typography variant="h6" sx={{ color: '#2f455c', mt: 0.25 }}>{record.successCount || 0}</Typography>
                                    </Grid>
                                    <Grid item xs={12}>
                                        <Typography variant="caption" sx={{ color: '#70839a' }}>冷却截止</Typography>
                                        <Typography variant="body2" sx={{ mt: 0.25, color: '#31465d' }}>
                                            {record.cooldownUntil$ || '未进入冷却'}
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12}>
                                        <Typography variant="caption" sx={{ color: '#70839a' }}>备注</Typography>
                                        <Typography variant="body2" sx={{ mt: 0.25, color: '#31465d', minHeight: 42 }}>
                                            {record.memo || '未填写备注'}
                                        </Typography>
                                    </Grid>
                                </Grid>
                                <Box sx={{ mt: 1.5 }}>
                                    <RouteCardActions record={record} />
                                </Box>
                            </Box>
                        </Grid>
                    ))}
                </Grid>
                <Box sx={{ mt: 2 }}>
                    <Pagination rowsPerPageOptions={[DEFAULT_PAGE_SIZE, 25, 50]} />
                </Box>
            </AiConsolePanel>
        </AiConsoleLayout>
    );
};
 
const AiRouteList = () => {
    const [createDialog, setCreateDialog] = useState(false);
 
    return (
        <Box display="flex" sx={{ width: '100%' }}>
            <List
                sx={{ width: '100%', flexGrow: 1 }}
                title={"menu.aiRoute"}
                empty={<EmptyData onClick={() => { setCreateDialog(true) }} />}
                filters={filters}
                sort={{ field: "priority", order: "asc" }}
                actions={(
                    <TopToolbar>
                        <FilterButton />
                        <MyCreateButton onClick={() => { setCreateDialog(true) }} />
                        <SelectColumnsButton preferenceKey='aiRoute' />
                        <MyExportButton />
                    </TopToolbar>
                )}
                perPage={DEFAULT_PAGE_SIZE}
                pagination={false}
            >
                <RouteBoard />
            </List>
            <AiRouteCreate open={createDialog} setOpen={setCreateDialog} />
        </Box>
    );
}
 
export default AiRouteList;