fix(groq): use Config class instead of hardcoded config.json (#136)
Config class writes YAML (config.yaml), but xiaoyuzhou.py, cli.py, and transcribe.sh were hardcoded to read config.json (JSON format). Users who configured groq-key via 'agent-reach configure' would not have their key detected because the wrong file was being read. Fixes #128 (related to config loading) Co-authored-by: Panniantong <panniantong@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
from agent_reach.config import Config
|
||||||
from .base import Channel
|
from .base import Channel
|
||||||
|
|
||||||
|
|
||||||
@@ -35,18 +36,14 @@ class XiaoyuzhouChannel(Channel):
|
|||||||
" 或手动复制 transcribe.sh 到 ~/.agent-reach/tools/xiaoyuzhou/"
|
" 或手动复制 transcribe.sh 到 ~/.agent-reach/tools/xiaoyuzhou/"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check GROQ_API_KEY — prefer env var, fall back to config file
|
# Check GROQ_API_KEY — prefer env var, fall back to Agent Reach config
|
||||||
has_key = bool(os.environ.get("GROQ_API_KEY"))
|
has_key = bool(os.environ.get("GROQ_API_KEY"))
|
||||||
if not has_key:
|
if not has_key:
|
||||||
config_path = os.path.expanduser("~/.agent-reach/config.json")
|
try:
|
||||||
if os.path.isfile(config_path):
|
cfg = config if config is not None else Config()
|
||||||
try:
|
has_key = bool(cfg.get("groq_api_key"))
|
||||||
import json
|
except Exception:
|
||||||
with open(config_path) as f:
|
has_key = False
|
||||||
cfg = json.load(f)
|
|
||||||
has_key = bool(cfg.get("groq_api_key"))
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not has_key:
|
if not has_key:
|
||||||
return "warn", (
|
return "warn", (
|
||||||
"需要配置 Groq API Key(免费)。步骤:\n"
|
"需要配置 Groq API Key(免费)。步骤:\n"
|
||||||
|
|||||||
+1
-9
@@ -490,15 +490,7 @@ def _install_xiaoyuzhou_deps():
|
|||||||
print(" -- ffmpeg not found. Install: apt install -y ffmpeg (or brew install ffmpeg)")
|
print(" -- ffmpeg not found. Install: apt install -y ffmpeg (or brew install ffmpeg)")
|
||||||
|
|
||||||
# Check GROQ_API_KEY
|
# Check GROQ_API_KEY
|
||||||
config_path = os.path.expanduser("~/.agent-reach/config.json")
|
has_key = bool(os.environ.get("GROQ_API_KEY")) or bool(config.get("groq_api_key"))
|
||||||
has_key = bool(os.environ.get("GROQ_API_KEY"))
|
|
||||||
if not has_key and os.path.isfile(config_path):
|
|
||||||
try:
|
|
||||||
with open(config_path) as f:
|
|
||||||
cfg = json.load(f)
|
|
||||||
has_key = bool(cfg.get("groq_api_key"))
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if has_key:
|
if has_key:
|
||||||
print(" ✅ Groq API key configured")
|
print(" ✅ Groq API key configured")
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ URL="${1:?用法: bash transcribe.sh <小宇宙链接> [输出文件路径]}"
|
|||||||
OUTPUT="${2:-/tmp/podcast_transcript.txt}"
|
OUTPUT="${2:-/tmp/podcast_transcript.txt}"
|
||||||
TMPDIR="/tmp/xiaoyuzhou_$$"
|
TMPDIR="/tmp/xiaoyuzhou_$$"
|
||||||
|
|
||||||
# Try env var first, then agent-reach config
|
# Try env var first, then agent-reach config.yaml
|
||||||
if [ -z "$GROQ_API_KEY" ]; then
|
if [ -z "$GROQ_API_KEY" ]; then
|
||||||
CONFIG_FILE="$HOME/.agent-reach/config.json"
|
CONFIG_FILE="$HOME/.agent-reach/config.yaml"
|
||||||
if [ -f "$CONFIG_FILE" ]; then
|
if [ -f "$CONFIG_FILE" ]; then
|
||||||
GROQ_API_KEY=$(python3 -c "import json; print(json.load(open('$CONFIG_FILE')).get('groq_api_key',''))" 2>/dev/null || true)
|
GROQ_API_KEY=$(python3 -c "import yaml; print((yaml.safe_load(open('$CONFIG_FILE')) or {}).get('groq_api_key',''))" 2>/dev/null || true)
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
GROQ_API_KEY="${GROQ_API_KEY:?请设置 GROQ_API_KEY 环境变量或运行 agent-reach configure groq-key}"
|
GROQ_API_KEY="${GROQ_API_KEY:?请设置 GROQ_API_KEY 环境变量或运行 agent-reach configure groq-key}"
|
||||||
|
|||||||
Reference in New Issue
Block a user