#AI
zhou zhou
5 天以前 51877df13075ad10ef51107f15bcd21f1661febe
rsf-admin/src/ai/AiChatWidget.jsx
@@ -32,6 +32,7 @@
const DRAWER_WIDTH = 720;
const SESSION_WIDTH = 220;
const DIAGNOSIS_MESSAGE = '请对当前WMS系统进行一次巡检诊断,结合库存、任务、设备站点数据识别异常并给出处理建议。';
const parseSseChunk = (chunk, onEvent) => {
    const blocks = chunk.split('\n\n');
@@ -269,14 +270,12 @@
        }
    };
    const handleSend = async () => {
        if (!draft.trim() || !activeSessionId || sending) {
    const streamChat = async ({ userContent, endpoint, sceneCode }) => {
        if (!userContent || !activeSessionId || sending) {
            return;
        }
        const userContent = draft.trim();
        const sessionId = activeSessionId;
        const modelCode = activeSession?.modelCode || models[0]?.code;
        setDraft('');
        setError('');
        setSending(true);
        setMessagesBySession((prev) => {
@@ -304,7 +303,7 @@
        let receivedDelta = false;
        try {
            const response = await fetch(`${PREFIX_BASE_URL}ai/chat/stream`, {
            const response = await fetch(`${PREFIX_BASE_URL}${endpoint}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
@@ -314,7 +313,8 @@
                body: JSON.stringify({
                    sessionId,
                    message: userContent,
                    modelCode
                    modelCode,
                    sceneCode
                }),
                signal: controller.signal
            });
@@ -392,6 +392,30 @@
            setSending(false);
            streamControllerRef.current = null;
        }
    };
    const handleSend = async () => {
        if (!draft.trim() || !activeSessionId || sending) {
            return;
        }
        const userContent = draft.trim();
        setDraft('');
        await streamChat({
            userContent,
            endpoint: 'ai/chat/stream',
            sceneCode: 'general_chat'
        });
    };
    const handleDiagnose = async () => {
        if (!activeSessionId || sending) {
            return;
        }
        await streamChat({
            userContent: DIAGNOSIS_MESSAGE,
            endpoint: 'ai/diagnose/stream',
            sceneCode: 'system_diagnose'
        });
    };
    const assistantReplyText = (messageList) => {
@@ -575,6 +599,15 @@
                                        </MenuItem>
                                    ))}
                                </Select>
                                <Button
                                    variant="outlined"
                                    size="small"
                                    disabled={sending || !activeSessionId}
                                    onClick={handleDiagnose}
                                    sx={{ borderRadius: 2, whiteSpace: 'nowrap' }}
                                >
                                    一键诊断
                                </Button>
                            </Stack>
                            <Divider />
                            <Box
@@ -609,8 +642,11 @@
                                            开始新的智能对话
                                        </Typography>
                                        <Typography variant="body2">
                                            可以直接提问仓储业务问题,或切换模型开始新的会话。
                                            可以直接提问仓储业务问题,或点击一键诊断快速巡检当前WMS状态。
                                        </Typography>
                                        <Button variant="outlined" onClick={handleDiagnose} disabled={sending || !activeSessionId}>
                                            一键诊断
                                        </Button>
                                    </Stack>
                                ) : (
                                    <Stack spacing={2}>
@@ -704,6 +740,15 @@
                                        label={activeSession?.modelCode || '未选择模型'}
                                        sx={{ bgcolor: 'rgba(25,118,210,0.08)', color: 'primary.main' }}
                                    />
                                    <Button
                                        variant="text"
                                        size="small"
                                        disabled={sending || !activeSessionId}
                                        onClick={handleDiagnose}
                                        sx={{ minWidth: 'auto', px: 1 }}
                                    >
                                        一键诊断
                                    </Button>
                                    <Typography variant="caption" color="text.secondary">
                                        `Enter` 发送,`Shift + Enter` 换行
                                    </Typography>
@@ -715,7 +760,7 @@
                                    maxRows={6}
                                    value={draft}
                                    onChange={(event) => setDraft(event.target.value)}
                                    placeholder="输入问题,支持多会话和模型切换"
                                    placeholder="输入问题,支持多会话、多模型和一键诊断"
                                    onKeyDown={(event) => {
                                        if (event.key === 'Enter' && !event.shiftKey) {
                                            event.preventDefault();