18586361686
2025-05-06 ddeefeda64d0e6206fcbdde181dac6d8172f4fc4
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import React, {useEffect, useState} from 'react';
 
import {useLayout} from '../../../hooks/useLayout.tsx';
import {Row} from 'antd/lib/index';
import {App, Avatar, Button, Col, Collapse, Modal, Select, Tooltip} from 'antd';
import Title from 'antd/es/typography/Title';
import {DeleteOutlined, PlusOutlined} from "@ant-design/icons";
import {useDetail, useGet, useList, usePostManual, useRemove, useSave, useUpdate} from "../../../hooks/useApis.ts";
import {useParams} from "react-router-dom";
import {WorkflowsModal} from "./Workflows.tsx";
import {DebouncedTextArea} from "../../../components/DebouncedTextArea";
import {LlmSlider} from "./LlmSlider.tsx";
import {useSse} from "../../../hooks/useSse.ts";
import {KnowledgeModal} from "./Knowledge.tsx";
import TextArea from "antd/es/input/TextArea";
import {getSessionId} from "../../../libs/getSessionId.ts";
import {AiProChat, ChatMessage} from "../../../components/AiProChat/AiProChat";
import {PluginModal} from "./Plugins.tsx";
 
const colStyle: React.CSSProperties = {
    background: '#fafafa',
    padding: '8px',
    border: "1px solid #eee",
    height: 'calc(100vh - 68px)',
    overflowY: 'auto'
};
 
 
const text = (
    <p style={{paddingInlineStart: 24}}>
    </p>
);
 
 
type CollapseLabelProps = {
    text: string,
    onClick: () => void,
    plusDisabled?: boolean
}
 
const CollapseLabel: React.FC<CollapseLabelProps> = ({text, onClick, plusDisabled = false}) => {
    return <div style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "space-between",
    }}>
        <span>{text}</span>
        {!plusDisabled && <PlusOutlined onClick={(event) => {
            event.stopPropagation();
            onClick?.();
        }}/>}
    </div>
}
 
 
export const ListItem: React.FC<{
    title?: string,
    description?: string | React.ReactNode,
    icon?: string,
    onButtonClick?: () => void
}> = ({title, description, icon = "/favicon.png", onButtonClick}) => {
    return (
        <div style={{
            display: "flex",
            gap: "20px",
            alignItems: "center",
            background: "#eaeaea",
            padding: "10px",
            borderRadius: "3px"
        }}>
            <div style={{"width": "40px"}}>
                <Avatar shape="square" src={icon || "/favicon.png"} />
            </div>
            <div style={{flexGrow: 1}}>
                <div>{title}</div>
                <div style={{color: "#999999"}}>
                    <Tooltip title={description}>
                        <div style={{
                            display: '-webkit-box',
                            WebkitLineClamp: 1,      // 限制显示行数
                            WebkitBoxOrient: 'vertical',
                            overflow: 'hidden',
                            textOverflow: 'ellipsis',
                        }}>
                            {description}
                        </div>
                    </Tooltip>
 
                </div>
            </div>
            <div>
                <Button type={"text"} onClick={() => {
                    onButtonClick?.()
                }} icon={<DeleteOutlined/>}/>
            </div>
        </div>
    )
}
 
 
const BotDesign: React.FC = () => {
 
    const [span] = useState(8)
    const params = useParams();
    const {result: detail, doGet: reGetDetail} = useDetail("aiBot", params.id);
    const {message} = App.useApp()
 
    const [systemPrompt, setSystemPrompt] = useState<string>('')
    const [welcomeMessage, setWelcomeMessage] = useState<string>('')
 
    const {setOptions} = useLayout();
    useEffect(() => {
        console.log('detail')
        console.log(detail)
        setOptions({
            leftMenuCollapsed: true, showBreadcrumb: false,
            headerLeftEl: <span style={{marginLeft: "20px"}}>{detail?.data?.title}</span>,
        })
        return () => {
            setOptions({leftMenuCollapsed: false, showBreadcrumb: true, headerLeftEl: ''})
        }
    }, [detail]);
 
    const {doUpdate} = useUpdate("aiBot");
 
    const updateBot = (values: any) => {
        doUpdate({
            data: {
                ...values,
                id: params.id,
            }
        }).then(reGetDetail)
            .then(() => {
                message.success("保存成功")
            })
    }
 
    const {doPost: updateBotLLMOptions} = usePostManual("/api/v1/aiBot/updateLlmOptions")
    const doUpdateBotLLMOptions = (values: any) => {
        updateBotLLMOptions({
            data: {
                llmOptions: values,
                id: params.id,
            }
        })
    }
 
    const {doPost: updateBotOptions} = usePostManual("/api/v1/aiBot/updateOptions")
    const doUpdateBotOptions = (values: any) => {
        updateBotOptions({
            data: {
                options: values,
                id: params.id,
            }
        }).then(reGetDetail)
            .then(() => {
                message.success("保存成功")
            })
    }
 
    useEffect(() => {
        setSystemPrompt(detail?.data?.llmOptions?.systemPrompt)
        setWelcomeMessage(detail?.data?.options?.welcomeMessage)
    }, [detail]);
 
    const {result: workflowResult, doGet: doGetWorkflow} = useList("aiBotWorkflow", {"botId": params.id});
    const {doSave: doSaveWorkflow} = useSave("aiBotWorkflow");
    const {doRemove: doRemoveAiBotWorkflow} = useRemove("aiBotWorkflow");
    const [workflowOpen, setWorkflowOpen] = useState(false)
 
    const {result: pluginResult, doGet: doGetPlugin} = useList("aiBotPlugins", {"botId": params.id});
    const {doSave: doSavePlugin} = useSave("aiBotPlugins");
    const {doRemove: doRemovePlugin} = useRemove("aiBotPlugins");
    const [pluginOpen, setPluginOpen] = useState(false)
 
 
    const {result: knowledgeResult, doGet: doGetKnowledge} = useList("aiBotKnowledge", {"botId": params.id});
    const {doSave: doSaveKnowledge} = useSave("aiBotKnowledge");
    const {doRemove: doRemoveAiBotKnowledge} = useRemove("aiBotKnowledge");
    const [knowledgeOpen, setKnowledgeOpen] = useState(false)
 
 
    const {result} = useGet("/api/v1/aiLlm/list", {supportFunctionCalling: true});
 
    const {start: startChat} = useSse("/api/v1/aiBot/chat");
 
    const [chats, setChats] = useState<ChatMessage[]>([]);
    const {result: messageResult} = useList("aiBotMessage", {
        botId: params.id,
        sessionId: getSessionId()
    });
    console.log('pluginResult')
    console.log(pluginResult)
    useEffect(() => {
        setChats(messageResult?.data)
    }, [messageResult]);
 
    return (
        <>
            <WorkflowsModal open={workflowOpen} onClose={() => setWorkflowOpen(false)}
                            onCancel={() => setWorkflowOpen(false)}
                            goToPage={"/ai/workflow"}
                            onSelectedItem={item => {
                                setWorkflowOpen(false)
                                doSaveWorkflow({
                                    data: {
                                        botId: params.id,
                                        workflowId: item.id,
                                    }
                                }).then(doGetWorkflow)
                            }}
            />
 
            <PluginModal open={pluginOpen} onClose={() => setPluginOpen(false)}
                         onCancel={() => setPluginOpen(false)}
                         goToPage={"/ai/plugins"}
                         onSelectedItem={item => {
                             setPluginOpen(false)
                             doSavePlugin({
                                 data: {
                                     botId: params.id,
                                     pluginId: item.id,
                                 }
                             }).then(doGetPlugin)
                         }}
            />
 
 
            <KnowledgeModal open={knowledgeOpen} onClose={() => setKnowledgeOpen(false)}
                            onCancel={() => setKnowledgeOpen(false)}
                            goToPage={"/ai/knowledge"}
                            onSelectedItem={item => {
                                setKnowledgeOpen(false)
                                doSaveKnowledge({
                                    data: {
                                        botId: params.id,
                                        knowledgeId: item.id,
                                    }
                                }).then(doGetKnowledge)
                            }}/>
            <div style={{height: 'calc(100vh - 68px)', display: "flex"}} className={"agentsflow"}>
                <Row style={{width: "100%"}}>
                    <Col span={span} style={colStyle}>
                        <Title level={5}>系统提示词(System Prompt)</Title>
                        <DebouncedTextArea value={systemPrompt} style={{height: "calc(100% - 30px)"}} autoSize={false}
                                           onChangeImmediately={(e) => {
                                               setSystemPrompt(e.target.value)
                                           }}
                                           onChange={e => {
                                               doUpdateBotLLMOptions({systemPrompt: e.target.value})
                                           }}/>
                    </Col>
                    <Col span={span} style={colStyle}>
                        <div style={{margin: "10px 0 5px", color: "#666"}}>大模型</div>
                        <div style={{
                            background: "#f5f5f5",
                            borderRadius: "3px",
                            padding: "10px 20px",
                            display: "flex",
                            flexDirection: "column",
                            gap: "10px"
                        }}>
                            <Select
                                style={{width: "100%"}}
                                placeholder="请选择大模型"
                                allowClear
                                fieldNames={{label: 'title', value: 'id'}}
                                options={[
                                    ...result?.data || []
                                ]}
                                value={detail?.data?.llmId}
                                onChange={(value: any) => {
                                    value = value || ''
                                    updateBot({llmId: value})
                                }}
                            />
                            <LlmSlider title="温度" min={0.1} max={1} step={0.1}
                                       defaultValue={detail?.data?.llmOptions?.temperature || 0.7}
                                       onChange={(value: number) => {
                                           doUpdateBotLLMOptions({temperature: value})
                                       }}/>
                            <LlmSlider title="TopK" min={1} max={10} step={1}
                                       defaultValue={detail?.data?.llmOptions?.topK || 4}
                                       onChange={(value: number) => {
                                           doUpdateBotLLMOptions({topK: value})
                                       }}/>
                            <LlmSlider title="TopP" min={0.1} max={1} step={0.1}
                                       defaultValue={detail?.data?.llmOptions?.topP || 0.7}
                                       onChange={(value: number) => {
                                           doUpdateBotLLMOptions({topP: value})
                                       }}/>
                            <LlmSlider title="最大回复长度" min={1024} max={10000}
                                       defaultValue={detail?.data?.llmOptions?.maxReplyLength || 2048}
                                       onChange={(value: number) => {
                                           doUpdateBotLLMOptions({maxReplyLength: value})
                                       }}/>
                            <LlmSlider title="携带历史条数" min={1} max={10} step={1}
                                       defaultValue={detail?.data?.llmOptions?.maxMessageCount || 3}
                                       onChange={(value: number) => {
                                           doUpdateBotLLMOptions({maxMessageCount: value})
                                       }}/>
                        </div>
 
                        <div style={{margin: "10px 0 5px", color: "#666"}}>技能</div>
                        <Collapse items={[
                            {
                                key: 'workflow',
                                label: <CollapseLabel text="工作流" onClick={() => {
                                    setWorkflowOpen(true)
                                }}/>,
                                children: <div>
                                    {workflowResult?.data?.map((item: any) => {
                                        return <ListItem key={item.id} title={item.workflow.title}
                                                         description={item.workflow.description}
                                                         icon={item.workflow.icon}
                                                         onButtonClick={() => {
                                                             Modal.confirm({
                                                                 title: '确定要删除该工作流吗?',
                                                                 content: '删除后,该工作流将不再关联该机器人,但工作流本身不会被删除。',
                                                                 onOk: () => {
                                                                     doRemoveAiBotWorkflow({
                                                                         data: {id: item.id}
                                                                     }).then(doGetWorkflow)
                                                                 }
                                                             })
                                                         }}
                                        />
                                    })}
                                </div>,
                            },
                            {
                                key: 'knowledge',
                                label: <CollapseLabel text="知识库" onClick={() => {
                                    setKnowledgeOpen(true)
                                }}/>,
                                children: <div>
                                    {knowledgeResult?.data?.map((item: any) => {
                                        return <ListItem key={item.id} title={item.knowledge.title}
                                                         description={item.knowledge.description}
                                                         icon={item.knowledge.icon}
                                                         onButtonClick={() => {
                                                             Modal.confirm({
                                                                 title: '确定要删除该知识库吗?',
                                                                 content: '删除后,该知识库将不再关联该机器人,但知识库本身不会被删除。',
                                                                 onOk: () => {
                                                                     doRemoveAiBotKnowledge({
                                                                         data: {id: item.id}
                                                                     }).then(doGetKnowledge)
                                                                 }
                                                             })
                                                         }}
                                        />
                                    })}
                                </div>,
                            },
                            {
                                key: 'plugins',
                                label: <CollapseLabel text="插件" onClick={() => {
                                    setPluginOpen(true)
                                }}/>,
                                children:
                                    <div>
                                        {pluginResult?.data?.map((item: any) => {
                                            return <ListItem key={item.id} title={item.aiPlugin.name}
                                                             description={item.aiPlugin.description}
                                                             icon={item.aiPlugin.icon}
                                                             onButtonClick={() => {
                                                                 Modal.confirm({
                                                                     title: '确定要删除该插件吗?',
                                                                     content: '删除后,该插件将不再关联该机器人,但插件本身不会被删除。',
                                                                     onOk: () => {
                                                                         doRemovePlugin({
                                                                             data: {id: item.id}
                                                                         }).then(doGetPlugin)
                                                                     }
                                                                 })
                                                             }}
                                            />
                                        })}
                                    </div>
                                ,
                            },
                        ]} bordered={false}/>
 
 
                        <div style={{margin: "20px 0 5px", color: "#666"}}>对话设置</div>
                        <Collapse items={[
                            {
                                key: 'questions',
                                label: <CollapseLabel text="问题预设" onClick={() => {
                                }}/>,
                                children: text,
                            },
                            {
                                key: 'welcomeMessage',
                                label: <CollapseLabel text="欢迎语" onClick={() => {
                                }} plusDisabled/>,
                                children: <div>
                                    <DebouncedTextArea
                                        value={welcomeMessage}
                                        onChange={(value) => {
                                            doUpdateBotOptions({welcomeMessage: value.target.value})
                                        }}
                                        onChangeImmediately={(event) => {
                                            setWelcomeMessage(event.target.value)
                                        }}
                                        placeholder="请输入欢迎语"
                                        autoSize={{minRows: 2, maxRows: 6}}
                                    />
                                </div>,
                            },
                            {
                                key: 'ui',
                                label: <CollapseLabel text="UI 与 Logo" onClick={() => {
                                }} plusDisabled/>,
                                children: text,
                            },
 
                        ]} bordered={false}/>
 
 
                        <div style={{margin: "20px 0 5px", color: "#666"}}>发布</div>
                        <Collapse items={[
                            {
                                key: 'embed',
                                label: <CollapseLabel text="嵌入" onClick={() => {
                                }} plusDisabled/>,
                                children: <div>
                                    <div>
                                        <span>
                                            外部访问地址 <a
                                            href={window.location.href.substring(0, window.location.href.indexOf('/ai')) + '/ai/externalBot/' + detail?.data.id}
                                            target={"_blank"}>打开</a>
                                        </span>
                                        <TextArea readOnly disabled
                                                  value={window.location.href.substring(0, window.location.href.indexOf('/ai')) + '/bot/chat/' + detail?.data.id}></TextArea>
                                    </div>
                                </div>,
                            },
                            {
                                key: 'api',
                                label: <CollapseLabel text="API" onClick={() => {
                                }} plusDisabled/>,
                                children: text,
                            },
                        ]} bordered={false}/>
 
                    </Col>
 
 
                    <Col span={span} style={colStyle}>
                        <Title level={5}>预览</Title>
                        <div style={{height: "calc(100vh - 122px)", width: "100%"}} className={"bot-chat"}>
                            <AiProChat
                                chats={chats}
                                onChatsChange={setChats} // 确保正确传递 onChatsChange
                                // style={{ height: '600px' }}
                                helloMessage={detail?.data?.options?.welcomeMessage}
                                request={async (messages) => {
                                    const readableStream = new ReadableStream({
                                        async start(controller) {
                                            const encoder = new TextEncoder();
                                            startChat({
                                                data: {
                                                    botId: params.id,
                                                    sessionId: getSessionId(),
                                                    prompt: messages[messages.length - 1].content as string,
                                                },
                                                onMessage: (msg) => {
                                                    controller.enqueue(encoder.encode(msg))
                                                },
                                                onFinished: () => {
                                                    controller.close()
                                                }
                                            })
                                        },
                                    });
                                    return new Response(readableStream);
                                }}
                            />
                        </div>
                    </Col>
                </Row>
            </div>
        </>
    );
};
 
export default {
    path: "/ai/bot/design/:id",
    element: BotDesign
};