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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
<!-- 订单计划、周期分析页面 -->
<template>
<div
class="plan-cycle-analysis"
:style="{ height: `${analysisContainerHeight}px` }"
>
<div
class="ai-mcp-container"
v-loading="operationLoading"
element-loading-text="请稍后..."
>
<!-- 提问 -->
<div class="sticky-question">
<el-input
v-model="question"
type="textarea"
:autosize="{ minRows: 5, maxRows: 10 }"
placeholder="请输入分析问题,如: 帮我看看 CSI25153160CA 这个订单的情况"
:disabled="isRunning"
/>
<div class="input-footer">
<div class="input-footer-left">
<el-button
icon="el-icon-connection"
circle
@click="showSkillContent"
:loading="isRunning"
/>
<span style="margin-left: 5px; color: #606266; cursor: text">
已选择 {{ skill }}
</span>
</div>
<div class="input-footer-right">
<el-button
v-if="isRunning"
type="danger"
round
@click="stopIntervalAndReset"
>
停止
</el-button>
<el-button
type="primary"
plain
round
icon="el-icon-data-analysis"
@click="startAnalysis"
:loading="isRunning"
:disabled="!question.trim()"
>
{{ isRunning ? '运行中...' : '开始分析' }}
</el-button>
</div>
</div>
</div>
<!-- 分析过程和结果 -->
<div v-if="!error && taskId" class="result-wrapper">
<!-- 任务概览信息 -->
<el-card shadow="never" class="info-card">
<div class="task-info">
<div class="info-item">
<span class="label">任务ID:</span>
<span class="value mono">{{ taskId }}</span>
</div>
<div class="info-item">
<span class="label">调用技能:</span>
<span class="value mono">{{ skill }}</span>
</div>
<div class="info-item">
<span class="label">耗时:</span>
<span class="value mono" v-if="elapsedSeconds">
{{ elapsedSeconds.toFixed(1) }}s
</span>
</div>
<div class="info-item">
<span class="label">状态:</span>
<el-tag size="small" :type="statusTagType" effect="dark">
{{ statusText }}
</el-tag>
</div>
</div>
</el-card>
<!-- 分析步骤时间线 -->
<el-card shadow="never" class="timeline-card">
<div slot="header" class="card-header timeline-header">
<span>
<i class="el-icon-s-marketing"></i>
分析步骤
</span>
</div>
<el-timeline>
<el-timeline-item
v-for="step in steps"
:key="step.round"
:timestamp="`第 ${step.round} 轮`"
placement="top"
type="primary"
>
<el-card shadow="hover" class="step-card">
<div class="step-header">
<span class="tool-label">调用工具:</span>
<el-tag size="small" effect="plain">{{ step.tool }}</el-tag>
</div>
<!-- 参数折叠面板 -->
<el-collapse
v-if="hasContent(step.arguments)"
class="step-collapse"
>
<el-collapse-item title="查看请求参数">
<pre class="json-block">{{
formatJson(step.arguments)
}}</pre>
</el-collapse-item>
</el-collapse>
<!-- 结果预览折叠面板 -->
<el-collapse
v-if="hasContent(step.result_preview)"
class="step-collapse"
>
<el-collapse-item title="查看结果预览">
<pre class="json-block">{{
formatJson(step.result_preview)
}}</pre>
</el-collapse-item>
</el-collapse>
</el-card>
</el-timeline-item>
</el-timeline>
<!-- 运行中等待动画 -->
<div v-if="isRunning" class="thinking-placeholder">
<i class="el-icon-loading"></i>
AI 正在思考...
</div>
</el-card>
<el-card
shadow="never"
class="result-card"
v-if="status === 'completed' && result"
>
<div slot="header" class="card-header result-header">
<span>
<i class="el-icon-s-claim"></i>
结果
</span>
</div>
<div class="result-content" v-html="resultHtmlContent"></div>
</el-card>
</div>
<!-- 错误信息 -->
<div v-if="error" class="result-wrapper">
<span>{{ error }}</span>
</div>
</div>
<el-dialog
:title="skillsDialog.title"
:visible.sync="skillsDialog.visible"
width="70%"
top="100px"
:close-on-click-modal="false"
append-to-body
>
<div v-loading="operationLoading" element-loading-text="请稍后...">
<el-select
v-model="skill"
placeholder="请选择"
filterable
style="width: 300px"
>
<el-option
v-for="(item, index) in skillData"
:key="index"
:label="item.skillName"
:value="item.skillName"
/>
</el-select>
<div class="skill-content-preview">
<pre>{{ selectedSkillContent }}</pre>
</div>
</div>
</el-dialog>
<!-- markdown 编辑器, 可用于将 markdown 转 html -->
<VabMarkdownEditor
ref="mde"
v-show="false"
v-model="result"
@show-html="handleShowResultHtml"
/>
</div>
</template>
<script>
import VabMarkdownEditor from '@/plugins/vabMarkdownEditor'
import {
getAiMcpSkills,
getAiMcpTools,
submitAiMcpTask,
getAiMcpTaskResult,
stopAiMcpTask,
} from '@/api/pyapi/commonBusiness'
export default {
name: 'PlanCycleAnalysis',
components: {
VabMarkdownEditor,
},
data() {
return {
analysisContainerHeight: 0,
operationLoading: false,
skillsDialog: {
title: null,
visible: false,
},
skillData: [],
question: '',
skill: 'order-plan-cycle-data-analysis-skill',
isRunning: false,
taskId: '',
status: '',
result: '',
error: null,
elapsedSeconds: 0,
steps: [],
resultHtmlContent: '',
timer: null,
// Mock 数据相关 (仅用于演示)
mockRound: 0,
mockTaskData: {
task_id: '8bc9bc4d-c8a9-4023-bf72-08c9c8a2a7e1',
question: '帮我看看 CSI25153160CA 这个订单的情况',
skill: 'order-plan-cycle-data-analysis-skill',
steps: [
{
round: 1,
tool: 'get_order_plan_cycle_table_msg',
arguments: '{}',
result_preview:
'{\n "result": [\n {\n "表名": "dmp_production_route",\n "表说明": "",\n "字段序号": 1,\n "字段名": "id",\n "标识": "yes",\n "主键": "yes",\n "类型": "int",\n "占用字节数": 4,\n "长度": 10',
},
{
round: 2,
tool: 'query_order_plan_cycle_data',
arguments:
'{"po_no_list": ["CSI25153160CA"], "limit": 999, "offset": 0}',
result_preview:
'{\n "result": [\n {\n "row_number": "1",\n "id": 101950,\n "poid": 201744,\n "po_no": "CSI25153160CA",\n "style_id": "CSI-SS26MT2"',
},
{
round: 3,
tool: 'query_order_plan_cycle_data',
arguments:
'{"po_no_list": ["CSI25153160CA"], "limit": 999, "offset": 0}',
result_preview:
'{\n "result": [\n {\n "row_number": "1",\n "id": 101950,\n "poid": 201744,\n "po_no": "CSI25153160CA",\n "style_id": "CSI-SS26MT2"',
},
],
final_result:
'已分析订单 CSI25153160CA 的计划与周期数据: \n\n**整体结论**: 该订单共发现2个异常环节【缝制、质检】。实际总耗时272.5天(计划5天),超时率高达5350%。主要问题集中在生产环节的时间失控和质检流程倒置。\n\n**各环节详情**: \n1. 缝制环节: \n - 计划耗时: 2.73天(2025-09-03至2025-09-06)\n - 实际耗时: 272.5天(2025-08-22至2026-05-22)\n - 偏差率: +20000%(系统显示"未齐套"状态)\n - 异常原因: 物料长期未齐套导致生产停滞\n\n2. 质检环节: \n - 计划耗时: 2天(2025-09-06至2025-09-08)\n - 实际耗时: 272.5天(2025-08-30至2026-05-22)\n - 异常原因: \n - 流程异常: 质检提前16天在缝制完成前启动\n - 检测周期失控(正常应0.5天完成)\n\n**改进建议**: \n1. 建立物料齐套预警机制,当material_qt_time(物料齐套时间)晚于计划开工时间时自动触发采购加急流程\n2. 优化质检流程,设置开工前检查点,防止未完成生产的产品进入质检环节\n3. 重新评估生产排期合理性,当前计划时间(5天)与实际耗时(272.5天)严重不符,建议采用历史数据均值法重新制定标准工时',
},
}
},
computed: {
// 状态标签样式
statusTagType() {
if (this.status === 'completed') return 'success'
if (this.status === 'running') return 'warning'
if (this.status === 'pending') return 'primary'
if (this.error) return 'danger'
return 'info'
},
// 状态文本
statusText() {
if (this.status === 'completed') return '分析完成'
if (this.status === 'running') return '运行中'
if (this.status === 'pending') return '等待中'
if (this.error) return '发生错误'
return this.status
},
// 获取所选 skill 的内容
selectedSkillContent() {
const idx = this.skillData.findIndex(
(item) => item.skillName === this.skill
)
return idx !== -1 ? this.skillData[idx].content : ''
},
},
created() {},
mounted() {
this.initContainerHeight()
this.addResizeListener()
},
// 组件销毁时
beforeDestroy() {
// 清除定时器
if (this.timer) {
clearInterval(this.timer)
}
this.removeResizeListener()
},
methods: {
async getSkills() {
try {
this.operationLoading = true
const resp = await getAiMcpSkills()
if (resp.code === 200) {
this.skillData = resp.data.skills
} else {
this.$message.error(resp.message)
}
} catch (err) {
this.$message.error(`查看当前 AI 可用的 Skill 列表失败, ${err.message}`)
} finally {
this.operationLoading = false
}
},
async stopIntervalAndReset() {
try {
this.operationLoading = true
const resp = await stopAiMcpTask({ taskId: this.taskId })
if (resp.code === 200) {
if (this.timer) {
clearInterval(this.timer)
}
this.isRunning = this.$options.data().isRunning
this.status = this.$options.data().status
this.taskId = this.$options.data().taskId
this.result = this.$options.data().result
this.error = this.$options.data().error
this.elapsedSeconds = this.$options.data().elapsedSeconds
this.steps = this.$options.data().steps
this.resultHtmlContent = this.$options.data().resultHtmlContent
} else {
this.$message.error(resp.message)
}
} catch (err) {
this.$message.error(`停止失败, ${err.message}`)
} finally {
this.operationLoading = false
}
},
// 开始分析
async startAnalysis() {
try {
if (!this.question.trim() || this.isRunning) return
// 重置数据
this.isRunning = true
this.status = 'running'
this.taskId = this.$options.data().taskId
this.result = this.$options.data().result
this.error = this.$options.data().error
this.elapsedSeconds = this.$options.data().elapsedSeconds
this.steps = this.$options.data().steps
this.resultHtmlContent = this.$options.data().resultHtmlContent
// ---------- 接口调用 ----------
// const resp = await submitAiMcpTask({
// question: this.question,
// skill: this.skill,
// })
// this.taskId = resp.data.task_id
// this.pollTask() // 开始轮询
// ---------- Mock 模拟 ----------
this.mockRound = 0
this.taskId = this.mockTaskData.task_id
this.skill = this.mockTaskData.skill
this.mockPollTask()
this.$message.success('AI 正在分析中,请稍候...')
} catch (err) {
this.$message.error('启动分析失败')
this.isRunning = false
}
},
// 真实轮询逻辑
async pollTask() {
this.timer = setInterval(async () => {
try {
const resp = await getAiMcpTaskResult({ taskId: this.taskId })
const data = resp.data
this.status = data.status
this.elapsedSeconds = data.elapsed_seconds || 0
this.error = data.error
this.steps = data.steps || []
if (this.status === 'completed') {
this.result = data.result
this.$refs.mde.add(this.result)
this.isRunning = false
clearInterval(this.timer)
this.timer = null
this.$message.success(resp.message)
} else if (this.error) {
this.isRunning = false
clearInterval(this.timer)
this.timer = null
this.$message.error(`分析失败: ${this.error}`)
}
} catch (err) {
console.error('轮询失败', err)
this.isRunning = false
clearInterval(this.timer)
this.timer = null
this.$message.error(`获取分析状态失败, ${err.message}`)
}
}, 5000) // 每5秒轮询一次
},
// 判断是否有内容
hasContent(str) {
return str && str.trim() !== '' && str.trim() !== '{}'
},
// 格式化JSON
formatJson(str) {
if (!str) return ''
try {
const obj = JSON.parse(str)
return JSON.stringify(obj, null, 2)
} catch (e) {
return str
}
},
handleShowResultHtml(html) {
this.resultHtmlContent = html
},
async showSkillContent() {
this.skillsDialog.title = '请选择技能'
this.skillsDialog.visible = true
await this.getSkills()
},
// -------------------- 页面高度 --------------------
// 初始化容器高度
initContainerHeight() {
this.$nextTick(() => {
this.calculateContainerHeight()
})
},
// 根据页面高度设置容器高度
calculateContainerHeight() {
// console.log(window.innerHeight)
this.reportContainerHeight = window.innerHeight - 86
// console.log(this.reportContainerHeight)
},
// 添加监听以自适应高度
addResizeListener() {
this.handleResize = this.debounce(this.calculateContainerHeight, 150)
window.addEventListener('resize', this.handleResize)
},
// 移除监听
removeResizeListener() {
if (this.handleResize) {
window.removeEventListener('resize', this.handleResize)
}
},
// 防抖函数
debounce(fn, delay) {
let timer = null
return function (...args) {
clearTimeout(timer)
timer = setTimeout(() => fn.apply(this, args), delay)
}
},
// -------------------- Mock 模拟方法 --------------------
mockPollTask() {
const mockData = this.mockTaskData
this.timer = setInterval(() => {
this.mockRound++
this.elapsedSeconds += 5
if (this.mockRound <= mockData.steps.length) {
// 每次轮询塞入一个step
this.steps.push(mockData.steps[this.mockRound - 1])
}
if (this.mockRound === mockData.steps.length + 1) {
// 轮询结束
this.status = 'completed'
this.result = mockData.final_result
this.$refs.mde.add(this.result)
this.elapsedSeconds = 74.42
this.isRunning = false
clearInterval(this.timer)
this.$message.success('分析完成')
}
}, 5000)
},
},
}
</script>
<style scoped>
省略。。。
</style>
|