Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(project): 新建项目选择现有目录时添加校验

kdgmpw216f8316806a
diff --git a/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json b/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json
index c5e7bd9742..4d3d6bfd73 100644
--- a/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json
+++ b/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json
@@ -2187,7 +2187,8 @@
"absolutePathError": "Enter an absolute path, for example /Users/name/work/project",
"errors": {
"pathExists": "This project path already exists. Choose another directory or start a new conversation from the existing project.",
- "nameExists": "This project name already exists. Please choose another name."
+ "nameExists": "This project name already exists. Please choose another name.",
+ "pathMissing": "Project directory does not exist. Choose an existing directory."
},
"cancel": "Cancel",
"confirm": "Confirm"
diff --git a/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json b/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json
index 205fac753c..b200770ac9 100644
--- a/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json
+++ b/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json
@@ -2191,7 +2191,8 @@
"absolutePathError": "请输入绝对路径,例如 /Users/name/work/project",
"errors": {
"pathExists": "该项目路径已存在,请选择其他目录或直接从现有项目开始新对话",
- "nameExists": "该项目名已存在,请换个名称"
+ "nameExists": "该项目名已存在,请换个名称",
+ "pathMissing": "项目目录不存在,请选择已存在的目录"
},
"cancel": "取消",
"confirm": "确定"
diff --git a/jiuwenswarm/channels/web/frontend/src/multi-session/sidebar/projectCreateErrors.ts b/jiuwenswarm/channels/web/frontend/src/multi-session/sidebar/projectCreateErrors.ts
index dee0c0b14e..9036ba2f88 100644
--- a/jiuwenswarm/channels/web/frontend/src/multi-session/sidebar/projectCreateErrors.ts
+++ b/jiuwenswarm/channels/web/frontend/src/multi-session/sidebar/projectCreateErrors.ts
@@ -1,10 +1,16 @@
export function projectCreateErrorKey(error: unknown): string | null {
const message = error instanceof Error ? error.message : String(error);
+ const code = error && typeof error === 'object' && 'code' in error
+ ? String((error as { code?: unknown }).code || '')
+ : '';
if (message.includes('project_dir already exists')) {
return 'multiSession.project.errors.pathExists';
}
if (message.includes('project name already exists')) {
return 'multiSession.project.errors.nameExists';
}
+ if (code === 'PROJECT_DIR_MISSING' || message.includes('project directory does not exist')) {
+ return 'multiSession.project.errors.pathMissing';
+ }
return null;
}
diff --git a/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py b/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py
index 9668be3bcd..48d30d9f4c 100644
--- a/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py
+++ b/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py
@@ -3216,6 +3216,14 @@ def _register_web_handlers(bind: WebHandlersBindParams) -> None:
code="BAD_REQUEST",
)
return
+ if project_dir and not os.path.isdir(project_dir):
+ await channel.send_response(
+ ws, req_id,
+ ok=False,
+ error="project directory does not exist",
+ code="PROJECT_DIR_MISSING",
+ )
+ return
# 解析 work_mode(严格校验:非法值返回 BAD_REQUEST,不静默回落)
from jiuwenswarm.server.runtime.session.work_mode import resolve_request_work_mode
work_mode, mode_error = resolve_request_work_mode(params, channel.channel_id)
diff --git a/tests/unit_tests/test_project_handlers.py b/tests/unit_tests/test_project_handlers.py
index eea9fb06f1..1117d00007 100644
--- a/tests/unit_tests/test_project_handlers.py
+++ b/tests/unit_tests/test_project_handlers.py
@@ -117,8 +117,10 @@ def _make_project(name, project_dir, *, pinned=False, pin_order=0, hidden=False)
def _abspath(tmp_path, name):
- """平台无关的绝对路径,用于 project.create 的 isabs 校验。"""
- return str(tmp_path / name)
+ """平台无关的已存在目录绝对路径,用于 project.create 的 isabs / isdir 校验。"""
+ path = tmp_path / name
+ path.mkdir(parents=True, exist_ok=True)
+ return str(path)
# ===========================================================================
@@ -403,6 +405,18 @@ class TestProjectCreate:
)
assert resp["code"] == "BAD_REQUEST"
+ @staticmethod
+ @pytest.mark.asyncio
+ async def test_rejects_missing_existing_project_dir(registered_channel, tmp_path):
+ """传 project_dir 表示选择现有项目,目录不存在时拒绝创建项目记录。"""
+ missing = str(tmp_path / "missing")
+ resp = await _call(
+ registered_channel, "project.create", {"name": "P", "project_dir": missing}
+ )
+ assert resp["ok"] is False
+ assert resp["code"] == "PROJECT_DIR_MISSING"
+ assert resp["error"] == "project directory does not exist"
+
# ===========================================================================
# project.rename + 名称唯一性