2025-07-11 13:50:08 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
API依赖注入模块
|
|
|
|
|
"""
|
|
|
|
|
|
2025-07-11 15:29:30 +08:00
|
|
|
from typing import Optional
|
2025-07-11 16:07:55 +08:00
|
|
|
from fastapi import Depends
|
2025-07-11 13:50:08 +08:00
|
|
|
from core.config import get_config_manager, ConfigManager
|
|
|
|
|
from core.ai import AIAgent
|
|
|
|
|
from utils.file_io import OutputManager
|
|
|
|
|
|
|
|
|
|
# 全局依赖
|
2025-07-11 15:29:30 +08:00
|
|
|
config_manager: Optional[ConfigManager] = None
|
|
|
|
|
ai_agent: Optional[AIAgent] = None
|
|
|
|
|
output_manager: Optional[OutputManager] = None
|
2025-07-11 13:50:08 +08:00
|
|
|
|
|
|
|
|
def initialize_dependencies():
|
|
|
|
|
"""初始化全局依赖"""
|
|
|
|
|
global config_manager, ai_agent, output_manager
|
|
|
|
|
|
2025-07-11 15:29:30 +08:00
|
|
|
# 初始化配置 - 使用服务器模式
|
2025-07-11 13:50:08 +08:00
|
|
|
config_manager = get_config_manager()
|
2025-07-11 15:29:30 +08:00
|
|
|
config_manager.load_from_directory("config", server_mode=True)
|
2025-07-11 13:50:08 +08:00
|
|
|
|
|
|
|
|
# 初始化输出管理器
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
run_id = f"api_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
|
|
|
|
output_manager = OutputManager("result", run_id)
|
|
|
|
|
|
|
|
|
|
# 初始化AI代理
|
|
|
|
|
from core.config import AIModelConfig
|
|
|
|
|
ai_config = config_manager.get_config('ai_model', AIModelConfig)
|
|
|
|
|
ai_agent = AIAgent(ai_config)
|
|
|
|
|
|
|
|
|
|
def get_config() -> ConfigManager:
|
|
|
|
|
"""获取配置管理器"""
|
2025-07-11 15:29:30 +08:00
|
|
|
if config_manager is None:
|
|
|
|
|
raise RuntimeError("配置管理器未初始化")
|
2025-07-11 13:50:08 +08:00
|
|
|
return config_manager
|
|
|
|
|
|
|
|
|
|
def get_ai_agent() -> AIAgent:
|
|
|
|
|
"""获取AI代理"""
|
2025-07-11 15:29:30 +08:00
|
|
|
if ai_agent is None:
|
|
|
|
|
raise RuntimeError("AI代理未初始化")
|
2025-07-11 13:50:08 +08:00
|
|
|
return ai_agent
|
|
|
|
|
|
|
|
|
|
def get_output_manager() -> OutputManager:
|
|
|
|
|
"""获取输出管理器"""
|
2025-07-11 15:29:30 +08:00
|
|
|
if output_manager is None:
|
|
|
|
|
raise RuntimeError("输出管理器未初始化")
|
2025-07-11 16:07:55 +08:00
|
|
|
return output_manager
|
|
|
|
|
|
|
|
|
|
def get_tweet_service():
|
|
|
|
|
"""获取文字内容服务"""
|
|
|
|
|
from api.services.tweet import TweetService
|
|
|
|
|
return TweetService(get_ai_agent(), get_config(), get_output_manager())
|