93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
可选片段:路径与环境变量(请复制到 scripts/ 或作为独立模块使用)
|
||
================================================================
|
||
|
||
【用途】
|
||
统一解析「用户数据根」「用户 ID」「本技能私有目录」,避免在业务代码里重复 os.getenv。
|
||
|
||
【使用步骤】
|
||
1. 将本文件复制到 skills/your-slug/scripts/paths.py(或任意模块名)。
|
||
2. 把 SKILL_SLUG 改为与 SKILL.md 中 metadata.skill.slug 一致。
|
||
3. 在业务代码中: from paths import get_skill_data_dir (按实际包路径调整)
|
||
|
||
【可移植性】
|
||
- 优先读取标准名 CLAW_*(见 docs/RUNTIME.md)。
|
||
- 若你的组织在宿主侧仍使用历史变量名,可在此文件 _aliases 列表中追加 (标准名, 备选名),
|
||
由宿主注入其一即可;不要在业务里再写第三套名字。
|
||
|
||
【注意】
|
||
- 未设置 CLAW_DATA_ROOT 时的 fallback 仅适合开发机;生产环境应由宿主注入。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
|
||
# TODO: 复制本文件后改为你的 slug(与 SKILL.md metadata.skill.slug 一致)
|
||
SKILL_SLUG = "your-skill-slug"
|
||
|
||
|
||
def _getenv_first(names: tuple[str, ...]) -> str:
|
||
"""按顺序读取多个环境变量名,返回第一个非空值。"""
|
||
for n in names:
|
||
v = (os.getenv(n) or "").strip()
|
||
if v:
|
||
return v
|
||
return ""
|
||
|
||
|
||
def get_data_root() -> str:
|
||
"""
|
||
用户数据根目录。
|
||
顺序:CLAW_DATA_ROOT → (可选)宿主别名,见下方元组。
|
||
若皆空:Windows 默认 D:\\claw-data;其它系统默认 ~/.claw-data —— 仅开发便利,生产请注入 CLAW_DATA_ROOT。
|
||
"""
|
||
root = _getenv_first(
|
||
(
|
||
"CLAW_DATA_ROOT",
|
||
# 在此追加组织内别名,例如 "MYORG_USER_DATA_ROOT",
|
||
)
|
||
)
|
||
if root:
|
||
return root
|
||
if sys.platform == "win32":
|
||
return r"D:\claw-data"
|
||
return os.path.join(os.path.expanduser("~"), ".claw-data")
|
||
|
||
|
||
def get_user_id() -> str:
|
||
"""当前用户或工作空间 ID;未设置时用 _anon(仅开发)。"""
|
||
uid = _getenv_first(
|
||
(
|
||
"CLAW_USER_ID",
|
||
# 在此追加别名,例如 "MYORG_WORKSPACE_ID",
|
||
)
|
||
)
|
||
return uid or "_anon"
|
||
|
||
|
||
def get_skill_data_dir() -> str:
|
||
"""
|
||
本技能可写目录:{数据根}/{用户ID}/{skill_slug}/
|
||
会自动 os.makedirs(..., exist_ok=True)。
|
||
"""
|
||
path = os.path.join(get_data_root(), get_user_id(), SKILL_SLUG)
|
||
os.makedirs(path, exist_ok=True)
|
||
return path
|
||
|
||
|
||
def get_skills_root() -> str:
|
||
"""
|
||
可选:并列安装的多技能根目录。
|
||
未设置 CLAW_SKILLS_ROOT 时,默认使用本文件所在仓库的上一级目录的上一级
|
||
(即:.../skill-template/scripts/ → 误推断;复制后请改为 BASE_DIR 逻辑)。
|
||
"""
|
||
root = _getenv_first(("CLAW_SKILLS_ROOT",))
|
||
if root:
|
||
return root
|
||
# 模板占位:脚本位于 scripts/,仓库根为 dirname(dirname(__file__))
|
||
here = os.path.dirname(os.path.abspath(__file__))
|
||
return os.path.dirname(here)
|