#!/usr/bin/env python3
"""wrapper: 用可用 key 覆盖 search_list_op.py 的硬编码失效 key 后执行"""
import sys, runpy

# 从 ~/.hermes/config.yaml 读取可用 key
import re, os
key = None
cfg_path = os.path.expanduser('~/.hermes/config.yaml')
try:
    with open(cfg_path) as f:
        m = re.search(r'api_key:\s*(sk-[A-Za-z0-9]+)', f.read())
        if m:
            key = m.group(1)
except Exception:
    pass

if not key:
    # fallback: 0819 已验证的可用 key
    key = 'sk-00d69ca50b124474b02236121d4cf147'

# 预加载模块并注入 key
import importlib.util
spec = importlib.util.spec_from_file_location('search_list_op', '/data/news/scripts/search_list_op.py')
mod = importlib.util.module_from_spec(spec)
sys.modules['search_list_op'] = mod
spec.loader.exec_module(mod)
mod.DEEPSEEK_API_KEY = key
print(f"[wrapper] 注入可用 DeepSeek key: {key[:15]}...", file=sys.stderr)

# 用注入后的模块执行 main（保留原命令行参数）
sys.argv = ['search_list_op.py'] + sys.argv[1:]
mod.main()
