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.
- package/CONTRIBUTING.md +51 -47
- package/LICENSE.md +37 -37
- package/README.en.md +32 -30
- package/README.md +90 -88
- package/agent-host.cjs +277 -277
- package/client.js +199 -92
- package/cordis.patch.yml +5 -5
- package/docs/enterprise-onboarding.md +10 -10
- package/docs/technical-reference.md +223 -223
- package/host.cjs +91 -124
- package/llm-bridge.cjs +234 -234
- package/locale/en.json +5 -5
- package/locale/zh.json +5 -5
- package/package.json +3 -2
- package/public/app.css +3 -0
- package/public/app.js +6 -2
- package/public/index.html +8 -9
- package/public/js/api.js +152 -152
- package/public/js/assistant-history.js +62 -62
- package/public/js/material-library.js +100 -100
- package/public/js/mobile-preview.js +119 -0
- package/public/js/pages-company-knowledge.js +9 -32
- package/public/js/pages-core.js +232 -232
- package/public/js/pages-growth.js +148 -148
- package/public/js/pages-qms.js +1 -1
- package/public/js/pages-qtools.js +410 -410
- package/public/js/pages-thanks.js +71 -71
- package/public/js/plugin-updates.js +53 -0
- package/public/js/quality-workflow.js +205 -205
- package/public/js/survey-dates.js +41 -41
- package/public/js/survey-knowledge.js +301 -301
- package/public/js/tool-wizards.js +621 -621
- package/public/js/ui.js +467 -467
- package/public/js/voice-capture-worklet.js +30 -30
- package/public/js/xiaozhi-voice.js +264 -264
- package/public/js/xiaozhi.js +468 -468
- package/public/platform-help.html +426 -426
- package/public/platform-site/assets/0468a68574112812.css +10703 -10703
- package/public/platform-site/assets/159e086d35adfa1c.css +2094 -2094
- package/public/platform-site/assets/169e3159cade0d9f.css +98 -98
- package/public/platform-site/assets/22b917c4a3d4ae3d.svg +14 -14
- package/public/platform-site/assets/476f9254ccf723e7.css +8265 -8265
- package/public/platform-site/assets/85a0a4fb4416ded9.css +120 -120
- package/public/platform-site/assets/a3b272383226f6d9.svg +135 -135
- package/public/platform-site/assets/fd16dc5f3cc9d97e.svg +104 -104
- package/public/platform-site/assets/qip-favicon.svg +5 -5
- package/public/platform-site/index.html +279 -279
- package/public/platform-site/offline.js +261 -261
- package/public/platform-site/qip-brand.css +566 -566
- package/public/platform-site/qip-module-guides.js +237 -237
- package/public/responsive.css +103 -101
- package/public/vendor/markdown-it-15.0.3.LICENSE +22 -22
- package/public/vendor/markdown-it-15.0.3.umd.min.js +13 -13
- package/public/workbench.css +7 -7
- package/public/xiaozhi.css +292 -292
- package/resources/qms-templates/019ffb58f24ed48d.docx +0 -0
- package/resources/qms-templates/04a69165fc15143a.docx +0 -0
- package/resources/qms-templates/09af69358c989a63.docx +0 -0
- package/resources/qms-templates/0e7ac77844e45311.docx +0 -0
- package/resources/qms-templates/12446a1b5e173c71.docx +0 -0
- package/resources/qms-templates/15ae40f67f9a3efc.docx +0 -0
- package/resources/qms-templates/18a69bbebf2ece7f.docx +0 -0
- package/resources/qms-templates/1978faa989134f02.docx +0 -0
- package/resources/qms-templates/1a30e121de752f7b.docx +0 -0
- package/resources/qms-templates/1b88907969ea7d4c.docx +0 -0
- package/resources/qms-templates/1e92b808722bf90a.docx +0 -0
- package/resources/qms-templates/2091cad340dc0e7f.docx +0 -0
- package/resources/qms-templates/271ae1002c896ed7.docx +0 -0
- package/resources/qms-templates/280ce53e5d3b5fcf.docx +0 -0
- package/resources/qms-templates/28ca8e4ede0adf6d.docx +0 -0
- package/resources/qms-templates/2ac6675f3cb1f993.docx +0 -0
- package/resources/qms-templates/2c3b799389909d8b.docx +0 -0
- package/resources/qms-templates/2c51038ba2cc3264.docx +0 -0
- package/resources/qms-templates/30c888851d16d764.docx +0 -0
- package/resources/qms-templates/3459fc4daaa2743d.docx +0 -0
- package/resources/qms-templates/3751dafc335e0e80.docx +0 -0
- package/resources/qms-templates/38798cd4298cd2db.docx +0 -0
- package/resources/qms-templates/38f551f9d738ab0a.docx +0 -0
- package/resources/qms-templates/3c85a23ea17018b4.docx +0 -0
- package/resources/qms-templates/3cdb7d14caf1b852.docx +0 -0
- package/resources/qms-templates/3f23239bb6b3e1db.docx +0 -0
- package/resources/qms-templates/42a5b9efeeaa8007.docx +0 -0
- package/resources/qms-templates/47352aafe9b8dac1.docx +0 -0
- package/resources/qms-templates/495359b1e3cbb8fe.docx +0 -0
- package/resources/qms-templates/4ca6920464785166.docx +0 -0
- package/resources/qms-templates/4ce862582a7791be.docx +0 -0
- package/resources/qms-templates/54c9a02a10991b9e.docx +0 -0
- package/resources/qms-templates/5aeb8fddf5f3693f.docx +0 -0
- package/resources/qms-templates/5d7e5576b0e73be7.docx +0 -0
- package/resources/qms-templates/5db3a40edac37791.docx +0 -0
- package/resources/qms-templates/5ffa21eb78eb6f77.docx +0 -0
- package/resources/qms-templates/60d0fbcb709b7161.docx +0 -0
- package/resources/qms-templates/63e1d3a166756095.docx +0 -0
- package/resources/qms-templates/65c724c080d32f10.docx +0 -0
- package/resources/qms-templates/674bd7826c6cbac2.docx +0 -0
- package/resources/qms-templates/677e851acca1b879.docx +0 -0
- package/resources/qms-templates/6bf15feb1a0d2a8d.docx +0 -0
- package/resources/qms-templates/6c3889c7d0918db9.docx +0 -0
- package/resources/qms-templates/6c4c349eeb0a96a8.docx +0 -0
- package/resources/qms-templates/6c5befbd33f759c1.docx +0 -0
- package/resources/qms-templates/6df8367901347508.docx +0 -0
- package/resources/qms-templates/71db51997037692b.docx +0 -0
- package/resources/qms-templates/78a8ef8c76510c99.docx +0 -0
- package/resources/qms-templates/8397bfd1c0a1b013.docx +0 -0
- package/resources/qms-templates/84d750cf858b60a1.docx +0 -0
- package/resources/qms-templates/880de58a4b334533.docx +0 -0
- package/resources/qms-templates/88c37d1b87d9b78c.docx +0 -0
- package/resources/qms-templates/88e9cd42f1849462.docx +0 -0
- package/resources/qms-templates/8956597403be35b6.docx +0 -0
- package/resources/qms-templates/94675c810a233e1f.docx +0 -0
- package/resources/qms-templates/94dff59dd7ebec6e.docx +0 -0
- package/resources/qms-templates/9670b9cd02141594.docx +0 -0
- package/resources/qms-templates/98c373ebd7b5163d.docx +0 -0
- package/resources/qms-templates/a4078cf2378a9388.docx +0 -0
- package/resources/qms-templates/a5b110c7ef917139.docx +0 -0
- package/resources/qms-templates/aec75820e45774be.docx +0 -0
- package/resources/qms-templates/b31c085e281d230c.docx +0 -0
- package/resources/qms-templates/b7c1c3c72f8ffeef.docx +0 -0
- package/resources/qms-templates/b81b82eaa5c8f422.docx +0 -0
- package/resources/qms-templates/b92a2fa31f75f137.docx +0 -0
- package/resources/qms-templates/b96c54ae9cfffc54.docx +0 -0
- package/resources/qms-templates/bc47cfd626c010e7.docx +0 -0
- package/resources/qms-templates/be60792734afca69.docx +0 -0
- package/resources/qms-templates/c0018d8ae9ad19e1.docx +0 -0
- package/resources/qms-templates/c171027c964c8be3.docx +0 -0
- package/resources/qms-templates/c4b93d71f5d98b4b.docx +0 -0
- package/resources/qms-templates/c531da29fcb3090f.docx +0 -0
- package/resources/qms-templates/c88eddd43358d015.docx +0 -0
- package/resources/qms-templates/cb978da542f0d892.docx +0 -0
- package/resources/qms-templates/ccde4a94532a8d36.docx +0 -0
- package/resources/qms-templates/cf3fb1e20ae6f24d.docx +0 -0
- package/resources/qms-templates/d231b4982fc8113e.docx +0 -0
- package/resources/qms-templates/d3c55aa5ed14055c.docx +0 -0
- package/resources/qms-templates/db45c44a00a13349.docx +0 -0
- package/resources/qms-templates/db849a41210f54e0.docx +0 -0
- package/resources/qms-templates/dba9ce363d83ddc8.docx +0 -0
- package/resources/qms-templates/dca3ea4c95eb2225.docx +0 -0
- package/resources/qms-templates/dfa4220d2b6b8131.docx +0 -0
- package/resources/qms-templates/dfa51222c777734f.docx +0 -0
- package/resources/qms-templates/e07ea511507e14f5.docx +0 -0
- package/resources/qms-templates/e0a2e00c03458dfa.docx +0 -0
- package/resources/qms-templates/e0e802f46b6764d1.docx +0 -0
- package/resources/qms-templates/e53fc85717f2852b.docx +0 -0
- package/resources/qms-templates/e9dd82d5e54520e5.docx +0 -0
- package/resources/qms-templates/ea7bfc22c06d82a9.docx +0 -0
- package/resources/qms-templates/ecf0db9f37294536.docx +0 -0
- package/resources/qms-templates/f05fcfc87e82ff39.docx +0 -0
- package/resources/qms-templates/fb9e56a17c4a4f97.docx +0 -0
- package/resources/qms-templates/manifest.json +932 -0
- package/src/data/seed-sme.js +317 -317
- package/src/data/seed.js +879 -879
- package/src/lib/backup-identity.js +57 -57
- package/src/lib/chat-answer.js +225 -225
- package/src/lib/chat-context.js +99 -99
- package/src/lib/company-library.js +28 -19
- package/src/lib/data-import.js +217 -217
- package/src/lib/data-sources.js +118 -118
- package/src/lib/demo-data.js +27 -1
- package/src/lib/employee-intake.js +84 -84
- package/src/lib/growth.js +53 -53
- package/src/lib/intake-packets.js +119 -119
- package/src/lib/issue-intake.js +88 -88
- package/src/lib/kb-revision.js +214 -214
- package/src/lib/llm.js +279 -279
- package/src/lib/model-error.js +14 -14
- package/src/lib/privacy.js +26 -26
- package/src/lib/qms-drafting.js +194 -194
- package/src/lib/qms-jobs.js +11 -7
- package/src/lib/qms-manual-review.js +185 -185
- package/src/lib/qms-procedure-review.js +109 -109
- package/src/lib/qms-templates.js +358 -326
- package/src/lib/qms.js +369 -369
- package/src/lib/quality-priority.js +100 -100
- package/src/lib/quality-state.js +92 -92
- package/src/lib/quality-workflow.js +65 -65
- package/src/lib/retrieval.js +159 -159
- package/src/lib/spc.js +127 -127
- package/src/lib/voice-hotwords.txt +22 -22
- package/src/lib/voice-text.js +13 -13
- package/src/lib/voice-worker.js +72 -72
- package/src/lib/voice.js +330 -330
- package/src/lib/workbench-notices.js +49 -49
- package/src/lib/workbench.js +103 -106
- package/src/package.json +3 -3
- package/src/tools/analysis.js +306 -306
- package/src/tools/data.js +596 -596
- package/src/tools/docs.js +430 -430
- package/src/tools/registry.js +57 -57
- package/tools-dsh.cjs +153 -153
- package//345/256/211/350/243/205/350/257/264/346/230/216.md +33 -25
- 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
- package//350/207/252/346/243/200.cjs +88 -88
- package/public/js/remote-knowledge.js +0 -165
- package/public/js/workbench-notices.js +0 -67
- package/public/remote-knowledge-help.html +0 -35
- package/public/remote-knowledge.css +0 -40
- package/src/lib/remote-knowledge.js +0 -284
package/public/js/xiaozhi.js
CHANGED
|
@@ -1,468 +1,468 @@
|
|
|
1
|
-
(function (global) {
|
|
2
|
-
'use strict';
|
|
3
|
-
const { esc, md } = UI;
|
|
4
|
-
const workspace = document.getElementById('qipWorkspace');
|
|
5
|
-
const pane = document.getElementById('xiaozhiPane');
|
|
6
|
-
const divider = document.getElementById('xiaozhiDivider');
|
|
7
|
-
const input = document.getElementById('xiaozhiInput');
|
|
8
|
-
const form = document.getElementById('xiaozhiForm');
|
|
9
|
-
const log = document.getElementById('xiaozhiMessages');
|
|
10
|
-
const toggle = document.getElementById('xiaozhiToggle');
|
|
11
|
-
const context = document.getElementById('xiaozhiContext');
|
|
12
|
-
const clear = document.getElementById('xiaozhiClear');
|
|
13
|
-
const historyToggle = document.getElementById('xiaozhiHistoryToggle');
|
|
14
|
-
const historyPanel = document.getElementById('xiaozhiHistory');
|
|
15
|
-
const stop = document.getElementById('xiaozhiStop');
|
|
16
|
-
const send = document.getElementById('xiaozhiSend');
|
|
17
|
-
const voiceButton = document.getElementById('xiaozhiVoice');
|
|
18
|
-
const voiceStatus = document.getElementById('xiaozhiVoiceStatus');
|
|
19
|
-
const compact = matchMedia('(max-width: 760px)');
|
|
20
|
-
const paneStyle = getComputedStyle(workspace);
|
|
21
|
-
const minWidth = parseFloat(paneStyle.getPropertyValue('--xiaozhi-min'));
|
|
22
|
-
const mainMinWidth = parseFloat(paneStyle.getPropertyValue('--xiaozhi-main-min'));
|
|
23
|
-
const widthKey = 'qip-real-workbench:xiaozhi-width';
|
|
24
|
-
let drag;
|
|
25
|
-
try {
|
|
26
|
-
const savedWidth = Number(localStorage.getItem(widthKey));
|
|
27
|
-
if (Number.isFinite(savedWidth) && savedWidth >= minWidth) {
|
|
28
|
-
workspace.style.setProperty('--xiaozhi-width', savedWidth + 'px');
|
|
29
|
-
}
|
|
30
|
-
} catch (_) { /* Storage may be unavailable in the host. */ }
|
|
31
|
-
const prompts = [
|
|
32
|
-
['今天最该处理什么', '今天有哪些高风险或即将到期的质量事项?请按优先级给我。'],
|
|
33
|
-
['查供应商资料', '怎样用平台已有的供应商资料核对来料检验要求?请列出能核实的资料和缺项。'],
|
|
34
|
-
['找相似案例与专家经验', '请结合历史案例和专家资料,查找重复发生的质量异常通常怎样排查。'],
|
|
35
|
-
['起草 8D', '根据当前最紧急的一条质量问题,帮我起草 8D 的问题描述、围堵和待核实证据。'],
|
|
36
|
-
];
|
|
37
|
-
let messages = [];
|
|
38
|
-
let request;
|
|
39
|
-
const makeSession = () => 'xz-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2);
|
|
40
|
-
let session = makeSession();
|
|
41
|
-
const sessionKey = () => 'qip-real-workbench:xiaozhi-session';
|
|
42
|
-
const chatsPath = '/qip-real-workbench/api/chats';
|
|
43
|
-
const historyScope = { mode: 'xiaozhi' };
|
|
44
|
-
const historyDate = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeStyle: 'short' });
|
|
45
|
-
let restoreTask;
|
|
46
|
-
let loading = false;
|
|
47
|
-
let historyView = false;
|
|
48
|
-
let historyRows = [];
|
|
49
|
-
let historyError = '';
|
|
50
|
-
let sessionEditable = true;
|
|
51
|
-
let revisionTarget;
|
|
52
|
-
|
|
53
|
-
function rememberedSession() {
|
|
54
|
-
try { return localStorage.getItem(sessionKey()); }
|
|
55
|
-
catch (_) { return null; }
|
|
56
|
-
}
|
|
57
|
-
function rememberSession(id) {
|
|
58
|
-
try { localStorage.setItem(sessionKey(), id); }
|
|
59
|
-
catch (_) { /* History remains available from the server. */ }
|
|
60
|
-
}
|
|
61
|
-
function forgetSession(id) {
|
|
62
|
-
try {
|
|
63
|
-
if (rememberedSession() === id) localStorage.removeItem(sessionKey());
|
|
64
|
-
} catch (_) { /* The available sessions come from the server. */ }
|
|
65
|
-
}
|
|
66
|
-
async function readHistory() {
|
|
67
|
-
const result = await API.get(chatsPath, historyScope);
|
|
68
|
-
if (!result.ok || !Array.isArray(result.rows)) throw new Error('无法载入历史会话');
|
|
69
|
-
return result.rows;
|
|
70
|
-
}
|
|
71
|
-
async function activateSession(id) {
|
|
72
|
-
const result = await API.get(chatsPath + '/' + encodeURIComponent(id), historyScope);
|
|
73
|
-
if (!result.ok || !Array.isArray(result.row?.messages)) throw new Error('无法载入这段会话');
|
|
74
|
-
session = result.row.id;
|
|
75
|
-
sessionEditable = result.row.editable !== false;
|
|
76
|
-
messages = result.row.messages.map(({ content, ...message }) => ({ ...message, text: content || '' }));
|
|
77
|
-
rememberSession(session);
|
|
78
|
-
}
|
|
79
|
-
function restore() {
|
|
80
|
-
if (restoreTask) return restoreTask;
|
|
81
|
-
restoreTask = (async () => {
|
|
82
|
-
loading = true;
|
|
83
|
-
render();
|
|
84
|
-
try {
|
|
85
|
-
historyRows = await readHistory();
|
|
86
|
-
const preferred = rememberedSession();
|
|
87
|
-
const choice = historyRows.find(({ id,editable }) => id === preferred && editable !== false) || historyRows.find((row) => row.editable !== false);
|
|
88
|
-
if (choice) await activateSession(choice.id);
|
|
89
|
-
} catch (_) {
|
|
90
|
-
historyError = '历史会话暂时无法载入,可点击「历史」重试。';
|
|
91
|
-
} finally {
|
|
92
|
-
loading = false;
|
|
93
|
-
render();
|
|
94
|
-
log.scrollTop = log.scrollHeight;
|
|
95
|
-
}
|
|
96
|
-
})();
|
|
97
|
-
return restoreTask;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function sourceHtml(source) {
|
|
101
|
-
const name = '[' + source.ref + '] ' + source.title;
|
|
102
|
-
const recordAction = source.issueId ? 'issueDetail' : source.cardId ? 'kbDetail' : '';
|
|
103
|
-
const link = recordAction ? '<button type="button" data-act="'+recordAction+'" data-args=\'' + esc(JSON.stringify({ id: source.issueId || source.cardId })) + '\'>' + esc(name) + '</button>'
|
|
104
|
-
: source.url?.startsWith('/qip-real-workbench/api/') ? '<a href="' + esc(source.url) + '" target="_blank" rel="noopener">' + esc(name) + '</a>'
|
|
105
|
-
: esc(name);
|
|
106
|
-
return '<li class="xiaozhi-source">' + link + '<small>' + esc([source.kind, source.detail].filter(Boolean).join(' · ')) +
|
|
107
|
-
'</small><p>' + esc(source.excerpt) + '</p></li>';
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function renderHistory() {
|
|
111
|
-
if (loading) {
|
|
112
|
-
historyPanel.innerHTML = '<p class="xiaozhi-history-note" role="status">正在载入历史会话…</p>';
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const error = historyError ? '<p class="xiaozhi-history-note" role="status">' + esc(historyError) + ' ' +
|
|
116
|
-
'<button type="button" class="btn btn-sm" data-xz-history-retry>重试</button></p>' : '';
|
|
117
|
-
historyPanel.innerHTML = error + (historyRows.length ? '<ul class="xiaozhi-history-list">' + historyRows.map((row) => {
|
|
118
|
-
const time = Date.parse(row.updatedAt);
|
|
119
|
-
const details = [row.count + ' 条消息', Number.isFinite(time) ? historyDate.format(time) : '',
|
|
120
|
-
row.shared ? '工作区共享' : row.legacy ? '旧版历史' : ''].filter(Boolean).join(' · ');
|
|
121
|
-
const title = row.title || '小质会话';
|
|
122
|
-
const id = esc(row.id);
|
|
123
|
-
return '<li class="xiaozhi-history-row"><button type="button" class="xiaozhi-history-open" data-xz-session="' + id + '"' +
|
|
124
|
-
(row.id === session ? ' aria-current="true"' : '') + ' title="' + esc(title) + '">' +
|
|
125
|
-
'<strong>' + esc(title) + '</strong><small>' + esc(details) + '</small></button>' +
|
|
126
|
-
'<span>' + (row.canShare ? '<button type="button" class="btn btn-sm" data-xz-share="'+id+'">'+(row.shared?'取消共享':'共享')+'</button> ' : '') +
|
|
127
|
-
(row.editable !== false ? '<button type="button" class="xiaozhi-history-delete" data-xz-delete="' + id + '" title="删除对话" aria-label="' +
|
|
128
|
-
esc('删除对话「' + title + '」') + '">删除</button>' : '') + '</span></li>';
|
|
129
|
-
}).join('') + '</ul>' : (!historyError ? '<p class="xiaozhi-history-note">暂无历史会话</p>' : ''));
|
|
130
|
-
}
|
|
131
|
-
function renderControls() {
|
|
132
|
-
const busy = !!request || loading;
|
|
133
|
-
log.hidden = historyView;
|
|
134
|
-
form.hidden = historyView;
|
|
135
|
-
historyPanel.hidden = !historyView;
|
|
136
|
-
historyToggle.setAttribute('aria-expanded', String(historyView));
|
|
137
|
-
historyToggle.disabled = busy;
|
|
138
|
-
clear.disabled = busy || (!historyView && !messages.length && !input.value);
|
|
139
|
-
send.disabled = busy || historyView || voice.active() || !input.value.trim();
|
|
140
|
-
voiceButton.disabled = busy || historyView;
|
|
141
|
-
stop.hidden = !request;
|
|
142
|
-
}
|
|
143
|
-
function render() {
|
|
144
|
-
const atBottom = log.scrollHeight - log.scrollTop - log.clientHeight < 48;
|
|
145
|
-
const open = new Set(Array.from(log.querySelectorAll('details[open]'), (row) => row.dataset.message));
|
|
146
|
-
log.innerHTML = messages.length ? messages.map((message, index) => {
|
|
147
|
-
if (message.role === 'user') return '<div class="xiaozhi-message xiaozhi-user">' + esc(message.text) + '</div>';
|
|
148
|
-
const answer = message.receipt ? '<p><strong>已完成上报</strong></p><p>'+esc(message.receipt.destination+' · '+message.receipt.issueId)+'</p>'+
|
|
149
|
-
'<button type="button" class="btn btn-sm" data-act="issueDetail" data-args=\''+esc(JSON.stringify({id:message.receipt.issueId}))+'\'>查看事项</button>' :
|
|
150
|
-
message.text ? md(message.text) : '';
|
|
151
|
-
const status = UI.answerStatus(message, '<button type="button" class="btn btn-sm" data-xz-resume="' + index + '">继续生成</button>');
|
|
152
|
-
const note = message.note ? '<p class="xiaozhi-history-note">' + esc(message.note) + '</p>' : '';
|
|
153
|
-
const sources = message.sources?.length
|
|
154
|
-
? '<details class="xiaozhi-sources" data-message="' + index + '"' + (open.has(String(index)) ? ' open' : '') +
|
|
155
|
-
'><summary>查看参考资料(' + message.sources.length + ')</summary><ol>' + message.sources.map(sourceHtml).join('') + '</ol></details>' : '';
|
|
156
|
-
const pending = UI.chatConfirmations(message.pending, 'xzDecision', index);
|
|
157
|
-
return '<article class="xiaozhi-message"><div class="xiaozhi-answer">' + answer + status + note + '</div>' + pending + sources + '</article>';
|
|
158
|
-
}).join('') : '<div class="xiaozhi-welcome"><h3>想了解什么质量问题?</h3>' +
|
|
159
|
-
'<p>可查企业知识库、体系文件、专家资料、供应商资料和平台质量数据。</p></div>' +
|
|
160
|
-
'<div class="xiaozhi-prompts">' + prompts.map(([label, q]) => '<button type="button" data-act="tdAsk" data-args=\'' +
|
|
161
|
-
esc(JSON.stringify({ q })) + '\'>' + esc(label) + '</button>').join('') + '</div>';
|
|
162
|
-
if (!historyView && loading && !messages.length) {
|
|
163
|
-
log.innerHTML = '<p class="xiaozhi-history-note" role="status">正在恢复最近会话…</p>';
|
|
164
|
-
} else if (!historyView && historyError) {
|
|
165
|
-
log.insertAdjacentHTML('afterbegin', '<p class="xiaozhi-history-note" role="status">' + esc(historyError) + '</p>');
|
|
166
|
-
}
|
|
167
|
-
if (historyView) renderHistory();
|
|
168
|
-
renderControls();
|
|
169
|
-
if (atBottom && !pane.hidden && !historyView) log.scrollTop = log.scrollHeight;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function maxPaneWidth() {
|
|
173
|
-
return Math.max(minWidth, workspace.clientWidth - mainMinWidth);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function syncDivider(width) {
|
|
177
|
-
divider.setAttribute('aria-valuemin', minWidth);
|
|
178
|
-
divider.setAttribute('aria-valuemax', maxPaneWidth());
|
|
179
|
-
divider.setAttribute('aria-valuenow', Math.round(width));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function setWidth(requestedWidth) {
|
|
183
|
-
const width = Math.round(Math.max(minWidth, Math.min(requestedWidth, maxPaneWidth())));
|
|
184
|
-
workspace.style.setProperty('--xiaozhi-width', width + 'px');
|
|
185
|
-
syncDivider(width);
|
|
186
|
-
return width;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function rememberWidth(width) {
|
|
190
|
-
try { localStorage.setItem(widthKey, String(width)); } catch (_) { /* Optional preference. */ }
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function finishDrag() {
|
|
194
|
-
if (!drag) return;
|
|
195
|
-
const { pointerId, width } = drag;
|
|
196
|
-
drag = null;
|
|
197
|
-
workspace.classList.remove('xiaozhi-resizing');
|
|
198
|
-
if (divider.hasPointerCapture(pointerId)) divider.releasePointerCapture(pointerId);
|
|
199
|
-
if (width !== undefined) rememberWidth(width);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function resize() {
|
|
203
|
-
document.getElementById('content').inert = !pane.hidden && compact.matches;
|
|
204
|
-
if (pane.hidden || compact.matches) finishDrag();
|
|
205
|
-
else syncDivider(pane.getBoundingClientRect().width);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
divider.addEventListener('pointerdown', (event) => {
|
|
209
|
-
if (compact.matches || drag || event.button !== 0) return;
|
|
210
|
-
event.preventDefault();
|
|
211
|
-
divider.focus({ preventScroll: true });
|
|
212
|
-
divider.setPointerCapture(event.pointerId);
|
|
213
|
-
drag = { pointerId: event.pointerId, startX: event.clientX, startWidth: pane.getBoundingClientRect().width };
|
|
214
|
-
workspace.classList.add('xiaozhi-resizing');
|
|
215
|
-
});
|
|
216
|
-
window.addEventListener('pointermove', (event) => {
|
|
217
|
-
if (drag?.pointerId !== event.pointerId) return;
|
|
218
|
-
drag.width = setWidth(drag.startWidth + drag.startX - event.clientX);
|
|
219
|
-
});
|
|
220
|
-
function finishPointerDrag(event) {
|
|
221
|
-
if (drag?.pointerId === event.pointerId) finishDrag();
|
|
222
|
-
}
|
|
223
|
-
window.addEventListener('pointerup', finishPointerDrag);
|
|
224
|
-
window.addEventListener('pointercancel', finishPointerDrag);
|
|
225
|
-
divider.addEventListener('lostpointercapture', finishPointerDrag);
|
|
226
|
-
divider.addEventListener('keydown', (event) => {
|
|
227
|
-
const width = pane.getBoundingClientRect().width;
|
|
228
|
-
const step = 24;
|
|
229
|
-
const requested = {
|
|
230
|
-
ArrowLeft: width + step, ArrowRight: width - step, Home: minWidth, End: maxPaneWidth(),
|
|
231
|
-
}[event.key];
|
|
232
|
-
if (!Number.isFinite(requested)) return;
|
|
233
|
-
event.preventDefault();
|
|
234
|
-
rememberWidth(setWidth(requested));
|
|
235
|
-
});
|
|
236
|
-
new ResizeObserver(resize).observe(workspace);
|
|
237
|
-
|
|
238
|
-
function display(open) {
|
|
239
|
-
if (pane.hidden === !open) return;
|
|
240
|
-
const focusWasInside = pane.contains(document.activeElement);
|
|
241
|
-
pane.hidden = !open;
|
|
242
|
-
if (!open) voice.cancel();
|
|
243
|
-
document.querySelectorAll('[aria-controls="xiaozhiPane"][aria-expanded]').forEach((button) => {
|
|
244
|
-
button.setAttribute('aria-expanded', String(open));
|
|
245
|
-
});
|
|
246
|
-
resize();
|
|
247
|
-
if (open) {
|
|
248
|
-
global.XiaozhiVoice.warm();
|
|
249
|
-
void restore();
|
|
250
|
-
log.scrollTop = log.scrollHeight;
|
|
251
|
-
if (!compact.matches && !historyView) input.focus({ preventScroll: true });
|
|
252
|
-
} else if (focusWasInside) {
|
|
253
|
-
const returnTo = document.querySelector('.qa-toolbar [aria-controls="xiaozhiPane"]') || toggle;
|
|
254
|
-
returnTo.focus({ preventScroll: true });
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
async function draft(text) {
|
|
259
|
-
const question = String(text || '').trim();
|
|
260
|
-
if (!question) return;
|
|
261
|
-
display(true);
|
|
262
|
-
await restore();
|
|
263
|
-
if (revisionTarget) {
|
|
264
|
-
UI.toast('请先处理输入框中的文件修订草稿。','warn');
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
if (!sessionEditable) resetConversation();
|
|
268
|
-
historyView = false;
|
|
269
|
-
const previous = input.value.trim();
|
|
270
|
-
input.value = previous && previous !== question ? previous + '\n\n' + question : question;
|
|
271
|
-
render();
|
|
272
|
-
input.focus();
|
|
273
|
-
input.setSelectionRange(input.value.length,input.value.length);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
async function ask(text, {issueId} = {}) {
|
|
277
|
-
display(true);
|
|
278
|
-
await restore();
|
|
279
|
-
if (request || loading) {
|
|
280
|
-
if (issueId) UI.toast('请等待当前回答完成,或点击停止后再分析这条事项','warn');
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
if (voice.active()) {
|
|
284
|
-
if (text == null) return;
|
|
285
|
-
voice.cancel();
|
|
286
|
-
}
|
|
287
|
-
historyView = false;
|
|
288
|
-
const question = String(text ?? input.value).trim();
|
|
289
|
-
if (!question) { input.focus(); return; }
|
|
290
|
-
const revisionFileId = text == null && revisionTarget && question.startsWith(revisionTarget.prefix)
|
|
291
|
-
? revisionTarget.id : undefined;
|
|
292
|
-
if (issueId || !sessionEditable) resetConversation();
|
|
293
|
-
if (text == null) { input.value = ''; revisionTarget = undefined; }
|
|
294
|
-
const history = messages.filter((message) => message.text && message.state !== 'incomplete')
|
|
295
|
-
.map((message) => ({ role: message.role, content: message.text })).slice(-24);
|
|
296
|
-
const reply = { role: 'assistant', text: '', pending: [] };
|
|
297
|
-
messages.push({ role: 'user', text: question }, reply);
|
|
298
|
-
await deliver(reply, (options) => API.answer('/qip-real-workbench/api/chat/stream', {
|
|
299
|
-
mode: 'xiaozhi', text: question, sessionId: session, history, issueId, revisionFileId,
|
|
300
|
-
}, reply, options));
|
|
301
|
-
if ((reply.text || reply.answerId) && !reply.historyWarning) rememberSession(session);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
async function reviseFile(file) {
|
|
305
|
-
display(true);
|
|
306
|
-
await restore();
|
|
307
|
-
if (request || loading) {
|
|
308
|
-
UI.toast('请等待当前回答完成后再修订文件。','warn');
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
if (input.value.trim() && input.value !== revisionTarget?.prefix) {
|
|
312
|
-
UI.toast('小质还有未发送的内容,请先处理当前输入后再选文件。','warn');
|
|
313
|
-
input.focus();
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
historyView = false;
|
|
317
|
-
if (!sessionEditable) resetConversation();
|
|
318
|
-
const prefix = '请修订《' + file.name + '》:';
|
|
319
|
-
revisionTarget = { id:file.id, prefix };
|
|
320
|
-
input.value = prefix;
|
|
321
|
-
render();
|
|
322
|
-
input.focus();
|
|
323
|
-
input.setSelectionRange(prefix.length,prefix.length);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async function deliver(reply, run) {
|
|
327
|
-
if (request) return;
|
|
328
|
-
const controller = new AbortController();
|
|
329
|
-
request = controller;
|
|
330
|
-
log.scrollTop = log.scrollHeight;
|
|
331
|
-
try {
|
|
332
|
-
await run({
|
|
333
|
-
signal: controller.signal,
|
|
334
|
-
render: () => { if (request === controller) render(); },
|
|
335
|
-
onAction: (event) => reply.pending.push(event),
|
|
336
|
-
});
|
|
337
|
-
} finally {
|
|
338
|
-
if (request === controller) { request = null; render(); }
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
log.addEventListener('click', (event) => {
|
|
343
|
-
const button = event.target.closest('[data-xz-resume]');
|
|
344
|
-
if (!button) return;
|
|
345
|
-
const message = messages[Number(button.dataset.xzResume)];
|
|
346
|
-
if (message?.answerId) deliver(message, (options) => API.resumeAnswer(message, options));
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
async function refreshHistory() {
|
|
350
|
-
loading = true;
|
|
351
|
-
historyError = '';
|
|
352
|
-
render();
|
|
353
|
-
try { historyRows = await readHistory(); }
|
|
354
|
-
catch (_) { historyError = '历史会话暂时无法载入。'; }
|
|
355
|
-
finally { loading = false; render(); }
|
|
356
|
-
}
|
|
357
|
-
historyToggle.addEventListener('click', () => {
|
|
358
|
-
if (request || loading) return;
|
|
359
|
-
voice.cancel();
|
|
360
|
-
historyView = !historyView;
|
|
361
|
-
if (historyView) void refreshHistory();
|
|
362
|
-
else render();
|
|
363
|
-
});
|
|
364
|
-
async function deleteSession(row) {
|
|
365
|
-
const accepted = await global.AskConfirm('删除对话', [['对话', row.title || '小质会话'], ['消息', row.count + ' 条']],
|
|
366
|
-
'这段对话及其消息将永久删除。', { sub: '', danger: true, label: '删除对话' });
|
|
367
|
-
if (!accepted || loading || request) return;
|
|
368
|
-
loading = true;
|
|
369
|
-
renderControls();
|
|
370
|
-
try {
|
|
371
|
-
const result = await API.del(chatsPath + '/' + encodeURIComponent(row.id) + '?mode=xiaozhi');
|
|
372
|
-
if (!result.ok) throw new Error(result.error || '服务未确认删除');
|
|
373
|
-
historyRows = historyRows.filter(({ id }) => id !== row.id);
|
|
374
|
-
historyError = '';
|
|
375
|
-
forgetSession(row.id);
|
|
376
|
-
if (row.id === session) resetConversation();
|
|
377
|
-
UI.toast('已删除这段对话', 'ok');
|
|
378
|
-
} catch (error) {
|
|
379
|
-
UI.toast('删除对话失败:' + error.message, 'err');
|
|
380
|
-
} finally {
|
|
381
|
-
loading = false;
|
|
382
|
-
render();
|
|
383
|
-
if (!pane.hidden) historyToggle.focus();
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
historyPanel.addEventListener('click', async (event) => {
|
|
387
|
-
if (loading || request) return;
|
|
388
|
-
if (event.target.closest('[data-xz-history-retry]')) return refreshHistory();
|
|
389
|
-
const share = event.target.closest('[data-xz-share]');
|
|
390
|
-
if (share) {
|
|
391
|
-
const row = historyRows.find(({id}) => id===share.dataset.xzShare);
|
|
392
|
-
if (!row?.canShare) return;
|
|
393
|
-
if (!row.shared && !global.confirm('共享后,本工作区的已登录员工可阅读这段对话。确定共享?')) return;
|
|
394
|
-
try {
|
|
395
|
-
await API.put(chatsPath+'/'+encodeURIComponent(row.id)+'/sharing',{shared:!row.shared});
|
|
396
|
-
await refreshHistory();
|
|
397
|
-
} catch (error) { UI.toast(error.message,'err'); }
|
|
398
|
-
return;
|
|
399
|
-
}
|
|
400
|
-
const remove = event.target.closest('[data-xz-delete]');
|
|
401
|
-
if (remove) {
|
|
402
|
-
const row = historyRows.find(({ id }) => id === remove.dataset.xzDelete);
|
|
403
|
-
if (row) await deleteSession(row);
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
const choice = event.target.closest('[data-xz-session]');
|
|
407
|
-
if (!choice) return;
|
|
408
|
-
const id = choice.dataset.xzSession;
|
|
409
|
-
loading = true;
|
|
410
|
-
historyError = '';
|
|
411
|
-
render();
|
|
412
|
-
try {
|
|
413
|
-
if (id !== session) {
|
|
414
|
-
await activateSession(id);
|
|
415
|
-
input.value = '';
|
|
416
|
-
}
|
|
417
|
-
historyView = false;
|
|
418
|
-
} catch (_) {
|
|
419
|
-
historyError = '这段会话暂时无法载入。';
|
|
420
|
-
} finally {
|
|
421
|
-
loading = false;
|
|
422
|
-
render();
|
|
423
|
-
if (!historyView) {
|
|
424
|
-
log.scrollTop = log.scrollHeight;
|
|
425
|
-
if (!pane.hidden) input.focus();
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
function resetConversation() {
|
|
430
|
-
voice.cancel();
|
|
431
|
-
revisionTarget = undefined;
|
|
432
|
-
messages = [];
|
|
433
|
-
session = makeSession();
|
|
434
|
-
sessionEditable = true;
|
|
435
|
-
input.value = '';
|
|
436
|
-
}
|
|
437
|
-
const voice = global.XiaozhiVoice.bind({ button: voiceButton, input, status: voiceStatus,
|
|
438
|
-
canStart: () => !pane.hidden && !historyView && !request && !loading, onChange: renderControls });
|
|
439
|
-
stop.addEventListener('click', () => request?.abort());
|
|
440
|
-
clear.addEventListener('click', () => {
|
|
441
|
-
if (request || loading) return;
|
|
442
|
-
resetConversation();
|
|
443
|
-
historyView = false;
|
|
444
|
-
historyError = '';
|
|
445
|
-
render();
|
|
446
|
-
input.focus();
|
|
447
|
-
});
|
|
448
|
-
form.addEventListener('submit', (event) => { event.preventDefault(); ask(); });
|
|
449
|
-
input.addEventListener('keydown', (event) => {
|
|
450
|
-
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing && event.keyCode !== 229) {
|
|
451
|
-
event.preventDefault();
|
|
452
|
-
form.requestSubmit();
|
|
453
|
-
}
|
|
454
|
-
});
|
|
455
|
-
input.addEventListener('input', renderControls);
|
|
456
|
-
compact.addEventListener('change', resize);
|
|
457
|
-
global.XiaoZhi = {
|
|
458
|
-
open: () => display(true),
|
|
459
|
-
close: () => display(false),
|
|
460
|
-
toggle: () => display(pane.hidden),
|
|
461
|
-
ask,
|
|
462
|
-
draft,
|
|
463
|
-
reviseFile,
|
|
464
|
-
decide: ({ i, id, approved }) => UI.decideChatConfirmation(messages[i], id, approved, render),
|
|
465
|
-
setPage: (title) => { context.textContent = '当前页面 · ' + title; },
|
|
466
|
-
};
|
|
467
|
-
render();
|
|
468
|
-
})(window);
|
|
1
|
+
(function (global) {
|
|
2
|
+
'use strict';
|
|
3
|
+
const { esc, md } = UI;
|
|
4
|
+
const workspace = document.getElementById('qipWorkspace');
|
|
5
|
+
const pane = document.getElementById('xiaozhiPane');
|
|
6
|
+
const divider = document.getElementById('xiaozhiDivider');
|
|
7
|
+
const input = document.getElementById('xiaozhiInput');
|
|
8
|
+
const form = document.getElementById('xiaozhiForm');
|
|
9
|
+
const log = document.getElementById('xiaozhiMessages');
|
|
10
|
+
const toggle = document.getElementById('xiaozhiToggle');
|
|
11
|
+
const context = document.getElementById('xiaozhiContext');
|
|
12
|
+
const clear = document.getElementById('xiaozhiClear');
|
|
13
|
+
const historyToggle = document.getElementById('xiaozhiHistoryToggle');
|
|
14
|
+
const historyPanel = document.getElementById('xiaozhiHistory');
|
|
15
|
+
const stop = document.getElementById('xiaozhiStop');
|
|
16
|
+
const send = document.getElementById('xiaozhiSend');
|
|
17
|
+
const voiceButton = document.getElementById('xiaozhiVoice');
|
|
18
|
+
const voiceStatus = document.getElementById('xiaozhiVoiceStatus');
|
|
19
|
+
const compact = matchMedia('(max-width: 760px)');
|
|
20
|
+
const paneStyle = getComputedStyle(workspace);
|
|
21
|
+
const minWidth = parseFloat(paneStyle.getPropertyValue('--xiaozhi-min'));
|
|
22
|
+
const mainMinWidth = parseFloat(paneStyle.getPropertyValue('--xiaozhi-main-min'));
|
|
23
|
+
const widthKey = 'qip-real-workbench:xiaozhi-width';
|
|
24
|
+
let drag;
|
|
25
|
+
try {
|
|
26
|
+
const savedWidth = Number(localStorage.getItem(widthKey));
|
|
27
|
+
if (Number.isFinite(savedWidth) && savedWidth >= minWidth) {
|
|
28
|
+
workspace.style.setProperty('--xiaozhi-width', savedWidth + 'px');
|
|
29
|
+
}
|
|
30
|
+
} catch (_) { /* Storage may be unavailable in the host. */ }
|
|
31
|
+
const prompts = [
|
|
32
|
+
['今天最该处理什么', '今天有哪些高风险或即将到期的质量事项?请按优先级给我。'],
|
|
33
|
+
['查供应商资料', '怎样用平台已有的供应商资料核对来料检验要求?请列出能核实的资料和缺项。'],
|
|
34
|
+
['找相似案例与专家经验', '请结合历史案例和专家资料,查找重复发生的质量异常通常怎样排查。'],
|
|
35
|
+
['起草 8D', '根据当前最紧急的一条质量问题,帮我起草 8D 的问题描述、围堵和待核实证据。'],
|
|
36
|
+
];
|
|
37
|
+
let messages = [];
|
|
38
|
+
let request;
|
|
39
|
+
const makeSession = () => 'xz-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2);
|
|
40
|
+
let session = makeSession();
|
|
41
|
+
const sessionKey = () => 'qip-real-workbench:xiaozhi-session';
|
|
42
|
+
const chatsPath = '/qip-real-workbench/api/chats';
|
|
43
|
+
const historyScope = { mode: 'xiaozhi' };
|
|
44
|
+
const historyDate = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeStyle: 'short' });
|
|
45
|
+
let restoreTask;
|
|
46
|
+
let loading = false;
|
|
47
|
+
let historyView = false;
|
|
48
|
+
let historyRows = [];
|
|
49
|
+
let historyError = '';
|
|
50
|
+
let sessionEditable = true;
|
|
51
|
+
let revisionTarget;
|
|
52
|
+
|
|
53
|
+
function rememberedSession() {
|
|
54
|
+
try { return localStorage.getItem(sessionKey()); }
|
|
55
|
+
catch (_) { return null; }
|
|
56
|
+
}
|
|
57
|
+
function rememberSession(id) {
|
|
58
|
+
try { localStorage.setItem(sessionKey(), id); }
|
|
59
|
+
catch (_) { /* History remains available from the server. */ }
|
|
60
|
+
}
|
|
61
|
+
function forgetSession(id) {
|
|
62
|
+
try {
|
|
63
|
+
if (rememberedSession() === id) localStorage.removeItem(sessionKey());
|
|
64
|
+
} catch (_) { /* The available sessions come from the server. */ }
|
|
65
|
+
}
|
|
66
|
+
async function readHistory() {
|
|
67
|
+
const result = await API.get(chatsPath, historyScope);
|
|
68
|
+
if (!result.ok || !Array.isArray(result.rows)) throw new Error('无法载入历史会话');
|
|
69
|
+
return result.rows;
|
|
70
|
+
}
|
|
71
|
+
async function activateSession(id) {
|
|
72
|
+
const result = await API.get(chatsPath + '/' + encodeURIComponent(id), historyScope);
|
|
73
|
+
if (!result.ok || !Array.isArray(result.row?.messages)) throw new Error('无法载入这段会话');
|
|
74
|
+
session = result.row.id;
|
|
75
|
+
sessionEditable = result.row.editable !== false;
|
|
76
|
+
messages = result.row.messages.map(({ content, ...message }) => ({ ...message, text: content || '' }));
|
|
77
|
+
rememberSession(session);
|
|
78
|
+
}
|
|
79
|
+
function restore() {
|
|
80
|
+
if (restoreTask) return restoreTask;
|
|
81
|
+
restoreTask = (async () => {
|
|
82
|
+
loading = true;
|
|
83
|
+
render();
|
|
84
|
+
try {
|
|
85
|
+
historyRows = await readHistory();
|
|
86
|
+
const preferred = rememberedSession();
|
|
87
|
+
const choice = historyRows.find(({ id,editable }) => id === preferred && editable !== false) || historyRows.find((row) => row.editable !== false);
|
|
88
|
+
if (choice) await activateSession(choice.id);
|
|
89
|
+
} catch (_) {
|
|
90
|
+
historyError = '历史会话暂时无法载入,可点击「历史」重试。';
|
|
91
|
+
} finally {
|
|
92
|
+
loading = false;
|
|
93
|
+
render();
|
|
94
|
+
log.scrollTop = log.scrollHeight;
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
return restoreTask;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function sourceHtml(source) {
|
|
101
|
+
const name = '[' + source.ref + '] ' + source.title;
|
|
102
|
+
const recordAction = source.issueId ? 'issueDetail' : source.cardId ? 'kbDetail' : '';
|
|
103
|
+
const link = recordAction ? '<button type="button" data-act="'+recordAction+'" data-args=\'' + esc(JSON.stringify({ id: source.issueId || source.cardId })) + '\'>' + esc(name) + '</button>'
|
|
104
|
+
: source.url?.startsWith('/qip-real-workbench/api/') ? '<a href="' + esc(source.url) + '" target="_blank" rel="noopener">' + esc(name) + '</a>'
|
|
105
|
+
: esc(name);
|
|
106
|
+
return '<li class="xiaozhi-source">' + link + '<small>' + esc([source.kind, source.detail].filter(Boolean).join(' · ')) +
|
|
107
|
+
'</small><p>' + esc(source.excerpt) + '</p></li>';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function renderHistory() {
|
|
111
|
+
if (loading) {
|
|
112
|
+
historyPanel.innerHTML = '<p class="xiaozhi-history-note" role="status">正在载入历史会话…</p>';
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const error = historyError ? '<p class="xiaozhi-history-note" role="status">' + esc(historyError) + ' ' +
|
|
116
|
+
'<button type="button" class="btn btn-sm" data-xz-history-retry>重试</button></p>' : '';
|
|
117
|
+
historyPanel.innerHTML = error + (historyRows.length ? '<ul class="xiaozhi-history-list">' + historyRows.map((row) => {
|
|
118
|
+
const time = Date.parse(row.updatedAt);
|
|
119
|
+
const details = [row.count + ' 条消息', Number.isFinite(time) ? historyDate.format(time) : '',
|
|
120
|
+
row.shared ? '工作区共享' : row.legacy ? '旧版历史' : ''].filter(Boolean).join(' · ');
|
|
121
|
+
const title = row.title || '小质会话';
|
|
122
|
+
const id = esc(row.id);
|
|
123
|
+
return '<li class="xiaozhi-history-row"><button type="button" class="xiaozhi-history-open" data-xz-session="' + id + '"' +
|
|
124
|
+
(row.id === session ? ' aria-current="true"' : '') + ' title="' + esc(title) + '">' +
|
|
125
|
+
'<strong>' + esc(title) + '</strong><small>' + esc(details) + '</small></button>' +
|
|
126
|
+
'<span>' + (row.canShare ? '<button type="button" class="btn btn-sm" data-xz-share="'+id+'">'+(row.shared?'取消共享':'共享')+'</button> ' : '') +
|
|
127
|
+
(row.editable !== false ? '<button type="button" class="xiaozhi-history-delete" data-xz-delete="' + id + '" title="删除对话" aria-label="' +
|
|
128
|
+
esc('删除对话「' + title + '」') + '">删除</button>' : '') + '</span></li>';
|
|
129
|
+
}).join('') + '</ul>' : (!historyError ? '<p class="xiaozhi-history-note">暂无历史会话</p>' : ''));
|
|
130
|
+
}
|
|
131
|
+
function renderControls() {
|
|
132
|
+
const busy = !!request || loading;
|
|
133
|
+
log.hidden = historyView;
|
|
134
|
+
form.hidden = historyView;
|
|
135
|
+
historyPanel.hidden = !historyView;
|
|
136
|
+
historyToggle.setAttribute('aria-expanded', String(historyView));
|
|
137
|
+
historyToggle.disabled = busy;
|
|
138
|
+
clear.disabled = busy || (!historyView && !messages.length && !input.value);
|
|
139
|
+
send.disabled = busy || historyView || voice.active() || !input.value.trim();
|
|
140
|
+
voiceButton.disabled = busy || historyView;
|
|
141
|
+
stop.hidden = !request;
|
|
142
|
+
}
|
|
143
|
+
function render() {
|
|
144
|
+
const atBottom = log.scrollHeight - log.scrollTop - log.clientHeight < 48;
|
|
145
|
+
const open = new Set(Array.from(log.querySelectorAll('details[open]'), (row) => row.dataset.message));
|
|
146
|
+
log.innerHTML = messages.length ? messages.map((message, index) => {
|
|
147
|
+
if (message.role === 'user') return '<div class="xiaozhi-message xiaozhi-user">' + esc(message.text) + '</div>';
|
|
148
|
+
const answer = message.receipt ? '<p><strong>已完成上报</strong></p><p>'+esc(message.receipt.destination+' · '+message.receipt.issueId)+'</p>'+
|
|
149
|
+
'<button type="button" class="btn btn-sm" data-act="issueDetail" data-args=\''+esc(JSON.stringify({id:message.receipt.issueId}))+'\'>查看事项</button>' :
|
|
150
|
+
message.text ? md(message.text) : '';
|
|
151
|
+
const status = UI.answerStatus(message, '<button type="button" class="btn btn-sm" data-xz-resume="' + index + '">继续生成</button>');
|
|
152
|
+
const note = message.note ? '<p class="xiaozhi-history-note">' + esc(message.note) + '</p>' : '';
|
|
153
|
+
const sources = message.sources?.length
|
|
154
|
+
? '<details class="xiaozhi-sources" data-message="' + index + '"' + (open.has(String(index)) ? ' open' : '') +
|
|
155
|
+
'><summary>查看参考资料(' + message.sources.length + ')</summary><ol>' + message.sources.map(sourceHtml).join('') + '</ol></details>' : '';
|
|
156
|
+
const pending = UI.chatConfirmations(message.pending, 'xzDecision', index);
|
|
157
|
+
return '<article class="xiaozhi-message"><div class="xiaozhi-answer">' + answer + status + note + '</div>' + pending + sources + '</article>';
|
|
158
|
+
}).join('') : '<div class="xiaozhi-welcome"><h3>想了解什么质量问题?</h3>' +
|
|
159
|
+
'<p>可查企业知识库、体系文件、专家资料、供应商资料和平台质量数据。</p></div>' +
|
|
160
|
+
'<div class="xiaozhi-prompts">' + prompts.map(([label, q]) => '<button type="button" data-act="tdAsk" data-args=\'' +
|
|
161
|
+
esc(JSON.stringify({ q })) + '\'>' + esc(label) + '</button>').join('') + '</div>';
|
|
162
|
+
if (!historyView && loading && !messages.length) {
|
|
163
|
+
log.innerHTML = '<p class="xiaozhi-history-note" role="status">正在恢复最近会话…</p>';
|
|
164
|
+
} else if (!historyView && historyError) {
|
|
165
|
+
log.insertAdjacentHTML('afterbegin', '<p class="xiaozhi-history-note" role="status">' + esc(historyError) + '</p>');
|
|
166
|
+
}
|
|
167
|
+
if (historyView) renderHistory();
|
|
168
|
+
renderControls();
|
|
169
|
+
if (atBottom && !pane.hidden && !historyView) log.scrollTop = log.scrollHeight;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function maxPaneWidth() {
|
|
173
|
+
return Math.max(minWidth, workspace.clientWidth - mainMinWidth);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function syncDivider(width) {
|
|
177
|
+
divider.setAttribute('aria-valuemin', minWidth);
|
|
178
|
+
divider.setAttribute('aria-valuemax', maxPaneWidth());
|
|
179
|
+
divider.setAttribute('aria-valuenow', Math.round(width));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function setWidth(requestedWidth) {
|
|
183
|
+
const width = Math.round(Math.max(minWidth, Math.min(requestedWidth, maxPaneWidth())));
|
|
184
|
+
workspace.style.setProperty('--xiaozhi-width', width + 'px');
|
|
185
|
+
syncDivider(width);
|
|
186
|
+
return width;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function rememberWidth(width) {
|
|
190
|
+
try { localStorage.setItem(widthKey, String(width)); } catch (_) { /* Optional preference. */ }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function finishDrag() {
|
|
194
|
+
if (!drag) return;
|
|
195
|
+
const { pointerId, width } = drag;
|
|
196
|
+
drag = null;
|
|
197
|
+
workspace.classList.remove('xiaozhi-resizing');
|
|
198
|
+
if (divider.hasPointerCapture(pointerId)) divider.releasePointerCapture(pointerId);
|
|
199
|
+
if (width !== undefined) rememberWidth(width);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function resize() {
|
|
203
|
+
document.getElementById('content').inert = !pane.hidden && compact.matches;
|
|
204
|
+
if (pane.hidden || compact.matches) finishDrag();
|
|
205
|
+
else syncDivider(pane.getBoundingClientRect().width);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
divider.addEventListener('pointerdown', (event) => {
|
|
209
|
+
if (compact.matches || drag || event.button !== 0) return;
|
|
210
|
+
event.preventDefault();
|
|
211
|
+
divider.focus({ preventScroll: true });
|
|
212
|
+
divider.setPointerCapture(event.pointerId);
|
|
213
|
+
drag = { pointerId: event.pointerId, startX: event.clientX, startWidth: pane.getBoundingClientRect().width };
|
|
214
|
+
workspace.classList.add('xiaozhi-resizing');
|
|
215
|
+
});
|
|
216
|
+
window.addEventListener('pointermove', (event) => {
|
|
217
|
+
if (drag?.pointerId !== event.pointerId) return;
|
|
218
|
+
drag.width = setWidth(drag.startWidth + drag.startX - event.clientX);
|
|
219
|
+
});
|
|
220
|
+
function finishPointerDrag(event) {
|
|
221
|
+
if (drag?.pointerId === event.pointerId) finishDrag();
|
|
222
|
+
}
|
|
223
|
+
window.addEventListener('pointerup', finishPointerDrag);
|
|
224
|
+
window.addEventListener('pointercancel', finishPointerDrag);
|
|
225
|
+
divider.addEventListener('lostpointercapture', finishPointerDrag);
|
|
226
|
+
divider.addEventListener('keydown', (event) => {
|
|
227
|
+
const width = pane.getBoundingClientRect().width;
|
|
228
|
+
const step = 24;
|
|
229
|
+
const requested = {
|
|
230
|
+
ArrowLeft: width + step, ArrowRight: width - step, Home: minWidth, End: maxPaneWidth(),
|
|
231
|
+
}[event.key];
|
|
232
|
+
if (!Number.isFinite(requested)) return;
|
|
233
|
+
event.preventDefault();
|
|
234
|
+
rememberWidth(setWidth(requested));
|
|
235
|
+
});
|
|
236
|
+
new ResizeObserver(resize).observe(workspace);
|
|
237
|
+
|
|
238
|
+
function display(open) {
|
|
239
|
+
if (pane.hidden === !open) return;
|
|
240
|
+
const focusWasInside = pane.contains(document.activeElement);
|
|
241
|
+
pane.hidden = !open;
|
|
242
|
+
if (!open) voice.cancel();
|
|
243
|
+
document.querySelectorAll('[aria-controls="xiaozhiPane"][aria-expanded]').forEach((button) => {
|
|
244
|
+
button.setAttribute('aria-expanded', String(open));
|
|
245
|
+
});
|
|
246
|
+
resize();
|
|
247
|
+
if (open) {
|
|
248
|
+
global.XiaozhiVoice.warm();
|
|
249
|
+
void restore();
|
|
250
|
+
log.scrollTop = log.scrollHeight;
|
|
251
|
+
if (!compact.matches && !historyView) input.focus({ preventScroll: true });
|
|
252
|
+
} else if (focusWasInside) {
|
|
253
|
+
const returnTo = document.querySelector('.qa-toolbar [aria-controls="xiaozhiPane"]') || toggle;
|
|
254
|
+
returnTo.focus({ preventScroll: true });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async function draft(text) {
|
|
259
|
+
const question = String(text || '').trim();
|
|
260
|
+
if (!question) return;
|
|
261
|
+
display(true);
|
|
262
|
+
await restore();
|
|
263
|
+
if (revisionTarget) {
|
|
264
|
+
UI.toast('请先处理输入框中的文件修订草稿。','warn');
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (!sessionEditable) resetConversation();
|
|
268
|
+
historyView = false;
|
|
269
|
+
const previous = input.value.trim();
|
|
270
|
+
input.value = previous && previous !== question ? previous + '\n\n' + question : question;
|
|
271
|
+
render();
|
|
272
|
+
input.focus();
|
|
273
|
+
input.setSelectionRange(input.value.length,input.value.length);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function ask(text, {issueId} = {}) {
|
|
277
|
+
display(true);
|
|
278
|
+
await restore();
|
|
279
|
+
if (request || loading) {
|
|
280
|
+
if (issueId) UI.toast('请等待当前回答完成,或点击停止后再分析这条事项','warn');
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (voice.active()) {
|
|
284
|
+
if (text == null) return;
|
|
285
|
+
voice.cancel();
|
|
286
|
+
}
|
|
287
|
+
historyView = false;
|
|
288
|
+
const question = String(text ?? input.value).trim();
|
|
289
|
+
if (!question) { input.focus(); return; }
|
|
290
|
+
const revisionFileId = text == null && revisionTarget && question.startsWith(revisionTarget.prefix)
|
|
291
|
+
? revisionTarget.id : undefined;
|
|
292
|
+
if (issueId || !sessionEditable) resetConversation();
|
|
293
|
+
if (text == null) { input.value = ''; revisionTarget = undefined; }
|
|
294
|
+
const history = messages.filter((message) => message.text && message.state !== 'incomplete')
|
|
295
|
+
.map((message) => ({ role: message.role, content: message.text })).slice(-24);
|
|
296
|
+
const reply = { role: 'assistant', text: '', pending: [] };
|
|
297
|
+
messages.push({ role: 'user', text: question }, reply);
|
|
298
|
+
await deliver(reply, (options) => API.answer('/qip-real-workbench/api/chat/stream', {
|
|
299
|
+
mode: 'xiaozhi', text: question, sessionId: session, history, issueId, revisionFileId,
|
|
300
|
+
}, reply, options));
|
|
301
|
+
if ((reply.text || reply.answerId) && !reply.historyWarning) rememberSession(session);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async function reviseFile(file) {
|
|
305
|
+
display(true);
|
|
306
|
+
await restore();
|
|
307
|
+
if (request || loading) {
|
|
308
|
+
UI.toast('请等待当前回答完成后再修订文件。','warn');
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if (input.value.trim() && input.value !== revisionTarget?.prefix) {
|
|
312
|
+
UI.toast('小质还有未发送的内容,请先处理当前输入后再选文件。','warn');
|
|
313
|
+
input.focus();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
historyView = false;
|
|
317
|
+
if (!sessionEditable) resetConversation();
|
|
318
|
+
const prefix = '请修订《' + file.name + '》:';
|
|
319
|
+
revisionTarget = { id:file.id, prefix };
|
|
320
|
+
input.value = prefix;
|
|
321
|
+
render();
|
|
322
|
+
input.focus();
|
|
323
|
+
input.setSelectionRange(prefix.length,prefix.length);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async function deliver(reply, run) {
|
|
327
|
+
if (request) return;
|
|
328
|
+
const controller = new AbortController();
|
|
329
|
+
request = controller;
|
|
330
|
+
log.scrollTop = log.scrollHeight;
|
|
331
|
+
try {
|
|
332
|
+
await run({
|
|
333
|
+
signal: controller.signal,
|
|
334
|
+
render: () => { if (request === controller) render(); },
|
|
335
|
+
onAction: (event) => reply.pending.push(event),
|
|
336
|
+
});
|
|
337
|
+
} finally {
|
|
338
|
+
if (request === controller) { request = null; render(); }
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
log.addEventListener('click', (event) => {
|
|
343
|
+
const button = event.target.closest('[data-xz-resume]');
|
|
344
|
+
if (!button) return;
|
|
345
|
+
const message = messages[Number(button.dataset.xzResume)];
|
|
346
|
+
if (message?.answerId) deliver(message, (options) => API.resumeAnswer(message, options));
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
async function refreshHistory() {
|
|
350
|
+
loading = true;
|
|
351
|
+
historyError = '';
|
|
352
|
+
render();
|
|
353
|
+
try { historyRows = await readHistory(); }
|
|
354
|
+
catch (_) { historyError = '历史会话暂时无法载入。'; }
|
|
355
|
+
finally { loading = false; render(); }
|
|
356
|
+
}
|
|
357
|
+
historyToggle.addEventListener('click', () => {
|
|
358
|
+
if (request || loading) return;
|
|
359
|
+
voice.cancel();
|
|
360
|
+
historyView = !historyView;
|
|
361
|
+
if (historyView) void refreshHistory();
|
|
362
|
+
else render();
|
|
363
|
+
});
|
|
364
|
+
async function deleteSession(row) {
|
|
365
|
+
const accepted = await global.AskConfirm('删除对话', [['对话', row.title || '小质会话'], ['消息', row.count + ' 条']],
|
|
366
|
+
'这段对话及其消息将永久删除。', { sub: '', danger: true, label: '删除对话' });
|
|
367
|
+
if (!accepted || loading || request) return;
|
|
368
|
+
loading = true;
|
|
369
|
+
renderControls();
|
|
370
|
+
try {
|
|
371
|
+
const result = await API.del(chatsPath + '/' + encodeURIComponent(row.id) + '?mode=xiaozhi');
|
|
372
|
+
if (!result.ok) throw new Error(result.error || '服务未确认删除');
|
|
373
|
+
historyRows = historyRows.filter(({ id }) => id !== row.id);
|
|
374
|
+
historyError = '';
|
|
375
|
+
forgetSession(row.id);
|
|
376
|
+
if (row.id === session) resetConversation();
|
|
377
|
+
UI.toast('已删除这段对话', 'ok');
|
|
378
|
+
} catch (error) {
|
|
379
|
+
UI.toast('删除对话失败:' + error.message, 'err');
|
|
380
|
+
} finally {
|
|
381
|
+
loading = false;
|
|
382
|
+
render();
|
|
383
|
+
if (!pane.hidden) historyToggle.focus();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
historyPanel.addEventListener('click', async (event) => {
|
|
387
|
+
if (loading || request) return;
|
|
388
|
+
if (event.target.closest('[data-xz-history-retry]')) return refreshHistory();
|
|
389
|
+
const share = event.target.closest('[data-xz-share]');
|
|
390
|
+
if (share) {
|
|
391
|
+
const row = historyRows.find(({id}) => id===share.dataset.xzShare);
|
|
392
|
+
if (!row?.canShare) return;
|
|
393
|
+
if (!row.shared && !global.confirm('共享后,本工作区的已登录员工可阅读这段对话。确定共享?')) return;
|
|
394
|
+
try {
|
|
395
|
+
await API.put(chatsPath+'/'+encodeURIComponent(row.id)+'/sharing',{shared:!row.shared});
|
|
396
|
+
await refreshHistory();
|
|
397
|
+
} catch (error) { UI.toast(error.message,'err'); }
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
const remove = event.target.closest('[data-xz-delete]');
|
|
401
|
+
if (remove) {
|
|
402
|
+
const row = historyRows.find(({ id }) => id === remove.dataset.xzDelete);
|
|
403
|
+
if (row) await deleteSession(row);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
const choice = event.target.closest('[data-xz-session]');
|
|
407
|
+
if (!choice) return;
|
|
408
|
+
const id = choice.dataset.xzSession;
|
|
409
|
+
loading = true;
|
|
410
|
+
historyError = '';
|
|
411
|
+
render();
|
|
412
|
+
try {
|
|
413
|
+
if (id !== session) {
|
|
414
|
+
await activateSession(id);
|
|
415
|
+
input.value = '';
|
|
416
|
+
}
|
|
417
|
+
historyView = false;
|
|
418
|
+
} catch (_) {
|
|
419
|
+
historyError = '这段会话暂时无法载入。';
|
|
420
|
+
} finally {
|
|
421
|
+
loading = false;
|
|
422
|
+
render();
|
|
423
|
+
if (!historyView) {
|
|
424
|
+
log.scrollTop = log.scrollHeight;
|
|
425
|
+
if (!pane.hidden) input.focus();
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
function resetConversation() {
|
|
430
|
+
voice.cancel();
|
|
431
|
+
revisionTarget = undefined;
|
|
432
|
+
messages = [];
|
|
433
|
+
session = makeSession();
|
|
434
|
+
sessionEditable = true;
|
|
435
|
+
input.value = '';
|
|
436
|
+
}
|
|
437
|
+
const voice = global.XiaozhiVoice.bind({ button: voiceButton, input, status: voiceStatus,
|
|
438
|
+
canStart: () => !pane.hidden && !historyView && !request && !loading, onChange: renderControls });
|
|
439
|
+
stop.addEventListener('click', () => request?.abort());
|
|
440
|
+
clear.addEventListener('click', () => {
|
|
441
|
+
if (request || loading) return;
|
|
442
|
+
resetConversation();
|
|
443
|
+
historyView = false;
|
|
444
|
+
historyError = '';
|
|
445
|
+
render();
|
|
446
|
+
input.focus();
|
|
447
|
+
});
|
|
448
|
+
form.addEventListener('submit', (event) => { event.preventDefault(); ask(); });
|
|
449
|
+
input.addEventListener('keydown', (event) => {
|
|
450
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing && event.keyCode !== 229) {
|
|
451
|
+
event.preventDefault();
|
|
452
|
+
form.requestSubmit();
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
input.addEventListener('input', renderControls);
|
|
456
|
+
compact.addEventListener('change', resize);
|
|
457
|
+
global.XiaoZhi = {
|
|
458
|
+
open: () => display(true),
|
|
459
|
+
close: () => display(false),
|
|
460
|
+
toggle: () => display(pane.hidden),
|
|
461
|
+
ask,
|
|
462
|
+
draft,
|
|
463
|
+
reviseFile,
|
|
464
|
+
decide: ({ i, id, approved }) => UI.decideChatConfirmation(messages[i], id, approved, render),
|
|
465
|
+
setPage: (title) => { context.textContent = '当前页面 · ' + title; },
|
|
466
|
+
};
|
|
467
|
+
render();
|
|
468
|
+
})(window);
|