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

feat: 统一管理config和env

Haowei_Du513645f22166
diff --git a/build.ps1 b/build.ps1
index 8f102f742c..8de162a4dd 100644
--- a/build.ps1
+++ b/build.ps1
@@ -44,18 +44,10 @@ if (Test-Path $NodeModules) {
$NodeModulesMoved = $true
}
-# 创建符号链接,让 config 和 workspace 在 jiuwenclaw 包内
+# 创建符号链接,让 workspace 和入口脚本在 jiuwenclaw 包内
$SymlinksRemoved = @()
$JiuwenclawDir = Join-Path $ProjectRoot "jiuwenclaw"
-$ConfigLink = Join-Path $JiuwenclawDir "config"
-if (-not (Test-Path $ConfigLink)) {
- Write-Host "[build] 创建 config 符号链接..." -ForegroundColor Gray
- $ConfigSource = Join-Path $ProjectRoot "config"
- New-Item -ItemType SymbolicLink -Path $ConfigLink -Target $ConfigSource | Out-Null
- $SymlinksRemoved += $ConfigLink
-}
-
$WorkspaceLink = Join-Path $JiuwenclawDir "workspace"
if (-not (Test-Path $WorkspaceLink)) {
Write-Host "[build] 创建 workspace 符号链接..." -ForegroundColor Gray
diff --git a/build.sh b/build.sh
index a7a36790b1..f229c58ef3 100644
--- a/build.sh
+++ b/build.sh
@@ -41,17 +41,10 @@ if [[ -d "$NODE_MODULES" ]]; then
NODE_MODULES_MOVED=true
fi
-# 创建符号链接,让 config 和 workspace 在 jiuwenclaw 包内
+# 创建符号链接,让 workspace 和入口脚本在 jiuwenclaw 包内
SYMLINKS_REMOVED=()
JIUWENCLAW_DIR="$PROJECT_ROOT/jiuwenclaw"
-CONFIG_LINK="$JIUWENCLAW_DIR/config"
-if [[ ! -e "$CONFIG_LINK" ]]; then
- echo "[build] 创建 config 符号链接..."
- ln -s "$PROJECT_ROOT/config" "$CONFIG_LINK"
- SYMLINKS_REMOVED+=("$CONFIG_LINK")
-fi
-
WORKSPACE_LINK="$JIUWENCLAW_DIR/workspace"
if [[ ! -e "$WORKSPACE_LINK" ]]; then
echo "[build] 创建 workspace 符号链接..."
diff --git a/jiuwenclaw/.env.template b/jiuwenclaw/.env.template
deleted file mode 100644
index 1eaf8fa630..0000000000
--- a/jiuwenclaw/.env.template
+++ /dev/null
@@ -1,52 +0,0 @@
-API_BASE="https://example.com/compatible-mode/v1"
-API_KEY="sk-xxxxxxxxx"
-MODEL_NAME="your-model-name"
-MODEL_PROVIDER=OpenAI # or other provider like SiliconFlow, etc.
-
-EMBED_API_BASE=
-EMBED_API_KEY=
-EMBED_MODEL=
-
-JINA_API_KEY=
-SERPER_API_KEY=
-PERPLEXITY_API_KEY=
-
-# Browser MCP runtime (optional)
-# 0: disabled, 1: enabled
-BROWSER_RUNTIME_MCP_ENABLED=1
-# supported here: stdio|sse|streamable-http
-# recommended: streamable-http
-BROWSER_RUNTIME_MCP_CLIENT_TYPE=streamable-http
-BROWSER_RUNTIME_MCP_SERVER_ID=playwright_runtime_wrapper
-BROWSER_RUNTIME_MCP_SERVER_NAME=playwright-runtime-wrapper
-# when client_type=streamable-http, default endpoint is http://127.0.0.1:8940/mcp
-# when client_type=sse, default endpoint is http://127.0.0.1:8940/sse
-# when client_type=stdio, this path can keep stdio://... as metadata only
-BROWSER_RUNTIME_MCP_SERVER_PATH=http://127.0.0.1:8940/mcp
-BROWSER_RUNTIME_MCP_TIMEOUT_S=300
-# optional local wrapper host/port/path for auto-start
-BROWSER_RUNTIME_MCP_HOST=127.0.0.1
-BROWSER_RUNTIME_MCP_PORT=8940
-BROWSER_RUNTIME_MCP_PATH=/mcp
-# stdio mode command override (empty means use current python executable)
-BROWSER_RUNTIME_MCP_COMMAND=
-# optional args override; leave empty to use built-in default:
-# <repo>/jiuwenclaw/agentserver/tools/browser-move/src/playwright_runtime_mcp_server.py --transport stdio --no-banner
-BROWSER_RUNTIME_MCP_ARGS=
-# stdio mode will prefer sse fallback first when enabled (recommended 1)
-BROWSER_RUNTIME_MCP_AUTO_SSE_FALLBACK=1
-
-# runtime wrapper internals
-PLAYWRIGHT_MCP_COMMAND=npx
-PLAYWRIGHT_MCP_ARGS=-y @playwright/mcp@latest
-PLAYWRIGHT_CDP_URL=http://127.0.0.1:9222
-PLAYWRIGHT_TOOL_TIMEOUT_S=300
-BROWSER_TIMEOUT_S=300
-# default behavior: browser_run_task will not honor model-provided timeout_s values below
-# BROWSER_TIMEOUT_S. Set this only if you explicitly want short per-call overrides.
-BROWSER_ALLOW_SHORT_TIMEOUT_OVERRIDE=0
-
-EMAIL_ADDRESS=""
-EMAIL_TOKEN=""
-
-EVOLUTION_AUTO_SCAN="false"
diff --git a/config/config.py b/jiuwenclaw/config.py
similarity index 100%
rename from config/config.py
rename to jiuwenclaw/config.py
diff --git a/jiuwenclaw/init_workspace.py b/jiuwenclaw/init_workspace.py
index b54c6ebe6c..59975329e9 100644
--- a/jiuwenclaw/init_workspace.py
+++ b/jiuwenclaw/init_workspace.py
@@ -1,10 +1,16 @@
-"""CLI for initializing package data into ~/.jiuwenclaw."""
+"""CLI for initializing runtime data into ~/.jiuwenclaw.
+
+无论是通过 pip/whl 安装,还是在源码目录里直接运行:
+- 都可以通过运行本脚本,把内置的 config.yaml / config.py / .env.template
+ 以及 workspace 模板复制到用户主目录 ~/.jiuwenclaw 下,
+ 之后运行 app.py 时会从该目录读取和写入配置。
+"""
from __future__ import annotations
import logging
-from jiuwenclaw.paths import init_user_workspace, is_package_installation
+from jiuwenclaw.paths import init_user_workspace
def main() -> None:
@@ -13,9 +19,6 @@ def main() -> None:
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
- if not is_package_installation():
- raise SystemExit("jiuwenclaw-init 仅支持 pip/whl 安装模式")
-
target = init_user_workspace(overwrite=True)
print(f"[jiuwenclaw-init] initialized: {target}")
diff --git a/jiuwenclaw/paths.py b/jiuwenclaw/paths.py
index a62b7a6e22..314bf9abde 100644
--- a/jiuwenclaw/paths.py
+++ b/jiuwenclaw/paths.py
@@ -3,8 +3,13 @@
"""Path management for JiuWenClaw.
Handles path resolution for both source and package installations:
-- Source: Use project root directory for config and workspace
-- Package (whl): Use user home directory (~/.jiuwenclaw) as runtime data root
+
+- When a user workspace exists (~/.jiuwenclaw), it is always used as the
+ runtime root for config, workspace and .env (for both source and package).
+- Before the user workspace is initialized:
+ - Source: Use project root directory for config and workspace.
+ - Package (whl): Use user home directory (~/.jiuwenclaw) as planned
+ runtime root, but templates are copied from installed package resources.
"""
import importlib.util
@@ -65,61 +70,104 @@ def _find_source_root() -> Path:
def _find_package_root() -> Path | None:
- """Find the package root directory containing config and workspace."""
- # In package mode (whl), __file__ is at site-packages/jiuwenclaw/paths.py
- # So parent is site-packages/jiuwenclaw/, which contains config and workspace
- current = Path(__file__).resolve().parent
-
- # Check if config and workspace exist at this level
- if (current / "config").exists() and (current / "workspace").exists():
- return current
+ """Best-effort detection of the jiuwenclaw package root.
- return None
+ In package mode (whl), __file__ is at site-packages/jiuwenclaw/paths.py,
+ so parent is site-packages/jiuwenclaw/.
+ In editable / source mode, __file__ is at <project>/jiuwenclaw/paths.py,
+ so parent is <project>/jiuwenclaw/.
+ """
+ current = Path(__file__).resolve().parent
+ return current
def init_user_workspace(overwrite: bool = True) -> Path:
- """Initialize ~/.jiuwenclaw from package resources.
+ """Initialize ~/.jiuwenclaw from package or source resources.
- Args:
- overwrite: When True, overwrite existing files with package defaults.
+ 资源布局(新):
+ - 模板配置: <package_root>/resources/config.yaml
+ - 模块实现: <package_root>/config.py
+ - .env 模板: <package_root>/resources/.env.template
+ - workspace: 优先 <package_root>/workspace,其次 <package_root>/../workspace
- Returns:
- Path to user workspace root (~/.jiuwenclaw).
+ 上述内容会被复制到:
+ - ~/.jiuwenclaw/config/config.yaml
+ - ~/.jiuwenclaw/config/config.py
+ - ~/.jiuwenclaw/.env
+ - ~/.jiuwenclaw/workspace/...
- Raises:
- RuntimeError: If not in package mode or package resources are missing.
+ 无论是通过 pip/whl 安装还是源码目录直接运行,效果保持一致。
"""
- if not _detect_installation_mode():
- raise RuntimeError("jiuwenclaw-init is only available in package installation mode")
-
package_root = _find_package_root()
if not package_root:
raise RuntimeError("package root not found")
USER_WORKSPACE_DIR.mkdir(parents=True, exist_ok=True)
- config_src = package_root / "config"
- config_dest = USER_WORKSPACE_DIR / "config"
- if not config_src.exists():
- raise RuntimeError(f"config source not found: {config_src}")
- if overwrite:
- shutil.copytree(config_src, config_dest, dirs_exist_ok=True)
- elif not config_dest.exists():
- shutil.copytree(config_src, config_dest)
-
- workspace_src = package_root / "workspace"
+ # ----- config: copy config.yaml + config.py -----
+ resources_dir = package_root / "resources"
+ config_yaml_src_candidates = [
+ resources_dir / "config.yaml",
+ package_root / "config" / "config.yaml",
+ ]
+ config_py_src_candidates = [
+ package_root / "config.py",
+ package_root / "config" / "config.py",
+ ]
+
+ config_yaml_src = next((p for p in config_yaml_src_candidates if p.exists()), None)
+ config_py_src = next((p for p in config_py_src_candidates if p.exists()), None)
+
+ if not config_yaml_src:
+ raise RuntimeError(
+ "config.yaml template not found; tried: "
+ + ", ".join(str(p) for p in config_yaml_src_candidates)
+ )
+ if not config_py_src:
+ raise RuntimeError(
+ "config.py source not found; tried: "
+ + ", ".join(str(p) for p in config_py_src_candidates)
+ )
+
+ config_dest_dir = USER_WORKSPACE_DIR / "config"
+ config_dest_dir.mkdir(parents=True, exist_ok=True)
+ config_yaml_dest = config_dest_dir / "config.yaml"
+ config_py_dest = config_dest_dir / "config.py"
+
+ if overwrite or not config_yaml_dest.exists():
+ shutil.copy2(config_yaml_src, config_yaml_dest)
+ if overwrite or not config_py_dest.exists():
+ shutil.copy2(config_py_src, config_py_dest)
+
+ # ----- workspace: copy tree -----
+ workspace_src_candidates = [
+ package_root / "workspace",
+ package_root.parent / "workspace",
+ ]
+ workspace_src = next((p for p in workspace_src_candidates if p.exists()), None)
+ if not workspace_src:
+ raise RuntimeError(
+ "workspace source not found; tried: "
+ + ", ".join(str(p) for p in workspace_src_candidates)
+ )
workspace_dest = USER_WORKSPACE_DIR / "workspace"
- if not workspace_src.exists():
- raise RuntimeError(f"workspace source not found: {workspace_src}")
if overwrite:
shutil.copytree(workspace_src, workspace_dest, dirs_exist_ok=True)
elif not workspace_dest.exists():
shutil.copytree(workspace_src, workspace_dest)
- env_template_src = package_root / ".env.template"
+ # ----- .env: copy from template -----
+ env_template_src_candidates = [
+ resources_dir / ".env.template",
+ package_root / ".env.template",
+ ]
+ env_template_src = next((p for p in env_template_src_candidates if p.exists()), None)
+ if not env_template_src:
+ raise RuntimeError(
+ "env template source not found; tried: "
+ + ", ".join(str(p) for p in env_template_src_candidates)
+ )
env_dest = USER_WORKSPACE_DIR / ".env"
- if not env_template_src.exists():
- raise RuntimeError(f"env template source not found: {env_template_src}")
if overwrite or not env_dest.exists():
shutil.copy2(env_template_src, env_dest)
@@ -133,25 +181,35 @@ def _resolve_paths() -> None:
if _initialized:
return
- if _detect_installation_mode():
- # Package mode
- package_root = _find_package_root()
- if package_root:
- _root_dir = USER_WORKSPACE_DIR
- _config_dir = USER_WORKSPACE_DIR / "config"
- _workspace_dir = USER_WORKSPACE_DIR / "workspace"
+ # 优先使用已初始化的用户工作区 (~/.jiuwenclaw),
+ # 保证源码运行与安装包运行后的读写路径完全一致。
+ user_config_dir = USER_WORKSPACE_DIR / "config"
+ user_workspace_dir = USER_WORKSPACE_DIR / "workspace"
+ if user_config_dir.exists():
+ _root_dir = USER_WORKSPACE_DIR
+ _config_dir = user_config_dir
+ _workspace_dir = user_workspace_dir
+ else:
+ if _detect_installation_mode():
+ # Package mode(尚未执行 init),计划根目录仍是用户家目录,
+ # 但 config/workspace 可能还不存在。
+ package_root = _find_package_root()
+ if package_root:
+ _root_dir = USER_WORKSPACE_DIR
+ _config_dir = USER_WORKSPACE_DIR / "config"
+ _workspace_dir = USER_WORKSPACE_DIR / "workspace"
+ else:
+ logger.warning("Could not find package root, falling back to source mode")
+ source_root = _find_source_root()
+ _root_dir = source_root
+ _config_dir = source_root / "config"
+ _workspace_dir = source_root / "workspace"
else:
- logger.warning("Could not find package root, falling back to source mode")
+ # 纯源码模式且未初始化用户工作区:直接使用工程根目录。
source_root = _find_source_root()
_root_dir = source_root
_config_dir = source_root / "config"
_workspace_dir = source_root / "workspace"
- else:
- # Source mode
- source_root = _find_source_root()
- _root_dir = source_root
- _config_dir = source_root / "config"
- _workspace_dir = source_root / "workspace"
_initialized = True
diff --git a/.env.template b/jiuwenclaw/resources/.env.template
similarity index 100%
rename from .env.template
rename to jiuwenclaw/resources/.env.template
diff --git a/config/config.yaml b/jiuwenclaw/resources/config.yaml
similarity index 100%
rename from config/config.yaml
rename to jiuwenclaw/resources/config.yaml
diff --git a/jiuwenclaw/web/package-lock.json b/jiuwenclaw/web/package-lock.json
index eaae7f29dc..76cbb37919 100644
--- a/jiuwenclaw/web/package-lock.json
+++ b/jiuwenclaw/web/package-lock.json
@@ -76,7 +76,6 @@
"integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==",
"dev": true,
"license": "MIT",
- "peer": true,
"dependencies": {
"@babel/code-frame": "^7.29.0",
"@babel/generator": "^7.29.0",
@@ -1457,7 +1456,6 @@
"integrity": "sha512-m0jEgYlYz+mDJZ2+F4v8D1AyQb+QzsNqRuI7xg1VQX/KlKS0qT9r1Mo16yo5F/MtifXFgaofIFsdFMox2SxIbQ==",
"dev": true,
"license": "MIT",
- "peer": true,
"dependencies": {
"undici-types": "~7.16.0"
}
@@ -1473,7 +1471,6 @@
"resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.27.tgz",
"integrity": "sha512-cisd7gxkzjBKU2GgdYrTdtQx1SORymWyaAFhaxQPK9bYO9ot3Y5OikQRvY0VYQtvwjeQnizCINJAenh/V7MK2w==",
"license": "MIT",
- "peer": true,
"dependencies": {
"@types/prop-types": "*",
"csstype": "^3.2.2"
@@ -1544,7 +1541,6 @@
"integrity": "sha512-tbsV1jPne5CkFQCgPBcDOt30ItF7aJoZL997JSF7MhGQqOeT3svWRYxiqlfA5RUdlHN6Fi+EI9bxqbdyAUZjYQ==",
"dev": true,
"license": "BSD-2-Clause",
- "peer": true,
"dependencies": {
"@typescript-eslint/scope-manager": "6.21.0",
"@typescript-eslint/types": "6.21.0",
@@ -1734,7 +1730,6 @@
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
"dev": true,
"license": "MIT",
- "peer": true,
"bin": {
"acorn": "bin/acorn"
},
@@ -1955,7 +1950,6 @@
}
],
"license": "MIT",
- "peer": true,
"dependencies": {
"baseline-browser-mapping": "^2.9.0",
"caniuse-lite": "^1.0.30001759",
@@ -2381,7 +2375,6 @@
"deprecated": "This version is no longer supported. Please see https://eslint.org/version-support for other options.",
"dev": true,
"license": "MIT",
- "peer": true,
"dependencies": {
"@eslint-community/eslint-utils": "^4.2.0",
"@eslint-community/regexpp": "^4.6.1",
@@ -3136,7 +3129,6 @@
"resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.7.tgz",
"integrity": "sha512-/imKNG4EbWNrVjoNC/1H5/9GFy+tqjGBHCaSsN+P2RnPqjsLmv6UD3Ej+Kj8nBWaRAwyk7kK5ZUc+OEatnTR3A==",
"license": "MIT",
- "peer": true,
"bin": {
"jiti": "bin/jiti.js"
}
@@ -4472,7 +4464,6 @@
}
],
"license": "MIT",
- "peer": true,
"dependencies": {
"nanoid": "^3.3.11",
"picocolors": "^1.1.1",
@@ -4665,7 +4656,6 @@
"resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz",
"integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==",
"license": "MIT",
- "peer": true,
"dependencies": {
"loose-envify": "^1.1.0"
},
@@ -5119,7 +5109,6 @@
"resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.19.tgz",
"integrity": "sha512-3ofp+LL8E+pK/JuPLPggVAIaEuhvIz4qNcf3nA1Xn2o/7fb7s/TYpHhwGDv1ZU3PkBluUVaF8PyCHcm48cKLWQ==",
"license": "MIT",
- "peer": true,
"dependencies": {
"@alloc/quick-lru": "^5.2.0",
"arg": "^5.0.2",
@@ -5218,7 +5207,6 @@
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.3.tgz",
"integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==",
"license": "MIT",
- "peer": true,
"engines": {
"node": ">=12"
},
@@ -5309,7 +5297,6 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
- "peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@@ -5502,7 +5489,6 @@
"integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==",
"dev": true,
"license": "MIT",
- "peer": true,
"dependencies": {
"esbuild": "^0.21.3",
"postcss": "^8.4.43",
diff --git a/pyproject.toml b/pyproject.toml
index 827e9ffb26..f49344434a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -34,7 +34,16 @@ where = ["."]
include = ["jiuwenclaw*"]
[tool.setuptools.package-data]
-jiuwenclaw = ["web/dist/**/*", "web/dist/*", "config/**/*", "config/*", "workspace/**/*", "workspace/*", ".env.template"]
+jiuwenclaw = [
+ "web/dist/**/*",
+ "web/dist/*",
+ # config / env 模板放在 resources 目录
+ "resources/**/*",
+ "resources/*",
+ "resources/.env.template",
+ "workspace/**/*",
+ "workspace/*",
+]
[tool.setuptools.exclude-package-data]
jiuwenclaw = ["web/node_modules/**", "web/node_modules/*"]