feat: 申报赛道推荐助手首版 v1.0.1(match-guide-track-default)
All checks were successful
技能自动化发布 / release (push) Successful in 6s
All checks were successful
技能自动化发布 / release (push) Successful in 6s
This commit is contained in:
12
scripts/util/argparse_zh.py
Normal file
12
scripts/util/argparse_zh.py
Normal file
@@ -0,0 +1,12 @@
|
||||
"""argparse 中文错误说明。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
class ZhArgumentParser(argparse.ArgumentParser):
|
||||
def error(self, message: str) -> None:
|
||||
print(f"参数错误:{message}\n请执行:python main.py -h 查看帮助", file=sys.stderr)
|
||||
self.exit(2)
|
||||
19
scripts/util/config_bootstrap.py
Normal file
19
scripts/util/config_bootstrap.py
Normal file
@@ -0,0 +1,19 @@
|
||||
"""技能配置初始化:.env.example 落盘与用户 .env 缺失项合并。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from jiangchang_skill_core import config
|
||||
|
||||
from util.constants import SKILL_SLUG, SKILL_VERSION
|
||||
from util.runtime_paths import get_skill_root
|
||||
|
||||
|
||||
def bootstrap_skill_config() -> str:
|
||||
"""确保用户数据目录 .env 存在,并追加 .env.example 中的新配置项。"""
|
||||
example_path = os.path.join(get_skill_root(), ".env.example")
|
||||
env_path = config.ensure_env_file(SKILL_SLUG, example_path)
|
||||
comment = f"{SKILL_SLUG} v{SKILL_VERSION}" if SKILL_VERSION else None
|
||||
config.merge_missing_env_keys(example_path, env_path, comment_skill=comment)
|
||||
return env_path
|
||||
6
scripts/util/constants.py
Normal file
6
scripts/util/constants.py
Normal file
@@ -0,0 +1,6 @@
|
||||
"""技能标识、版本与平台公共库约束(申报赛道推荐助手)。"""
|
||||
|
||||
SKILL_SLUG = "match-guide-track-default"
|
||||
SKILL_VERSION = "1.0.1"
|
||||
LOG_LOGGER_NAME = "openclaw.skill.match_guide_track_default"
|
||||
PLATFORM_KIT_MIN_VERSION = "1.2.11"
|
||||
21
scripts/util/logging_config.py
Normal file
21
scripts/util/logging_config.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Re-export unified logging (implementation: jiangchang_skill_core.unified_logging)."""
|
||||
|
||||
from jiangchang_skill_core.unified_logging import (
|
||||
attach_unified_file_handler,
|
||||
ensure_trace_for_process,
|
||||
get_skill_log_file_path,
|
||||
get_skill_logger,
|
||||
get_unified_logs_dir,
|
||||
setup_skill_logging,
|
||||
subprocess_env_with_trace,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"attach_unified_file_handler",
|
||||
"ensure_trace_for_process",
|
||||
"get_skill_log_file_path",
|
||||
"get_skill_logger",
|
||||
"get_unified_logs_dir",
|
||||
"setup_skill_logging",
|
||||
"subprocess_env_with_trace",
|
||||
]
|
||||
155
scripts/util/runtime_paths.py
Normal file
155
scripts/util/runtime_paths.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""数据根、技能目录、兄弟技能根路径与用户数据子目录解析。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from jiangchang_skill_core.runtime_env import get_data_root, get_sibling_skills_root, get_user_id
|
||||
|
||||
from util.constants import SKILL_SLUG
|
||||
|
||||
_SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# 用户可见的上行/下行目录(数据管理「输入/输出」文件浏览器)
|
||||
STANDARD_SUBDIR_INPUT = "input"
|
||||
STANDARD_SUBDIR_OUTPUT = "output"
|
||||
|
||||
# 宿主扫描的 HTML 报告页(数据管理侧栏 htmlPages,非 input/output 文件浏览器)
|
||||
STANDARD_SUBDIR_HTML = "html"
|
||||
|
||||
# 内部/可选目录(不上侧栏)
|
||||
STANDARD_SUBDIR_UPLOADS = "uploads"
|
||||
STANDARD_SUBDIR_CACHE = "cache"
|
||||
STANDARD_SUBDIR_TEMP = "temp"
|
||||
STANDARD_SUBDIR_RPA_ARTIFACTS = "rpa-artifacts"
|
||||
STANDARD_SUBDIR_VIDEOS = "videos"
|
||||
|
||||
CONFIG_KEY_INPUT_DIR = "SKILL_INPUT_DIR"
|
||||
CONFIG_KEY_OUTPUT_DIR = "SKILL_OUTPUT_DIR"
|
||||
CONFIG_KEY_UPLOAD_DIR = "SKILL_UPLOAD_DIR"
|
||||
CONFIG_KEY_CACHE_DIR = "SKILL_CACHE_DIR"
|
||||
CONFIG_KEY_TEMP_DIR = "SKILL_TEMP_DIR"
|
||||
|
||||
|
||||
def get_skill_root() -> str:
|
||||
return os.path.dirname(_SCRIPTS_DIR)
|
||||
|
||||
|
||||
def get_openclaw_root() -> str:
|
||||
return get_sibling_skills_root(_SCRIPTS_DIR)
|
||||
|
||||
|
||||
def get_skills_root() -> str:
|
||||
return get_sibling_skills_root(_SCRIPTS_DIR)
|
||||
|
||||
|
||||
def get_skill_data_dir() -> str:
|
||||
path = os.path.join(get_data_root(), get_user_id(), SKILL_SLUG)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def get_db_path(filename: str | None = None) -> str:
|
||||
name = filename or f"{SKILL_SLUG}.db"
|
||||
return os.path.join(get_skill_data_dir(), name)
|
||||
|
||||
|
||||
def _normalize_subdir(value: str) -> str:
|
||||
cleaned = value.strip().strip("/\\")
|
||||
if not cleaned or cleaned.startswith(".."):
|
||||
raise ValueError(f"invalid skill data subdir: {value!r}")
|
||||
return cleaned.replace("/", os.sep).replace("\\", os.sep)
|
||||
|
||||
|
||||
def resolve_data_path(
|
||||
config_key: str | None,
|
||||
default_subdir: str,
|
||||
*,
|
||||
create: bool = True,
|
||||
) -> str:
|
||||
"""解析技能拥有路径:空配置 → 数据目录下 default_subdir;相对 → 相对数据目录;绝对 → 原样。"""
|
||||
from jiangchang_skill_core import config
|
||||
|
||||
base = get_skill_data_dir()
|
||||
raw = (config.get(config_key) or "").strip() if config_key else ""
|
||||
if not raw:
|
||||
path = os.path.join(base, _normalize_subdir(default_subdir))
|
||||
elif os.path.isabs(raw):
|
||||
path = os.path.abspath(os.path.expanduser(raw))
|
||||
else:
|
||||
path = os.path.join(base, _normalize_subdir(raw))
|
||||
|
||||
if create:
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def resolve_input_path(raw_path: str, *, create_parent: bool = False) -> str:
|
||||
"""解析 CLI 输入路径:绝对路径原样;相对路径相对 {skill_data_dir}/input/。"""
|
||||
text = (raw_path or "").strip()
|
||||
if not text:
|
||||
return text
|
||||
expanded = os.path.expanduser(text)
|
||||
if os.path.isabs(expanded):
|
||||
return os.path.abspath(expanded)
|
||||
input_dir = get_input_dir(create=create_parent)
|
||||
return os.path.abspath(os.path.join(input_dir, os.path.normpath(expanded)))
|
||||
|
||||
|
||||
def get_input_dir(*, create: bool = True) -> str:
|
||||
"""上行:用户/Agent 放入的待处理文件(清单、CSV、产品表等)。"""
|
||||
return resolve_data_path(CONFIG_KEY_INPUT_DIR, STANDARD_SUBDIR_INPUT, create=create)
|
||||
|
||||
|
||||
def get_output_dir(*, create: bool = True) -> str:
|
||||
"""下行:技能/宿主交给用户拿走的文件(视频、报表、导出 CSV 等)。"""
|
||||
return resolve_data_path(CONFIG_KEY_OUTPUT_DIR, STANDARD_SUBDIR_OUTPUT, create=create)
|
||||
|
||||
|
||||
def get_uploads_dir(*, create: bool = True) -> str:
|
||||
return resolve_data_path(CONFIG_KEY_UPLOAD_DIR, STANDARD_SUBDIR_UPLOADS, create=create)
|
||||
|
||||
|
||||
def get_cache_dir(*, create: bool = True) -> str:
|
||||
return resolve_data_path(CONFIG_KEY_CACHE_DIR, STANDARD_SUBDIR_CACHE, create=create)
|
||||
|
||||
|
||||
def get_temp_dir(*, create: bool = True) -> str:
|
||||
return resolve_data_path(CONFIG_KEY_TEMP_DIR, STANDARD_SUBDIR_TEMP, create=create)
|
||||
|
||||
|
||||
def get_rpa_artifacts_dir(batch_id: str, *, create: bool = True) -> str:
|
||||
batch = (batch_id or "").strip() or "default"
|
||||
path = os.path.join(get_skill_data_dir(), STANDARD_SUBDIR_RPA_ARTIFACTS, batch)
|
||||
if create:
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def get_videos_dir(*, create: bool = True) -> str:
|
||||
path = os.path.join(get_skill_data_dir(), STANDARD_SUBDIR_VIDEOS)
|
||||
if create:
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def get_html_dir(*, create: bool = True) -> str:
|
||||
"""HTML 报告页目录:宿主扫描 `{skill_data}/html/*.html`(一层、非递归)。"""
|
||||
path = os.path.join(get_skill_data_dir(), STANDARD_SUBDIR_HTML)
|
||||
if create:
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def list_resolved_data_paths() -> dict[str, str]:
|
||||
"""供 health / config-path 输出已解析的数据子目录(不创建目录)。"""
|
||||
return {
|
||||
"skill_data_dir": get_skill_data_dir(),
|
||||
"input_dir": get_input_dir(create=False),
|
||||
"output_dir": get_output_dir(create=False),
|
||||
"html_dir": get_html_dir(create=False),
|
||||
"uploads_dir": get_uploads_dir(create=False),
|
||||
"cache_dir": get_cache_dir(create=False),
|
||||
"temp_dir": get_temp_dir(create=False),
|
||||
"videos_dir": get_videos_dir(create=False),
|
||||
}
|
||||
227
scripts/util/slug_naming.py
Normal file
227
scripts/util/slug_naming.py
Normal file
@@ -0,0 +1,227 @@
|
||||
"""技能 slug 语义校验(verb-noun-platform 规范,见 development/NAMING.md)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
_KEBAB_SLUG = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||
_MAX_SLUG_LENGTH = 48
|
||||
_MIN_SEGMENTS = 3
|
||||
_MAX_SEGMENTS = 5
|
||||
_MAX_NOUN_SEGMENTS = 3
|
||||
|
||||
_SKILL_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
_NAMING_DIR = os.path.join(_SKILL_ROOT, "assets", "naming")
|
||||
|
||||
_BUILTIN_VERBS = frozenset(
|
||||
{
|
||||
"scrape",
|
||||
"receive",
|
||||
"download",
|
||||
"export",
|
||||
"add",
|
||||
"fill",
|
||||
"enter",
|
||||
"bind",
|
||||
"withdraw",
|
||||
"reconcile",
|
||||
"verify",
|
||||
"submit",
|
||||
"query",
|
||||
"process",
|
||||
"reply",
|
||||
"generate",
|
||||
"draft",
|
||||
"review",
|
||||
"confirm",
|
||||
"track",
|
||||
"upload",
|
||||
"match",
|
||||
"triage",
|
||||
"ship",
|
||||
"reprice",
|
||||
}
|
||||
)
|
||||
_BUILTIN_PLATFORMS = frozenset(
|
||||
{
|
||||
"amazon",
|
||||
"shopee",
|
||||
"xinghang",
|
||||
"alibaba",
|
||||
"kingdee",
|
||||
"icbc",
|
||||
"pingpong",
|
||||
"worldfirst",
|
||||
"paypal",
|
||||
"lianlian",
|
||||
"single-window",
|
||||
"export-rebate",
|
||||
"etax",
|
||||
}
|
||||
)
|
||||
_BUILTIN_SCOPES = frozenset({"all", "batch", "multi", "default"})
|
||||
|
||||
LEGACY_EXEMPT_SLUGS: frozenset[str] = frozenset(
|
||||
{
|
||||
"account-manager",
|
||||
"skill-template",
|
||||
"your-skill-slug",
|
||||
"your_skill_slug",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def load_wordlist(filename: str) -> frozenset[str]:
|
||||
"""从 assets/naming/{filename} 加载词表;文件不存在时 fallback 内置集合。"""
|
||||
fallbacks = {
|
||||
"verbs.txt": _BUILTIN_VERBS,
|
||||
"platforms.txt": _BUILTIN_PLATFORMS,
|
||||
"scopes.txt": _BUILTIN_SCOPES,
|
||||
}
|
||||
fallback = fallbacks.get(filename, frozenset())
|
||||
path = os.path.join(_NAMING_DIR, filename)
|
||||
if not os.path.isfile(path):
|
||||
return fallback
|
||||
words: set[str] = set()
|
||||
with open(path, encoding="utf-8") as f:
|
||||
for line in f:
|
||||
word = line.strip()
|
||||
if word and not word.startswith("#"):
|
||||
words.add(word)
|
||||
return frozenset(words) if words else fallback
|
||||
|
||||
|
||||
def parse_slug_segments(slug: str) -> list[str]:
|
||||
"""按 '-' 分割;非法字符由上层 kebab 校验处理。"""
|
||||
slug = slug.strip()
|
||||
if not slug:
|
||||
return []
|
||||
return slug.split("-")
|
||||
|
||||
|
||||
def _match_platform_suffix(
|
||||
segments: list[str],
|
||||
platforms: frozenset[str],
|
||||
) -> tuple[str, int] | None:
|
||||
"""返回 (platform_name, platform_part_count) 或 None。"""
|
||||
for platform in sorted(platforms, key=lambda item: (-item.count("-"), item)):
|
||||
parts = platform.split("-")
|
||||
part_count = len(parts)
|
||||
if len(segments) < part_count + 2:
|
||||
continue
|
||||
if segments[-part_count:] == parts:
|
||||
return platform, part_count
|
||||
return None
|
||||
|
||||
|
||||
def classify_slug_form(segments: list[str], *, slug: str = "") -> str:
|
||||
"""
|
||||
返回: 'legacy_exempt' | 'standard' | 'scope' | 'invalid'
|
||||
|
||||
- legacy_exempt: slug in LEGACY_EXEMPT_SLUGS
|
||||
- standard: 末段匹配 platforms, 首段 in verbs, 3<=len<=5
|
||||
- scope: 末段 in scopes, 首段 in verbs, 3<=len<=5
|
||||
- invalid: 其他
|
||||
"""
|
||||
if slug in LEGACY_EXEMPT_SLUGS:
|
||||
return "legacy_exempt"
|
||||
|
||||
if not segments or not (_MIN_SEGMENTS <= len(segments) <= _MAX_SEGMENTS):
|
||||
return "invalid"
|
||||
|
||||
verbs = load_wordlist("verbs.txt")
|
||||
platforms = load_wordlist("platforms.txt")
|
||||
scopes = load_wordlist("scopes.txt")
|
||||
|
||||
if segments[0] not in verbs:
|
||||
return "invalid"
|
||||
|
||||
if segments[-1] in scopes:
|
||||
return "scope"
|
||||
|
||||
if _match_platform_suffix(segments, platforms) is not None:
|
||||
return "standard"
|
||||
|
||||
return "invalid"
|
||||
|
||||
|
||||
def validate_slug_semantics(
|
||||
slug: str,
|
||||
*,
|
||||
strict: bool = True,
|
||||
) -> tuple[list[str], list[str]]:
|
||||
"""返回 (errors, warnings)。"""
|
||||
slug = slug.strip()
|
||||
if slug in LEGACY_EXEMPT_SLUGS:
|
||||
return [], []
|
||||
|
||||
errors: list[str] = []
|
||||
warnings: list[str] = []
|
||||
|
||||
if not _KEBAB_SLUG.fullmatch(slug):
|
||||
msg = f"slug 必须为 kebab-case(小写字母、数字、连字符):{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
return errors, warnings
|
||||
|
||||
if len(slug) > _MAX_SLUG_LENGTH:
|
||||
msg = f"slug 长度不得超过 {_MAX_SLUG_LENGTH} 字符(当前 {len(slug)}):{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
segments = parse_slug_segments(slug)
|
||||
segment_count = len(segments)
|
||||
|
||||
if segment_count < _MIN_SEGMENTS:
|
||||
msg = (
|
||||
f"slug 段数应为 {_MIN_SEGMENTS}~{_MAX_SEGMENTS}(verb-noun-platform),"
|
||||
f"当前为 {segment_count} 段:{slug!r};新技能禁止 2 段无平台形态(如 reconcile-finance)"
|
||||
)
|
||||
(errors if strict else warnings).append(msg)
|
||||
return errors, warnings
|
||||
|
||||
if segment_count > _MAX_SEGMENTS:
|
||||
msg = f"slug 段数不得超过 {_MAX_SEGMENTS}(当前 {segment_count}):{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
verbs = load_wordlist("verbs.txt")
|
||||
platforms = load_wordlist("platforms.txt")
|
||||
scopes = load_wordlist("scopes.txt")
|
||||
|
||||
if segments[0] in platforms and segments[0] not in verbs:
|
||||
msg = f"平台名不应放在 slug 最前:{slug!r}(应为 verb-noun-platform)"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
if segments[0] not in verbs:
|
||||
msg = f"slug 首段应为动词白名单中的词:{segments[0]!r}(slug={slug!r})"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
form = classify_slug_form(segments, slug=slug)
|
||||
if form == "scope":
|
||||
if segments[-1] not in scopes:
|
||||
msg = f"slug 末段应为 scope 白名单中的词:{segments[-1]!r}(slug={slug!r})"
|
||||
(errors if strict else warnings).append(msg)
|
||||
noun_count = segment_count - 2
|
||||
elif form == "standard":
|
||||
platform_match = _match_platform_suffix(segments, platforms)
|
||||
if platform_match is None:
|
||||
msg = f"slug 末段应匹配平台白名单:{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
noun_count = 0
|
||||
else:
|
||||
_platform_name, platform_part_count = platform_match
|
||||
noun_count = segment_count - 1 - platform_part_count
|
||||
else:
|
||||
if segment_count >= _MIN_SEGMENTS:
|
||||
if segments[-1] not in scopes:
|
||||
platform_match = _match_platform_suffix(segments, platforms)
|
||||
if platform_match is None:
|
||||
msg = f"slug 末段应匹配平台或 scope 白名单:{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
noun_count = 0
|
||||
|
||||
if noun_count > _MAX_NOUN_SEGMENTS:
|
||||
warnings.append(
|
||||
f"slug 中间名词段建议不超过 {_MAX_NOUN_SEGMENTS} 个词(当前 {noun_count}):{slug!r}"
|
||||
)
|
||||
|
||||
return errors, warnings
|
||||
20
scripts/util/timeutil.py
Normal file
20
scripts/util/timeutil.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""时间戳与 ISO 展示。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def now_unix() -> int:
|
||||
return int(time.time())
|
||||
|
||||
|
||||
def unix_to_iso(ts: Optional[int]) -> Optional[str]:
|
||||
if ts is None:
|
||||
return None
|
||||
try:
|
||||
return datetime.fromtimestamp(int(ts)).isoformat(timespec="seconds")
|
||||
except (ValueError, OSError, OverflowError):
|
||||
return None
|
||||
Reference in New Issue
Block a user