ai-helper-agent 2.5.1 → 2.5.2
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/dist/assets/{agent-store-D0oEv0PW.js → agent-store-TSvufiWG.js} +1 -1
- package/dist/assets/background.js +71 -70
- package/dist/assets/{chat-export-MTTSSIwM.js → chat-export-BFXv3fdM.js} +1 -1
- package/dist/assets/content.js +43 -43
- package/dist/{dist/assets/i18n-Br_sF5pb.js → assets/i18n-DJ3tjLY-.js} +4 -4
- package/dist/{dist/assets/markdown-render-lSSxW953.js → assets/markdown-render-C9SKCrR4.js} +4 -4
- package/dist/assets/{model-profiles-BdQaV4EZ.js → model-profiles-CfvHSLkL.js} +1 -1
- package/dist/{dist/assets/options-JtYgxtsc.css → assets/options-DU7Wz1w_.css} +1 -1
- package/dist/assets/options.js +89 -89
- package/dist/assets/{page-snapshot-renderer-Cl0qk7U1.js → page-snapshot-renderer-Cdzmf6hl.js} +1 -1
- package/dist/assets/session-store-CRTEYLCo.js +81 -0
- package/dist/assets/side_panel.css +1 -1
- package/dist/assets/side_panel.js +219 -215
- package/dist/{dist/assets/utils-ZJjxSlg_.js → assets/utils-DukUruXz.js} +26 -26
- package/dist/assets/{version-C0CY79zF.js → version-s8LDcTZ-.js} +1 -1
- package/dist/assets/voice-shortcut-Dtt3nhkz.js +1 -0
- package/dist/assets/workspace-manager-CgkNrxoo.js +1 -0
- package/dist/manifest.json +1 -1
- package/dist/offscreen/offscreen.js +6 -6
- package/dist/options.html +34 -4
- package/dist/side_panel.html +22 -13
- package/package.json +1 -1
- package/src/asr/install.js +26 -0
- package/src/documents/routes.js +4 -3
- package/src/locales/en.json +10 -1
- package/src/locales/zh.json +10 -1
- package/src/native-lib-guard.js +194 -0
- package/src/rag/embedding/embed-worker.mjs +182 -0
- package/src/rag/embedding/local.js +42 -62
- package/src/rag/embedding/worker-client.js +476 -0
- package/src/rag/errors.js +60 -2
- package/src/rag/install.js +27 -0
- package/src/rag/manager.js +5 -2
- package/src/rag/routes.js +4 -2
- package/src/search.js +4 -4
- package/src/security.js +97 -33
- package/src/server.js +284 -40
- package/src/session-workdirs.js +241 -0
- package/src/skill/executor.js +25 -9
- package/src/skill/registry.js +3 -2
- package/dist/assets/i18n-Br_sF5pb.js +0 -49
- package/dist/assets/markdown-render-Dq5F_dqP.js +0 -63
- package/dist/assets/options-JtYgxtsc.css +0 -1
- package/dist/assets/session-store-KkrAlIc6.js +0 -81
- package/dist/assets/utils-ZJjxSlg_.js +0 -112
- package/dist/dist/_locales/en/messages.json +0 -11
- package/dist/dist/_locales/zh_CN/messages.json +0 -11
- package/dist/dist/assets/__vite-browser-external-CM3Eme-n.js +0 -1
- package/dist/dist/assets/agent-store-D0oEv0PW.js +0 -1
- package/dist/dist/assets/background.js +0 -451
- package/dist/dist/assets/chat-export-7wjGjcA8.js +0 -37
- package/dist/dist/assets/chunk-CMxvf4Kt.js +0 -1
- package/dist/dist/assets/content.js +0 -985
- package/dist/dist/assets/dist-BUuoW_84.js +0 -24
- package/dist/dist/assets/dist-DuuG_8Br.js +0 -133
- package/dist/dist/assets/jszip.min-DO6SOhXU.js +0 -2
- package/dist/dist/assets/lib-YwkWjNcC.js +0 -220
- package/dist/dist/assets/marked.min.js-D2rXgLAQ.js +0 -56
- package/dist/dist/assets/marked.min.js-loader-tKH5SQVQ.js +0 -13
- package/dist/dist/assets/model-profiles-BdQaV4EZ.js +0 -24
- package/dist/dist/assets/network-scope-CNRYXk74.js +0 -1
- package/dist/dist/assets/options.js +0 -859
- package/dist/dist/assets/page-snapshot-renderer-Cl0qk7U1.js +0 -3
- package/dist/dist/assets/pdf-DnVqBaAN.js +0 -6
- package/dist/dist/assets/session-store-KkrAlIc6.js +0 -81
- package/dist/dist/assets/side_panel.css +0 -1
- package/dist/dist/assets/side_panel.js +0 -2595
- package/dist/dist/assets/theme-BRAX5X2F.css +0 -1
- package/dist/dist/assets/theme-CLwYsx9o.js +0 -1
- package/dist/dist/assets/version-C7x4HYX1.js +0 -1
- package/dist/dist/assets/xlsx-DGkz7cnk.js +0 -105
- package/dist/dist/icons/README.md +0 -11
- package/dist/dist/icons/design-philosophy.md +0 -41
- package/dist/dist/icons/icon128.png +0 -0
- package/dist/dist/icons/icon128.svg +0 -68
- package/dist/dist/icons/icon16.png +0 -0
- package/dist/dist/icons/icon16.svg +0 -32
- package/dist/dist/icons/icon48.png +0 -0
- package/dist/dist/icons/icon48.svg +0 -56
- package/dist/dist/libs/github-markdown-light.min.css +0 -8
- package/dist/dist/libs/html2canvas.min.js +0 -20
- package/dist/dist/libs/jspdf.min.js +0 -398
- package/dist/dist/libs/marked.min.js +0 -69
- package/dist/dist/libs/mermaid.min.js +0 -2024
- package/dist/dist/libs/pdf.worker.min.js +0 -22
- package/dist/dist/libs/qrcode.min.js +0 -7
- package/dist/dist/manifest.json +0 -94
- package/dist/dist/offscreen/offscreen.html +0 -11
- package/dist/dist/offscreen/offscreen.js +0 -49
- package/dist/dist/options.html +0 -1091
- package/dist/dist/service-worker-loader.js +0 -1
- package/dist/dist/side_panel.html +0 -1357
- package/dist/dist/styles/styles.css +0 -262
- package/dist/dist/voice-capture-worklet.js +0 -47
package/src/server.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// agent/src/server.js - HTTP Router + WebSocket 服务器
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import { WebSocketServer } from 'ws';
|
|
4
|
-
import { readFileSync, writeFileSync, createWriteStream, createReadStream, statSync, existsSync, watch } from 'fs';
|
|
4
|
+
import { readFileSync, writeFileSync, createWriteStream, createReadStream, statSync, existsSync, realpathSync, watch } from 'fs';
|
|
5
5
|
import { readFile, writeFile, readdir, stat, unlink, rmdir, chmod, mkdir, rename } from 'fs/promises';
|
|
6
|
-
import { join, dirname, basename, resolve, isAbsolute, relative } from 'path';
|
|
6
|
+
import { join, dirname, basename, resolve, isAbsolute, relative, sep } from 'path';
|
|
7
7
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
8
8
|
import { ZipArchive } from 'archiver';
|
|
9
9
|
import { homedir, tmpdir } from 'os';
|
|
@@ -11,14 +11,16 @@ import { spawn } from 'child_process';
|
|
|
11
11
|
import { randomBytes } from 'crypto';
|
|
12
12
|
import os from 'os';
|
|
13
13
|
import { loadConfig, saveConfig, setConfigLang } from './config.js';
|
|
14
|
+
import { getSessionWorkdir, bindSessionWorkdir, unbindSessionWorkdir, isValidSessionId, getBaseSessionId } from './session-workdirs.js';
|
|
14
15
|
import { verifyToken, startPairCodeRotation, stopPairCodeRotation, handlePairRequest, setAuthLang } from './auth.js';
|
|
15
|
-
import { checkPath, checkCommand, normalizePathFormat, setSecurityLang } from './security.js';
|
|
16
|
+
import { checkPath, checkCommand, normalizePathFormat, setSecurityLang, isHardBlocked } from './security.js';
|
|
16
17
|
import { moveToTrash, restoreFromTrash, listTrash, startPeriodicCleanup, stopPeriodicCleanup, setTrashLang } from './trash.js';
|
|
17
18
|
import { executeCommand, executeCommandSync, addWsClient, disconnectWsClient, killProcess, detachProcess, getRunningProcesses, setExecutorLang } from './executor.js';
|
|
18
19
|
import { setConsoleOutput, setLoggerLocale, logAuth, logFs, logExec, logSecurity, logSystem, logError, queryLogs, getLogDates } from './logger.js';
|
|
19
20
|
import { initSearchTools, getSearchToolsAvailable, searchFiles, searchContent, setSearchLang } from './search.js';
|
|
20
21
|
import { detectRagAvailable, isRagAvailable, resetRagDetection, setRagLang, getLastRagProbeError } from './rag/detect.js';
|
|
21
22
|
import { startRagInstall, getRagInstallStatus, stopRagInstall, maybeAutoRestoreRagDeps } from './rag/install.js';
|
|
23
|
+
import { closeEmbedWorker } from './rag/embedding/worker-client.js';
|
|
22
24
|
import { detectAsrAvailable, isAsrAvailable, resetAsrDetection, setAsrLang, getLastAsrProbeError } from './asr/detect.js';
|
|
23
25
|
import { startAsrInstall, getAsrInstallStatus, stopAsrInstall } from './asr/install.js';
|
|
24
26
|
import { asrRouter, isDepsIndependentPath as isAsrDepsIndependentPath, resolveAsrModels } from './asr/routes.js';
|
|
@@ -390,6 +392,142 @@ function parseMultipartBody(req) {
|
|
|
390
392
|
});
|
|
391
393
|
}
|
|
392
394
|
|
|
395
|
+
// ==================== 会话级工作目录:请求级上下文 ====================
|
|
396
|
+
|
|
397
|
+
/** 跨平台路径比较归一(Windows 大小写不敏感) */
|
|
398
|
+
function normForCompare(p) {
|
|
399
|
+
const s = String(p || '').replace(/\\/g, '/');
|
|
400
|
+
return process.platform === 'win32' ? s.toLowerCase() : s;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* 解析本次请求的会话上下文(会话级工作目录的唯一入口)。
|
|
405
|
+
*
|
|
406
|
+
* 优先组:
|
|
407
|
+
* 1. X-AIH-Workdir(扩展端冻结的执行期目录):仅在等于该会话的注册值、
|
|
408
|
+
* 或会话未注册但等于全局默认时采纳;否则忽略并记审计日志(继续走下一级)。
|
|
409
|
+
* 2. 该 sessionId 的注册表值。
|
|
410
|
+
* 3. 全局 config.workdir。
|
|
411
|
+
*
|
|
412
|
+
* 为什么带第 1 级:并行会话可能同时执行,扩展端会在任务派发时冻结目录;
|
|
413
|
+
* 这一级把“冻结值”显式带到代端并留下审计痕迹,同时不构成提权——
|
|
414
|
+
* 接受的条件仍是「该会话本就注册了这个目录」。
|
|
415
|
+
*
|
|
416
|
+
* @param {import('http').IncomingMessage} req
|
|
417
|
+
* @returns {{ sessionId: string|null, workdir: string, globalWorkdir: string, registered: string|null, scope: 'session'|'default' }}
|
|
418
|
+
*/
|
|
419
|
+
export function resolveRequestScope(req) {
|
|
420
|
+
const config = loadConfig();
|
|
421
|
+
const globalWorkdir = resolve(normalizePathFormat(config.workdir));
|
|
422
|
+
|
|
423
|
+
const rawId = req.headers['x-aih-session-id'];
|
|
424
|
+
const sessionId = typeof rawId === 'string' && isValidSessionId(rawId) ? rawId : null;
|
|
425
|
+
// 派生会话(子任务/子代理)没有自己的绑定:回落到主会话的注册值
|
|
426
|
+
const baseSessionId = getBaseSessionId(sessionId);
|
|
427
|
+
const registeredRaw = sessionId
|
|
428
|
+
? (getSessionWorkdir(sessionId) || (baseSessionId !== sessionId ? getSessionWorkdir(baseSessionId) : null))
|
|
429
|
+
: null;
|
|
430
|
+
const registered = registeredRaw ? resolve(normalizePathFormat(registeredRaw)) : null;
|
|
431
|
+
|
|
432
|
+
let workdir = registered || globalWorkdir;
|
|
433
|
+
const scope = registered ? 'session' : 'default';
|
|
434
|
+
|
|
435
|
+
const rawDeclared = req.headers['x-aih-workdir'];
|
|
436
|
+
if (typeof rawDeclared === 'string' && rawDeclared) {
|
|
437
|
+
let declared = null;
|
|
438
|
+
try {
|
|
439
|
+
declared = resolve(normalizePathFormat(decodeURIComponent(rawDeclared)));
|
|
440
|
+
} catch { declared = null; }
|
|
441
|
+
|
|
442
|
+
if (declared) {
|
|
443
|
+
const expected = registered || globalWorkdir;
|
|
444
|
+
if (normForCompare(declared) === normForCompare(expected)) {
|
|
445
|
+
workdir = declared;
|
|
446
|
+
} else {
|
|
447
|
+
// 冻结值已失效(例如会话绑定被改过):宁可回落,也不按旧目录执行
|
|
448
|
+
logSecurity('session_workdir_header_ignored', {
|
|
449
|
+
sessionId,
|
|
450
|
+
declared,
|
|
451
|
+
effective: expected,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return { sessionId, workdir, globalWorkdir, registered, scope };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* /api/exec 的 cwd 归一(导出供单测覆盖「缺省 / 相对 / 越界」三种语义)
|
|
462
|
+
* - 缺省:用本会话有效目录
|
|
463
|
+
* - 相对:按会话有效目录解析(checkPath 内部同规则)
|
|
464
|
+
* - 绝对:越界即拒绝,hint 给出本会话目录,便于模型自我纠正而不是反复猜旧路径
|
|
465
|
+
*
|
|
466
|
+
* @param {string|undefined} cwd 模型给出的 cwd(可能缺省/相对/绝对)
|
|
467
|
+
* @param {{ sessionId: string|null, workdir: string }} scope 请求作用域
|
|
468
|
+
* @param {Function} tFn 翻译函数
|
|
469
|
+
* @returns {Promise<{ requested: string, cwd: string, allowed: boolean, reason?: string, hint: string }>}
|
|
470
|
+
*/
|
|
471
|
+
export async function resolveExecCwd(cwd, scope, tFn) {
|
|
472
|
+
const requested = typeof cwd === 'string' ? cwd.trim() : '';
|
|
473
|
+
const candidate = requested || scope.workdir;
|
|
474
|
+
const checked = await checkPath(candidate, tFn, scope);
|
|
475
|
+
return {
|
|
476
|
+
requested,
|
|
477
|
+
cwd: checked.resolved,
|
|
478
|
+
allowed: !!checked.allowed,
|
|
479
|
+
reason: checked.reason,
|
|
480
|
+
hint: scope.workdir,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* 工作目录目标是否允许(全局切换与会话级绑定共用同一判据)
|
|
486
|
+
*
|
|
487
|
+
* 只禁止 **Agent 根目录本身**(`~/.ai-helper-agent`)与 **HARD_BLOCKED_PATHS 声明的敏感面**
|
|
488
|
+
* (`config.json` / `pairings.json` / `logs` / `.trash` …)。
|
|
489
|
+
* 其下其余子目录是合法工作区:默认 workdir 就是 `~/.ai-helper-agent/workspace`,
|
|
490
|
+
* 若在此处把整棵子树一起封掉,用户连默认目录都切不回去。
|
|
491
|
+
*
|
|
492
|
+
* 判据与 checkPath 同源(复用 isHardBlocked):"能设为工作目录" ⊆ "设完后能读写"。
|
|
493
|
+
* 否则绑定到 logs/ 这类目录会成功,但之后每次读写都被 403,用户只能看到一片报错。
|
|
494
|
+
*
|
|
495
|
+
* 大小写与符号链接变体(macOS 上 `.AI-HELPER-AGENT` 指向同一目录)也算根目录。
|
|
496
|
+
*
|
|
497
|
+
* @param {string} workdir 已 resolve 过的绝对路径
|
|
498
|
+
* @returns {boolean}
|
|
499
|
+
*/
|
|
500
|
+
export function isWorkdirTargetAllowed(workdir) {
|
|
501
|
+
const raw = String(workdir || '').trim();
|
|
502
|
+
if (!raw) return false;
|
|
503
|
+
|
|
504
|
+
const agentDir = resolve(join(homedir(), '.ai-helper-agent'));
|
|
505
|
+
const target = resolve(normalizePathFormat(raw));
|
|
506
|
+
|
|
507
|
+
// 目标可能尚不存在(待创建的目录),realpath 失败就按原路径判;
|
|
508
|
+
// 两侧都归一,才能处理 macOS 的 /var → /private/var 与指向根目录的短链
|
|
509
|
+
let targetReal = target;
|
|
510
|
+
let agentReal = agentDir;
|
|
511
|
+
try { agentReal = realpathSync(agentDir); } catch { /* 保留原值 */ }
|
|
512
|
+
try { targetReal = realpathSync(target); } catch { /* 保留原值 */ }
|
|
513
|
+
|
|
514
|
+
// 相对 Agent 目录的位置:'' = 根目录本身,'logs/x' = 其下子路径,null = 不在其内
|
|
515
|
+
const insideAgent = (p, base) => {
|
|
516
|
+
if (p === base) return '';
|
|
517
|
+
return p.startsWith(base + sep) ? p.slice(base.length + 1) : null;
|
|
518
|
+
};
|
|
519
|
+
const rel = insideAgent(targetReal, agentReal) ?? insideAgent(target, agentDir);
|
|
520
|
+
|
|
521
|
+
if (rel === '') return false; // Agent 根目录本身(含符号链接 / 大小写变体)
|
|
522
|
+
if (rel) {
|
|
523
|
+
// 与 checkPath 同源:HARD_BLOCKED_PATHS 里的敏感面设为 workdir 后每次读写都会被拦,
|
|
524
|
+
// 不如在设置时就拒;其余子目录(workspace / skills / 自建目录)照旧放行
|
|
525
|
+
return !isHardBlocked(join(agentDir, rel));
|
|
526
|
+
}
|
|
527
|
+
// 不在 Agent 目录内:仍用同一份清单兜底(如符号链接指向某个敏感文件)
|
|
528
|
+
return !isHardBlocked(target) && !isHardBlocked(targetReal);
|
|
529
|
+
}
|
|
530
|
+
|
|
393
531
|
/**
|
|
394
532
|
* 创建并启动服务器
|
|
395
533
|
*/
|
|
@@ -452,7 +590,7 @@ export function startServer() {
|
|
|
452
590
|
// CORS 预检(仅放行 Chrome 扩展来源)
|
|
453
591
|
if (req.method === 'OPTIONS') {
|
|
454
592
|
const optHeaders = {
|
|
455
|
-
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Memory-Caller',
|
|
593
|
+
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Memory-Caller, X-AIH-Session-Id, X-AIH-Workdir',
|
|
456
594
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS'
|
|
457
595
|
};
|
|
458
596
|
const allowedOrigin = getAllowedOrigin(req);
|
|
@@ -558,6 +696,10 @@ export function startServer() {
|
|
|
558
696
|
return jsonResponse(res, 403, { success: false, error: t('error.invalidToken') });
|
|
559
697
|
}
|
|
560
698
|
|
|
699
|
+
// 会话级工作目录:本次请求的有效目录(会话绑定 > 全局默认)。
|
|
700
|
+
// 认证后才解析:配对 token 是唯一信任边界,未配对请求不应触及会话状态。
|
|
701
|
+
const scope = resolveRequestScope(req);
|
|
702
|
+
|
|
561
703
|
// 心跳接口(需认证):仅刷新 lastAuthTime 维持"插件在线"状态,不返回敏感信息
|
|
562
704
|
if (req.method === 'GET' && pathname === '/api/heartbeat') {
|
|
563
705
|
return jsonResponse(res, 200, { success: true, time: Date.now() });
|
|
@@ -739,7 +881,14 @@ export function startServer() {
|
|
|
739
881
|
success: true,
|
|
740
882
|
version: AGENT_VERSION,
|
|
741
883
|
pairCodeTTL: config.pairCodeTTL,
|
|
742
|
-
|
|
884
|
+
// workdir:本次请求的有效目录(带会话头且已绑定时为会话目录,否则为全局默认)
|
|
885
|
+
workdir: scope.workdir,
|
|
886
|
+
// defaultWorkdir:代理全局默认目录(未绑定会话的兜底),扩展端用它渲染「默认」态
|
|
887
|
+
defaultWorkdir: config.workdir,
|
|
888
|
+
scope: scope.scope,
|
|
889
|
+
sessionId: scope.sessionId,
|
|
890
|
+
sessionWorkdir: scope.registered,
|
|
891
|
+
sessionWorkdirSupported: true,
|
|
743
892
|
allowedPaths: config.allowedPaths,
|
|
744
893
|
commandTimeout: config.commandTimeout,
|
|
745
894
|
fileMaxSize: config.fileMaxSize, // 读取大小限制(/api/fs/read、/api/fs/download)
|
|
@@ -766,9 +915,10 @@ export function startServer() {
|
|
|
766
915
|
try {
|
|
767
916
|
const { fileBuffer, fileName, mimeType } = await parseMultipartBody(req);
|
|
768
917
|
|
|
769
|
-
//
|
|
918
|
+
// 安全校验:过滤危险字符 + basename 防路径穿越(.. 等)
|
|
770
919
|
const safeName = basename(fileName.replace(/[/\\:*?"<>|]/g, '_')) || 'unnamed_file';
|
|
771
|
-
|
|
920
|
+
// 落盘目录 = 本会话有效目录(会话绑定优先,未绑定跟全局)
|
|
921
|
+
const destPath = join(scope.workdir, safeName);
|
|
772
922
|
|
|
773
923
|
// 去重:如果文件已存在,添加后缀
|
|
774
924
|
let finalPath = destPath;
|
|
@@ -777,13 +927,13 @@ export function startServer() {
|
|
|
777
927
|
const base = safeName.substring(0, safeName.length - ext.length) || safeName;
|
|
778
928
|
let counter = 1;
|
|
779
929
|
while (await exists(finalPath)) {
|
|
780
|
-
finalPath = join(
|
|
930
|
+
finalPath = join(scope.workdir, `${base}_${counter}${ext}`);
|
|
781
931
|
counter++;
|
|
782
932
|
}
|
|
783
933
|
}
|
|
784
934
|
|
|
785
935
|
// 确保工作目录存在
|
|
786
|
-
await mkdir(
|
|
936
|
+
await mkdir(scope.workdir, { recursive: true });
|
|
787
937
|
|
|
788
938
|
// 写入文件
|
|
789
939
|
await writeFile(finalPath, fileBuffer);
|
|
@@ -809,7 +959,7 @@ export function startServer() {
|
|
|
809
959
|
if (!targetPath || typeof targetPath !== 'string') {
|
|
810
960
|
return jsonResponse(res, 400, { success: false, error: t('error.missingPath') });
|
|
811
961
|
}
|
|
812
|
-
const check = await checkPath(targetPath, t);
|
|
962
|
+
const check = await checkPath(targetPath, t, scope);
|
|
813
963
|
if (!check.allowed) {
|
|
814
964
|
logSecurity('fs_upload_stream_blocked', { path: targetPath, reason: check.reason });
|
|
815
965
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -889,6 +1039,29 @@ export function startServer() {
|
|
|
889
1039
|
return jsonResponse(res, 400, { success: false, error: t('error.missingWorkdir') });
|
|
890
1040
|
}
|
|
891
1041
|
|
|
1042
|
+
// 会话级绑定:带 sessionId 时只写会话注册表,不动全局 config / allowedPaths。
|
|
1043
|
+
// 显式传了非法 sessionId 必须报错,不能默默降级成全局切换(那会影响所有未绑定会话)
|
|
1044
|
+
const hasSessionParam = body.sessionId !== undefined && body.sessionId !== null && body.sessionId !== '';
|
|
1045
|
+
if (hasSessionParam && !isValidSessionId(body.sessionId)) {
|
|
1046
|
+
return jsonResponse(res, 400, { success: false, error: t('error.invalidSessionId') });
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
// 意图显式化:scope 声明「只改本会话」还是「改全局默认」。
|
|
1050
|
+
// 只靠「有没有传 sessionId」推断太脆——上层漏传一个字段就会静默变成全局切换,
|
|
1051
|
+
// 把并行会话(含正在跑和排队的任务)的目录一起改掉。显式声明后能直接 400。
|
|
1052
|
+
// 未传 scope(undefined/null/空串)= 旧客户端,保留旧推断;传了就必须是合法值,
|
|
1053
|
+
// 否则拼写错误(如 'Session')会静默降到全局切换,正是本条要防的失效模式。
|
|
1054
|
+
const rawScope = body.scope;
|
|
1055
|
+
const hasScopeParam = rawScope !== undefined && rawScope !== null && rawScope !== '';
|
|
1056
|
+
if (hasScopeParam && rawScope !== 'session' && rawScope !== 'global') {
|
|
1057
|
+
return jsonResponse(res, 400, { success: false, error: t('error.invalidScope') });
|
|
1058
|
+
}
|
|
1059
|
+
const declaredScope = hasScopeParam ? rawScope : null;
|
|
1060
|
+
if (declaredScope === 'session' && !hasSessionParam) {
|
|
1061
|
+
return jsonResponse(res, 400, { success: false, error: t('error.invalidSessionId') });
|
|
1062
|
+
}
|
|
1063
|
+
const targetSessionId = declaredScope === 'global' ? null : (hasSessionParam ? body.sessionId : null);
|
|
1064
|
+
|
|
892
1065
|
// 展开 ~ 到用户主目录
|
|
893
1066
|
let resolvedWorkdir = newWorkdir.trim();
|
|
894
1067
|
if (resolvedWorkdir === '~') {
|
|
@@ -906,13 +1079,40 @@ export function startServer() {
|
|
|
906
1079
|
}
|
|
907
1080
|
resolvedWorkdir = resolve(resolvedWorkdir);
|
|
908
1081
|
|
|
909
|
-
// 禁止设为 Agent
|
|
910
|
-
|
|
911
|
-
if (resolvedWorkdir === AGENT_DIR_PATH) {
|
|
1082
|
+
// 禁止设为 Agent 根目录本身(子目录合法:默认 workdir 就是其下的 workspace)
|
|
1083
|
+
if (!isWorkdirTargetAllowed(resolvedWorkdir)) {
|
|
912
1084
|
logSecurity('workdir_switch_blocked', { path: resolvedWorkdir, reason: t('error.workdirIsSystemDir') });
|
|
913
1085
|
return jsonResponse(res, 403, { success: false, error: t('error.workdirIsSystemDir') });
|
|
914
1086
|
}
|
|
915
1087
|
|
|
1088
|
+
// 目标已存在但不是目录(选到了文件):后面 mkdir 会抛 EEXIST 变成 500,
|
|
1089
|
+
// 用户只能看到一句“切换失败”,分不清是权限还是选错了路径
|
|
1090
|
+
try {
|
|
1091
|
+
if (!statSync(resolvedWorkdir).isDirectory()) {
|
|
1092
|
+
logSecurity('workdir_switch_blocked', { path: resolvedWorkdir, reason: t('error.workdirNotDirectory') });
|
|
1093
|
+
return jsonResponse(res, 400, { success: false, error: t('error.workdirNotDirectory') });
|
|
1094
|
+
}
|
|
1095
|
+
} catch { /* 不存在:按“待创建目录”继续 */ }
|
|
1096
|
+
|
|
1097
|
+
if (targetSessionId) {
|
|
1098
|
+
try {
|
|
1099
|
+
await mkdir(resolvedWorkdir, { recursive: true });
|
|
1100
|
+
const entry = await bindSessionWorkdir(targetSessionId, resolvedWorkdir);
|
|
1101
|
+
logSystem('session_workdir_bound', { sessionId: targetSessionId, workdir: resolvedWorkdir, extId });
|
|
1102
|
+
return jsonResponse(res, 200, {
|
|
1103
|
+
success: true,
|
|
1104
|
+
workdir: resolvedWorkdir,
|
|
1105
|
+
scope: 'session',
|
|
1106
|
+
sessionId: targetSessionId,
|
|
1107
|
+
updatedAt: entry?.updatedAt || Date.now(),
|
|
1108
|
+
message: t('message.workdirBoundToSession')
|
|
1109
|
+
});
|
|
1110
|
+
} catch (err) {
|
|
1111
|
+
logError('config', 'session_workdir_bind_error', { sessionId: targetSessionId, path: resolvedWorkdir, error: err.message });
|
|
1112
|
+
return jsonResponse(res, 500, { success: false, error: t('error.switchWorkdirFailed', { message: err.message }) });
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
916
1116
|
try {
|
|
917
1117
|
// 目录不存在则自动创建(mkdir -p)
|
|
918
1118
|
await mkdir(resolvedWorkdir, { recursive: true });
|
|
@@ -942,6 +1142,29 @@ export function startServer() {
|
|
|
942
1142
|
}
|
|
943
1143
|
}
|
|
944
1144
|
|
|
1145
|
+
// === 解除会话工作目录绑定(恢复跟随代理默认目录) ===
|
|
1146
|
+
if (pathname === '/api/config/workdir/unbind' && req.method === 'POST') {
|
|
1147
|
+
if (!isValidSessionId(body.sessionId)) {
|
|
1148
|
+
return jsonResponse(res, 400, { success: false, error: t('error.invalidSessionId') });
|
|
1149
|
+
}
|
|
1150
|
+
try {
|
|
1151
|
+
const removed = await unbindSessionWorkdir(body.sessionId);
|
|
1152
|
+
const defaultWorkdir = loadConfig().workdir;
|
|
1153
|
+
logSystem('session_workdir_unbound', { sessionId: body.sessionId, removed, extId });
|
|
1154
|
+
return jsonResponse(res, 200, {
|
|
1155
|
+
success: true,
|
|
1156
|
+
removed,
|
|
1157
|
+
workdir: defaultWorkdir,
|
|
1158
|
+
scope: 'default',
|
|
1159
|
+
sessionId: body.sessionId,
|
|
1160
|
+
message: t('message.workdirUnbound')
|
|
1161
|
+
});
|
|
1162
|
+
} catch (err) {
|
|
1163
|
+
logError('config', 'session_workdir_unbind_error', { sessionId: body.sessionId, error: err.message });
|
|
1164
|
+
return jsonResponse(res, 500, { success: false, error: t('error.switchWorkdirFailed', { message: err.message }) });
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
|
|
945
1168
|
// === 移除允许访问的目录(需认证,不可移除当前工作目录) ===
|
|
946
1169
|
if (pathname === '/api/config/allowed-paths/remove' && req.method === 'POST') {
|
|
947
1170
|
const targetPath = body.path;
|
|
@@ -1060,7 +1283,8 @@ export function startServer() {
|
|
|
1060
1283
|
body.pattern || '*',
|
|
1061
1284
|
body.recursive !== false,
|
|
1062
1285
|
maxResults,
|
|
1063
|
-
t
|
|
1286
|
+
t,
|
|
1287
|
+
scope
|
|
1064
1288
|
);
|
|
1065
1289
|
if (result.success) {
|
|
1066
1290
|
logFs('search_files', { path: result.path, pattern: body.pattern, total: result.total, engine: result.engine });
|
|
@@ -1086,7 +1310,8 @@ export function startServer() {
|
|
|
1086
1310
|
body.recursive !== false,
|
|
1087
1311
|
maxResults,
|
|
1088
1312
|
body.contextLines !== undefined ? body.contextLines : 2,
|
|
1089
|
-
t
|
|
1313
|
+
t,
|
|
1314
|
+
scope
|
|
1090
1315
|
);
|
|
1091
1316
|
if (result.success) {
|
|
1092
1317
|
logFs('search_content', { path: result.path, pattern: body.pattern, total: result.total, engine: result.engine });
|
|
@@ -1102,7 +1327,7 @@ export function startServer() {
|
|
|
1102
1327
|
|
|
1103
1328
|
// 读取文件
|
|
1104
1329
|
if (pathname === '/api/fs/read') {
|
|
1105
|
-
const check = await checkPath(body.path, t);
|
|
1330
|
+
const check = await checkPath(body.path, t, scope);
|
|
1106
1331
|
if (!check.allowed) {
|
|
1107
1332
|
logSecurity('fs_read_blocked', { path: body.path, reason: check.reason });
|
|
1108
1333
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1118,7 +1343,7 @@ export function startServer() {
|
|
|
1118
1343
|
|
|
1119
1344
|
// 写入文件
|
|
1120
1345
|
if (pathname === '/api/fs/write') {
|
|
1121
|
-
const check = await checkPath(body.path, t);
|
|
1346
|
+
const check = await checkPath(body.path, t, scope);
|
|
1122
1347
|
if (!check.allowed) {
|
|
1123
1348
|
logSecurity('fs_write_blocked', { path: body.path, reason: check.reason });
|
|
1124
1349
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1156,7 +1381,7 @@ export function startServer() {
|
|
|
1156
1381
|
// 列出目录
|
|
1157
1382
|
if (pathname === '/api/fs/list') {
|
|
1158
1383
|
const dirPath = body.path || '.';
|
|
1159
|
-
const check = await checkPath(dirPath, t);
|
|
1384
|
+
const check = await checkPath(dirPath, t, scope);
|
|
1160
1385
|
if (!check.allowed) {
|
|
1161
1386
|
logSecurity('fs_list_blocked', { path: dirPath, reason: check.reason });
|
|
1162
1387
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1178,7 +1403,7 @@ export function startServer() {
|
|
|
1178
1403
|
|
|
1179
1404
|
// 获取文件详细信息(权限、创建/访问/修改时间、MIME 类型等)
|
|
1180
1405
|
if (pathname === '/api/fs/stat') {
|
|
1181
|
-
const check = await checkPath(body.path, t);
|
|
1406
|
+
const check = await checkPath(body.path, t, scope);
|
|
1182
1407
|
if (!check.allowed) {
|
|
1183
1408
|
logSecurity('fs_stat_blocked', { path: body.path, reason: check.reason });
|
|
1184
1409
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1210,7 +1435,7 @@ export function startServer() {
|
|
|
1210
1435
|
|
|
1211
1436
|
// 删除文件/目录(移至回收站,7天后自动清理)
|
|
1212
1437
|
if (pathname === '/api/fs/delete') {
|
|
1213
|
-
const check = await checkPath(body.path, t);
|
|
1438
|
+
const check = await checkPath(body.path, t, scope);
|
|
1214
1439
|
if (!check.allowed) {
|
|
1215
1440
|
logSecurity('fs_delete_blocked', { path: body.path, reason: check.reason });
|
|
1216
1441
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1229,7 +1454,7 @@ export function startServer() {
|
|
|
1229
1454
|
|
|
1230
1455
|
// === 文档图片抽取(docx;图片落盘到文档旁 _attachments/) ===
|
|
1231
1456
|
if (pathname === '/api/documents/extract-images') {
|
|
1232
|
-
const { status, payload } = await extractImagesHandler(body, { t });
|
|
1457
|
+
const { status, payload } = await extractImagesHandler(body, { t, sessionCtx: scope });
|
|
1233
1458
|
if (payload.success) {
|
|
1234
1459
|
logFs('extract_images', { path: body.path, count: payload.images.length, truncated: payload.truncated });
|
|
1235
1460
|
} else {
|
|
@@ -1244,7 +1469,7 @@ export function startServer() {
|
|
|
1244
1469
|
if (!targetPath || typeof targetPath !== 'string') {
|
|
1245
1470
|
return jsonResponse(res, 400, { success: false, error: t('error.missingPath') });
|
|
1246
1471
|
}
|
|
1247
|
-
const check = await checkPath(targetPath, t);
|
|
1472
|
+
const check = await checkPath(targetPath, t, scope);
|
|
1248
1473
|
if (!check.allowed) {
|
|
1249
1474
|
logSecurity('fs_download_blocked', { path: targetPath, reason: check.reason });
|
|
1250
1475
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1306,7 +1531,7 @@ export function startServer() {
|
|
|
1306
1531
|
|
|
1307
1532
|
const resolvedPaths = [];
|
|
1308
1533
|
for (const p of paths) {
|
|
1309
|
-
const check = await checkPath(p, t);
|
|
1534
|
+
const check = await checkPath(p, t, scope);
|
|
1310
1535
|
if (!check.allowed) {
|
|
1311
1536
|
logSecurity('fs_download_blocked', { path: p, reason: check.reason });
|
|
1312
1537
|
return jsonResponse(res, 403, { success: false, error: t('error.pathNotAllowed', { path: p }) });
|
|
@@ -1388,7 +1613,7 @@ export function startServer() {
|
|
|
1388
1613
|
// 多文件打包流式下载
|
|
1389
1614
|
const resolvedPaths = [];
|
|
1390
1615
|
for (const p of paths) {
|
|
1391
|
-
const check = await checkPath(p, t);
|
|
1616
|
+
const check = await checkPath(p, t, scope);
|
|
1392
1617
|
if (!check.allowed) {
|
|
1393
1618
|
logSecurity('fs_download_blocked', { path: p, reason: check.reason });
|
|
1394
1619
|
return jsonResponse(res, 403, { success: false, error: t('error.pathNotAllowed', { path: p }) });
|
|
@@ -1412,7 +1637,7 @@ export function startServer() {
|
|
|
1412
1637
|
}
|
|
1413
1638
|
} else if (targetPath && typeof targetPath === 'string') {
|
|
1414
1639
|
// 单文件/目录流式下载
|
|
1415
|
-
const check = await checkPath(targetPath, t);
|
|
1640
|
+
const check = await checkPath(targetPath, t, scope);
|
|
1416
1641
|
if (!check.allowed) {
|
|
1417
1642
|
logSecurity('fs_download_blocked', { path: targetPath, reason: check.reason });
|
|
1418
1643
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1468,7 +1693,7 @@ export function startServer() {
|
|
|
1468
1693
|
if (!filePath || typeof filePath !== 'string') {
|
|
1469
1694
|
return jsonResponse(res, 400, { success: false, error: t('error.missingPath') });
|
|
1470
1695
|
}
|
|
1471
|
-
const check = await checkPath(filePath, t);
|
|
1696
|
+
const check = await checkPath(filePath, t, scope);
|
|
1472
1697
|
if (!check.allowed) {
|
|
1473
1698
|
logSecurity('fs_preview_xlsx_blocked', { path: filePath, reason: check.reason });
|
|
1474
1699
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1525,7 +1750,7 @@ export function startServer() {
|
|
|
1525
1750
|
if (!dirPath || typeof dirPath !== 'string') {
|
|
1526
1751
|
return jsonResponse(res, 400, { success: false, error: t('error.missingPath') });
|
|
1527
1752
|
}
|
|
1528
|
-
const check = await checkPath(dirPath, t);
|
|
1753
|
+
const check = await checkPath(dirPath, t, scope);
|
|
1529
1754
|
if (!check.allowed) {
|
|
1530
1755
|
logSecurity('fs_mkdir_blocked', { path: dirPath, reason: check.reason });
|
|
1531
1756
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1556,7 +1781,7 @@ export function startServer() {
|
|
|
1556
1781
|
if (os.platform() === 'win32' && /[:*?"<>|]/.test(newName)) {
|
|
1557
1782
|
return jsonResponse(res, 400, { success: false, error: t('error.newNameInvalidChars') });
|
|
1558
1783
|
}
|
|
1559
|
-
const check = await checkPath(oldPath, t);
|
|
1784
|
+
const check = await checkPath(oldPath, t, scope);
|
|
1560
1785
|
if (!check.allowed) {
|
|
1561
1786
|
logSecurity('fs_rename_blocked', { path: oldPath, reason: check.reason });
|
|
1562
1787
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1566,7 +1791,7 @@ export function startServer() {
|
|
|
1566
1791
|
}
|
|
1567
1792
|
const newFullPath = join(dirname(check.resolved), newName);
|
|
1568
1793
|
// 确保目标路径也在允许范围内
|
|
1569
|
-
const newCheck = await checkPath(newFullPath, t);
|
|
1794
|
+
const newCheck = await checkPath(newFullPath, t, scope);
|
|
1570
1795
|
if (!newCheck.allowed) {
|
|
1571
1796
|
logSecurity('fs_rename_blocked', { path: newFullPath, reason: newCheck.reason });
|
|
1572
1797
|
return jsonResponse(res, 403, { success: false, error: t('error.targetPathNotAllowed', { reason: newCheck.reason }) });
|
|
@@ -1596,12 +1821,12 @@ export function startServer() {
|
|
|
1596
1821
|
if (!srcPath || typeof srcPath !== 'string' || !destDir || typeof destDir !== 'string') {
|
|
1597
1822
|
return jsonResponse(res, 400, { success: false, error: t('error.missingPathOrDestDir') });
|
|
1598
1823
|
}
|
|
1599
|
-
const srcCheck = await checkPath(srcPath, t);
|
|
1824
|
+
const srcCheck = await checkPath(srcPath, t, scope);
|
|
1600
1825
|
if (!srcCheck.allowed) {
|
|
1601
1826
|
logSecurity('fs_move_blocked', { path: srcPath, reason: srcCheck.reason });
|
|
1602
1827
|
return jsonResponse(res, 403, { success: false, error: srcCheck.reason });
|
|
1603
1828
|
}
|
|
1604
|
-
const destCheck = await checkPath(destDir, t);
|
|
1829
|
+
const destCheck = await checkPath(destDir, t, scope);
|
|
1605
1830
|
if (!destCheck.allowed) {
|
|
1606
1831
|
logSecurity('fs_move_blocked', { path: destDir, reason: destCheck.reason });
|
|
1607
1832
|
return jsonResponse(res, 403, { success: false, error: destCheck.reason });
|
|
@@ -1615,7 +1840,7 @@ export function startServer() {
|
|
|
1615
1840
|
}
|
|
1616
1841
|
const itemName = basename(srcCheck.resolved);
|
|
1617
1842
|
const destPath = join(destCheck.resolved, itemName);
|
|
1618
|
-
const destCheck2 = await checkPath(destPath, t);
|
|
1843
|
+
const destCheck2 = await checkPath(destPath, t, scope);
|
|
1619
1844
|
if (!destCheck2.allowed) {
|
|
1620
1845
|
logSecurity('fs_move_blocked', { path: destPath, reason: destCheck2.reason });
|
|
1621
1846
|
return jsonResponse(res, 403, { success: false, error: t('error.moveTargetNotAllowed', { reason: destCheck2.reason }) });
|
|
@@ -1642,7 +1867,7 @@ export function startServer() {
|
|
|
1642
1867
|
if (!targetPath || typeof targetPath !== 'string') {
|
|
1643
1868
|
return jsonResponse(res, 400, { success: false, error: t('error.missingPath') });
|
|
1644
1869
|
}
|
|
1645
|
-
const check = await checkPath(targetPath, t);
|
|
1870
|
+
const check = await checkPath(targetPath, t, scope);
|
|
1646
1871
|
if (!check.allowed) {
|
|
1647
1872
|
logSecurity('browser_open_blocked', { path: targetPath, reason: check.reason });
|
|
1648
1873
|
return jsonResponse(res, 403, { success: false, error: check.reason });
|
|
@@ -1684,13 +1909,28 @@ export function startServer() {
|
|
|
1684
1909
|
const { command, cwd, wait, force, timeoutMs } = body;
|
|
1685
1910
|
if (!command || typeof command !== 'string') return jsonResponse(res, 400, { success: false, error: t('error.missingCommand') });
|
|
1686
1911
|
|
|
1687
|
-
// 校验 cwd
|
|
1688
|
-
|
|
1689
|
-
const cwdCheck = await checkPath(resolvedCwd, t);
|
|
1912
|
+
// 校验 cwd:缺省用本会话有效目录;相对路径按它解析(checkPath 内部同规则)
|
|
1913
|
+
const cwdCheck = await resolveExecCwd(cwd, scope, t);
|
|
1690
1914
|
if (!cwdCheck.allowed) {
|
|
1691
|
-
logSecurity('exec_cwd_blocked', { command, cwd:
|
|
1692
|
-
|
|
1915
|
+
logSecurity('exec_cwd_blocked', { command, cwd: cwdCheck.cwd, reason: cwdCheck.reason, sessionId: scope.sessionId });
|
|
1916
|
+
// 会话目录被删/被改名的常见情形:只说「越界」用户会去查权限配置,
|
|
1917
|
+
// 这里先区分出来,提示重新选择目录
|
|
1918
|
+
const scopeDirMissing = !!scope.workdir && !existsSync(scope.workdir);
|
|
1919
|
+
// hint 带上本会话有效目录:模型拿到 403 后能自我纠正,而不是反复猜旧路径
|
|
1920
|
+
return jsonResponse(res, 403, {
|
|
1921
|
+
success: false,
|
|
1922
|
+
// 模型显式给了 cwd 却被拒:用「不在本会话目录内」的措辞,比笼统的校验失败更好自我纠正
|
|
1923
|
+
error: scopeDirMissing
|
|
1924
|
+
? t('error.sessionWorkdirMissing', { workdir: scope.workdir })
|
|
1925
|
+
: (cwd
|
|
1926
|
+
? t('error.workdirNotInSessionScope', { workdir: scope.workdir })
|
|
1927
|
+
: t('error.execDirCheckFailed', { reason: cwdCheck.reason })),
|
|
1928
|
+
reason: scopeDirMissing ? 'session_workdir_missing' : cwdCheck.reason,
|
|
1929
|
+
hint: cwdCheck.hint,
|
|
1930
|
+
});
|
|
1693
1931
|
}
|
|
1932
|
+
// 相对 cwd 已在 checkPath 内按会话目录解析,这里必须用解析后的绝对路径
|
|
1933
|
+
const resolvedCwd = cwdCheck.cwd;
|
|
1694
1934
|
|
|
1695
1935
|
// 安全检查
|
|
1696
1936
|
const cmdCheck = checkCommand(command, !!force, t);
|
|
@@ -1805,7 +2045,9 @@ export function startServer() {
|
|
|
1805
2045
|
// 执行 Skill
|
|
1806
2046
|
if (pathname === '/api/skill/run') {
|
|
1807
2047
|
if (!body.name) return jsonResponse(res, 400, { success: false, error: t('error.missingName') });
|
|
1808
|
-
|
|
2048
|
+
// 技能的 agent_file / agent_exec / agent_search 步骤走同一套沙箱,
|
|
2049
|
+
// 必须带本会话的目录上下文,否则会话目录会被判成越界
|
|
2050
|
+
const result = await runSkill(body.name, body.params || {}, undefined, scope);
|
|
1809
2051
|
logSystem('skill_run', { skillName: body.name, success: result.success });
|
|
1810
2052
|
return jsonResponse(res, result.success ? 200 : 400, result);
|
|
1811
2053
|
}
|
|
@@ -2141,7 +2383,7 @@ export function startServer() {
|
|
|
2141
2383
|
logError('system', 'rag_route_load_failed', { error: err.message });
|
|
2142
2384
|
return jsonResponse(res, 500, { success: false, error: t('error.ragLoadFailed', { message: err.message }) });
|
|
2143
2385
|
}
|
|
2144
|
-
return ragRouter(req, res, pathname, url, t, requestBody);
|
|
2386
|
+
return ragRouter(req, res, pathname, url, t, requestBody, scope);
|
|
2145
2387
|
}
|
|
2146
2388
|
|
|
2147
2389
|
// ========== 语音输入(本地 STT)接口(可选组件,默认不装不启用) ==========
|
|
@@ -2451,6 +2693,8 @@ export function startServer() {
|
|
|
2451
2693
|
try { asrSessions.closeAll(); } catch {}
|
|
2452
2694
|
// offline 定稿线程也要收掉:native 会话持有几百 MB,进程退出前让它先 free
|
|
2453
2695
|
try { await closeOfflinePool(); } catch {}
|
|
2696
|
+
// 本地 embedding 子进程(onnxruntime)同样要收:它跑在独立进程里,父进程退出不会自动回收
|
|
2697
|
+
try { await closeEmbedWorker(); } catch {}
|
|
2454
2698
|
|
|
2455
2699
|
// 终止所有运行中的进程
|
|
2456
2700
|
for (const entry of getRunningProcesses()) {
|