zen-gitsync 2.16.7 → 2.16.8
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/package.json +1 -1
- package/src/cli/ai/modelSetup.js +594 -594
- package/src/cli/ai/modelSetup.test.js +496 -496
- package/src/cli/ai/platformGuard.js +200 -200
- package/src/cli/ai/platformGuard.test.js +253 -253
- package/src/gitCommit.js +484 -484
- package/src/ui/public/assets/AgentView-DSmUynN-.js +4 -0
- package/src/ui/public/assets/AppVersionBadge-CcFagCnq.js +2 -0
- package/src/ui/public/assets/BranchSelector-DcYVBQJh.js +1 -0
- package/src/ui/public/assets/CommitForm-CkTI8Xyz.js +6 -0
- package/src/ui/public/assets/CommonDialog-BFyhw0Qj.js +1 -0
- package/src/ui/public/assets/EditorView-DuHkBSc3.js +0 -0
- package/src/ui/public/assets/{FlowExecutionViewer-DjtFCci0.js → FlowExecutionViewer-C063Qjmk.js} +1 -1
- package/src/ui/public/assets/FlowOrchestrationWorkspace-BeJRId5G.js +21 -0
- package/src/ui/public/assets/LogList-GSeER4cv.js +15 -0
- package/src/ui/public/assets/MindmapView-p7szLCtQ.js +2 -0
- package/src/ui/public/assets/MonitorView-Be6KEUCe.js +1 -0
- package/src/ui/public/assets/ProjectStartupButton-F1k5Km2p.js +1 -0
- package/src/ui/public/assets/{RemoteRepoCard-Yxfc0pxB.js → RemoteRepoCard-CSMFlWDl.js} +2 -2
- package/src/ui/public/assets/SourceMapView-Dg5NP_gR.js +3 -0
- package/src/ui/public/assets/{SvgIcon-32uxY152.js → SvgIcon-CYZGHQAE.js} +1 -1
- package/src/ui/public/assets/UserInputNode-3OAXiGVh.js +1 -0
- package/src/ui/public/assets/WorkbenchView-8xyxdWAg.js +28 -0
- package/src/ui/public/assets/{_plugin-vue_export-helper-Dvk21zJS.js → _plugin-vue_export-helper-DgLDQTWK.js} +1 -1
- package/src/ui/public/assets/{configStore-Bmoxm5fk.js → configStore-D5t05s4G.js} +1 -1
- package/src/ui/public/assets/element-plus-CyGhXd7X.js +25 -0
- package/src/ui/public/assets/element-plus-D3Dn7nX0.css +1 -0
- package/src/ui/public/assets/{flow-mindmap-E8TqjpXN.js → flow-mindmap-CUvJaYn2.js} +1 -1
- package/src/ui/public/assets/index-DDGvh1mp.js +68 -0
- package/src/ui/public/assets/{socket-io-Bcae2P7B.js → socket-io-BSgn8Jx2.js} +1 -1
- package/src/ui/public/assets/vendor-DVvq7qtS.css +1 -0
- package/src/ui/public/assets/{vendor-D79HTB0H.js → vendor-jF--I87-.js} +85 -41
- package/src/ui/public/assets/{vue-flow-BYcesAAH.js → vue-flow-CWmfpfPN.js} +1 -1
- package/src/ui/public/favicon.svg +75 -75
- package/src/ui/public/index.html +28 -28
- package/src/ui/public/logo.svg +74 -74
- package/src/ui/server/middleware/requestLogger.js +53 -53
- package/src/ui/server/routes/branchStatus.js +105 -105
- package/src/ui/server/routes/fileOpen.js +3 -2
- package/src/ui/server/routes/fs.js +791 -791
- package/src/ui/server/routes/git/diff.js +426 -426
- package/src/ui/server/routes/git/diffUtils.js +149 -149
- package/src/ui/server/routes/git/stash.js +550 -550
- package/src/ui/server/routes/git/tags.js +176 -176
- package/src/ui/server/routes/git.js +181 -181
- package/src/ui/server/routes/gitOps.js +1255 -1255
- package/src/ui/server/routes/process.js +86 -86
- package/src/ui/server/routes/status.js +67 -67
- package/src/ui/server/routes/workbench/pdfText.js +145 -145
- package/src/ui/server/utils/createSavePortToFile.js +48 -48
- package/src/ui/server/utils/instanceRegistry.js +282 -282
- package/src/ui/server/utils/startServerOnAvailablePort.js +116 -116
- package/src/utils/index.js +1173 -1173
- package/src/ui/public/assets/AgentView-DEUMkOst.js +0 -4
- package/src/ui/public/assets/AppVersionBadge-oo55wrtg.js +0 -2
- package/src/ui/public/assets/BranchSelector--__REaeG.js +0 -1
- package/src/ui/public/assets/CommitForm-CpUP5zax.js +0 -6
- package/src/ui/public/assets/CommonDialog-DbPF7rKY.js +0 -1
- package/src/ui/public/assets/EditorView-LZ_WHmfb.js +0 -0
- package/src/ui/public/assets/FlowOrchestrationWorkspace-CKMLLhKM.js +0 -21
- package/src/ui/public/assets/LogList--c3ACWbL.js +0 -15
- package/src/ui/public/assets/MindmapView-B_VWP6CS.js +0 -2
- package/src/ui/public/assets/MonitorView-C9BEPazj.js +0 -1
- package/src/ui/public/assets/ProjectStartupButton-qLmpoh5m.js +0 -1
- package/src/ui/public/assets/SourceMapView-BXqR3S2Y.js +0 -3
- package/src/ui/public/assets/UserInputNode-B0xDrmk2.js +0 -1
- package/src/ui/public/assets/WorkbenchView-COtdz_io.js +0 -28
- package/src/ui/public/assets/ai-chat-ui-BsH9JBEz.js +0 -45
- package/src/ui/public/assets/ai-chat-ui-BwlJovCq.css +0 -1
- package/src/ui/public/assets/element-plus-CQrjg9us.css +0 -1
- package/src/ui/public/assets/element-plus-C_2iiXUS.js +0 -32
- package/src/ui/public/assets/index-Zn4T8oXe.js +0 -68
- package/src/ui/public/assets/vendor-CdOLCu7-.css +0 -1
- package/src/ui/server/.claude/codediff.txt +0 -8
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
// Copyright 2026 xz333221
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
//
|
|
15
|
-
import { asyncRoute, HttpError } from '../utils/asyncRoute.js'
|
|
16
|
-
|
|
17
|
-
import logger from '../utils/logger.js'
|
|
18
|
-
|
|
19
|
-
export function registerProcessRoutes({
|
|
20
|
-
app,
|
|
21
|
-
runningProcesses,
|
|
22
|
-
exec
|
|
23
|
-
}) {
|
|
24
|
-
// 停止正在运行的进程
|
|
25
|
-
app.post('/api/kill-process', asyncRoute(async (req, res) => {
|
|
26
|
-
try {
|
|
27
|
-
const { processId } = req.body || {};
|
|
28
|
-
if (!processId || typeof processId !== 'number') {
|
|
29
|
-
throw new HttpError(400, 'processId 必须是数字');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const processInfo = runningProcesses.get(processId);
|
|
33
|
-
if (!processInfo) {
|
|
34
|
-
return res.status(404).json({
|
|
35
|
-
success: false,
|
|
36
|
-
error: `进程 #${processId} 不存在或已结束`
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
logger.info(`[进程管理] 尝试停止进程 #${processId}: ${processInfo.command}`);
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
// 在 Windows 上需要使用 taskkill 来杀死整个进程树
|
|
44
|
-
if (process.platform === 'win32') {
|
|
45
|
-
// 使用已导入的 exec
|
|
46
|
-
// /F 强制终止, /T 终止进程树
|
|
47
|
-
exec(`taskkill /pid ${processInfo.childProcess.pid} /T /F`, { windowsHide: true }, (error) => {
|
|
48
|
-
if (error) {
|
|
49
|
-
logger.error(`[进程管理] taskkill 失败:`, error);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
} else {
|
|
53
|
-
// Unix/Linux/Mac 使用 SIGTERM
|
|
54
|
-
processInfo.childProcess.kill('SIGTERM');
|
|
55
|
-
|
|
56
|
-
// 如果 2 秒后还没结束,使用 SIGKILL 强制终止
|
|
57
|
-
setTimeout(() => {
|
|
58
|
-
if (runningProcesses.has(processId)) {
|
|
59
|
-
logger.info(`[进程管理] 进程 #${processId} 未响应 SIGTERM,使用 SIGKILL 强制终止`);
|
|
60
|
-
processInfo.childProcess.kill('SIGKILL');
|
|
61
|
-
}
|
|
62
|
-
}, 2000);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
res.json({
|
|
66
|
-
success: true,
|
|
67
|
-
message: `已发送停止信号到进程 #${processId}`,
|
|
68
|
-
processId,
|
|
69
|
-
command: processInfo.command
|
|
70
|
-
});
|
|
71
|
-
} catch (killError) {
|
|
72
|
-
logger.error(`[进程管理] 停止进程失败:`, killError);
|
|
73
|
-
res.status(500).json({
|
|
74
|
-
success: false,
|
|
75
|
-
error: `停止进程失败: ${killError.message}`
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
} catch (error) {
|
|
79
|
-
logger.error('停止进程接口失败:', error);
|
|
80
|
-
res.status(500).json({
|
|
81
|
-
success: false,
|
|
82
|
-
error: `停止进程失败: ${error.message}`
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
1
|
+
// Copyright 2026 xz333221
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
import { asyncRoute, HttpError } from '../utils/asyncRoute.js'
|
|
16
|
+
|
|
17
|
+
import logger from '../utils/logger.js'
|
|
18
|
+
|
|
19
|
+
export function registerProcessRoutes({
|
|
20
|
+
app,
|
|
21
|
+
runningProcesses,
|
|
22
|
+
exec
|
|
23
|
+
}) {
|
|
24
|
+
// 停止正在运行的进程
|
|
25
|
+
app.post('/api/kill-process', asyncRoute(async (req, res) => {
|
|
26
|
+
try {
|
|
27
|
+
const { processId } = req.body || {};
|
|
28
|
+
if (!processId || typeof processId !== 'number') {
|
|
29
|
+
throw new HttpError(400, 'processId 必须是数字');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const processInfo = runningProcesses.get(processId);
|
|
33
|
+
if (!processInfo) {
|
|
34
|
+
return res.status(404).json({
|
|
35
|
+
success: false,
|
|
36
|
+
error: `进程 #${processId} 不存在或已结束`
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
logger.info(`[进程管理] 尝试停止进程 #${processId}: ${processInfo.command}`);
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// 在 Windows 上需要使用 taskkill 来杀死整个进程树
|
|
44
|
+
if (process.platform === 'win32') {
|
|
45
|
+
// 使用已导入的 exec
|
|
46
|
+
// /F 强制终止, /T 终止进程树
|
|
47
|
+
exec(`taskkill /pid ${processInfo.childProcess.pid} /T /F`, { windowsHide: true }, (error) => {
|
|
48
|
+
if (error) {
|
|
49
|
+
logger.error(`[进程管理] taskkill 失败:`, error);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
// Unix/Linux/Mac 使用 SIGTERM
|
|
54
|
+
processInfo.childProcess.kill('SIGTERM');
|
|
55
|
+
|
|
56
|
+
// 如果 2 秒后还没结束,使用 SIGKILL 强制终止
|
|
57
|
+
setTimeout(() => {
|
|
58
|
+
if (runningProcesses.has(processId)) {
|
|
59
|
+
logger.info(`[进程管理] 进程 #${processId} 未响应 SIGTERM,使用 SIGKILL 强制终止`);
|
|
60
|
+
processInfo.childProcess.kill('SIGKILL');
|
|
61
|
+
}
|
|
62
|
+
}, 2000);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
res.json({
|
|
66
|
+
success: true,
|
|
67
|
+
message: `已发送停止信号到进程 #${processId}`,
|
|
68
|
+
processId,
|
|
69
|
+
command: processInfo.command
|
|
70
|
+
});
|
|
71
|
+
} catch (killError) {
|
|
72
|
+
logger.error(`[进程管理] 停止进程失败:`, killError);
|
|
73
|
+
res.status(500).json({
|
|
74
|
+
success: false,
|
|
75
|
+
error: `停止进程失败: ${killError.message}`
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
logger.error('停止进程接口失败:', error);
|
|
80
|
+
res.status(500).json({
|
|
81
|
+
success: false,
|
|
82
|
+
error: `停止进程失败: ${error.message}`
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
// Copyright 2026 xz333221
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
//
|
|
15
|
-
import { promises as fs } from 'fs';
|
|
16
|
-
import { asyncRoute, HttpError } from '../utils/asyncRoute.js';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
|
|
19
|
-
export function registerStatusRoutes({
|
|
20
|
-
app,
|
|
21
|
-
getCommandHistory,
|
|
22
|
-
execGitCommand
|
|
23
|
-
}) {
|
|
24
|
-
// Add new endpoint for command history
|
|
25
|
-
app.get('/api/command-history', asyncRoute(async (req, res) => {
|
|
26
|
-
try {
|
|
27
|
-
const history = getCommandHistory();
|
|
28
|
-
res.json({ success: true, history });
|
|
29
|
-
} catch (error) {
|
|
30
|
-
res.status(500).json({ success: false, error: error.message });
|
|
31
|
-
}
|
|
32
|
-
}));
|
|
33
|
-
|
|
34
|
-
app.get('/api/status_porcelain', asyncRoute(async (req, res) => {
|
|
35
|
-
try {
|
|
36
|
-
const { stdout } = await execGitCommand(['status', '--porcelain', '--untracked-files=all']);
|
|
37
|
-
// 检测是否处于 MERGING 状态(MERGE_HEAD 存在)
|
|
38
|
-
let isMergeInProgress = false;
|
|
39
|
-
let mergeMessage = '';
|
|
40
|
-
try {
|
|
41
|
-
const { stdout: mergeHead } = await execGitCommand(['rev-parse', '-q', '--verify', 'MERGE_HEAD']);
|
|
42
|
-
isMergeInProgress = mergeHead.trim().length > 0;
|
|
43
|
-
} catch (_) {
|
|
44
|
-
// MERGE_HEAD 不存在时命令会报错,属正常情况
|
|
45
|
-
isMergeInProgress = false;
|
|
46
|
-
}
|
|
47
|
-
if (isMergeInProgress) {
|
|
48
|
-
try {
|
|
49
|
-
const { stdout: gitDir } = await execGitCommand(['rev-parse', '--git-dir']);
|
|
50
|
-
const mergeMsgPath = path.resolve(gitDir.trim(), 'MERGE_MSG');
|
|
51
|
-
const raw = await fs.readFile(mergeMsgPath, 'utf-8');
|
|
52
|
-
// 过滤掉以 # 开头的注释行,拼接所有非空非注释行(保留多段信息如合并分支列表)
|
|
53
|
-
mergeMessage = raw
|
|
54
|
-
.split('\n')
|
|
55
|
-
.filter(line => line.trim() && !line.startsWith('#'))
|
|
56
|
-
.join('\n')
|
|
57
|
-
.trim();
|
|
58
|
-
} catch (_) {
|
|
59
|
-
// MERGE_MSG 不存在/无权限时忽略
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
res.json({ status: stdout, isMergeInProgress, mergeMessage });
|
|
63
|
-
} catch (error) {
|
|
64
|
-
res.status(500).json({ error: error.message });
|
|
65
|
-
}
|
|
66
|
-
}));
|
|
67
|
-
}
|
|
1
|
+
// Copyright 2026 xz333221
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
import { promises as fs } from 'fs';
|
|
16
|
+
import { asyncRoute, HttpError } from '../utils/asyncRoute.js';
|
|
17
|
+
import path from 'path';
|
|
18
|
+
|
|
19
|
+
export function registerStatusRoutes({
|
|
20
|
+
app,
|
|
21
|
+
getCommandHistory,
|
|
22
|
+
execGitCommand
|
|
23
|
+
}) {
|
|
24
|
+
// Add new endpoint for command history
|
|
25
|
+
app.get('/api/command-history', asyncRoute(async (req, res) => {
|
|
26
|
+
try {
|
|
27
|
+
const history = getCommandHistory();
|
|
28
|
+
res.json({ success: true, history });
|
|
29
|
+
} catch (error) {
|
|
30
|
+
res.status(500).json({ success: false, error: error.message });
|
|
31
|
+
}
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
app.get('/api/status_porcelain', asyncRoute(async (req, res) => {
|
|
35
|
+
try {
|
|
36
|
+
const { stdout } = await execGitCommand(['status', '--porcelain', '--untracked-files=all']);
|
|
37
|
+
// 检测是否处于 MERGING 状态(MERGE_HEAD 存在)
|
|
38
|
+
let isMergeInProgress = false;
|
|
39
|
+
let mergeMessage = '';
|
|
40
|
+
try {
|
|
41
|
+
const { stdout: mergeHead } = await execGitCommand(['rev-parse', '-q', '--verify', 'MERGE_HEAD']);
|
|
42
|
+
isMergeInProgress = mergeHead.trim().length > 0;
|
|
43
|
+
} catch (_) {
|
|
44
|
+
// MERGE_HEAD 不存在时命令会报错,属正常情况
|
|
45
|
+
isMergeInProgress = false;
|
|
46
|
+
}
|
|
47
|
+
if (isMergeInProgress) {
|
|
48
|
+
try {
|
|
49
|
+
const { stdout: gitDir } = await execGitCommand(['rev-parse', '--git-dir']);
|
|
50
|
+
const mergeMsgPath = path.resolve(gitDir.trim(), 'MERGE_MSG');
|
|
51
|
+
const raw = await fs.readFile(mergeMsgPath, 'utf-8');
|
|
52
|
+
// 过滤掉以 # 开头的注释行,拼接所有非空非注释行(保留多段信息如合并分支列表)
|
|
53
|
+
mergeMessage = raw
|
|
54
|
+
.split('\n')
|
|
55
|
+
.filter(line => line.trim() && !line.startsWith('#'))
|
|
56
|
+
.join('\n')
|
|
57
|
+
.trim();
|
|
58
|
+
} catch (_) {
|
|
59
|
+
// MERGE_MSG 不存在/无权限时忽略
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
res.json({ status: stdout, isMergeInProgress, mergeMessage });
|
|
63
|
+
} catch (error) {
|
|
64
|
+
res.status(500).json({ error: error.message });
|
|
65
|
+
}
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
@@ -1,145 +1,145 @@
|
|
|
1
|
-
// Copyright 2026 xz333221
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
//
|
|
15
|
-
// PDF 文本预提取工具。
|
|
16
|
-
//
|
|
17
|
-
// Claude CLI (claude.exe v2.1.x) 在解析 PDF 时内部 pdfParse 函数不可用,
|
|
18
|
-
// 报错 "pdfParse is not a function"。本模块在服务端用 pdf-parse 库
|
|
19
|
-
// 预提取 PDF 文本,将提取出的内容直接拼入 prompt,绕过 Claude CLI
|
|
20
|
-
// 的 PDF 解析 bug。
|
|
21
|
-
//
|
|
22
|
-
// 核心 API:
|
|
23
|
-
// - extractPdfText(filePath) 读取 PDF 文件并返回纯文本,失败返回 null
|
|
24
|
-
// - isPdfAttachment(att) 判断附件是否为 PDF
|
|
25
|
-
// - buildAttachmentBlock(attachments) 构建附件文本块,PDF 自动预提取
|
|
26
|
-
|
|
27
|
-
import fs from 'fs';
|
|
28
|
-
import { isImageExt } from './attachmentUtils.js';
|
|
29
|
-
|
|
30
|
-
// 最大提取字符数(避免超大 PDF 把 prompt 撑爆)
|
|
31
|
-
const MAX_PDF_TEXT_CHARS = 80_000;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 判断附件是否为 PDF
|
|
35
|
-
* @param {{ext?: string, mimeType?: string}} att
|
|
36
|
-
* @returns {boolean}
|
|
37
|
-
*/
|
|
38
|
-
export function isPdfAttachment(att) {
|
|
39
|
-
if (!att) return false;
|
|
40
|
-
const ext = String(att.ext || '').toLowerCase();
|
|
41
|
-
const mime = String(att.mimeType || '').toLowerCase();
|
|
42
|
-
return ext === 'pdf' || mime === 'application/pdf';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 从 PDF 文件中提取纯文本。
|
|
47
|
-
* 使用 pdf-parse 库(node 端),失败时返回 null。
|
|
48
|
-
*
|
|
49
|
-
* @param {string} filePath PDF 文件的绝对路径
|
|
50
|
-
* @returns {Promise<string|null>} 提取的文本(可能截断),失败返回 null
|
|
51
|
-
*/
|
|
52
|
-
export async function extractPdfText(filePath) {
|
|
53
|
-
try {
|
|
54
|
-
const buffer = fs.readFileSync(filePath);
|
|
55
|
-
// 动态 import 避免在非 PDF 场景加载 pdfjs
|
|
56
|
-
const { PDFParse } = await import('pdf-parse');
|
|
57
|
-
const parser = new PDFParse({ data: new Uint8Array(buffer) });
|
|
58
|
-
const result = await parser.getText();
|
|
59
|
-
await parser.destroy();
|
|
60
|
-
const text = result?.text || '';
|
|
61
|
-
if (!text.trim()) return null;
|
|
62
|
-
// 截断超长文本
|
|
63
|
-
if (text.length > MAX_PDF_TEXT_CHARS) {
|
|
64
|
-
return text.slice(0, MAX_PDF_TEXT_CHARS) + '\n\n[... 文档过长,已截断 ...]';
|
|
65
|
-
}
|
|
66
|
-
return text;
|
|
67
|
-
} catch (err) {
|
|
68
|
-
// 常见失败:加密 PDF / 扫描版 PDF(无可提取文本) / 损坏文件
|
|
69
|
-
console.error('[pdfText] PDF 解析失败:', err?.message || String(err));
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 构建附件信息块。
|
|
76
|
-
* - 图片:列路径(Claude CLI 可直接读取)
|
|
77
|
-
* - PDF:预提取文本并内联,不列路径(绕过 Claude CLI 的 pdfParse bug)
|
|
78
|
-
* - 其他:列路径
|
|
79
|
-
*
|
|
80
|
-
* @param {Array<{absolutePath?: string, ext?: string, mimeType?: string}>} attachments
|
|
81
|
-
* @param {object} [opts]
|
|
82
|
-
* @param {boolean} [opts.withImageData=false] 是否读取图片为 data URL(预览/拆分场景需要)
|
|
83
|
-
* @returns {Promise<{ block: string, imageDataUrls: string[] }>}
|
|
84
|
-
*/
|
|
85
|
-
export async function buildAttachmentBlock(attachments, opts) {
|
|
86
|
-
const withImages = opts?.withImageData ?? false;
|
|
87
|
-
const imageDataUrls = [];
|
|
88
|
-
if (!attachments || attachments.length === 0) {
|
|
89
|
-
return { block: '', imageDataUrls };
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const lines = [];
|
|
93
|
-
let pdfCount = 0;
|
|
94
|
-
let pdfFailedCount = 0;
|
|
95
|
-
|
|
96
|
-
for (let i = 0; i < attachments.length; i++) {
|
|
97
|
-
const a = attachments[i];
|
|
98
|
-
if (!a || !a.absolutePath) continue;
|
|
99
|
-
|
|
100
|
-
if (isPdfAttachment(a)) {
|
|
101
|
-
// PDF:预提取文本,不把路径交给 Claude CLI
|
|
102
|
-
const text = await extractPdfText(a.absolutePath);
|
|
103
|
-
if (text) {
|
|
104
|
-
pdfCount++;
|
|
105
|
-
lines.push(` ${i + 1}. [PDF 全文提取] ${a.absolutePath}`);
|
|
106
|
-
lines.push(` <pdf_content>\n${text}\n </pdf_content>`);
|
|
107
|
-
} else {
|
|
108
|
-
pdfFailedCount++;
|
|
109
|
-
lines.push(` ${i + 1}. [PDF 解析失败] ${a.absolutePath}(可能是扫描版/加密/损坏,请尝试用其他工具转换为文本后重新上传)`);
|
|
110
|
-
}
|
|
111
|
-
} else if (isImageExt(a.ext)) {
|
|
112
|
-
// 图片:列路径,可选读 data URL
|
|
113
|
-
lines.push(` ${i + 1}. [${a.mimeType || 'image'}] ${a.absolutePath}`);
|
|
114
|
-
if (withImages) {
|
|
115
|
-
try {
|
|
116
|
-
const buf = fs.readFileSync(a.absolutePath);
|
|
117
|
-
const mime = a.mimeType || 'image/png';
|
|
118
|
-
imageDataUrls.push(`data:${mime};base64,${buf.toString('base64')}`);
|
|
119
|
-
} catch { /* 文件丢失就跳过 */ }
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
// 其他文件:列路径
|
|
123
|
-
lines.push(` ${i + 1}. [${a.mimeType || 'application/octet-stream'}] ${a.absolutePath}`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (lines.length === 0) {
|
|
128
|
-
return { block: '', imageDataUrls };
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const parts = [];
|
|
132
|
-
if (pdfCount > 0) {
|
|
133
|
-
parts.push(`其中 ${pdfCount} 个 PDF 已预提取全文`);
|
|
134
|
-
}
|
|
135
|
-
if (pdfFailedCount > 0) {
|
|
136
|
-
parts.push(`${pdfFailedCount} 个 PDF 解析失败`);
|
|
137
|
-
}
|
|
138
|
-
const imgNote = withImages && imageDataUrls.length > 0
|
|
139
|
-
? `(其中 ${imageDataUrls.length} 张图片已随消息一并发送,请直接基于图片内容分析)`
|
|
140
|
-
: '';
|
|
141
|
-
const note = parts.length > 0 ? `(${parts.join(',')})` : imgNote;
|
|
142
|
-
|
|
143
|
-
const block = `\n\n## 任务附件${note}\n${lines.join('\n')}`;
|
|
144
|
-
return { block, imageDataUrls };
|
|
145
|
-
}
|
|
1
|
+
// Copyright 2026 xz333221
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// PDF 文本预提取工具。
|
|
16
|
+
//
|
|
17
|
+
// Claude CLI (claude.exe v2.1.x) 在解析 PDF 时内部 pdfParse 函数不可用,
|
|
18
|
+
// 报错 "pdfParse is not a function"。本模块在服务端用 pdf-parse 库
|
|
19
|
+
// 预提取 PDF 文本,将提取出的内容直接拼入 prompt,绕过 Claude CLI
|
|
20
|
+
// 的 PDF 解析 bug。
|
|
21
|
+
//
|
|
22
|
+
// 核心 API:
|
|
23
|
+
// - extractPdfText(filePath) 读取 PDF 文件并返回纯文本,失败返回 null
|
|
24
|
+
// - isPdfAttachment(att) 判断附件是否为 PDF
|
|
25
|
+
// - buildAttachmentBlock(attachments) 构建附件文本块,PDF 自动预提取
|
|
26
|
+
|
|
27
|
+
import fs from 'fs';
|
|
28
|
+
import { isImageExt } from './attachmentUtils.js';
|
|
29
|
+
|
|
30
|
+
// 最大提取字符数(避免超大 PDF 把 prompt 撑爆)
|
|
31
|
+
const MAX_PDF_TEXT_CHARS = 80_000;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 判断附件是否为 PDF
|
|
35
|
+
* @param {{ext?: string, mimeType?: string}} att
|
|
36
|
+
* @returns {boolean}
|
|
37
|
+
*/
|
|
38
|
+
export function isPdfAttachment(att) {
|
|
39
|
+
if (!att) return false;
|
|
40
|
+
const ext = String(att.ext || '').toLowerCase();
|
|
41
|
+
const mime = String(att.mimeType || '').toLowerCase();
|
|
42
|
+
return ext === 'pdf' || mime === 'application/pdf';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 从 PDF 文件中提取纯文本。
|
|
47
|
+
* 使用 pdf-parse 库(node 端),失败时返回 null。
|
|
48
|
+
*
|
|
49
|
+
* @param {string} filePath PDF 文件的绝对路径
|
|
50
|
+
* @returns {Promise<string|null>} 提取的文本(可能截断),失败返回 null
|
|
51
|
+
*/
|
|
52
|
+
export async function extractPdfText(filePath) {
|
|
53
|
+
try {
|
|
54
|
+
const buffer = fs.readFileSync(filePath);
|
|
55
|
+
// 动态 import 避免在非 PDF 场景加载 pdfjs
|
|
56
|
+
const { PDFParse } = await import('pdf-parse');
|
|
57
|
+
const parser = new PDFParse({ data: new Uint8Array(buffer) });
|
|
58
|
+
const result = await parser.getText();
|
|
59
|
+
await parser.destroy();
|
|
60
|
+
const text = result?.text || '';
|
|
61
|
+
if (!text.trim()) return null;
|
|
62
|
+
// 截断超长文本
|
|
63
|
+
if (text.length > MAX_PDF_TEXT_CHARS) {
|
|
64
|
+
return text.slice(0, MAX_PDF_TEXT_CHARS) + '\n\n[... 文档过长,已截断 ...]';
|
|
65
|
+
}
|
|
66
|
+
return text;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
// 常见失败:加密 PDF / 扫描版 PDF(无可提取文本) / 损坏文件
|
|
69
|
+
console.error('[pdfText] PDF 解析失败:', err?.message || String(err));
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 构建附件信息块。
|
|
76
|
+
* - 图片:列路径(Claude CLI 可直接读取)
|
|
77
|
+
* - PDF:预提取文本并内联,不列路径(绕过 Claude CLI 的 pdfParse bug)
|
|
78
|
+
* - 其他:列路径
|
|
79
|
+
*
|
|
80
|
+
* @param {Array<{absolutePath?: string, ext?: string, mimeType?: string}>} attachments
|
|
81
|
+
* @param {object} [opts]
|
|
82
|
+
* @param {boolean} [opts.withImageData=false] 是否读取图片为 data URL(预览/拆分场景需要)
|
|
83
|
+
* @returns {Promise<{ block: string, imageDataUrls: string[] }>}
|
|
84
|
+
*/
|
|
85
|
+
export async function buildAttachmentBlock(attachments, opts) {
|
|
86
|
+
const withImages = opts?.withImageData ?? false;
|
|
87
|
+
const imageDataUrls = [];
|
|
88
|
+
if (!attachments || attachments.length === 0) {
|
|
89
|
+
return { block: '', imageDataUrls };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const lines = [];
|
|
93
|
+
let pdfCount = 0;
|
|
94
|
+
let pdfFailedCount = 0;
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < attachments.length; i++) {
|
|
97
|
+
const a = attachments[i];
|
|
98
|
+
if (!a || !a.absolutePath) continue;
|
|
99
|
+
|
|
100
|
+
if (isPdfAttachment(a)) {
|
|
101
|
+
// PDF:预提取文本,不把路径交给 Claude CLI
|
|
102
|
+
const text = await extractPdfText(a.absolutePath);
|
|
103
|
+
if (text) {
|
|
104
|
+
pdfCount++;
|
|
105
|
+
lines.push(` ${i + 1}. [PDF 全文提取] ${a.absolutePath}`);
|
|
106
|
+
lines.push(` <pdf_content>\n${text}\n </pdf_content>`);
|
|
107
|
+
} else {
|
|
108
|
+
pdfFailedCount++;
|
|
109
|
+
lines.push(` ${i + 1}. [PDF 解析失败] ${a.absolutePath}(可能是扫描版/加密/损坏,请尝试用其他工具转换为文本后重新上传)`);
|
|
110
|
+
}
|
|
111
|
+
} else if (isImageExt(a.ext)) {
|
|
112
|
+
// 图片:列路径,可选读 data URL
|
|
113
|
+
lines.push(` ${i + 1}. [${a.mimeType || 'image'}] ${a.absolutePath}`);
|
|
114
|
+
if (withImages) {
|
|
115
|
+
try {
|
|
116
|
+
const buf = fs.readFileSync(a.absolutePath);
|
|
117
|
+
const mime = a.mimeType || 'image/png';
|
|
118
|
+
imageDataUrls.push(`data:${mime};base64,${buf.toString('base64')}`);
|
|
119
|
+
} catch { /* 文件丢失就跳过 */ }
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
// 其他文件:列路径
|
|
123
|
+
lines.push(` ${i + 1}. [${a.mimeType || 'application/octet-stream'}] ${a.absolutePath}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (lines.length === 0) {
|
|
128
|
+
return { block: '', imageDataUrls };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const parts = [];
|
|
132
|
+
if (pdfCount > 0) {
|
|
133
|
+
parts.push(`其中 ${pdfCount} 个 PDF 已预提取全文`);
|
|
134
|
+
}
|
|
135
|
+
if (pdfFailedCount > 0) {
|
|
136
|
+
parts.push(`${pdfFailedCount} 个 PDF 解析失败`);
|
|
137
|
+
}
|
|
138
|
+
const imgNote = withImages && imageDataUrls.length > 0
|
|
139
|
+
? `(其中 ${imageDataUrls.length} 张图片已随消息一并发送,请直接基于图片内容分析)`
|
|
140
|
+
: '';
|
|
141
|
+
const note = parts.length > 0 ? `(${parts.join(',')})` : imgNote;
|
|
142
|
+
|
|
143
|
+
const block = `\n\n## 任务附件${note}\n${lines.join('\n')}`;
|
|
144
|
+
return { block, imageDataUrls };
|
|
145
|
+
}
|