()
| 244 | |
| 245 | |
| 246 | def main(): |
| 247 | parser = argparse.ArgumentParser( |
| 248 | description="将 raw/ 目录中的文档批量转换为 Markdown(基于 markitdown)" |
| 249 | ) |
| 250 | parser.add_argument( |
| 251 | "--dir", |
| 252 | type=str, |
| 253 | default=None, |
| 254 | help="显式扫描目录路径(覆盖 --workspace);默认按 --workspace 取 workspaces/<name>/raw/", |
| 255 | ) |
| 256 | parser.add_argument( |
| 257 | "--workspace", |
| 258 | type=str, |
| 259 | default=os.environ.get("KB_WORKSPACE", "smb-ecommerce"), |
| 260 | help=( |
| 261 | "工作区名称(在 workspaces/ 下查找),默认 smb-ecommerce;" |
| 262 | "可用环境变量 KB_WORKSPACE 覆盖。--dir 显式给出时本项被忽略" |
| 263 | ), |
| 264 | ) |
| 265 | parser.add_argument( |
| 266 | "--force", |
| 267 | action="store_true", |
| 268 | help="强制重新转换所有文件(忽略增量检查)", |
| 269 | ) |
| 270 | parser.add_argument( |
| 271 | "--dry-run", |
| 272 | action="store_true", |
| 273 | help="只列出待转换的文件,不执行转换", |
| 274 | ) |
| 275 | parser.add_argument( |
| 276 | "--ext", |
| 277 | type=str, |
| 278 | default=None, |
| 279 | help="只处理指定格式,逗号分隔(如 --ext .pdf,.docx)", |
| 280 | ) |
| 281 | args = parser.parse_args() |
| 282 | |
| 283 | # 确定扫描目录 + doc_path 基准 |
| 284 | global _BASE_ROOT |
| 285 | # 数据根:默认 = 引擎根;设 KB_ROOT 指向独立项目的数据目录(含 workspaces/), |
| 286 | # 与 k.py、web/lib/kb.ts 的 KB_ROOT 同义,让一份引擎服务多个独立项目。 |
| 287 | _kb_root_env = os.environ.get("KB_ROOT") |
| 288 | data_root = Path(_kb_root_env).expanduser().resolve() if _kb_root_env else get_project_root() |
| 289 | if args.dir: |
| 290 | # 显式 --dir:doc_path 以数据根为基准 |
| 291 | scan_dir = Path(args.dir).resolve() |
| 292 | _BASE_ROOT = data_root |
| 293 | else: |
| 294 | # 解析 workspace(对齐 k.py:workspaces/<name>,挡 ../ 穿越与不存在的名字) |
| 295 | workspaces_root = (data_root / "workspaces").resolve() |
| 296 | ws_root = (workspaces_root / args.workspace).resolve() |
| 297 | valid = [ |
| 298 | d.name for d in sorted(workspaces_root.iterdir()) |
| 299 | if d.is_dir() and not d.name.startswith(".") |
| 300 | ] if workspaces_root.is_dir() else [] |
| 301 | if not ws_root.is_dir() or ws_root.parent != workspaces_root: |
| 302 | print( |
| 303 | f"错误: 非法 --workspace {args.workspace!r}(必须是 workspaces/ 下的目录)。" |
no test coverage detected