feat: LLM 解耦 bot、RSS 外置与内容过滤
daily 自建 Cursor bridge;DAILY_LLM_PROVIDER 控制后端;config/feeds.yaml 与 sensitive_words 可配置。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,17 +3,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import certifi
|
||||
import httpx
|
||||
|
||||
from daily.config import ROOT, env, env_int
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from daily.config import env, env_int
|
||||
from daily.cursor_client import cursor_chat
|
||||
|
||||
_JSON_BLOCK = re.compile(r"```(?:json)?\s*([\s\S]*?)```", re.IGNORECASE)
|
||||
|
||||
@@ -44,6 +41,47 @@ def extract_json_object(text: str) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
|
||||
def _has_openai_configured() -> bool:
|
||||
return bool(env("DAILY_LLM_API_KEY") or env("OPENAI_API_KEY"))
|
||||
|
||||
|
||||
def _has_cursor_configured() -> bool:
|
||||
return bool(env("CURSOR_API_KEY"))
|
||||
|
||||
|
||||
def llm_provider() -> str:
|
||||
raw = (env("DAILY_LLM_PROVIDER") or "auto").strip().lower()
|
||||
if raw in {"openai", "cursor"}:
|
||||
return raw
|
||||
return "auto"
|
||||
|
||||
|
||||
def resolve_llm_backend() -> str:
|
||||
"""返回 openai | cursor | 空字符串。"""
|
||||
provider = llm_provider()
|
||||
has_openai = _has_openai_configured()
|
||||
has_cursor = _has_cursor_configured()
|
||||
|
||||
if provider == "openai":
|
||||
if has_openai:
|
||||
return "openai"
|
||||
return "cursor" if has_cursor else ""
|
||||
|
||||
if provider == "cursor":
|
||||
if has_cursor:
|
||||
return "cursor"
|
||||
return "openai" if has_openai else ""
|
||||
|
||||
agent_mode = (env("DAILY_REPORT_MODE") or "").strip().lower() == "agent"
|
||||
if agent_mode and has_cursor:
|
||||
return "cursor"
|
||||
if has_openai:
|
||||
return "openai"
|
||||
if has_cursor:
|
||||
return "cursor"
|
||||
return ""
|
||||
|
||||
|
||||
def _openai_chat(system: str, user: str) -> str:
|
||||
api_key = (env("DAILY_LLM_API_KEY") or env("OPENAI_API_KEY") or "").strip()
|
||||
if not api_key:
|
||||
@@ -70,53 +108,14 @@ def _openai_chat(system: str, user: str) -> str:
|
||||
return str(data["choices"][0]["message"]["content"] or "").strip()
|
||||
|
||||
|
||||
def _cursor_chat(system: str, user: str) -> str:
|
||||
api_key = (env("CURSOR_API_KEY") or "").strip()
|
||||
if not api_key:
|
||||
return ""
|
||||
import sys
|
||||
|
||||
from daily.config import ROOT
|
||||
from cursor_sdk import Agent, AgentOptions, CursorAgentError, LocalAgentOptions
|
||||
|
||||
_bot = str(ROOT / "bot")
|
||||
if _bot not in sys.path:
|
||||
sys.path.insert(0, _bot)
|
||||
try:
|
||||
from bridge_manager import warm_cursor_bridge
|
||||
except ImportError:
|
||||
warm_cursor_bridge = lambda: None # noqa: E731
|
||||
|
||||
cwd = env("DAILY_CURSOR_CWD") or str(ROOT)
|
||||
# bridge_manager 读 bot env_config 的 CURSOR_CWD,早报侧须先对齐工作目录
|
||||
os.environ["CURSOR_CWD"] = cwd
|
||||
warm_cursor_bridge()
|
||||
model = env("CURSOR_MODEL") or "composer-2.5"
|
||||
prompt = f"{system}\n\n{user}"
|
||||
try:
|
||||
result = Agent.prompt(
|
||||
prompt,
|
||||
AgentOptions(
|
||||
api_key=api_key,
|
||||
model=model,
|
||||
local=LocalAgentOptions(cwd=cwd),
|
||||
),
|
||||
)
|
||||
except CursorAgentError as exc:
|
||||
raise RuntimeError(f"LLM 调用失败:{exc.message}") from exc
|
||||
if result.status == "error":
|
||||
raise RuntimeError(f"LLM 调用失败:{result.result or '未知错误'}")
|
||||
return (result.result or "").strip()
|
||||
|
||||
|
||||
def llm_chat(system: str, user: str) -> str:
|
||||
"""优先 OpenAI 兼容 API,否则 Cursor SDK。"""
|
||||
if env("DAILY_LLM_API_KEY") or env("OPENAI_API_KEY"):
|
||||
backend = resolve_llm_backend()
|
||||
if backend == "openai":
|
||||
return _openai_chat(system, user)
|
||||
if env("CURSOR_API_KEY"):
|
||||
return _cursor_chat(system, user)
|
||||
if backend == "cursor":
|
||||
return cursor_chat(system, user)
|
||||
return ""
|
||||
|
||||
|
||||
def has_llm_configured() -> bool:
|
||||
return bool(env("DAILY_LLM_API_KEY") or env("OPENAI_API_KEY") or env("CURSOR_API_KEY"))
|
||||
return bool(resolve_llm_backend())
|
||||
|
||||
Reference in New Issue
Block a user