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/api.js
CHANGED
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
/* ==================================================================
|
|
2
|
-
API 客户端:REST 封装 + SSE 流式对话
|
|
3
|
-
================================================================== */
|
|
4
|
-
(function (global) {
|
|
5
|
-
'use strict';
|
|
6
|
-
|
|
7
|
-
async function req(method, path, body, signal) {
|
|
8
|
-
const opt = { method, headers: {}, signal };
|
|
9
|
-
if (body !== undefined) { opt.headers['Content-Type'] = 'application/json'; opt.body = JSON.stringify(body); }
|
|
10
|
-
const res = await fetch(path, opt);
|
|
11
|
-
const txt = await res.text();
|
|
12
|
-
let json;
|
|
13
|
-
try { json = txt ? JSON.parse(txt) : {}; } catch (e) { throw new Error('服务端返回了非 JSON 内容(HTTP ' + res.status + '):' + txt.slice(0, 200)); }
|
|
14
|
-
if (!res.ok) throw Object.assign(new Error(json.error || '请求失败(HTTP ' + res.status + ')'),{status:res.status});
|
|
15
|
-
return json;
|
|
16
|
-
}
|
|
17
|
-
function qs(params) {
|
|
18
|
-
const p = new URLSearchParams();
|
|
19
|
-
Object.keys(params || {}).forEach((k) => { if (params[k] !== undefined && params[k] !== null && params[k] !== '') p.set(k, params[k]); });
|
|
20
|
-
const s = p.toString();
|
|
21
|
-
return s ? '?' + s : '';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const API = {
|
|
25
|
-
get: (path, params, signal) => req('GET', path + qs(params), undefined, signal),
|
|
26
|
-
post: (path, body, signal) => req('POST', path, body || {}, signal),
|
|
27
|
-
put: (path, body) => req('PUT', path, body || {}),
|
|
28
|
-
del: (path) => req('DELETE', path),
|
|
29
|
-
bootstrap: () => req('GET', '/qip-real-workbench/api/bootstrap'),
|
|
30
|
-
health: () => req('GET', '/qip-real-workbench/api/health'),
|
|
31
|
-
|
|
32
|
-
/** 工具直接调用(界面按钮) */
|
|
33
|
-
tool: (name, args) => req('POST', '/qip-real-workbench/api/tools/' + name, args || {}),
|
|
34
|
-
|
|
35
|
-
chat(payload, onEvent, signal) {
|
|
36
|
-
return API.stream('/qip-real-workbench/api/chat/stream', payload, onEvent, signal);
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
async stream(path, payload, onEvent, signal, requireTerminal = false) {
|
|
40
|
-
const res = await fetch(path, {
|
|
41
|
-
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
42
|
-
body: JSON.stringify(payload), signal,
|
|
43
|
-
});
|
|
44
|
-
if (!res.ok) {
|
|
45
|
-
let msg = 'HTTP ' + res.status;
|
|
46
|
-
try { const j = await res.json(); msg = j.error || msg; } catch (e) {}
|
|
47
|
-
onEvent({ type: 'error', message: msg, status: res.status });
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
if (requireTerminal && res.headers?.get('Content-Type')?.includes('application/json')) {
|
|
51
|
-
const result = await res.json();
|
|
52
|
-
if (!result.ok) throw new Error(result.error || '回答失败。');
|
|
53
|
-
onEvent({ ...result, type: 'done', content: result.content ?? result.text ?? '' });
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const reader = res.body.getReader();
|
|
57
|
-
const dec = new TextDecoder('utf-8');
|
|
58
|
-
let buf = '';
|
|
59
|
-
let terminal = false;
|
|
60
|
-
const dispatch = (block) => {
|
|
61
|
-
const payload = block.split(/\r?\n/).filter((line) => line.startsWith('data:'))
|
|
62
|
-
.map((line) => line.slice(5).replace(/^ /, '')).join('\n');
|
|
63
|
-
if (!payload || payload === '[DONE]') return;
|
|
64
|
-
const event = JSON.parse(payload);
|
|
65
|
-
if (event.type === 'done' || event.type === 'error') terminal = true;
|
|
66
|
-
onEvent(event);
|
|
67
|
-
};
|
|
68
|
-
try {
|
|
69
|
-
for (;;) {
|
|
70
|
-
const { done, value } = await reader.read();
|
|
71
|
-
buf += done ? dec.decode() : dec.decode(value, { stream: true });
|
|
72
|
-
let boundary;
|
|
73
|
-
while ((boundary = /\r?\n\r?\n/.exec(buf))) {
|
|
74
|
-
dispatch(buf.slice(0, boundary.index));
|
|
75
|
-
buf = buf.slice(boundary.index + boundary[0].length);
|
|
76
|
-
}
|
|
77
|
-
if (done) break;
|
|
78
|
-
}
|
|
79
|
-
if (buf.trim()) dispatch(buf);
|
|
80
|
-
} finally { reader.releaseLock?.(); }
|
|
81
|
-
if (requireTerminal && !terminal && !signal?.aborted) {
|
|
82
|
-
throw new Error('连接中断,回复尚未完成。');
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
async answer(path, payload, message, { field = 'text', signal, render, onAction } = {}) {
|
|
87
|
-
message.running = true;
|
|
88
|
-
message.error = '';
|
|
89
|
-
render?.();
|
|
90
|
-
try {
|
|
91
|
-
await API.stream(path, payload, (event) => {
|
|
92
|
-
if (event.answerId) message.answerId = event.answerId;
|
|
93
|
-
if (event.sessionId) message.sessionId = event.sessionId;
|
|
94
|
-
switch (event.type) {
|
|
95
|
-
case 'status': message.status = event.message; break;
|
|
96
|
-
case 'session': break;
|
|
97
|
-
case 'notice': message.sourceNotice = event.message; break;
|
|
98
|
-
case 'sources': message.sources = event.sources; break;
|
|
99
|
-
case 'content':
|
|
100
|
-
message[field] = (message[field] || '') + event.text;
|
|
101
|
-
message.status = '';
|
|
102
|
-
break;
|
|
103
|
-
case 'done':
|
|
104
|
-
message[field] = event.content;
|
|
105
|
-
message.state = event.state || 'complete';
|
|
106
|
-
for (const key of ['note', 'mode', 'scope', 'sources', 'finishReason', 'historyWarning']) {
|
|
107
|
-
if (event[key] !== undefined) message[key] = event[key];
|
|
108
|
-
}
|
|
109
|
-
break;
|
|
110
|
-
case 'error':
|
|
111
|
-
message.error = event.message;
|
|
112
|
-
message.state = 'incomplete';
|
|
113
|
-
if (event.status === 410) delete message.answerId;
|
|
114
|
-
break;
|
|
115
|
-
case 'confirm_required': onAction?.(event); break;
|
|
116
|
-
default: return;
|
|
117
|
-
}
|
|
118
|
-
render?.();
|
|
119
|
-
}, signal, true);
|
|
120
|
-
} catch (error) {
|
|
121
|
-
message.state = 'incomplete';
|
|
122
|
-
if (!signal?.aborted) message.error = error.message || '连接中断,回复尚未完成。';
|
|
123
|
-
} finally {
|
|
124
|
-
if (signal?.aborted && message.state !== 'complete') {
|
|
125
|
-
message.state = 'incomplete';
|
|
126
|
-
message.error = '已停止生成。';
|
|
127
|
-
}
|
|
128
|
-
message.running = false;
|
|
129
|
-
message.status = '';
|
|
130
|
-
render?.();
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
resumeAnswer(message, options) {
|
|
135
|
-
return API.answer('/qip-real-workbench/api/chat/resume?stream=1', { answerId: message.answerId }, message, options);
|
|
136
|
-
},
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
/** 文件导入:转 base64 上传 */
|
|
140
|
-
async importFile(file, target, options = {}) {
|
|
141
|
-
const b64 = await new Promise((resolve, reject) => {
|
|
142
|
-
const fr = new FileReader();
|
|
143
|
-
fr.onload = () => resolve(String(fr.result).split(',')[1]);
|
|
144
|
-
fr.onerror = reject;
|
|
145
|
-
fr.readAsDataURL(file);
|
|
146
|
-
});
|
|
147
|
-
return req('POST', '/qip-real-workbench/api/import', { filename: file.name, base64: b64, target, ...options });
|
|
148
|
-
},
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
global.API = API;
|
|
152
|
-
})(window);
|
|
1
|
+
/* ==================================================================
|
|
2
|
+
API 客户端:REST 封装 + SSE 流式对话
|
|
3
|
+
================================================================== */
|
|
4
|
+
(function (global) {
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
async function req(method, path, body, signal) {
|
|
8
|
+
const opt = { method, headers: {}, signal };
|
|
9
|
+
if (body !== undefined) { opt.headers['Content-Type'] = 'application/json'; opt.body = JSON.stringify(body); }
|
|
10
|
+
const res = await fetch(path, opt);
|
|
11
|
+
const txt = await res.text();
|
|
12
|
+
let json;
|
|
13
|
+
try { json = txt ? JSON.parse(txt) : {}; } catch (e) { throw new Error('服务端返回了非 JSON 内容(HTTP ' + res.status + '):' + txt.slice(0, 200)); }
|
|
14
|
+
if (!res.ok) throw Object.assign(new Error(json.error || '请求失败(HTTP ' + res.status + ')'),{status:res.status});
|
|
15
|
+
return json;
|
|
16
|
+
}
|
|
17
|
+
function qs(params) {
|
|
18
|
+
const p = new URLSearchParams();
|
|
19
|
+
Object.keys(params || {}).forEach((k) => { if (params[k] !== undefined && params[k] !== null && params[k] !== '') p.set(k, params[k]); });
|
|
20
|
+
const s = p.toString();
|
|
21
|
+
return s ? '?' + s : '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const API = {
|
|
25
|
+
get: (path, params, signal) => req('GET', path + qs(params), undefined, signal),
|
|
26
|
+
post: (path, body, signal) => req('POST', path, body || {}, signal),
|
|
27
|
+
put: (path, body) => req('PUT', path, body || {}),
|
|
28
|
+
del: (path) => req('DELETE', path),
|
|
29
|
+
bootstrap: () => req('GET', '/qip-real-workbench/api/bootstrap'),
|
|
30
|
+
health: () => req('GET', '/qip-real-workbench/api/health'),
|
|
31
|
+
|
|
32
|
+
/** 工具直接调用(界面按钮) */
|
|
33
|
+
tool: (name, args) => req('POST', '/qip-real-workbench/api/tools/' + name, args || {}),
|
|
34
|
+
|
|
35
|
+
chat(payload, onEvent, signal) {
|
|
36
|
+
return API.stream('/qip-real-workbench/api/chat/stream', payload, onEvent, signal);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
async stream(path, payload, onEvent, signal, requireTerminal = false) {
|
|
40
|
+
const res = await fetch(path, {
|
|
41
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
42
|
+
body: JSON.stringify(payload), signal,
|
|
43
|
+
});
|
|
44
|
+
if (!res.ok) {
|
|
45
|
+
let msg = 'HTTP ' + res.status;
|
|
46
|
+
try { const j = await res.json(); msg = j.error || msg; } catch (e) {}
|
|
47
|
+
onEvent({ type: 'error', message: msg, status: res.status });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (requireTerminal && res.headers?.get('Content-Type')?.includes('application/json')) {
|
|
51
|
+
const result = await res.json();
|
|
52
|
+
if (!result.ok) throw new Error(result.error || '回答失败。');
|
|
53
|
+
onEvent({ ...result, type: 'done', content: result.content ?? result.text ?? '' });
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const reader = res.body.getReader();
|
|
57
|
+
const dec = new TextDecoder('utf-8');
|
|
58
|
+
let buf = '';
|
|
59
|
+
let terminal = false;
|
|
60
|
+
const dispatch = (block) => {
|
|
61
|
+
const payload = block.split(/\r?\n/).filter((line) => line.startsWith('data:'))
|
|
62
|
+
.map((line) => line.slice(5).replace(/^ /, '')).join('\n');
|
|
63
|
+
if (!payload || payload === '[DONE]') return;
|
|
64
|
+
const event = JSON.parse(payload);
|
|
65
|
+
if (event.type === 'done' || event.type === 'error') terminal = true;
|
|
66
|
+
onEvent(event);
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
for (;;) {
|
|
70
|
+
const { done, value } = await reader.read();
|
|
71
|
+
buf += done ? dec.decode() : dec.decode(value, { stream: true });
|
|
72
|
+
let boundary;
|
|
73
|
+
while ((boundary = /\r?\n\r?\n/.exec(buf))) {
|
|
74
|
+
dispatch(buf.slice(0, boundary.index));
|
|
75
|
+
buf = buf.slice(boundary.index + boundary[0].length);
|
|
76
|
+
}
|
|
77
|
+
if (done) break;
|
|
78
|
+
}
|
|
79
|
+
if (buf.trim()) dispatch(buf);
|
|
80
|
+
} finally { reader.releaseLock?.(); }
|
|
81
|
+
if (requireTerminal && !terminal && !signal?.aborted) {
|
|
82
|
+
throw new Error('连接中断,回复尚未完成。');
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
async answer(path, payload, message, { field = 'text', signal, render, onAction } = {}) {
|
|
87
|
+
message.running = true;
|
|
88
|
+
message.error = '';
|
|
89
|
+
render?.();
|
|
90
|
+
try {
|
|
91
|
+
await API.stream(path, payload, (event) => {
|
|
92
|
+
if (event.answerId) message.answerId = event.answerId;
|
|
93
|
+
if (event.sessionId) message.sessionId = event.sessionId;
|
|
94
|
+
switch (event.type) {
|
|
95
|
+
case 'status': message.status = event.message; break;
|
|
96
|
+
case 'session': break;
|
|
97
|
+
case 'notice': message.sourceNotice = event.message; break;
|
|
98
|
+
case 'sources': message.sources = event.sources; break;
|
|
99
|
+
case 'content':
|
|
100
|
+
message[field] = (message[field] || '') + event.text;
|
|
101
|
+
message.status = '';
|
|
102
|
+
break;
|
|
103
|
+
case 'done':
|
|
104
|
+
message[field] = event.content;
|
|
105
|
+
message.state = event.state || 'complete';
|
|
106
|
+
for (const key of ['note', 'mode', 'scope', 'sources', 'finishReason', 'historyWarning']) {
|
|
107
|
+
if (event[key] !== undefined) message[key] = event[key];
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case 'error':
|
|
111
|
+
message.error = event.message;
|
|
112
|
+
message.state = 'incomplete';
|
|
113
|
+
if (event.status === 410) delete message.answerId;
|
|
114
|
+
break;
|
|
115
|
+
case 'confirm_required': onAction?.(event); break;
|
|
116
|
+
default: return;
|
|
117
|
+
}
|
|
118
|
+
render?.();
|
|
119
|
+
}, signal, true);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
message.state = 'incomplete';
|
|
122
|
+
if (!signal?.aborted) message.error = error.message || '连接中断,回复尚未完成。';
|
|
123
|
+
} finally {
|
|
124
|
+
if (signal?.aborted && message.state !== 'complete') {
|
|
125
|
+
message.state = 'incomplete';
|
|
126
|
+
message.error = '已停止生成。';
|
|
127
|
+
}
|
|
128
|
+
message.running = false;
|
|
129
|
+
message.status = '';
|
|
130
|
+
render?.();
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
resumeAnswer(message, options) {
|
|
135
|
+
return API.answer('/qip-real-workbench/api/chat/resume?stream=1', { answerId: message.answerId }, message, options);
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
/** 文件导入:转 base64 上传 */
|
|
140
|
+
async importFile(file, target, options = {}) {
|
|
141
|
+
const b64 = await new Promise((resolve, reject) => {
|
|
142
|
+
const fr = new FileReader();
|
|
143
|
+
fr.onload = () => resolve(String(fr.result).split(',')[1]);
|
|
144
|
+
fr.onerror = reject;
|
|
145
|
+
fr.readAsDataURL(file);
|
|
146
|
+
});
|
|
147
|
+
return req('POST', '/qip-real-workbench/api/import', { filename: file.name, base64: b64, target, ...options });
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
global.API = API;
|
|
152
|
+
})(window);
|
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
(function (global) {
|
|
2
|
-
'use strict';
|
|
3
|
-
const base = '/qip-real-workbench/api/chats';
|
|
4
|
-
const {esc,modal,closeModal,toast,dt} = global.UI;
|
|
5
|
-
const API = global.API;
|
|
6
|
-
const url = (id) => base + '/' + encodeURIComponent(id);
|
|
7
|
-
|
|
8
|
-
async function latest(scope) {
|
|
9
|
-
const list = await API.get(base,scope);
|
|
10
|
-
const row = list.rows?.find((item) => item.editable !== false);
|
|
11
|
-
if (!row) return null;
|
|
12
|
-
return (await API.get(url(row.id),scope)).row || null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async function open(scope, onLoad, onDelete) {
|
|
16
|
-
try {
|
|
17
|
-
const list = await API.get(base,scope);
|
|
18
|
-
const rows = list.rows || [];
|
|
19
|
-
modal({
|
|
20
|
-
title:'历史对话',
|
|
21
|
-
body: rows.length ? '<ul class="assistant-history-list">' + rows.map((row) =>
|
|
22
|
-
'<li><div><button type="button" class="btn btn-sm" data-history-open="'+esc(row.id)+'">'+esc(row.title)+'</button><small>'+
|
|
23
|
-
esc(dt(row.updatedAt) + (row.shared ? ' · 工作区共享' : row.legacy ? ' · 旧版历史' : ''))+'</small></div><div>'+
|
|
24
|
-
(row.canShare ? '<button type="button" class="btn btn-sm" data-history-share="'+esc(row.id)+'">'+(row.shared ? '取消共享' : '分享给工作区')+'</button>' : '')+
|
|
25
|
-
(row.editable !== false ? '<button type="button" class="btn btn-sm" data-history-delete="'+esc(row.id)+'">删除</button>' : '')+
|
|
26
|
-
'</div></li>').join('') + '</ul>' : '<p>暂无历史对话。</p>',
|
|
27
|
-
footer:'<button type="button" class="btn" data-act="__closeModal">关闭</button>',
|
|
28
|
-
onMount(panel) {
|
|
29
|
-
panel.addEventListener('click',async (event) => {
|
|
30
|
-
const control = event.target.closest('[data-history-open],[data-history-share],[data-history-delete]');
|
|
31
|
-
if (!control || control.disabled) return;
|
|
32
|
-
const {historyOpen,historyShare,historyDelete} = control.dataset;
|
|
33
|
-
control.disabled = true;
|
|
34
|
-
try {
|
|
35
|
-
if (historyOpen) {
|
|
36
|
-
const {row} = await API.get(url(historyOpen),scope);
|
|
37
|
-
if (!row) throw new Error('会话无法加载');
|
|
38
|
-
closeModal();
|
|
39
|
-
await onLoad(row);
|
|
40
|
-
} else if (historyDelete) {
|
|
41
|
-
if (!global.confirm('确定删除这段对话?')) return;
|
|
42
|
-
const result = await API.del(url(historyDelete)+'?'+new URLSearchParams(scope));
|
|
43
|
-
if (!result.ok) throw new Error(result.error || '删除失败');
|
|
44
|
-
await onDelete(historyDelete);
|
|
45
|
-
closeModal();
|
|
46
|
-
await open(scope,onLoad,onDelete);
|
|
47
|
-
} else {
|
|
48
|
-
const row = rows.find((item) => item.id === historyShare);
|
|
49
|
-
if (!row.shared && !global.confirm('共享后,本工作区的已登录员工可阅读这段对话。确定共享?')) return;
|
|
50
|
-
await API.put(url(historyShare)+'/sharing',{shared:!row.shared});
|
|
51
|
-
closeModal();
|
|
52
|
-
await open(scope,onLoad,onDelete);
|
|
53
|
-
}
|
|
54
|
-
} catch (error) { toast(error.message,'err'); }
|
|
55
|
-
finally { control.disabled = false; }
|
|
56
|
-
});
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
} catch (error) { toast(error.message,'err'); }
|
|
60
|
-
}
|
|
61
|
-
global.AssistantHistory = {latest,open};
|
|
62
|
-
})(window);
|
|
1
|
+
(function (global) {
|
|
2
|
+
'use strict';
|
|
3
|
+
const base = '/qip-real-workbench/api/chats';
|
|
4
|
+
const {esc,modal,closeModal,toast,dt} = global.UI;
|
|
5
|
+
const API = global.API;
|
|
6
|
+
const url = (id) => base + '/' + encodeURIComponent(id);
|
|
7
|
+
|
|
8
|
+
async function latest(scope) {
|
|
9
|
+
const list = await API.get(base,scope);
|
|
10
|
+
const row = list.rows?.find((item) => item.editable !== false);
|
|
11
|
+
if (!row) return null;
|
|
12
|
+
return (await API.get(url(row.id),scope)).row || null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function open(scope, onLoad, onDelete) {
|
|
16
|
+
try {
|
|
17
|
+
const list = await API.get(base,scope);
|
|
18
|
+
const rows = list.rows || [];
|
|
19
|
+
modal({
|
|
20
|
+
title:'历史对话',
|
|
21
|
+
body: rows.length ? '<ul class="assistant-history-list">' + rows.map((row) =>
|
|
22
|
+
'<li><div><button type="button" class="btn btn-sm" data-history-open="'+esc(row.id)+'">'+esc(row.title)+'</button><small>'+
|
|
23
|
+
esc(dt(row.updatedAt) + (row.shared ? ' · 工作区共享' : row.legacy ? ' · 旧版历史' : ''))+'</small></div><div>'+
|
|
24
|
+
(row.canShare ? '<button type="button" class="btn btn-sm" data-history-share="'+esc(row.id)+'">'+(row.shared ? '取消共享' : '分享给工作区')+'</button>' : '')+
|
|
25
|
+
(row.editable !== false ? '<button type="button" class="btn btn-sm" data-history-delete="'+esc(row.id)+'">删除</button>' : '')+
|
|
26
|
+
'</div></li>').join('') + '</ul>' : '<p>暂无历史对话。</p>',
|
|
27
|
+
footer:'<button type="button" class="btn" data-act="__closeModal">关闭</button>',
|
|
28
|
+
onMount(panel) {
|
|
29
|
+
panel.addEventListener('click',async (event) => {
|
|
30
|
+
const control = event.target.closest('[data-history-open],[data-history-share],[data-history-delete]');
|
|
31
|
+
if (!control || control.disabled) return;
|
|
32
|
+
const {historyOpen,historyShare,historyDelete} = control.dataset;
|
|
33
|
+
control.disabled = true;
|
|
34
|
+
try {
|
|
35
|
+
if (historyOpen) {
|
|
36
|
+
const {row} = await API.get(url(historyOpen),scope);
|
|
37
|
+
if (!row) throw new Error('会话无法加载');
|
|
38
|
+
closeModal();
|
|
39
|
+
await onLoad(row);
|
|
40
|
+
} else if (historyDelete) {
|
|
41
|
+
if (!global.confirm('确定删除这段对话?')) return;
|
|
42
|
+
const result = await API.del(url(historyDelete)+'?'+new URLSearchParams(scope));
|
|
43
|
+
if (!result.ok) throw new Error(result.error || '删除失败');
|
|
44
|
+
await onDelete(historyDelete);
|
|
45
|
+
closeModal();
|
|
46
|
+
await open(scope,onLoad,onDelete);
|
|
47
|
+
} else {
|
|
48
|
+
const row = rows.find((item) => item.id === historyShare);
|
|
49
|
+
if (!row.shared && !global.confirm('共享后,本工作区的已登录员工可阅读这段对话。确定共享?')) return;
|
|
50
|
+
await API.put(url(historyShare)+'/sharing',{shared:!row.shared});
|
|
51
|
+
closeModal();
|
|
52
|
+
await open(scope,onLoad,onDelete);
|
|
53
|
+
}
|
|
54
|
+
} catch (error) { toast(error.message,'err'); }
|
|
55
|
+
finally { control.disabled = false; }
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
} catch (error) { toast(error.message,'err'); }
|
|
60
|
+
}
|
|
61
|
+
global.AssistantHistory = {latest,open};
|
|
62
|
+
})(window);
|
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
|
|
2
|
-
/* Directory and file panes shared by supplier and R&D project libraries. */
|
|
3
|
-
(function (global) {
|
|
4
|
-
'use strict';
|
|
5
|
-
const {esc} = global.UI;
|
|
6
|
-
function button(label, action, args = {}, cls = 'btn', attrs = '') {
|
|
7
|
-
return '<button type="button" class="'+cls+'" data-act="'+action+'" data-args="'+esc(JSON.stringify(args))+'"'+attrs+'>'+esc(label)+'</button>';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function directoryRow(item, selected, pick, edit, remove, label) {
|
|
11
|
-
const args = {id:item.id};
|
|
12
|
-
const row = button(item.name,pick,args,'sqm-lib-item',
|
|
13
|
-
' title="'+esc(item.name)+'" aria-pressed="'+selected+'"');
|
|
14
|
-
const controls = selected && (edit || remove) ? '<div class="sqm-directory-actions">'+
|
|
15
|
-
(edit ? button('✎',edit,args,'sqm-directory-action',' aria-label="'+esc(label.edit)+'" title="'+esc(label.edit)+'"') : '')+
|
|
16
|
-
(remove ? button('×',remove,args,'sqm-directory-action sqm-directory-delete',' aria-label="'+esc(label.remove)+'" title="'+esc(label.remove)+'"') : '')+
|
|
17
|
-
'</div>' : '';
|
|
18
|
-
return '<div class="sqm-directory-row'+(selected?' on':'')+'">'+row+controls+'</div>';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function selectedFolder(data, folderId) {
|
|
22
|
-
return folderId === '' || data.folders.some(({id})=>id === folderId) ? folderId : data.defaultFolderId || '';
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function fileSize(bytes) {
|
|
26
|
-
return bytes >= 1024 ** 2 ? (bytes / 1024 ** 2).toFixed(1) + ' MB' : Math.max(1, Math.ceil(bytes / 1024)) + ' KB';
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function panes(data, {prefix, path, folderId, ownerName, fileLabel, ask}) {
|
|
30
|
-
const folders = new Map(data.folders.map((folder)=>[folder.id,folder]));
|
|
31
|
-
const children = new Map();
|
|
32
|
-
for (const folder of data.folders) {
|
|
33
|
-
if (!children.has(folder.parentId)) children.set(folder.parentId,[]);
|
|
34
|
-
children.get(folder.parentId).push(folder);
|
|
35
|
-
}
|
|
36
|
-
const trail = [];
|
|
37
|
-
for (let f=folders.get(folderId); f; f=folders.get(f.parentId)) trail.unshift(f);
|
|
38
|
-
const expanded = new Set(trail.map(({id})=>id));
|
|
39
|
-
const renderTree = (id) => (children.get(id) || []).map((folder)=>{
|
|
40
|
-
const fixed = folder.id === data.defaultFolderId;
|
|
41
|
-
const row = directoryRow(folder, folder.id === folderId, prefix+'PickFolder',
|
|
42
|
-
fixed ? '' : prefix+'FolderForm', fixed ? '' : prefix+'DeleteFolder',
|
|
43
|
-
{edit:'重命名目录',remove:'删除目录'});
|
|
44
|
-
const nested = expanded.has(folder.id) ? renderTree(folder.id) : '';
|
|
45
|
-
return '<li>'+row+(nested ? '<ul class="sqm-folder-children">'+nested+'</ul>' : '')+'</li>';
|
|
46
|
-
}).join('');
|
|
47
|
-
const documents = data.documents.filter((doc)=>doc.folderId === folderId);
|
|
48
|
-
const indexedCount = documents.filter((doc)=>doc.indexStatus !== 'metadata' || doc.description).length;
|
|
49
|
-
const fileList = documents.map((doc)=>{
|
|
50
|
-
const status = doc.indexStatus === 'indexed' ? '正文可检索' : doc.indexStatus === 'partial' ? '前段正文可检索'
|
|
51
|
-
: doc.description ? '文件名与说明可检索' : '仅文件名可检索';
|
|
52
|
-
return `<li class="sqm-file-row"><div><b>${esc(doc.name)}</b><small>${esc(global.UI.day(doc.createdAt))} · ${fileSize(doc.size)} · ${status}</small></div>
|
|
53
|
-
<div class="sqm-lib-actions"><a class="btn" href="${esc(path+'/files/'+encodeURIComponent(doc.id))}" download>下载</a>
|
|
54
|
-
${button(doc.description?'编辑说明':'添加说明',prefix+'DescribeFile',{id:doc.id})}
|
|
55
|
-
${button('删除',prefix+'DeleteFile',{id:doc.id})}</div></li>`;
|
|
56
|
-
}).join('') || '<li class="sqm-empty">当前目录还没有文件,可以直接上传。</li>';
|
|
57
|
-
const breadcrumb = [{id:'',name:'全部目录'},...trail].map(({id,name})=>
|
|
58
|
-
button(name,prefix+'PickFolder',{id},'sqm-folder-link')).join('<span aria-hidden="true">/</span>');
|
|
59
|
-
return `<section class="sqm-lib-pane" aria-label="资料目录">
|
|
60
|
-
<header><b>资料目录</b>${button(folderId?'新建子目录':'新建目录',prefix+'FolderForm')}</header>
|
|
61
|
-
<nav class="sqm-breadcrumb" aria-label="目录路径">${breadcrumb}</nav>
|
|
62
|
-
<ul class="sqm-lib-list" id="${prefix}Folders">${renderTree('') || '<li class="sqm-empty">没有子目录</li>'}</ul>
|
|
63
|
-
</section>
|
|
64
|
-
<section class="sqm-lib-pane" aria-label="${esc(fileLabel)}">
|
|
65
|
-
<header><div><b>${esc(ownerName)}</b><small>${esc(trail.at(-1)?.name || '根目录')} · ${documents.length} 份文件 · ${indexedCount} 份有可检索内容</small></div>
|
|
66
|
-
<div class="sqm-lib-actions">${ask}${button('上传文件',prefix+'ChooseFiles',{},'btn btn-primary')}</div></header>
|
|
67
|
-
<input type="file" id="${prefix}FileInput" multiple hidden>
|
|
68
|
-
<p id="${prefix}UploadStatus" class="sqm-upload-status" role="status"></p>
|
|
69
|
-
<ul class="sqm-lib-list">${fileList}</ul>
|
|
70
|
-
</section>`;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function upload(picker, path, folder, status) {
|
|
74
|
-
const files = Array.from(picker.files || []);
|
|
75
|
-
if (!files.length) return null;
|
|
76
|
-
let saved = 0, duplicates = 0, metadataOnly = 0;
|
|
77
|
-
const errors = [];
|
|
78
|
-
for (const [index,file] of files.entries()) {
|
|
79
|
-
if (status) status.textContent = '上传 '+(index+1)+' / '+files.length+' · '+file.name;
|
|
80
|
-
try {
|
|
81
|
-
if (file.size > 48 * 1024 ** 2) throw Error('超过 48 MB');
|
|
82
|
-
const query = new URLSearchParams({folder,name:file.name});
|
|
83
|
-
const response = await fetch(path+'/files?'+query,{method:'POST',headers:{'Content-Type':'application/octet-stream'},body:file});
|
|
84
|
-
const result = await response.json();
|
|
85
|
-
if (!response.ok || !result.ok) throw Error(result.error || '上传失败');
|
|
86
|
-
if (result.duplicate) duplicates++;
|
|
87
|
-
else {
|
|
88
|
-
saved++;
|
|
89
|
-
if (result.document.indexStatus === 'metadata') metadataOnly++;
|
|
90
|
-
}
|
|
91
|
-
} catch (error) { errors.push(file.name+'('+error.message+')'); }
|
|
92
|
-
}
|
|
93
|
-
picker.value = '';
|
|
94
|
-
return {text:'已上传 '+saved+' 份'+(duplicates?',已跳过 '+duplicates+' 份重复文件':'')+
|
|
95
|
-
(metadataOnly?','+metadataOnly+' 份仅检索文件名,可补充说明':'')+(errors.length?'。失败:'+errors.join(';'):'。'),failed:!!errors.length};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
global.MaterialLibrary = {directoryRow, selectedFolder, panes, upload};
|
|
99
|
-
})(window);
|
|
100
|
-
|
|
1
|
+
|
|
2
|
+
/* Directory and file panes shared by supplier and R&D project libraries. */
|
|
3
|
+
(function (global) {
|
|
4
|
+
'use strict';
|
|
5
|
+
const {esc} = global.UI;
|
|
6
|
+
function button(label, action, args = {}, cls = 'btn', attrs = '') {
|
|
7
|
+
return '<button type="button" class="'+cls+'" data-act="'+action+'" data-args="'+esc(JSON.stringify(args))+'"'+attrs+'>'+esc(label)+'</button>';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function directoryRow(item, selected, pick, edit, remove, label) {
|
|
11
|
+
const args = {id:item.id};
|
|
12
|
+
const row = button(item.name,pick,args,'sqm-lib-item',
|
|
13
|
+
' title="'+esc(item.name)+'" aria-pressed="'+selected+'"');
|
|
14
|
+
const controls = selected && (edit || remove) ? '<div class="sqm-directory-actions">'+
|
|
15
|
+
(edit ? button('✎',edit,args,'sqm-directory-action',' aria-label="'+esc(label.edit)+'" title="'+esc(label.edit)+'"') : '')+
|
|
16
|
+
(remove ? button('×',remove,args,'sqm-directory-action sqm-directory-delete',' aria-label="'+esc(label.remove)+'" title="'+esc(label.remove)+'"') : '')+
|
|
17
|
+
'</div>' : '';
|
|
18
|
+
return '<div class="sqm-directory-row'+(selected?' on':'')+'">'+row+controls+'</div>';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function selectedFolder(data, folderId) {
|
|
22
|
+
return folderId === '' || data.folders.some(({id})=>id === folderId) ? folderId : data.defaultFolderId || '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function fileSize(bytes) {
|
|
26
|
+
return bytes >= 1024 ** 2 ? (bytes / 1024 ** 2).toFixed(1) + ' MB' : Math.max(1, Math.ceil(bytes / 1024)) + ' KB';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function panes(data, {prefix, path, folderId, ownerName, fileLabel, ask}) {
|
|
30
|
+
const folders = new Map(data.folders.map((folder)=>[folder.id,folder]));
|
|
31
|
+
const children = new Map();
|
|
32
|
+
for (const folder of data.folders) {
|
|
33
|
+
if (!children.has(folder.parentId)) children.set(folder.parentId,[]);
|
|
34
|
+
children.get(folder.parentId).push(folder);
|
|
35
|
+
}
|
|
36
|
+
const trail = [];
|
|
37
|
+
for (let f=folders.get(folderId); f; f=folders.get(f.parentId)) trail.unshift(f);
|
|
38
|
+
const expanded = new Set(trail.map(({id})=>id));
|
|
39
|
+
const renderTree = (id) => (children.get(id) || []).map((folder)=>{
|
|
40
|
+
const fixed = folder.id === data.defaultFolderId;
|
|
41
|
+
const row = directoryRow(folder, folder.id === folderId, prefix+'PickFolder',
|
|
42
|
+
fixed ? '' : prefix+'FolderForm', fixed ? '' : prefix+'DeleteFolder',
|
|
43
|
+
{edit:'重命名目录',remove:'删除目录'});
|
|
44
|
+
const nested = expanded.has(folder.id) ? renderTree(folder.id) : '';
|
|
45
|
+
return '<li>'+row+(nested ? '<ul class="sqm-folder-children">'+nested+'</ul>' : '')+'</li>';
|
|
46
|
+
}).join('');
|
|
47
|
+
const documents = data.documents.filter((doc)=>doc.folderId === folderId);
|
|
48
|
+
const indexedCount = documents.filter((doc)=>doc.indexStatus !== 'metadata' || doc.description).length;
|
|
49
|
+
const fileList = documents.map((doc)=>{
|
|
50
|
+
const status = doc.indexStatus === 'indexed' ? '正文可检索' : doc.indexStatus === 'partial' ? '前段正文可检索'
|
|
51
|
+
: doc.description ? '文件名与说明可检索' : '仅文件名可检索';
|
|
52
|
+
return `<li class="sqm-file-row"><div><b>${esc(doc.name)}</b><small>${esc(global.UI.day(doc.createdAt))} · ${fileSize(doc.size)} · ${status}</small></div>
|
|
53
|
+
<div class="sqm-lib-actions"><a class="btn" href="${esc(path+'/files/'+encodeURIComponent(doc.id))}" download>下载</a>
|
|
54
|
+
${button(doc.description?'编辑说明':'添加说明',prefix+'DescribeFile',{id:doc.id})}
|
|
55
|
+
${button('删除',prefix+'DeleteFile',{id:doc.id})}</div></li>`;
|
|
56
|
+
}).join('') || '<li class="sqm-empty">当前目录还没有文件,可以直接上传。</li>';
|
|
57
|
+
const breadcrumb = [{id:'',name:'全部目录'},...trail].map(({id,name})=>
|
|
58
|
+
button(name,prefix+'PickFolder',{id},'sqm-folder-link')).join('<span aria-hidden="true">/</span>');
|
|
59
|
+
return `<section class="sqm-lib-pane" aria-label="资料目录">
|
|
60
|
+
<header><b>资料目录</b>${button(folderId?'新建子目录':'新建目录',prefix+'FolderForm')}</header>
|
|
61
|
+
<nav class="sqm-breadcrumb" aria-label="目录路径">${breadcrumb}</nav>
|
|
62
|
+
<ul class="sqm-lib-list" id="${prefix}Folders">${renderTree('') || '<li class="sqm-empty">没有子目录</li>'}</ul>
|
|
63
|
+
</section>
|
|
64
|
+
<section class="sqm-lib-pane" aria-label="${esc(fileLabel)}">
|
|
65
|
+
<header><div><b>${esc(ownerName)}</b><small>${esc(trail.at(-1)?.name || '根目录')} · ${documents.length} 份文件 · ${indexedCount} 份有可检索内容</small></div>
|
|
66
|
+
<div class="sqm-lib-actions">${ask}${button('上传文件',prefix+'ChooseFiles',{},'btn btn-primary')}</div></header>
|
|
67
|
+
<input type="file" id="${prefix}FileInput" multiple hidden>
|
|
68
|
+
<p id="${prefix}UploadStatus" class="sqm-upload-status" role="status"></p>
|
|
69
|
+
<ul class="sqm-lib-list">${fileList}</ul>
|
|
70
|
+
</section>`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function upload(picker, path, folder, status) {
|
|
74
|
+
const files = Array.from(picker.files || []);
|
|
75
|
+
if (!files.length) return null;
|
|
76
|
+
let saved = 0, duplicates = 0, metadataOnly = 0;
|
|
77
|
+
const errors = [];
|
|
78
|
+
for (const [index,file] of files.entries()) {
|
|
79
|
+
if (status) status.textContent = '上传 '+(index+1)+' / '+files.length+' · '+file.name;
|
|
80
|
+
try {
|
|
81
|
+
if (file.size > 48 * 1024 ** 2) throw Error('超过 48 MB');
|
|
82
|
+
const query = new URLSearchParams({folder,name:file.name});
|
|
83
|
+
const response = await fetch(path+'/files?'+query,{method:'POST',headers:{'Content-Type':'application/octet-stream'},body:file});
|
|
84
|
+
const result = await response.json();
|
|
85
|
+
if (!response.ok || !result.ok) throw Error(result.error || '上传失败');
|
|
86
|
+
if (result.duplicate) duplicates++;
|
|
87
|
+
else {
|
|
88
|
+
saved++;
|
|
89
|
+
if (result.document.indexStatus === 'metadata') metadataOnly++;
|
|
90
|
+
}
|
|
91
|
+
} catch (error) { errors.push(file.name+'('+error.message+')'); }
|
|
92
|
+
}
|
|
93
|
+
picker.value = '';
|
|
94
|
+
return {text:'已上传 '+saved+' 份'+(duplicates?',已跳过 '+duplicates+' 份重复文件':'')+
|
|
95
|
+
(metadataOnly?','+metadataOnly+' 份仅检索文件名,可补充说明':'')+(errors.length?'。失败:'+errors.join(';'):'。'),failed:!!errors.length};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
global.MaterialLibrary = {directoryRow, selectedFolder, panes, upload};
|
|
99
|
+
})(window);
|
|
100
|
+
|