dsh-total-quality 2.0.55 → 2.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/CONTRIBUTING.md +51 -47
  2. package/LICENSE.md +37 -37
  3. package/README.en.md +32 -30
  4. package/README.md +90 -88
  5. package/agent-host.cjs +277 -277
  6. package/client.js +199 -92
  7. package/cordis.patch.yml +5 -5
  8. package/docs/enterprise-onboarding.md +10 -10
  9. package/docs/technical-reference.md +223 -223
  10. package/host.cjs +91 -124
  11. package/llm-bridge.cjs +234 -234
  12. package/locale/en.json +5 -5
  13. package/locale/zh.json +5 -5
  14. package/package.json +3 -2
  15. package/public/app.css +3 -0
  16. package/public/app.js +6 -2
  17. package/public/index.html +8 -9
  18. package/public/js/api.js +152 -152
  19. package/public/js/assistant-history.js +62 -62
  20. package/public/js/material-library.js +100 -100
  21. package/public/js/mobile-preview.js +119 -0
  22. package/public/js/pages-company-knowledge.js +9 -32
  23. package/public/js/pages-core.js +232 -232
  24. package/public/js/pages-growth.js +148 -148
  25. package/public/js/pages-qms.js +1 -1
  26. package/public/js/pages-qtools.js +410 -410
  27. package/public/js/pages-thanks.js +71 -71
  28. package/public/js/plugin-updates.js +53 -0
  29. package/public/js/quality-workflow.js +205 -205
  30. package/public/js/survey-dates.js +41 -41
  31. package/public/js/survey-knowledge.js +301 -301
  32. package/public/js/tool-wizards.js +621 -621
  33. package/public/js/ui.js +467 -467
  34. package/public/js/voice-capture-worklet.js +30 -30
  35. package/public/js/xiaozhi-voice.js +264 -264
  36. package/public/js/xiaozhi.js +468 -468
  37. package/public/platform-help.html +426 -426
  38. package/public/platform-site/assets/0468a68574112812.css +10703 -10703
  39. package/public/platform-site/assets/159e086d35adfa1c.css +2094 -2094
  40. package/public/platform-site/assets/169e3159cade0d9f.css +98 -98
  41. package/public/platform-site/assets/22b917c4a3d4ae3d.svg +14 -14
  42. package/public/platform-site/assets/476f9254ccf723e7.css +8265 -8265
  43. package/public/platform-site/assets/85a0a4fb4416ded9.css +120 -120
  44. package/public/platform-site/assets/a3b272383226f6d9.svg +135 -135
  45. package/public/platform-site/assets/fd16dc5f3cc9d97e.svg +104 -104
  46. package/public/platform-site/assets/qip-favicon.svg +5 -5
  47. package/public/platform-site/index.html +279 -279
  48. package/public/platform-site/offline.js +261 -261
  49. package/public/platform-site/qip-brand.css +566 -566
  50. package/public/platform-site/qip-module-guides.js +237 -237
  51. package/public/responsive.css +103 -101
  52. package/public/vendor/markdown-it-15.0.3.LICENSE +22 -22
  53. package/public/vendor/markdown-it-15.0.3.umd.min.js +13 -13
  54. package/public/workbench.css +7 -7
  55. package/public/xiaozhi.css +292 -292
  56. package/resources/qms-templates/019ffb58f24ed48d.docx +0 -0
  57. package/resources/qms-templates/04a69165fc15143a.docx +0 -0
  58. package/resources/qms-templates/09af69358c989a63.docx +0 -0
  59. package/resources/qms-templates/0e7ac77844e45311.docx +0 -0
  60. package/resources/qms-templates/12446a1b5e173c71.docx +0 -0
  61. package/resources/qms-templates/15ae40f67f9a3efc.docx +0 -0
  62. package/resources/qms-templates/18a69bbebf2ece7f.docx +0 -0
  63. package/resources/qms-templates/1978faa989134f02.docx +0 -0
  64. package/resources/qms-templates/1a30e121de752f7b.docx +0 -0
  65. package/resources/qms-templates/1b88907969ea7d4c.docx +0 -0
  66. package/resources/qms-templates/1e92b808722bf90a.docx +0 -0
  67. package/resources/qms-templates/2091cad340dc0e7f.docx +0 -0
  68. package/resources/qms-templates/271ae1002c896ed7.docx +0 -0
  69. package/resources/qms-templates/280ce53e5d3b5fcf.docx +0 -0
  70. package/resources/qms-templates/28ca8e4ede0adf6d.docx +0 -0
  71. package/resources/qms-templates/2ac6675f3cb1f993.docx +0 -0
  72. package/resources/qms-templates/2c3b799389909d8b.docx +0 -0
  73. package/resources/qms-templates/2c51038ba2cc3264.docx +0 -0
  74. package/resources/qms-templates/30c888851d16d764.docx +0 -0
  75. package/resources/qms-templates/3459fc4daaa2743d.docx +0 -0
  76. package/resources/qms-templates/3751dafc335e0e80.docx +0 -0
  77. package/resources/qms-templates/38798cd4298cd2db.docx +0 -0
  78. package/resources/qms-templates/38f551f9d738ab0a.docx +0 -0
  79. package/resources/qms-templates/3c85a23ea17018b4.docx +0 -0
  80. package/resources/qms-templates/3cdb7d14caf1b852.docx +0 -0
  81. package/resources/qms-templates/3f23239bb6b3e1db.docx +0 -0
  82. package/resources/qms-templates/42a5b9efeeaa8007.docx +0 -0
  83. package/resources/qms-templates/47352aafe9b8dac1.docx +0 -0
  84. package/resources/qms-templates/495359b1e3cbb8fe.docx +0 -0
  85. package/resources/qms-templates/4ca6920464785166.docx +0 -0
  86. package/resources/qms-templates/4ce862582a7791be.docx +0 -0
  87. package/resources/qms-templates/54c9a02a10991b9e.docx +0 -0
  88. package/resources/qms-templates/5aeb8fddf5f3693f.docx +0 -0
  89. package/resources/qms-templates/5d7e5576b0e73be7.docx +0 -0
  90. package/resources/qms-templates/5db3a40edac37791.docx +0 -0
  91. package/resources/qms-templates/5ffa21eb78eb6f77.docx +0 -0
  92. package/resources/qms-templates/60d0fbcb709b7161.docx +0 -0
  93. package/resources/qms-templates/63e1d3a166756095.docx +0 -0
  94. package/resources/qms-templates/65c724c080d32f10.docx +0 -0
  95. package/resources/qms-templates/674bd7826c6cbac2.docx +0 -0
  96. package/resources/qms-templates/677e851acca1b879.docx +0 -0
  97. package/resources/qms-templates/6bf15feb1a0d2a8d.docx +0 -0
  98. package/resources/qms-templates/6c3889c7d0918db9.docx +0 -0
  99. package/resources/qms-templates/6c4c349eeb0a96a8.docx +0 -0
  100. package/resources/qms-templates/6c5befbd33f759c1.docx +0 -0
  101. package/resources/qms-templates/6df8367901347508.docx +0 -0
  102. package/resources/qms-templates/71db51997037692b.docx +0 -0
  103. package/resources/qms-templates/78a8ef8c76510c99.docx +0 -0
  104. package/resources/qms-templates/8397bfd1c0a1b013.docx +0 -0
  105. package/resources/qms-templates/84d750cf858b60a1.docx +0 -0
  106. package/resources/qms-templates/880de58a4b334533.docx +0 -0
  107. package/resources/qms-templates/88c37d1b87d9b78c.docx +0 -0
  108. package/resources/qms-templates/88e9cd42f1849462.docx +0 -0
  109. package/resources/qms-templates/8956597403be35b6.docx +0 -0
  110. package/resources/qms-templates/94675c810a233e1f.docx +0 -0
  111. package/resources/qms-templates/94dff59dd7ebec6e.docx +0 -0
  112. package/resources/qms-templates/9670b9cd02141594.docx +0 -0
  113. package/resources/qms-templates/98c373ebd7b5163d.docx +0 -0
  114. package/resources/qms-templates/a4078cf2378a9388.docx +0 -0
  115. package/resources/qms-templates/a5b110c7ef917139.docx +0 -0
  116. package/resources/qms-templates/aec75820e45774be.docx +0 -0
  117. package/resources/qms-templates/b31c085e281d230c.docx +0 -0
  118. package/resources/qms-templates/b7c1c3c72f8ffeef.docx +0 -0
  119. package/resources/qms-templates/b81b82eaa5c8f422.docx +0 -0
  120. package/resources/qms-templates/b92a2fa31f75f137.docx +0 -0
  121. package/resources/qms-templates/b96c54ae9cfffc54.docx +0 -0
  122. package/resources/qms-templates/bc47cfd626c010e7.docx +0 -0
  123. package/resources/qms-templates/be60792734afca69.docx +0 -0
  124. package/resources/qms-templates/c0018d8ae9ad19e1.docx +0 -0
  125. package/resources/qms-templates/c171027c964c8be3.docx +0 -0
  126. package/resources/qms-templates/c4b93d71f5d98b4b.docx +0 -0
  127. package/resources/qms-templates/c531da29fcb3090f.docx +0 -0
  128. package/resources/qms-templates/c88eddd43358d015.docx +0 -0
  129. package/resources/qms-templates/cb978da542f0d892.docx +0 -0
  130. package/resources/qms-templates/ccde4a94532a8d36.docx +0 -0
  131. package/resources/qms-templates/cf3fb1e20ae6f24d.docx +0 -0
  132. package/resources/qms-templates/d231b4982fc8113e.docx +0 -0
  133. package/resources/qms-templates/d3c55aa5ed14055c.docx +0 -0
  134. package/resources/qms-templates/db45c44a00a13349.docx +0 -0
  135. package/resources/qms-templates/db849a41210f54e0.docx +0 -0
  136. package/resources/qms-templates/dba9ce363d83ddc8.docx +0 -0
  137. package/resources/qms-templates/dca3ea4c95eb2225.docx +0 -0
  138. package/resources/qms-templates/dfa4220d2b6b8131.docx +0 -0
  139. package/resources/qms-templates/dfa51222c777734f.docx +0 -0
  140. package/resources/qms-templates/e07ea511507e14f5.docx +0 -0
  141. package/resources/qms-templates/e0a2e00c03458dfa.docx +0 -0
  142. package/resources/qms-templates/e0e802f46b6764d1.docx +0 -0
  143. package/resources/qms-templates/e53fc85717f2852b.docx +0 -0
  144. package/resources/qms-templates/e9dd82d5e54520e5.docx +0 -0
  145. package/resources/qms-templates/ea7bfc22c06d82a9.docx +0 -0
  146. package/resources/qms-templates/ecf0db9f37294536.docx +0 -0
  147. package/resources/qms-templates/f05fcfc87e82ff39.docx +0 -0
  148. package/resources/qms-templates/fb9e56a17c4a4f97.docx +0 -0
  149. package/resources/qms-templates/manifest.json +932 -0
  150. package/src/data/seed-sme.js +317 -317
  151. package/src/data/seed.js +879 -879
  152. package/src/lib/backup-identity.js +57 -57
  153. package/src/lib/chat-answer.js +225 -225
  154. package/src/lib/chat-context.js +99 -99
  155. package/src/lib/company-library.js +28 -19
  156. package/src/lib/data-import.js +217 -217
  157. package/src/lib/data-sources.js +118 -118
  158. package/src/lib/demo-data.js +27 -1
  159. package/src/lib/employee-intake.js +84 -84
  160. package/src/lib/growth.js +53 -53
  161. package/src/lib/intake-packets.js +119 -119
  162. package/src/lib/issue-intake.js +88 -88
  163. package/src/lib/kb-revision.js +214 -214
  164. package/src/lib/llm.js +279 -279
  165. package/src/lib/model-error.js +14 -14
  166. package/src/lib/privacy.js +26 -26
  167. package/src/lib/qms-drafting.js +194 -194
  168. package/src/lib/qms-jobs.js +11 -7
  169. package/src/lib/qms-manual-review.js +185 -185
  170. package/src/lib/qms-procedure-review.js +109 -109
  171. package/src/lib/qms-templates.js +358 -326
  172. package/src/lib/qms.js +369 -369
  173. package/src/lib/quality-priority.js +100 -100
  174. package/src/lib/quality-state.js +92 -92
  175. package/src/lib/quality-workflow.js +65 -65
  176. package/src/lib/retrieval.js +159 -159
  177. package/src/lib/spc.js +127 -127
  178. package/src/lib/voice-hotwords.txt +22 -22
  179. package/src/lib/voice-text.js +13 -13
  180. package/src/lib/voice-worker.js +72 -72
  181. package/src/lib/voice.js +330 -330
  182. package/src/lib/workbench-notices.js +49 -49
  183. package/src/lib/workbench.js +103 -106
  184. package/src/package.json +3 -3
  185. package/src/tools/analysis.js +306 -306
  186. package/src/tools/data.js +596 -596
  187. package/src/tools/docs.js +430 -430
  188. package/src/tools/registry.js +57 -57
  189. package/tools-dsh.cjs +153 -153
  190. package//345/256/211/350/243/205/350/257/264/346/230/216.md +33 -25
  191. package//346/234/254/346/234/272/347/213/254/347/253/213/345/256/211/350/243/205/350/257/264/346/230/216.md +17 -17
  192. package//350/207/252/346/243/200.cjs +88 -88
  193. package/public/js/remote-knowledge.js +0 -165
  194. package/public/js/workbench-notices.js +0 -67
  195. package/public/remote-knowledge-help.html +0 -35
  196. package/public/remote-knowledge.css +0 -40
  197. package/src/lib/remote-knowledge.js +0 -284
package/public/js/ui.js CHANGED
@@ -1,467 +1,467 @@
1
- /* ==================================================================
2
- UI 基础库:DOM 生成、Markdown 渲染、SVG 图表、Toast、弹窗
3
- ================================================================== */
4
- (function (global) {
5
- 'use strict';
6
-
7
- /* ---------------- 基础工具 ---------------- */
8
- function esc(s) {
9
- return String(s === undefined || s === null ? '' : s)
10
- .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
11
- .replace(/"/g, '&quot;').replace(/'/g, '&#39;');
12
- }
13
- function attr(obj) {
14
- return Object.keys(obj || {}).filter((k) => obj[k] !== undefined && obj[k] !== null && obj[k] !== false)
15
- .map((k) => k + '="' + esc(obj[k]) + '"').join(' ');
16
- }
17
- /** 声明式操作按钮:data-act + data-args(JSON) */
18
- function act(label, action, args, cls) {
19
- return '<span class="' + (cls || 'btn') + '" data-act="' + esc(action) + '" data-args=\'' + esc(JSON.stringify(args || {})) + '\'>' + label + '</span>';
20
- }
21
- function tag(text, color) { return '<span class="tag tag-' + (color || 'blue') + '">' + esc(text) + '</span>'; }
22
- function sevTag(s) { return tag(s || '—', s === 'A' ? 'red' : s === 'B' ? 'orange' : 'gray'); }
23
- function statusTag(s) {
24
- const m = { '已关闭': 'green', '整改中': 'blue', '监控中': 'gray', 'QRQC 跟踪': 'orange' };
25
- return tag(s || '—', m[s] || 'blue');
26
- }
27
- function num(n, d) { return (n === null || n === undefined || n === '' || isNaN(n)) ? '—' : Number(n).toFixed(d === undefined ? 2 : d); }
28
- function pct(n, d) { return (n === null || n === undefined || isNaN(n)) ? '—' : Number(n).toFixed(d === undefined ? 1 : d) + '%'; }
29
- function dt(s) { return s ? String(s).slice(0, 16).replace('T', ' ') : '—'; }
30
- function day(s) { return s ? String(s).slice(0, 10) : '—'; }
31
- function bar(p, color) {
32
- const w = Math.max(0, Math.min(100, Number(p) || 0));
33
- return '<div class="bar-track"><div class="bar-fill" style="width:' + w + '%;background:' + (color || '#1a56db') + ';"></div></div>';
34
- }
35
- function card(title, sub, body, extra) {
36
- return '<div class="card"><div class="card-hd"><span class="t">' + esc(title) + '</span>' +
37
- (sub ? '<span class="s">' + sub + '</span>' : '') + (extra ? '<span class="more">' + extra + '</span>' : '') +
38
- '</div>' + body + '</div>';
39
- }
40
- function kpi(o) {
41
- return '<div class="kpi"' + (o.act ? ' data-act="' + esc(o.act) + '" data-args=\'' + esc(JSON.stringify(o.args || {})) + '\'' : '') + '>' +
42
- (o.ico ? '<div class="k-ico" style="background:' + (o.icoBg || '#e4edff') + ';">' + o.ico + '</div>' : '') +
43
- '<div class="k-label">' + esc(o.label) + '</div>' +
44
- '<div class="k-value" style="color:' + (o.color || '#0b3ea8') + ';">' + (o.value === undefined ? '—' : o.value) + '</div>' +
45
- '<div class="k-delta" style="' + (o.deltaColor ? 'color:' + o.deltaColor : '') + '">' + (o.delta || '') + '</div></div>';
46
- }
47
- function table(headers, rows, opts) {
48
- opts = opts || {};
49
- let h = '<div class="tbl-wrap"><table class="tbl"><thead><tr>' + headers.map((x) => '<th>' + (x.html || esc(x)) + '</th>').join('') + '</tr></thead><tbody>';
50
- if (!rows || !rows.length) h += '<tr><td colspan="' + headers.length + '"><div class="empty"><span class="e-ico">📭</span>' + esc(opts.empty || '暂无数据') + '</div></td></tr>';
51
- else rows.forEach((r) => {
52
- const rowAttr = r.__act ? ' class="rowlink" data-act="' + esc(r.__act) + '" data-args=\'' + esc(JSON.stringify(r.__args || {})) + '\'' : '';
53
- h += '<tr' + rowAttr + '>' + (r.cells || r).map((c, index) =>
54
- '<td' + (opts.mobileLabels ? ' data-label="' + esc(headers[index]) + '"' : '') + '>' +
55
- (c === undefined || c === null ? '—' : c) + '</td>').join('') + '</tr>';
56
- });
57
- return h + '</tbody></table></div>';
58
- }
59
- function empty(text, ico) { return '<div class="empty"><span class="e-ico">' + (ico || '📭') + '</span>' + esc(text || '暂无数据') + '</div>'; }
60
- function recentAudit({ rows, total }, key, detailed = false) {
61
- const entries = (rows || []).slice(0, 10);
62
- const columns = ['时间', '操作人', '插件', '动作', '结果摘要', ...(detailed ? ['参数', '用途'] : [])];
63
- const details = entries.map((row) => [
64
- esc(dt(row.ts)), esc(row.user), tag(row.plugin, 'navy'), esc(row.action), esc((row.resultSummary || '').slice(0, 70)),
65
- ...(detailed ? [esc(JSON.stringify(row.params || {}).slice(0, 100)), esc(row.purpose || '')] : []),
66
- ]);
67
- return `<details class="qip-audit">
68
- <summary><span data-live-region="${key}-summary">${entries.length} 条最近记录 · 共 ${total ?? entries.length} 条</span>
69
- <span class="qip-audit-expand">展开</span><span class="qip-audit-collapse">折叠</span></summary>
70
- <div data-live-region="${key}-entries">${table(columns, details, { empty:'暂无审计记录',mobileLabels:true })}</div>
71
- </details>`;
72
- }
73
- function loadingBlock(text) { return '<div class="empty"><span class="spin"></span> ' + esc(text || '加载中…') + '</div>'; }
74
- function infoGrid(items, cls) {
75
- return '<div class="info-grid' + (cls ? ' ' + cls : '') + '">' + items.map(([l, v, span]) =>
76
- '<div class="info-item"' + (span ? ' style="grid-column:1/-1"' : '') + '><div class="lbl">' + esc(l) + '</div><div class="val">' + (v === undefined || v === null || v === '' ? '—' : v) + '</div></div>').join('') + '</div>';
77
- }
78
-
79
- /* ---------------- Toast ---------------- */
80
- function toast(msg, type, ms) {
81
- let wrap = document.getElementById('toastWrap');
82
- if (!wrap) { wrap = document.createElement('div'); wrap.id = 'toastWrap'; document.body.appendChild(wrap); }
83
- const el = document.createElement('div');
84
- el.className = 'toast-item ' + (type || '');
85
- const ico = { ok: '✅', ai: '🤖', warn: '⚠️', err: '❌' }[type] || 'ℹ️';
86
- el.innerHTML = '<span>' + ico + '</span><span>' + esc(msg) + '</span>';
87
- wrap.appendChild(el);
88
- setTimeout(() => { el.classList.add('out'); setTimeout(() => el.remove(), 320); }, ms || (type === 'err' ? 7000 : 3600));
89
- }
90
-
91
- /* ---------------- 弹窗 ---------------- */
92
- let curModal = null;
93
- let onModalClose;
94
- function modal(o) {
95
- closeModal();
96
- const div = document.createElement('div');
97
- div.className = 'dm-overlay show';
98
- div.innerHTML = '<div class="dm-panel ' + (o.width || '') + '">' +
99
- '<div class="dm-head"><div><div class="dm-t">' + (o.title || '') + '</div>' + (o.sub ? '<div class="dm-s">' + o.sub + '</div>' : '') + '</div><div class="dm-close" data-act="__closeModal">✕</div></div>' +
100
- '<div class="dm-body">' + (o.body || '') + '</div>' +
101
- (o.footer ? '<div class="dm-footer">' + o.footer + '</div>' : '') + '</div>';
102
- div.addEventListener('click', (e) => { if (e.target === div) closeModal(); });
103
- document.body.appendChild(div);
104
- curModal = div;
105
- onModalClose = o.onClose;
106
- if (o.onMount) o.onMount(div);
107
- return div;
108
- }
109
- function closeModal() {
110
- if (!curModal) return;
111
- const onClose = onModalClose;
112
- curModal.remove();
113
- curModal = null;
114
- onModalClose = null;
115
- onClose?.();
116
- }
117
-
118
- /* ---------------- Markdown 渲染 ---------------- */
119
- const markdown = global.markdownit?.({ html: false, breaks: true, linkify: false });
120
- if (markdown) {
121
- const rules = markdown.renderer.rules;
122
- rules.table_open = () => '<div class="markdown-table-scroll" tabindex="0" role="region" aria-label="表格,可横向滚动"><table>\n';
123
- rules.table_close = () => '</table></div>\n';
124
- rules.image = (tokens, index, options, env, renderer) =>
125
- esc(renderer.renderInlineAsText(tokens[index].children, options, env));
126
- rules.link_open = (tokens, index, options, env, renderer) => {
127
- tokens[index].attrSet('target', '_blank');
128
- tokens[index].attrSet('rel', 'noopener noreferrer');
129
- return renderer.renderToken(tokens, index, options);
130
- };
131
- }
132
-
133
- function normalizeMarkdown(text) {
134
- let fence;
135
- return text.split(/\r\n?|\n/).map((line) => {
136
- const marker = /^( {0,3})(\x60{3,}|~{3,})(.*)$/.exec(line.replace(/^(?: {0,3}>[ \t]?)+/, ''));
137
- if (fence) {
138
- if (marker && marker[2][0] === fence[0] && marker[2].length >= fence.length && !marker[3].trim()) fence = undefined;
139
- return line;
140
- }
141
- if (marker) { fence = marker[2]; return line; }
142
- // Generated answers sometimes leave empty list entries or lone hard-break markers.
143
- if (/^ {0,3}(?:(?:[-+*]|\d{1,9}[.)、])(?:[ \t]+\\)?|\\)[ \t]*$/.test(line)) return '';
144
- return line
145
- .replace(/^( {0,3}\d{1,9})、[ \t]+/, '$1. ')
146
- .replace(/(\*\*|__)[ \t]*\\[ \t]*$/, '$1');
147
- }).join('\n');
148
- }
149
-
150
- function md(text) {
151
- if (!text) return '';
152
- const source = normalizeMarkdown(String(text));
153
- return markdown ? markdown.render(source) : '<p>' + esc(source).replace(/\n/g, '<br>') + '</p>';
154
- }
155
-
156
- function revisionCard(item, action, i) {
157
- const { id, name, state, documentId, changes } = item;
158
- const submit = (label,approved) => '<button type="button" class="btn btn-sm' + (approved ? ' btn-primary' : '') +
159
- '" data-act="' + esc(action) + '" data-args=\'' + esc(JSON.stringify({i,id,approved})) + '\'' +
160
- (item.submitting ? ' disabled' : '') + '>' + label + '</button>';
161
- if (state === 'updated' || state === 'saved') return '<div class="confirm-card kb-revision"><strong>' +
162
- (state === 'updated' ? '文件已更新' : '此前已保存修订稿') + '</strong><p>' +
163
- esc(name) + '</p><div class="cc-a"><a class="btn btn-sm" download href="/qip-real-workbench/api/enterprise-kb/files/' +
164
- encodeURIComponent(documentId) + '">下载修订稿</a><a class="btn btn-sm" href="#companyLibrary">打开企业知识库</a></div></div>';
165
- if (state !== 'pending') {
166
- const label = state === 'rejected' ? '修订已取消' : state === 'removed' ? '修订稿已移除' :
167
- state === 'pruned' ? '旧版已按保留规则清理' : '修订预览已过期';
168
- return '<div class="confirm-card kb-revision"><strong>' + label + '</strong><p>' + esc(name) + '</p></div>';
169
- }
170
- return '<div class="confirm-card kb-revision"><strong>修订预览</strong><p>' + esc(name) +
171
- '</p><ol class="kb-revision-edits">' + changes.map(({where,from,to}) =>
172
- '<li><span>' + esc(where) + '</span><div class="kb-revision-diff"><del aria-label="原文">' + esc(from) +
173
- '</del><ins aria-label="改为">' + esc(to || '删去这段文字') + '</ins></div></li>').join('') +
174
- '</ol><div class="cc-a">' + submit('确认更新',true) + submit('取消修订',false) + '</div></div>';
175
- }
176
-
177
- function chatConfirmations(pending, action, i) {
178
- return (pending || []).map(item => {
179
- if (item.kind === 'knowledgeRevision') return revisionCard(item,action,i);
180
- const {id,label} = item;
181
- return '<div class="confirm-card"><strong>待人工确认 · ' + esc(label) +
182
- '</strong><div class="cc-a">' + act('查看并确认', action, { i, id, approved: true }, 'btn btn-sm') +
183
- act('拒绝', action, { i, id, approved: false }, 'btn btn-sm') + '</div></div>';
184
- }).join('');
185
- }
186
-
187
- async function decideChatConfirmation(message, id, approved, render) {
188
- const pending = message?.pending?.find((item) => item.id === id);
189
- if (!pending) return;
190
- if (pending.kind === 'knowledgeRevision') {
191
- if (pending.state !== 'pending' || pending.submitting) return;
192
- pending.submitting = true;
193
- render();
194
- try {
195
- const result = await API.post('/qip-real-workbench/api/enterprise-kb/revisions/' + encodeURIComponent(id),{approved});
196
- if (!result.ok) throw new Error(result.error || '修订未保存');
197
- Object.assign(pending,result.preview);
198
- toast(approved ? '文件已更新,知识库已采用新版。' : '已取消修订','ok');
199
- if (approved) global.dispatchEvent(new Event('qip:knowledge-revised'));
200
- } finally { pending.submitting = false; render(); }
201
- return;
202
- }
203
- if (approved) {
204
- const confirmed = await global.AskConfirm('确认业务操作', [
205
- ['操作', pending.label], ['提交内容', JSON.stringify(pending.args || {})],
206
- ]);
207
- if (!confirmed) return;
208
- }
209
- const result = await API.post('/qip-real-workbench/api/chat/' + (approved ? 'confirm' : 'reject'),
210
- { id, ...(approved ? {} : { reason: '用户拒绝' }) });
211
- if (!result.ok) throw new Error(result.error || result.result?.error || '操作未完成');
212
- message.pending = message.pending.filter((item) => item.id !== id);
213
- toast(approved ? result.summary || '操作已执行' : '已拒绝该动作', 'ok');
214
- render();
215
- }
216
-
217
- function attachDraftContinuation(box, result) {
218
- if (result?.state !== 'incomplete' || !result.answerId) return;
219
- const pre = box.querySelector('pre');
220
- if (!pre) return;
221
- const status = document.createElement('div');
222
- box.appendChild(status);
223
- const message = { answerId: result.answerId, state: 'incomplete', finishReason:result.finishReason, text: '' };
224
- const click = async () => {
225
- if (message.running) return;
226
- await API.resumeAnswer(message, { render });
227
- if (message.text) pre.textContent = message.text;
228
- if (message.state === 'complete') box.querySelector('.sqm-warnbox')?.remove();
229
- };
230
- const render = () => {
231
- status.innerHTML = answerStatus(message, '<button type="button" class="btn btn-sm">继续生成</button>');
232
- status.querySelector('button')?.addEventListener('click', click);
233
- };
234
- render();
235
- }
236
-
237
- function answerStatus(message, resumeButton) {
238
- if (message.running) return '<p class="run-status" role="status">' + esc(message.status || '正在生成回答…') + '</p>';
239
- if (message.state !== 'incomplete' && !message.error) {
240
- return [message.sourceNotice,message.historyWarning].filter(Boolean)
241
- .map(text=>'<p class="run-status" role="status">'+esc(text)+'</p>').join('');
242
- }
243
- const reason = message.finishReason === 'model-balance' ? 'DeepSeek 调用额度不足。恢复额度或接入可用模型后可继续生成。' :
244
- message.finishReason === 'model-credentials' ? 'DSH 当前没有可用的模型接入。请在 DSH 模型设置中检查。' : '';
245
- return '<div class="run-status" role="status">' + esc(message.error || reason || '这条回复尚未完成。') +
246
- (message.answerId && resumeButton ? ' ' + resumeButton : '') + '</div>';
247
- }
248
-
249
- /* ---------------- SVG 图表 ---------------- */
250
- /** 折线图(可多序列、双 Y 轴) */
251
- function lineChart(o) {
252
- const W = o.width || 560, H = o.height || 200, pad = { l: 46, r: o.rightAxis ? 46 : 16, t: 16, b: 28 };
253
- const series = (o.series || []).filter((s) => s.values && s.values.length);
254
- if (!series.length) return empty('暂无趋势数据');
255
- const all = series.filter((s) => !s.right).flatMap((s) => s.values);
256
- const allR = series.filter((s) => s.right).flatMap((s) => s.values);
257
- const labels = o.labels || series[0].values.map((_, i) => String(i + 1));
258
- let min = o.min !== undefined ? o.min : Math.min.apply(null, all.length ? all : [0]);
259
- let max = o.max !== undefined ? o.max : Math.max.apply(null, all.length ? all : [1]);
260
- if (min === max) { min -= 1; max += 1; }
261
- const padY = (max - min) * 0.12; min -= padY; max += padY;
262
- let rmin = allR.length ? Math.min.apply(null, allR) : 0, rmax = allR.length ? Math.max.apply(null, allR) : 1;
263
- if (rmin === rmax) { rmin -= 1; rmax += 1; }
264
- const rp = (rmax - rmin) * 0.12; rmin -= rp; rmax += rp;
265
- const px = (i) => pad.l + (W - pad.l - pad.r) * (labels.length <= 1 ? 0.5 : i / (labels.length - 1));
266
- const py = (v) => H - pad.b - (H - pad.t - pad.b) * ((v - min) / (max - min));
267
- const pry = (v) => H - pad.b - (H - pad.t - pad.b) * ((v - rmin) / (rmax - rmin));
268
- let g = '';
269
- for (let i = 0; i <= 4; i++) {
270
- const y = pad.t + (H - pad.t - pad.b) * i / 4;
271
- const v = max - (max - min) * i / 4;
272
- g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/>' +
273
- '<text x="' + (pad.l - 6) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + v.toFixed(o.yDigits === undefined ? 1 : o.yDigits) + '</text>';
274
- if (o.rightAxis && allR.length) g += '<text x="' + (W - pad.r + 6) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#18a058">' + (rmax - (rmax - rmin) * i / 4).toFixed(o.rDigits === undefined ? 2 : o.rDigits) + '</text>';
275
- }
276
- series.forEach((s) => {
277
- const pts = s.values.map((v, i) => px(i).toFixed(1) + ',' + (s.right ? pry(v) : py(v)).toFixed(1)).join(' ');
278
- g += '<polyline points="' + pts + '" fill="none" stroke="' + (s.color || '#1a56db') + '" stroke-width="' + (s.width || 2) + '"' + (s.dash ? ' stroke-dasharray="' + s.dash + '"' : '') + '/>';
279
- if (s.dots !== false) s.values.forEach((v, i) => { g += '<circle cx="' + px(i).toFixed(1) + '" cy="' + (s.right ? pry(v) : py(v)).toFixed(1) + '" r="2.6" fill="' + (s.color || '#1a56db') + '"/>'; });
280
- });
281
- // X 轴标签(抽样显示,避免重叠)
282
- const step = Math.max(1, Math.ceil(labels.length / 8));
283
- labels.forEach((l, i) => { if (i % step === 0 || i === labels.length - 1) g += '<text x="' + px(i).toFixed(1) + '" y="' + (H - 8) + '" font-size="9" fill="#8791a5" text-anchor="middle">' + esc(String(l).length > 10 ? String(l).slice(5) : l) + '</text>'; });
284
- return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
285
- }
286
-
287
- /** 控制图:数据点 + UCL/CL/LCL + 规格限 + 判异点高亮 */
288
- function controlChart(o) {
289
- const W = o.width || 640, H = o.height || 240, pad = { l: 52, r: 18, t: 18, b: 34 };
290
- const pts = o.points && o.points.length ? o.points : (o.values || []);
291
- const values = pts.map((p) => (typeof p === 'number' ? p : p.value));
292
- if (!values.length) return empty('暂无控制图数据');
293
- const dynamic = pts.length && pts.every((p) => typeof p.ucl === 'number' && typeof p.lcl === 'number');
294
- const lines = [o.ucl, o.cl, o.lcl, ...(dynamic ? pts.flatMap((p) => [p.ucl, p.lcl]) : [])].filter((x) => typeof x === 'number');
295
- const spec = [o.usl, o.lsl].filter((x) => typeof x === 'number');
296
- let min = Math.min.apply(null, values.concat(lines, spec.length ? spec : lines));
297
- let max = Math.max.apply(null, values.concat(lines, spec.length ? spec : lines));
298
- const pAdv = (max - min) * 0.1 || 1; min -= pAdv; max += pAdv;
299
- const px = (i) => pad.l + (W - pad.l - pad.r) * (values.length <= 1 ? 0.5 : i / (values.length - 1));
300
- const py = (v) => H - pad.b - (H - pad.t - pad.b) * ((v - min) / (max - min));
301
- let g = '';
302
- for (let i = 0; i <= 4; i++) {
303
- const y = pad.t + (H - pad.t - pad.b) * i / 4, v = max - (max - min) * i / 4;
304
- g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/>' +
305
- '<text x="' + (pad.l - 6) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + v.toFixed(3) + '</text>';
306
- }
307
- function hline(v, color, dash, label) {
308
- if (typeof v !== 'number') return '';
309
- const y = py(v).toFixed(1);
310
- return '<line x1="' + pad.l + '" y1="' + y + '" x2="' + (W - pad.r) + '" y2="' + y + '" stroke="' + color + '" stroke-width="1.3"' + (dash ? ' stroke-dasharray="' + dash + '"' : '') + '/>' +
311
- '<text x="' + (W - pad.r - 2) + '" y="' + (Number(y) - 3) + '" font-size="9" fill="' + color + '" text-anchor="end">' + label + '</text>';
312
- }
313
- if (!dynamic) g += hline(o.ucl, '#e5484d', '6 4', 'UCL ' + (o.ucl !== undefined ? Number(o.ucl).toFixed(3) : ''));
314
- g += hline(o.cl, '#18a058', '', 'CL ' + (o.cl !== undefined ? Number(o.cl).toFixed(3) : ''));
315
- if (!dynamic) g += hline(o.lcl, '#e5484d', '6 4', 'LCL ' + (o.lcl !== undefined ? Number(o.lcl).toFixed(3) : ''));
316
- if (typeof o.usl === 'number') g += hline(o.usl, '#c8620a', '2 3', 'USL ' + o.usl);
317
- if (typeof o.lsl === 'number') g += hline(o.lsl, '#c8620a', '2 3', 'LSL ' + o.lsl);
318
- if (dynamic) {
319
- for (const name of ['ucl', 'lcl']) {
320
- const limit = pts.map((p, i) => px(i).toFixed(1) + ',' + py(p[name]).toFixed(1)).join(' ');
321
- g += '<polyline points="' + limit + '" fill="none" stroke="#e5484d" stroke-dasharray="6 4"/>';
322
- }
323
- }
324
- const poly = values.map((v, i) => px(i).toFixed(1) + ',' + py(v).toFixed(1)).join(' ');
325
- g += '<polyline points="' + poly + '" fill="none" stroke="#1a56db" stroke-width="1.8"/>';
326
- values.forEach((v, i) => {
327
- const p = pts[i];
328
- const ooc = typeof p === 'object' ? p.outOfControl : (typeof o.ucl === 'number' && (v > o.ucl || v < o.lcl));
329
- const color = ooc ? '#e5484d' : p?.alarm ? '#c8620a' : '#1a56db';
330
- g += '<circle cx="' + px(i).toFixed(1) + '" cy="' + py(v).toFixed(1) + '" r="' + (ooc ? 4.2 : 2.6) + '" fill="' + color + '"' + (ooc ? ' stroke="#fff" stroke-width="1.2"' : '') + '><title>' + esc([p?.label, p?.time].filter(Boolean).join(' ')) + ' ' + v + '</title></circle>';
331
- });
332
- const labels = o.labels || [];
333
- const step = Math.max(1, Math.ceil(values.length / 10));
334
- values.forEach((v, i) => { if (i % step === 0 || i === values.length - 1) g += '<text x="' + px(i).toFixed(1) + '" y="' + (H - 10) + '" font-size="8.5" fill="#8791a5" text-anchor="middle">' + (labels[i] !== undefined ? esc(String(labels[i]).slice(5, 10)) : (i + 1)) + '</text>'; });
335
- return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
336
- }
337
-
338
- /** 帕累托图(柱 + 累计折线 + 80% 线) */
339
- function paretoChart(rows, o) {
340
- o = o || {};
341
- if (!rows || !rows.length) return empty('暂无数据');
342
- const W = 620, H = 250, pad = { l: 44, r: 46, t: 20, b: 46 };
343
- const max = Math.max.apply(null, rows.map((r) => r.value));
344
- const bw = (W - pad.l - pad.r) / rows.length;
345
- const py = (v) => H - pad.b - (H - pad.t - pad.b) * (v / max);
346
- const pyc = (p) => H - pad.b - (H - pad.t - pad.b) * (p / 100);
347
- let g = '';
348
- for (let i = 0; i <= 4; i++) {
349
- const y = pad.t + (H - pad.t - pad.b) * i / 4;
350
- g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/>' +
351
- '<text x="' + (pad.l - 5) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + Math.round(max - max * i / 4) + '</text>' +
352
- '<text x="' + (W - pad.r + 5) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#e5484d">' + Math.round(100 - 100 * i / 4) + '%</text>';
353
- }
354
- const colors = ['#1a56db', '#5a8bff', '#7a5af8', '#0a9e8f', '#95a3bd', '#c3cbd9'];
355
- rows.forEach((r, i) => {
356
- const x = pad.l + i * bw + bw * 0.15, w = bw * 0.7, y = py(r.value);
357
- g += '<rect x="' + x.toFixed(1) + '" y="' + y.toFixed(1) + '" width="' + w.toFixed(1) + '" height="' + (H - pad.b - y).toFixed(1) + '" rx="3" fill="' + colors[i % colors.length] + '"><title>' + esc(r.name) + ':' + r.value + '</title></rect>' +
358
- '<text x="' + (x + w / 2).toFixed(1) + '" y="' + (y - 4).toFixed(1) + '" font-size="9.5" fill="' + colors[i % colors.length] + '" text-anchor="middle" font-weight="600">' + r.value + '</text>' +
359
- '<text x="' + (x + w / 2).toFixed(1) + '" y="' + (H - 30) + '" font-size="9" fill="#5e6b85" text-anchor="middle">' + esc(String(r.name).slice(0, 5)) + '</text>';
360
- });
361
- if (rows[0].cumPct !== undefined) {
362
- const pts = rows.map((r, i) => (pad.l + i * bw + bw / 2).toFixed(1) + ',' + pyc(r.cumPct).toFixed(1)).join(' ');
363
- g += '<polyline points="' + pts + '" fill="none" stroke="#e5484d" stroke-width="1.8"/>';
364
- rows.forEach((r, i) => { g += '<circle cx="' + (pad.l + i * bw + bw / 2).toFixed(1) + '" cy="' + pyc(r.cumPct).toFixed(1) + '" r="3" fill="#e5484d"><title>累计 ' + r.cumPct.toFixed(1) + '%</title></circle>'; });
365
- g += '<line x1="' + pad.l + '" y1="' + pyc(80).toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + pyc(80).toFixed(1) + '" stroke="#e5484d" stroke-dasharray="5 4"/>' +
366
- '<text x="' + (pad.l + 4) + '" y="' + (pyc(80) - 4).toFixed(1) + '" font-size="9" fill="#e5484d">80% 关键少数线</text>';
367
- }
368
- g += '<line x1="' + pad.l + '" y1="' + (H - pad.b) + '" x2="' + (W - pad.r) + '" y2="' + (H - pad.b) + '" stroke="#e4e8f0"/>';
369
- return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
370
- }
371
-
372
- /** 直方图 + 规格限 */
373
- function histogramChart(bins, lsl, usl) {
374
- if (!bins || !bins.length) return empty('暂无分布数据');
375
- const W = 620, H = 210, pad = { l: 44, r: 18, t: 16, b: 34 };
376
- const max = Math.max.apply(null, bins.map((b) => b.count)) || 1;
377
- const lo = bins[0].from, hi = bins[bins.length - 1].to;
378
- const bw = (W - pad.l - pad.r) / bins.length;
379
- const px = (v) => pad.l + (W - pad.l - pad.r) * ((v - lo) / ((hi - lo) || 1));
380
- let g = '';
381
- for (let i = 0; i <= 3; i++) {
382
- const y = pad.t + (H - pad.t - pad.b) * i / 3;
383
- g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/><text x="' + (pad.l - 5) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + Math.round(max - max * i / 3) + '</text>';
384
- }
385
- bins.forEach((b, i) => {
386
- const h = (H - pad.t - pad.b) * (b.count / max);
387
- const x = pad.l + i * bw, y = H - pad.b - h;
388
- const out = (usl !== undefined && b.from >= usl) || (lsl !== undefined && b.to <= lsl);
389
- g += '<rect x="' + (x + 1).toFixed(1) + '" y="' + y.toFixed(1) + '" width="' + (bw - 2).toFixed(1) + '" height="' + h.toFixed(1) + '" fill="' + (out ? '#e5484d' : '#5a8bff') + '" rx="2"><title>' + b.from + ' ~ ' + b.to + ':' + b.count + ' 个</title></rect>';
390
- });
391
- if (lsl !== undefined) g += '<line x1="' + px(lsl).toFixed(1) + '" y1="' + pad.t + '" x2="' + px(lsl).toFixed(1) + '" y2="' + (H - pad.b) + '" stroke="#c8620a" stroke-dasharray="4 3"/><text x="' + (px(lsl) + 3).toFixed(1) + '" y="' + (pad.t + 10) + '" font-size="9" fill="#c8620a">LSL</text>';
392
- if (usl !== undefined) g += '<line x1="' + px(usl).toFixed(1) + '" y1="' + pad.t + '" x2="' + px(usl).toFixed(1) + '" y2="' + (H - pad.b) + '" stroke="#c8620a" stroke-dasharray="4 3"/><text x="' + (px(usl) - 24).toFixed(1) + '" y="' + (pad.t + 10) + '" font-size="9" fill="#c8620a">USL</text>';
393
- g += '<line x1="' + pad.l + '" y1="' + (H - pad.b) + '" x2="' + (W - pad.r) + '" y2="' + (H - pad.b) + '" stroke="#e4e8f0"/>';
394
- g += '<text x="' + pad.l + '" y="' + (H - 12) + '" font-size="9" fill="#8791a5">' + lo + '</text><text x="' + (W - pad.r) + '" y="' + (H - 12) + '" font-size="9" fill="#8791a5" text-anchor="end">' + hi + '</text>';
395
- return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
396
- }
397
-
398
- /** 因果图(鱼骨图):按【人机料法环测】六分支渲染 */
399
- function fishbone(o) {
400
- const W = 720, H = 300;
401
- const cats = o.categories || [];
402
- const spineY = H / 2;
403
- let g = '<line x1="40" y1="' + spineY + '" x2="600" y2="' + spineY + '" stroke="#33415c" stroke-width="2"/>' +
404
- '<polygon points="600,' + spineY + ' 586,' + (spineY - 8) + ' 586,' + (spineY + 8) + '" fill="#33415c"/>' +
405
- '<rect x="602" y="' + (spineY - 26) + '" width="108" height="52" rx="7" fill="#e5484d"/>' +
406
- '<text x="656" y="' + (spineY - 4) + '" font-size="11.5" fill="#fff" text-anchor="middle">' + esc(String(o.effect || '问题').slice(0, 10)) + '</text>' +
407
- '<text x="656" y="' + (spineY + 12) + '" font-size="9" fill="#fff" text-anchor="middle">' + esc(String(o.effectValue || '').slice(0, 14)) + '</text>';
408
- const top = cats.filter((_, i) => i % 2 === 0).slice(0, 3);
409
- const bottom = cats.filter((_, i) => i % 2 === 1).slice(0, 3);
410
- const xs = [120, 280, 440];
411
- function branch(cat, x, up) {
412
- const y = up ? 60 : H - 60;
413
- let s = '<line x1="' + x + '" y1="' + spineY + '" x2="' + (x + 24) + '" y2="' + y + '" stroke="#95a3bd"/>';
414
- s += '<rect x="' + (x - 46) + '" y="' + (up ? y - 22 : y + 4) + '" width="92" height="19" rx="5" fill="#e4edff"/>' +
415
- '<text x="' + x + '" y="' + (up ? y - 9 : y + 17) + '" font-size="10.5" fill="#1a56db" text-anchor="middle" font-weight="600">' + esc(cat.name || '') + '</text>';
416
- (cat.items || []).slice(0, 3).forEach((it, i) => {
417
- const iy = up ? y + 14 + i * 15 : y - 14 - i * 15;
418
- const p = it.p !== undefined ? ' P=' + it.p : '';
419
- s += '<text x="' + (x + 30) + '" y="' + iy + '" font-size="9.5" fill="' + (it.p >= 0.5 ? '#e5484d' : it.p >= 0.2 ? '#c8620a' : '#8791a5') + '">' + esc(String(it.text || it).slice(0, 22)) + p + '</text>';
420
- });
421
- return s;
422
- }
423
- top.forEach((c, i) => { g += branch(c, xs[i], true); });
424
- bottom.forEach((c, i) => { g += branch(c, xs[i], false); });
425
- return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
426
- }
427
-
428
- /** 环形进度 */
429
- function ring(p, color, label) {
430
- const r = 26, c = 2 * Math.PI * r, off = c * (1 - Math.max(0, Math.min(100, p)) / 100);
431
- return '<svg width="66" height="66" viewBox="0 0 66 66"><circle cx="33" cy="33" r="' + r + '" fill="none" stroke="#eef1f6" stroke-width="7"/>' +
432
- '<circle cx="33" cy="33" r="' + r + '" fill="none" stroke="' + (color || '#1a56db') + '" stroke-width="7" stroke-linecap="round" stroke-dasharray="' + c.toFixed(1) + '" stroke-dashoffset="' + off.toFixed(1) + '" transform="rotate(-90 33 33)"/>' +
433
- '<text x="33" y="37" font-size="13" font-weight="700" fill="' + (color || '#1a56db') + '" text-anchor="middle">' + Math.round(p) + '</text></svg>' + (label ? '<div style="font-size:10.5px;color:#8791a5;text-align:center;">' + esc(label) + '</div>' : '');
434
- }
435
-
436
- /* ---------------- 表单 ---------------- */
437
- function input(name, value, o) {
438
- o = o || {};
439
- if (o.type === 'select') {
440
- return '<select class="inp" data-field="' + esc(name) + '"' + (o.style ? ' style="' + o.style + '"' : '') + '>' +
441
- (o.options || []).map((op) => '<option value="' + esc(op.value !== undefined ? op.value : op) + '"' + (String(op.value !== undefined ? op.value : op) === String(value) ? ' selected' : '') + '>' + esc(op.label || op) + '</option>').join('') + '</select>';
442
- }
443
- if (o.type === 'textarea') return '<textarea class="inp" rows="' + (o.rows || 3) + '" data-field="' + esc(name) + '" placeholder="' + esc(o.placeholder || '') + '">' + esc(value || '') + '</textarea>';
444
- if (o.type === 'checkbox') return '<label class="switch"><input type="checkbox" data-field="' + esc(name) + '"' + (value ? ' checked' : '') + '/> ' + esc(o.label || '') + '</label>';
445
- return '<input class="inp' + (o.small ? ' inp-sm' : '') + '" type="' + (o.type || 'text') + '" data-field="' + esc(name) + '" value="' + esc(value === undefined ? '' : value) + '" placeholder="' + esc(o.placeholder || '') + '"' + (o.style ? ' style="' + o.style + '"' : '') + '/>';
446
- }
447
- function readForm(scope) {
448
- const out = {};
449
- scope.querySelectorAll('[data-field]').forEach((el) => {
450
- out[el.getAttribute('data-field')] = el.type === 'checkbox' ? el.checked : (el.type === 'number' ? (el.value === '' ? null : Number(el.value)) : el.value);
451
- });
452
- return out;
453
- }
454
-
455
- /* ---------------- 平台显示名 ---------------- */
456
- /** 初始设置里填了公司名称就用公司名称,否则用「全质开源质量平台」 */
457
- function brand() {
458
- const b = (global.State && global.State.bootstrap) || {};
459
- const v = typeof b.brand === 'string' && b.brand.trim();
460
- return v || '全质开源质量平台';
461
- }
462
-
463
- global.UI = {
464
- answerStatus, attachDraftContinuation, chatConfirmations, decideChatConfirmation, esc, attr, act, tag, sevTag, statusTag, num, pct, dt, day, bar, card, kpi, table, recentAudit, empty, loadingBlock, infoGrid,
465
- toast, modal, closeModal, md, lineChart, controlChart, paretoChart, histogramChart, fishbone, ring, input, readForm, brand,
466
- };
467
- })(window);
1
+ /* ==================================================================
2
+ UI 基础库:DOM 生成、Markdown 渲染、SVG 图表、Toast、弹窗
3
+ ================================================================== */
4
+ (function (global) {
5
+ 'use strict';
6
+
7
+ /* ---------------- 基础工具 ---------------- */
8
+ function esc(s) {
9
+ return String(s === undefined || s === null ? '' : s)
10
+ .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
11
+ .replace(/"/g, '&quot;').replace(/'/g, '&#39;');
12
+ }
13
+ function attr(obj) {
14
+ return Object.keys(obj || {}).filter((k) => obj[k] !== undefined && obj[k] !== null && obj[k] !== false)
15
+ .map((k) => k + '="' + esc(obj[k]) + '"').join(' ');
16
+ }
17
+ /** 声明式操作按钮:data-act + data-args(JSON) */
18
+ function act(label, action, args, cls) {
19
+ return '<span class="' + (cls || 'btn') + '" data-act="' + esc(action) + '" data-args=\'' + esc(JSON.stringify(args || {})) + '\'>' + label + '</span>';
20
+ }
21
+ function tag(text, color) { return '<span class="tag tag-' + (color || 'blue') + '">' + esc(text) + '</span>'; }
22
+ function sevTag(s) { return tag(s || '—', s === 'A' ? 'red' : s === 'B' ? 'orange' : 'gray'); }
23
+ function statusTag(s) {
24
+ const m = { '已关闭': 'green', '整改中': 'blue', '监控中': 'gray', 'QRQC 跟踪': 'orange' };
25
+ return tag(s || '—', m[s] || 'blue');
26
+ }
27
+ function num(n, d) { return (n === null || n === undefined || n === '' || isNaN(n)) ? '—' : Number(n).toFixed(d === undefined ? 2 : d); }
28
+ function pct(n, d) { return (n === null || n === undefined || isNaN(n)) ? '—' : Number(n).toFixed(d === undefined ? 1 : d) + '%'; }
29
+ function dt(s) { return s ? String(s).slice(0, 16).replace('T', ' ') : '—'; }
30
+ function day(s) { return s ? String(s).slice(0, 10) : '—'; }
31
+ function bar(p, color) {
32
+ const w = Math.max(0, Math.min(100, Number(p) || 0));
33
+ return '<div class="bar-track"><div class="bar-fill" style="width:' + w + '%;background:' + (color || '#1a56db') + ';"></div></div>';
34
+ }
35
+ function card(title, sub, body, extra) {
36
+ return '<div class="card"><div class="card-hd"><span class="t">' + esc(title) + '</span>' +
37
+ (sub ? '<span class="s">' + sub + '</span>' : '') + (extra ? '<span class="more">' + extra + '</span>' : '') +
38
+ '</div>' + body + '</div>';
39
+ }
40
+ function kpi(o) {
41
+ return '<div class="kpi"' + (o.act ? ' data-act="' + esc(o.act) + '" data-args=\'' + esc(JSON.stringify(o.args || {})) + '\'' : '') + '>' +
42
+ (o.ico ? '<div class="k-ico" style="background:' + (o.icoBg || '#e4edff') + ';">' + o.ico + '</div>' : '') +
43
+ '<div class="k-label">' + esc(o.label) + '</div>' +
44
+ '<div class="k-value" style="color:' + (o.color || '#0b3ea8') + ';">' + (o.value === undefined ? '—' : o.value) + '</div>' +
45
+ '<div class="k-delta" style="' + (o.deltaColor ? 'color:' + o.deltaColor : '') + '">' + (o.delta || '') + '</div></div>';
46
+ }
47
+ function table(headers, rows, opts) {
48
+ opts = opts || {};
49
+ let h = '<div class="tbl-wrap"><table class="tbl"><thead><tr>' + headers.map((x) => '<th>' + (x.html || esc(x)) + '</th>').join('') + '</tr></thead><tbody>';
50
+ if (!rows || !rows.length) h += '<tr><td colspan="' + headers.length + '"><div class="empty"><span class="e-ico">📭</span>' + esc(opts.empty || '暂无数据') + '</div></td></tr>';
51
+ else rows.forEach((r) => {
52
+ const rowAttr = r.__act ? ' class="rowlink" data-act="' + esc(r.__act) + '" data-args=\'' + esc(JSON.stringify(r.__args || {})) + '\'' : '';
53
+ h += '<tr' + rowAttr + '>' + (r.cells || r).map((c, index) =>
54
+ '<td' + (opts.mobileLabels ? ' data-label="' + esc(headers[index]) + '"' : '') + '>' +
55
+ (c === undefined || c === null ? '—' : c) + '</td>').join('') + '</tr>';
56
+ });
57
+ return h + '</tbody></table></div>';
58
+ }
59
+ function empty(text, ico) { return '<div class="empty"><span class="e-ico">' + (ico || '📭') + '</span>' + esc(text || '暂无数据') + '</div>'; }
60
+ function recentAudit({ rows, total }, key, detailed = false) {
61
+ const entries = (rows || []).slice(0, 10);
62
+ const columns = ['时间', '操作人', '插件', '动作', '结果摘要', ...(detailed ? ['参数', '用途'] : [])];
63
+ const details = entries.map((row) => [
64
+ esc(dt(row.ts)), esc(row.user), tag(row.plugin, 'navy'), esc(row.action), esc((row.resultSummary || '').slice(0, 70)),
65
+ ...(detailed ? [esc(JSON.stringify(row.params || {}).slice(0, 100)), esc(row.purpose || '')] : []),
66
+ ]);
67
+ return `<details class="qip-audit">
68
+ <summary><span data-live-region="${key}-summary">${entries.length} 条最近记录 · 共 ${total ?? entries.length} 条</span>
69
+ <span class="qip-audit-expand">展开</span><span class="qip-audit-collapse">折叠</span></summary>
70
+ <div data-live-region="${key}-entries">${table(columns, details, { empty:'暂无审计记录',mobileLabels:true })}</div>
71
+ </details>`;
72
+ }
73
+ function loadingBlock(text) { return '<div class="empty"><span class="spin"></span> ' + esc(text || '加载中…') + '</div>'; }
74
+ function infoGrid(items, cls) {
75
+ return '<div class="info-grid' + (cls ? ' ' + cls : '') + '">' + items.map(([l, v, span]) =>
76
+ '<div class="info-item"' + (span ? ' style="grid-column:1/-1"' : '') + '><div class="lbl">' + esc(l) + '</div><div class="val">' + (v === undefined || v === null || v === '' ? '—' : v) + '</div></div>').join('') + '</div>';
77
+ }
78
+
79
+ /* ---------------- Toast ---------------- */
80
+ function toast(msg, type, ms) {
81
+ let wrap = document.getElementById('toastWrap');
82
+ if (!wrap) { wrap = document.createElement('div'); wrap.id = 'toastWrap'; document.body.appendChild(wrap); }
83
+ const el = document.createElement('div');
84
+ el.className = 'toast-item ' + (type || '');
85
+ const ico = { ok: '✅', ai: '🤖', warn: '⚠️', err: '❌' }[type] || 'ℹ️';
86
+ el.innerHTML = '<span>' + ico + '</span><span>' + esc(msg) + '</span>';
87
+ wrap.appendChild(el);
88
+ setTimeout(() => { el.classList.add('out'); setTimeout(() => el.remove(), 320); }, ms || (type === 'err' ? 7000 : 3600));
89
+ }
90
+
91
+ /* ---------------- 弹窗 ---------------- */
92
+ let curModal = null;
93
+ let onModalClose;
94
+ function modal(o) {
95
+ closeModal();
96
+ const div = document.createElement('div');
97
+ div.className = 'dm-overlay show';
98
+ div.innerHTML = '<div class="dm-panel ' + (o.width || '') + '">' +
99
+ '<div class="dm-head"><div><div class="dm-t">' + (o.title || '') + '</div>' + (o.sub ? '<div class="dm-s">' + o.sub + '</div>' : '') + '</div><div class="dm-close" data-act="__closeModal">✕</div></div>' +
100
+ '<div class="dm-body">' + (o.body || '') + '</div>' +
101
+ (o.footer ? '<div class="dm-footer">' + o.footer + '</div>' : '') + '</div>';
102
+ div.addEventListener('click', (e) => { if (e.target === div) closeModal(); });
103
+ document.body.appendChild(div);
104
+ curModal = div;
105
+ onModalClose = o.onClose;
106
+ if (o.onMount) o.onMount(div);
107
+ return div;
108
+ }
109
+ function closeModal() {
110
+ if (!curModal) return;
111
+ const onClose = onModalClose;
112
+ curModal.remove();
113
+ curModal = null;
114
+ onModalClose = null;
115
+ onClose?.();
116
+ }
117
+
118
+ /* ---------------- Markdown 渲染 ---------------- */
119
+ const markdown = global.markdownit?.({ html: false, breaks: true, linkify: false });
120
+ if (markdown) {
121
+ const rules = markdown.renderer.rules;
122
+ rules.table_open = () => '<div class="markdown-table-scroll" tabindex="0" role="region" aria-label="表格,可横向滚动"><table>\n';
123
+ rules.table_close = () => '</table></div>\n';
124
+ rules.image = (tokens, index, options, env, renderer) =>
125
+ esc(renderer.renderInlineAsText(tokens[index].children, options, env));
126
+ rules.link_open = (tokens, index, options, env, renderer) => {
127
+ tokens[index].attrSet('target', '_blank');
128
+ tokens[index].attrSet('rel', 'noopener noreferrer');
129
+ return renderer.renderToken(tokens, index, options);
130
+ };
131
+ }
132
+
133
+ function normalizeMarkdown(text) {
134
+ let fence;
135
+ return text.split(/\r\n?|\n/).map((line) => {
136
+ const marker = /^( {0,3})(\x60{3,}|~{3,})(.*)$/.exec(line.replace(/^(?: {0,3}>[ \t]?)+/, ''));
137
+ if (fence) {
138
+ if (marker && marker[2][0] === fence[0] && marker[2].length >= fence.length && !marker[3].trim()) fence = undefined;
139
+ return line;
140
+ }
141
+ if (marker) { fence = marker[2]; return line; }
142
+ // Generated answers sometimes leave empty list entries or lone hard-break markers.
143
+ if (/^ {0,3}(?:(?:[-+*]|\d{1,9}[.)、])(?:[ \t]+\\)?|\\)[ \t]*$/.test(line)) return '';
144
+ return line
145
+ .replace(/^( {0,3}\d{1,9})、[ \t]+/, '$1. ')
146
+ .replace(/(\*\*|__)[ \t]*\\[ \t]*$/, '$1');
147
+ }).join('\n');
148
+ }
149
+
150
+ function md(text) {
151
+ if (!text) return '';
152
+ const source = normalizeMarkdown(String(text));
153
+ return markdown ? markdown.render(source) : '<p>' + esc(source).replace(/\n/g, '<br>') + '</p>';
154
+ }
155
+
156
+ function revisionCard(item, action, i) {
157
+ const { id, name, state, documentId, changes } = item;
158
+ const submit = (label,approved) => '<button type="button" class="btn btn-sm' + (approved ? ' btn-primary' : '') +
159
+ '" data-act="' + esc(action) + '" data-args=\'' + esc(JSON.stringify({i,id,approved})) + '\'' +
160
+ (item.submitting ? ' disabled' : '') + '>' + label + '</button>';
161
+ if (state === 'updated' || state === 'saved') return '<div class="confirm-card kb-revision"><strong>' +
162
+ (state === 'updated' ? '文件已更新' : '此前已保存修订稿') + '</strong><p>' +
163
+ esc(name) + '</p><div class="cc-a"><a class="btn btn-sm" download href="/qip-real-workbench/api/enterprise-kb/files/' +
164
+ encodeURIComponent(documentId) + '">下载修订稿</a><a class="btn btn-sm" href="#companyLibrary">打开企业知识库</a></div></div>';
165
+ if (state !== 'pending') {
166
+ const label = state === 'rejected' ? '修订已取消' : state === 'removed' ? '修订稿已移除' :
167
+ state === 'pruned' ? '旧版已按保留规则清理' : '修订预览已过期';
168
+ return '<div class="confirm-card kb-revision"><strong>' + label + '</strong><p>' + esc(name) + '</p></div>';
169
+ }
170
+ return '<div class="confirm-card kb-revision"><strong>修订预览</strong><p>' + esc(name) +
171
+ '</p><ol class="kb-revision-edits">' + changes.map(({where,from,to}) =>
172
+ '<li><span>' + esc(where) + '</span><div class="kb-revision-diff"><del aria-label="原文">' + esc(from) +
173
+ '</del><ins aria-label="改为">' + esc(to || '删去这段文字') + '</ins></div></li>').join('') +
174
+ '</ol><div class="cc-a">' + submit('确认更新',true) + submit('取消修订',false) + '</div></div>';
175
+ }
176
+
177
+ function chatConfirmations(pending, action, i) {
178
+ return (pending || []).map(item => {
179
+ if (item.kind === 'knowledgeRevision') return revisionCard(item,action,i);
180
+ const {id,label} = item;
181
+ return '<div class="confirm-card"><strong>待人工确认 · ' + esc(label) +
182
+ '</strong><div class="cc-a">' + act('查看并确认', action, { i, id, approved: true }, 'btn btn-sm') +
183
+ act('拒绝', action, { i, id, approved: false }, 'btn btn-sm') + '</div></div>';
184
+ }).join('');
185
+ }
186
+
187
+ async function decideChatConfirmation(message, id, approved, render) {
188
+ const pending = message?.pending?.find((item) => item.id === id);
189
+ if (!pending) return;
190
+ if (pending.kind === 'knowledgeRevision') {
191
+ if (pending.state !== 'pending' || pending.submitting) return;
192
+ pending.submitting = true;
193
+ render();
194
+ try {
195
+ const result = await API.post('/qip-real-workbench/api/enterprise-kb/revisions/' + encodeURIComponent(id),{approved});
196
+ if (!result.ok) throw new Error(result.error || '修订未保存');
197
+ Object.assign(pending,result.preview);
198
+ toast(approved ? '文件已更新,知识库已采用新版。' : '已取消修订','ok');
199
+ if (approved) global.dispatchEvent(new Event('qip:knowledge-revised'));
200
+ } finally { pending.submitting = false; render(); }
201
+ return;
202
+ }
203
+ if (approved) {
204
+ const confirmed = await global.AskConfirm('确认业务操作', [
205
+ ['操作', pending.label], ['提交内容', JSON.stringify(pending.args || {})],
206
+ ]);
207
+ if (!confirmed) return;
208
+ }
209
+ const result = await API.post('/qip-real-workbench/api/chat/' + (approved ? 'confirm' : 'reject'),
210
+ { id, ...(approved ? {} : { reason: '用户拒绝' }) });
211
+ if (!result.ok) throw new Error(result.error || result.result?.error || '操作未完成');
212
+ message.pending = message.pending.filter((item) => item.id !== id);
213
+ toast(approved ? result.summary || '操作已执行' : '已拒绝该动作', 'ok');
214
+ render();
215
+ }
216
+
217
+ function attachDraftContinuation(box, result) {
218
+ if (result?.state !== 'incomplete' || !result.answerId) return;
219
+ const pre = box.querySelector('pre');
220
+ if (!pre) return;
221
+ const status = document.createElement('div');
222
+ box.appendChild(status);
223
+ const message = { answerId: result.answerId, state: 'incomplete', finishReason:result.finishReason, text: '' };
224
+ const click = async () => {
225
+ if (message.running) return;
226
+ await API.resumeAnswer(message, { render });
227
+ if (message.text) pre.textContent = message.text;
228
+ if (message.state === 'complete') box.querySelector('.sqm-warnbox')?.remove();
229
+ };
230
+ const render = () => {
231
+ status.innerHTML = answerStatus(message, '<button type="button" class="btn btn-sm">继续生成</button>');
232
+ status.querySelector('button')?.addEventListener('click', click);
233
+ };
234
+ render();
235
+ }
236
+
237
+ function answerStatus(message, resumeButton) {
238
+ if (message.running) return '<p class="run-status" role="status">' + esc(message.status || '正在生成回答…') + '</p>';
239
+ if (message.state !== 'incomplete' && !message.error) {
240
+ return [message.sourceNotice,message.historyWarning].filter(Boolean)
241
+ .map(text=>'<p class="run-status" role="status">'+esc(text)+'</p>').join('');
242
+ }
243
+ const reason = message.finishReason === 'model-balance' ? 'DeepSeek 调用额度不足。恢复额度或接入可用模型后可继续生成。' :
244
+ message.finishReason === 'model-credentials' ? 'DSH 当前没有可用的模型接入。请在 DSH 模型设置中检查。' : '';
245
+ return '<div class="run-status" role="status">' + esc(message.error || reason || '这条回复尚未完成。') +
246
+ (message.answerId && resumeButton ? ' ' + resumeButton : '') + '</div>';
247
+ }
248
+
249
+ /* ---------------- SVG 图表 ---------------- */
250
+ /** 折线图(可多序列、双 Y 轴) */
251
+ function lineChart(o) {
252
+ const W = o.width || 560, H = o.height || 200, pad = { l: 46, r: o.rightAxis ? 46 : 16, t: 16, b: 28 };
253
+ const series = (o.series || []).filter((s) => s.values && s.values.length);
254
+ if (!series.length) return empty('暂无趋势数据');
255
+ const all = series.filter((s) => !s.right).flatMap((s) => s.values);
256
+ const allR = series.filter((s) => s.right).flatMap((s) => s.values);
257
+ const labels = o.labels || series[0].values.map((_, i) => String(i + 1));
258
+ let min = o.min !== undefined ? o.min : Math.min.apply(null, all.length ? all : [0]);
259
+ let max = o.max !== undefined ? o.max : Math.max.apply(null, all.length ? all : [1]);
260
+ if (min === max) { min -= 1; max += 1; }
261
+ const padY = (max - min) * 0.12; min -= padY; max += padY;
262
+ let rmin = allR.length ? Math.min.apply(null, allR) : 0, rmax = allR.length ? Math.max.apply(null, allR) : 1;
263
+ if (rmin === rmax) { rmin -= 1; rmax += 1; }
264
+ const rp = (rmax - rmin) * 0.12; rmin -= rp; rmax += rp;
265
+ const px = (i) => pad.l + (W - pad.l - pad.r) * (labels.length <= 1 ? 0.5 : i / (labels.length - 1));
266
+ const py = (v) => H - pad.b - (H - pad.t - pad.b) * ((v - min) / (max - min));
267
+ const pry = (v) => H - pad.b - (H - pad.t - pad.b) * ((v - rmin) / (rmax - rmin));
268
+ let g = '';
269
+ for (let i = 0; i <= 4; i++) {
270
+ const y = pad.t + (H - pad.t - pad.b) * i / 4;
271
+ const v = max - (max - min) * i / 4;
272
+ g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/>' +
273
+ '<text x="' + (pad.l - 6) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + v.toFixed(o.yDigits === undefined ? 1 : o.yDigits) + '</text>';
274
+ if (o.rightAxis && allR.length) g += '<text x="' + (W - pad.r + 6) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#18a058">' + (rmax - (rmax - rmin) * i / 4).toFixed(o.rDigits === undefined ? 2 : o.rDigits) + '</text>';
275
+ }
276
+ series.forEach((s) => {
277
+ const pts = s.values.map((v, i) => px(i).toFixed(1) + ',' + (s.right ? pry(v) : py(v)).toFixed(1)).join(' ');
278
+ g += '<polyline points="' + pts + '" fill="none" stroke="' + (s.color || '#1a56db') + '" stroke-width="' + (s.width || 2) + '"' + (s.dash ? ' stroke-dasharray="' + s.dash + '"' : '') + '/>';
279
+ if (s.dots !== false) s.values.forEach((v, i) => { g += '<circle cx="' + px(i).toFixed(1) + '" cy="' + (s.right ? pry(v) : py(v)).toFixed(1) + '" r="2.6" fill="' + (s.color || '#1a56db') + '"/>'; });
280
+ });
281
+ // X 轴标签(抽样显示,避免重叠)
282
+ const step = Math.max(1, Math.ceil(labels.length / 8));
283
+ labels.forEach((l, i) => { if (i % step === 0 || i === labels.length - 1) g += '<text x="' + px(i).toFixed(1) + '" y="' + (H - 8) + '" font-size="9" fill="#8791a5" text-anchor="middle">' + esc(String(l).length > 10 ? String(l).slice(5) : l) + '</text>'; });
284
+ return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
285
+ }
286
+
287
+ /** 控制图:数据点 + UCL/CL/LCL + 规格限 + 判异点高亮 */
288
+ function controlChart(o) {
289
+ const W = o.width || 640, H = o.height || 240, pad = { l: 52, r: 18, t: 18, b: 34 };
290
+ const pts = o.points && o.points.length ? o.points : (o.values || []);
291
+ const values = pts.map((p) => (typeof p === 'number' ? p : p.value));
292
+ if (!values.length) return empty('暂无控制图数据');
293
+ const dynamic = pts.length && pts.every((p) => typeof p.ucl === 'number' && typeof p.lcl === 'number');
294
+ const lines = [o.ucl, o.cl, o.lcl, ...(dynamic ? pts.flatMap((p) => [p.ucl, p.lcl]) : [])].filter((x) => typeof x === 'number');
295
+ const spec = [o.usl, o.lsl].filter((x) => typeof x === 'number');
296
+ let min = Math.min.apply(null, values.concat(lines, spec.length ? spec : lines));
297
+ let max = Math.max.apply(null, values.concat(lines, spec.length ? spec : lines));
298
+ const pAdv = (max - min) * 0.1 || 1; min -= pAdv; max += pAdv;
299
+ const px = (i) => pad.l + (W - pad.l - pad.r) * (values.length <= 1 ? 0.5 : i / (values.length - 1));
300
+ const py = (v) => H - pad.b - (H - pad.t - pad.b) * ((v - min) / (max - min));
301
+ let g = '';
302
+ for (let i = 0; i <= 4; i++) {
303
+ const y = pad.t + (H - pad.t - pad.b) * i / 4, v = max - (max - min) * i / 4;
304
+ g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/>' +
305
+ '<text x="' + (pad.l - 6) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + v.toFixed(3) + '</text>';
306
+ }
307
+ function hline(v, color, dash, label) {
308
+ if (typeof v !== 'number') return '';
309
+ const y = py(v).toFixed(1);
310
+ return '<line x1="' + pad.l + '" y1="' + y + '" x2="' + (W - pad.r) + '" y2="' + y + '" stroke="' + color + '" stroke-width="1.3"' + (dash ? ' stroke-dasharray="' + dash + '"' : '') + '/>' +
311
+ '<text x="' + (W - pad.r - 2) + '" y="' + (Number(y) - 3) + '" font-size="9" fill="' + color + '" text-anchor="end">' + label + '</text>';
312
+ }
313
+ if (!dynamic) g += hline(o.ucl, '#e5484d', '6 4', 'UCL ' + (o.ucl !== undefined ? Number(o.ucl).toFixed(3) : ''));
314
+ g += hline(o.cl, '#18a058', '', 'CL ' + (o.cl !== undefined ? Number(o.cl).toFixed(3) : ''));
315
+ if (!dynamic) g += hline(o.lcl, '#e5484d', '6 4', 'LCL ' + (o.lcl !== undefined ? Number(o.lcl).toFixed(3) : ''));
316
+ if (typeof o.usl === 'number') g += hline(o.usl, '#c8620a', '2 3', 'USL ' + o.usl);
317
+ if (typeof o.lsl === 'number') g += hline(o.lsl, '#c8620a', '2 3', 'LSL ' + o.lsl);
318
+ if (dynamic) {
319
+ for (const name of ['ucl', 'lcl']) {
320
+ const limit = pts.map((p, i) => px(i).toFixed(1) + ',' + py(p[name]).toFixed(1)).join(' ');
321
+ g += '<polyline points="' + limit + '" fill="none" stroke="#e5484d" stroke-dasharray="6 4"/>';
322
+ }
323
+ }
324
+ const poly = values.map((v, i) => px(i).toFixed(1) + ',' + py(v).toFixed(1)).join(' ');
325
+ g += '<polyline points="' + poly + '" fill="none" stroke="#1a56db" stroke-width="1.8"/>';
326
+ values.forEach((v, i) => {
327
+ const p = pts[i];
328
+ const ooc = typeof p === 'object' ? p.outOfControl : (typeof o.ucl === 'number' && (v > o.ucl || v < o.lcl));
329
+ const color = ooc ? '#e5484d' : p?.alarm ? '#c8620a' : '#1a56db';
330
+ g += '<circle cx="' + px(i).toFixed(1) + '" cy="' + py(v).toFixed(1) + '" r="' + (ooc ? 4.2 : 2.6) + '" fill="' + color + '"' + (ooc ? ' stroke="#fff" stroke-width="1.2"' : '') + '><title>' + esc([p?.label, p?.time].filter(Boolean).join(' ')) + ' ' + v + '</title></circle>';
331
+ });
332
+ const labels = o.labels || [];
333
+ const step = Math.max(1, Math.ceil(values.length / 10));
334
+ values.forEach((v, i) => { if (i % step === 0 || i === values.length - 1) g += '<text x="' + px(i).toFixed(1) + '" y="' + (H - 10) + '" font-size="8.5" fill="#8791a5" text-anchor="middle">' + (labels[i] !== undefined ? esc(String(labels[i]).slice(5, 10)) : (i + 1)) + '</text>'; });
335
+ return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
336
+ }
337
+
338
+ /** 帕累托图(柱 + 累计折线 + 80% 线) */
339
+ function paretoChart(rows, o) {
340
+ o = o || {};
341
+ if (!rows || !rows.length) return empty('暂无数据');
342
+ const W = 620, H = 250, pad = { l: 44, r: 46, t: 20, b: 46 };
343
+ const max = Math.max.apply(null, rows.map((r) => r.value));
344
+ const bw = (W - pad.l - pad.r) / rows.length;
345
+ const py = (v) => H - pad.b - (H - pad.t - pad.b) * (v / max);
346
+ const pyc = (p) => H - pad.b - (H - pad.t - pad.b) * (p / 100);
347
+ let g = '';
348
+ for (let i = 0; i <= 4; i++) {
349
+ const y = pad.t + (H - pad.t - pad.b) * i / 4;
350
+ g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/>' +
351
+ '<text x="' + (pad.l - 5) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + Math.round(max - max * i / 4) + '</text>' +
352
+ '<text x="' + (W - pad.r + 5) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#e5484d">' + Math.round(100 - 100 * i / 4) + '%</text>';
353
+ }
354
+ const colors = ['#1a56db', '#5a8bff', '#7a5af8', '#0a9e8f', '#95a3bd', '#c3cbd9'];
355
+ rows.forEach((r, i) => {
356
+ const x = pad.l + i * bw + bw * 0.15, w = bw * 0.7, y = py(r.value);
357
+ g += '<rect x="' + x.toFixed(1) + '" y="' + y.toFixed(1) + '" width="' + w.toFixed(1) + '" height="' + (H - pad.b - y).toFixed(1) + '" rx="3" fill="' + colors[i % colors.length] + '"><title>' + esc(r.name) + ':' + r.value + '</title></rect>' +
358
+ '<text x="' + (x + w / 2).toFixed(1) + '" y="' + (y - 4).toFixed(1) + '" font-size="9.5" fill="' + colors[i % colors.length] + '" text-anchor="middle" font-weight="600">' + r.value + '</text>' +
359
+ '<text x="' + (x + w / 2).toFixed(1) + '" y="' + (H - 30) + '" font-size="9" fill="#5e6b85" text-anchor="middle">' + esc(String(r.name).slice(0, 5)) + '</text>';
360
+ });
361
+ if (rows[0].cumPct !== undefined) {
362
+ const pts = rows.map((r, i) => (pad.l + i * bw + bw / 2).toFixed(1) + ',' + pyc(r.cumPct).toFixed(1)).join(' ');
363
+ g += '<polyline points="' + pts + '" fill="none" stroke="#e5484d" stroke-width="1.8"/>';
364
+ rows.forEach((r, i) => { g += '<circle cx="' + (pad.l + i * bw + bw / 2).toFixed(1) + '" cy="' + pyc(r.cumPct).toFixed(1) + '" r="3" fill="#e5484d"><title>累计 ' + r.cumPct.toFixed(1) + '%</title></circle>'; });
365
+ g += '<line x1="' + pad.l + '" y1="' + pyc(80).toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + pyc(80).toFixed(1) + '" stroke="#e5484d" stroke-dasharray="5 4"/>' +
366
+ '<text x="' + (pad.l + 4) + '" y="' + (pyc(80) - 4).toFixed(1) + '" font-size="9" fill="#e5484d">80% 关键少数线</text>';
367
+ }
368
+ g += '<line x1="' + pad.l + '" y1="' + (H - pad.b) + '" x2="' + (W - pad.r) + '" y2="' + (H - pad.b) + '" stroke="#e4e8f0"/>';
369
+ return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
370
+ }
371
+
372
+ /** 直方图 + 规格限 */
373
+ function histogramChart(bins, lsl, usl) {
374
+ if (!bins || !bins.length) return empty('暂无分布数据');
375
+ const W = 620, H = 210, pad = { l: 44, r: 18, t: 16, b: 34 };
376
+ const max = Math.max.apply(null, bins.map((b) => b.count)) || 1;
377
+ const lo = bins[0].from, hi = bins[bins.length - 1].to;
378
+ const bw = (W - pad.l - pad.r) / bins.length;
379
+ const px = (v) => pad.l + (W - pad.l - pad.r) * ((v - lo) / ((hi - lo) || 1));
380
+ let g = '';
381
+ for (let i = 0; i <= 3; i++) {
382
+ const y = pad.t + (H - pad.t - pad.b) * i / 3;
383
+ g += '<line x1="' + pad.l + '" y1="' + y.toFixed(1) + '" x2="' + (W - pad.r) + '" y2="' + y.toFixed(1) + '" stroke="#eef1f6"/><text x="' + (pad.l - 5) + '" y="' + (y + 3.5).toFixed(1) + '" font-size="9" fill="#8791a5" text-anchor="end">' + Math.round(max - max * i / 3) + '</text>';
384
+ }
385
+ bins.forEach((b, i) => {
386
+ const h = (H - pad.t - pad.b) * (b.count / max);
387
+ const x = pad.l + i * bw, y = H - pad.b - h;
388
+ const out = (usl !== undefined && b.from >= usl) || (lsl !== undefined && b.to <= lsl);
389
+ g += '<rect x="' + (x + 1).toFixed(1) + '" y="' + y.toFixed(1) + '" width="' + (bw - 2).toFixed(1) + '" height="' + h.toFixed(1) + '" fill="' + (out ? '#e5484d' : '#5a8bff') + '" rx="2"><title>' + b.from + ' ~ ' + b.to + ':' + b.count + ' 个</title></rect>';
390
+ });
391
+ if (lsl !== undefined) g += '<line x1="' + px(lsl).toFixed(1) + '" y1="' + pad.t + '" x2="' + px(lsl).toFixed(1) + '" y2="' + (H - pad.b) + '" stroke="#c8620a" stroke-dasharray="4 3"/><text x="' + (px(lsl) + 3).toFixed(1) + '" y="' + (pad.t + 10) + '" font-size="9" fill="#c8620a">LSL</text>';
392
+ if (usl !== undefined) g += '<line x1="' + px(usl).toFixed(1) + '" y1="' + pad.t + '" x2="' + px(usl).toFixed(1) + '" y2="' + (H - pad.b) + '" stroke="#c8620a" stroke-dasharray="4 3"/><text x="' + (px(usl) - 24).toFixed(1) + '" y="' + (pad.t + 10) + '" font-size="9" fill="#c8620a">USL</text>';
393
+ g += '<line x1="' + pad.l + '" y1="' + (H - pad.b) + '" x2="' + (W - pad.r) + '" y2="' + (H - pad.b) + '" stroke="#e4e8f0"/>';
394
+ g += '<text x="' + pad.l + '" y="' + (H - 12) + '" font-size="9" fill="#8791a5">' + lo + '</text><text x="' + (W - pad.r) + '" y="' + (H - 12) + '" font-size="9" fill="#8791a5" text-anchor="end">' + hi + '</text>';
395
+ return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
396
+ }
397
+
398
+ /** 因果图(鱼骨图):按【人机料法环测】六分支渲染 */
399
+ function fishbone(o) {
400
+ const W = 720, H = 300;
401
+ const cats = o.categories || [];
402
+ const spineY = H / 2;
403
+ let g = '<line x1="40" y1="' + spineY + '" x2="600" y2="' + spineY + '" stroke="#33415c" stroke-width="2"/>' +
404
+ '<polygon points="600,' + spineY + ' 586,' + (spineY - 8) + ' 586,' + (spineY + 8) + '" fill="#33415c"/>' +
405
+ '<rect x="602" y="' + (spineY - 26) + '" width="108" height="52" rx="7" fill="#e5484d"/>' +
406
+ '<text x="656" y="' + (spineY - 4) + '" font-size="11.5" fill="#fff" text-anchor="middle">' + esc(String(o.effect || '问题').slice(0, 10)) + '</text>' +
407
+ '<text x="656" y="' + (spineY + 12) + '" font-size="9" fill="#fff" text-anchor="middle">' + esc(String(o.effectValue || '').slice(0, 14)) + '</text>';
408
+ const top = cats.filter((_, i) => i % 2 === 0).slice(0, 3);
409
+ const bottom = cats.filter((_, i) => i % 2 === 1).slice(0, 3);
410
+ const xs = [120, 280, 440];
411
+ function branch(cat, x, up) {
412
+ const y = up ? 60 : H - 60;
413
+ let s = '<line x1="' + x + '" y1="' + spineY + '" x2="' + (x + 24) + '" y2="' + y + '" stroke="#95a3bd"/>';
414
+ s += '<rect x="' + (x - 46) + '" y="' + (up ? y - 22 : y + 4) + '" width="92" height="19" rx="5" fill="#e4edff"/>' +
415
+ '<text x="' + x + '" y="' + (up ? y - 9 : y + 17) + '" font-size="10.5" fill="#1a56db" text-anchor="middle" font-weight="600">' + esc(cat.name || '') + '</text>';
416
+ (cat.items || []).slice(0, 3).forEach((it, i) => {
417
+ const iy = up ? y + 14 + i * 15 : y - 14 - i * 15;
418
+ const p = it.p !== undefined ? ' P=' + it.p : '';
419
+ s += '<text x="' + (x + 30) + '" y="' + iy + '" font-size="9.5" fill="' + (it.p >= 0.5 ? '#e5484d' : it.p >= 0.2 ? '#c8620a' : '#8791a5') + '">' + esc(String(it.text || it).slice(0, 22)) + p + '</text>';
420
+ });
421
+ return s;
422
+ }
423
+ top.forEach((c, i) => { g += branch(c, xs[i], true); });
424
+ bottom.forEach((c, i) => { g += branch(c, xs[i], false); });
425
+ return '<svg class="chart-svg" viewBox="0 0 ' + W + ' ' + H + '">' + g + '</svg>';
426
+ }
427
+
428
+ /** 环形进度 */
429
+ function ring(p, color, label) {
430
+ const r = 26, c = 2 * Math.PI * r, off = c * (1 - Math.max(0, Math.min(100, p)) / 100);
431
+ return '<svg width="66" height="66" viewBox="0 0 66 66"><circle cx="33" cy="33" r="' + r + '" fill="none" stroke="#eef1f6" stroke-width="7"/>' +
432
+ '<circle cx="33" cy="33" r="' + r + '" fill="none" stroke="' + (color || '#1a56db') + '" stroke-width="7" stroke-linecap="round" stroke-dasharray="' + c.toFixed(1) + '" stroke-dashoffset="' + off.toFixed(1) + '" transform="rotate(-90 33 33)"/>' +
433
+ '<text x="33" y="37" font-size="13" font-weight="700" fill="' + (color || '#1a56db') + '" text-anchor="middle">' + Math.round(p) + '</text></svg>' + (label ? '<div style="font-size:10.5px;color:#8791a5;text-align:center;">' + esc(label) + '</div>' : '');
434
+ }
435
+
436
+ /* ---------------- 表单 ---------------- */
437
+ function input(name, value, o) {
438
+ o = o || {};
439
+ if (o.type === 'select') {
440
+ return '<select class="inp" data-field="' + esc(name) + '"' + (o.style ? ' style="' + o.style + '"' : '') + '>' +
441
+ (o.options || []).map((op) => '<option value="' + esc(op.value !== undefined ? op.value : op) + '"' + (String(op.value !== undefined ? op.value : op) === String(value) ? ' selected' : '') + '>' + esc(op.label || op) + '</option>').join('') + '</select>';
442
+ }
443
+ if (o.type === 'textarea') return '<textarea class="inp" rows="' + (o.rows || 3) + '" data-field="' + esc(name) + '" placeholder="' + esc(o.placeholder || '') + '">' + esc(value || '') + '</textarea>';
444
+ if (o.type === 'checkbox') return '<label class="switch"><input type="checkbox" data-field="' + esc(name) + '"' + (value ? ' checked' : '') + '/> ' + esc(o.label || '') + '</label>';
445
+ return '<input class="inp' + (o.small ? ' inp-sm' : '') + '" type="' + (o.type || 'text') + '" data-field="' + esc(name) + '" value="' + esc(value === undefined ? '' : value) + '" placeholder="' + esc(o.placeholder || '') + '"' + (o.style ? ' style="' + o.style + '"' : '') + '/>';
446
+ }
447
+ function readForm(scope) {
448
+ const out = {};
449
+ scope.querySelectorAll('[data-field]').forEach((el) => {
450
+ out[el.getAttribute('data-field')] = el.type === 'checkbox' ? el.checked : (el.type === 'number' ? (el.value === '' ? null : Number(el.value)) : el.value);
451
+ });
452
+ return out;
453
+ }
454
+
455
+ /* ---------------- 平台显示名 ---------------- */
456
+ /** 初始设置里填了公司名称就用公司名称,否则用「全质开源质量平台」 */
457
+ function brand() {
458
+ const b = (global.State && global.State.bootstrap) || {};
459
+ const v = typeof b.brand === 'string' && b.brand.trim();
460
+ return v || '全质开源质量平台';
461
+ }
462
+
463
+ global.UI = {
464
+ answerStatus, attachDraftContinuation, chatConfirmations, decideChatConfirmation, esc, attr, act, tag, sevTag, statusTag, num, pct, dt, day, bar, card, kpi, table, recentAudit, empty, loadingBlock, infoGrid,
465
+ toast, modal, closeModal, md, lineChart, controlChart, paretoChart, histogramChart, fishbone, ring, input, readForm, brand,
466
+ };
467
+ })(window);