-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathserver.py
More file actions
1190 lines (1047 loc) · 39.2 KB
/
server.py
File metadata and controls
1190 lines (1047 loc) · 39.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""PanWatch 统一服务入口 - Web 后台 + Agent 调度"""
import logging
import os
import time
from contextlib import asynccontextmanager
import uvicorn
from src.web.database import init_db, SessionLocal
from src.web.models import (
AgentConfig,
Stock,
StockAgent,
AIService,
AIModel,
NotifyChannel,
AppSettings,
DataSource,
)
from src.web.log_handler import DBLogHandler
from src.config import Settings, AppConfig, StockConfig
from src.models.market import MarketCode
from src.core.ai_client import AIClient
from src.core.notifier import NotifierManager
from src.core.scheduler import AgentScheduler
from src.core.price_alert_scheduler import PriceAlertScheduler
from src.core.paper_trading_scheduler import PaperTradingScheduler
from src.core.context_scheduler import ContextMaintenanceScheduler
from src.core.agent_runs import record_agent_run
from src.core.log_context import install_log_record_factory, log_context
from src.core.agent_catalog import (
AGENT_SEED_SPECS,
AGENT_KIND_WORKFLOW,
)
from src.core.strategy_catalog import ensure_strategy_catalog
from src.agents.base import AgentContext, PortfolioInfo, AccountInfo, PositionInfo
from src.agents.daily_report import DailyReportAgent
from src.agents.news_digest import NewsDigestAgent
from src.agents.chart_analyst import ChartAnalystAgent
from src.agents.intraday_monitor import IntradayMonitorAgent
from src.agents.premarket_outlook import PremarketOutlookAgent
logger = logging.getLogger(__name__)
# 全局 scheduler 实例,供 agents API 调用
scheduler: AgentScheduler | None = None
price_alert_scheduler: PriceAlertScheduler | None = None
paper_trading_scheduler: PaperTradingScheduler | None = None
context_maintenance_scheduler: ContextMaintenanceScheduler | None = None
def setup_ssl():
"""设置 SSL 证书环境(企业代理环境)"""
settings = Settings()
ca_cert = settings.ca_cert_file
if not ca_cert or not os.path.exists(ca_cert):
return
import certifi
bundle_path = os.path.join(os.path.dirname(__file__), "data", "ca-bundle.pem")
os.makedirs(os.path.dirname(bundle_path), exist_ok=True)
need_rebuild = not os.path.exists(bundle_path) or os.path.getmtime(
ca_cert
) > os.path.getmtime(bundle_path)
if need_rebuild:
with open(bundle_path, "w") as out:
with open(certifi.where(), "r") as f:
out.write(f.read())
out.write("\n")
with open(ca_cert, "r") as f:
out.write(f.read())
os.environ["SSL_CERT_FILE"] = bundle_path
os.environ["REQUESTS_CA_BUNDLE"] = bundle_path
logger.info(f"SSL 证书已加载: {bundle_path}")
def setup_logging():
"""配置日志: 控制台 + 数据库"""
root = logging.getLogger()
root.setLevel(logging.INFO)
install_log_record_factory()
# reload/server restart 时避免重复 handler 导致日志放大。
for h in list(root.handlers):
if isinstance(h, DBLogHandler) or getattr(h, "_panwatch_console", False):
root.removeHandler(h)
try:
h.close()
except Exception:
pass
# 控制台输出
console = logging.StreamHandler()
console._panwatch_console = True # type: ignore[attr-defined]
console.setFormatter(
logging.Formatter(
"%(asctime)s %(levelname)-5s [%(name)s] %(message)s", datefmt="%H:%M:%S"
)
)
root.addHandler(console)
# 数据库持久化
db_handler = DBLogHandler(level=logging.DEBUG)
db_handler.setFormatter(logging.Formatter("%(message)s"))
root.addHandler(db_handler)
def setup_playwright():
"""检查并安装 Playwright 浏览器
本地开发时使用系统安装的 Playwright,Docker 环境下安装到 data 目录。
通过 DOCKER 环境变量或显式设置的 PLAYWRIGHT_BROWSERS_PATH 来判断。
"""
import subprocess
# 允许通过环境变量跳过首次安装(例如不需要截图功能时)
if os.environ.get("PLAYWRIGHT_SKIP_BROWSER_INSTALL") == "1":
logger.info(
"已设置 PLAYWRIGHT_SKIP_BROWSER_INSTALL=1,跳过 Playwright 浏览器安装"
)
return
# 如果用户已显式设置 PLAYWRIGHT_BROWSERS_PATH,尊重该设置
if "PLAYWRIGHT_BROWSERS_PATH" in os.environ:
browser_dir = os.environ["PLAYWRIGHT_BROWSERS_PATH"]
logger.info(f"使用自定义 Playwright 路径: {browser_dir}")
# Docker 环境下安装到 data 目录
elif os.environ.get("DOCKER") == "1":
data_dir = os.environ.get("DATA_DIR", "./data")
browser_dir = os.path.join(data_dir, "playwright")
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = browser_dir
logger.info(f"Docker 环境,Playwright 路径: {browser_dir}")
else:
# 本地开发,使用系统默认路径,不做任何安装
logger.info("本地开发环境,使用系统 Playwright")
return
# 检查是否已安装
if os.path.exists(browser_dir):
try:
dirs = os.listdir(browser_dir)
if any(
d.startswith("chromium")
for d in dirs
if os.path.isdir(os.path.join(browser_dir, d))
):
logger.info(f"Playwright 浏览器已就绪: {browser_dir}")
return
except Exception:
pass
# 首次安装
logger.info("首次启动,正在安装 Playwright 浏览器(可能需要几分钟)...")
os.makedirs(browser_dir, exist_ok=True)
try:
result = subprocess.run(
["playwright", "install", "chromium"],
env={**os.environ, "PLAYWRIGHT_BROWSERS_PATH": browser_dir},
capture_output=True,
text=True,
timeout=600, # 10 分钟超时
)
if result.returncode == 0:
logger.info("Playwright 浏览器安装完成")
else:
logger.error(f"Playwright 安装失败: {result.stderr}")
except subprocess.TimeoutExpired:
logger.error("Playwright 安装超时(网络问题?)")
except FileNotFoundError:
logger.warning("Playwright 命令不可用,K线截图功能不可用")
except Exception as e:
logger.error(f"Playwright 安装失败: {e}")
def seed_sample_stocks():
"""首次启动时添加示例股票"""
db = SessionLocal()
try:
# 只在没有任何股票时才添加示例
if db.query(Stock).count() > 0:
return
samples = [
{"symbol": "600519", "name": "贵州茅台", "market": "CN"},
{"symbol": "002594", "name": "比亚迪", "market": "CN"},
{"symbol": "300750", "name": "宁德时代", "market": "CN"},
{"symbol": "00700", "name": "腾讯控股", "market": "HK"},
{"symbol": "AAPL", "name": "苹果", "market": "US"},
]
for s in samples:
db.add(Stock(**s))
db.commit()
logger.info("已添加 5 只示例股票(首次启动)")
finally:
db.close()
def seed_agents():
"""初始化内置 Agent 配置"""
db = SessionLocal()
for spec in AGENT_SEED_SPECS:
existing = db.query(AgentConfig).filter(AgentConfig.name == spec.name).first()
if not existing:
db.add(
AgentConfig(
name=spec.name,
display_name=spec.display_name,
description=spec.description,
kind=spec.kind,
visible=spec.visible,
lifecycle_status=spec.lifecycle_status,
replaced_by=spec.replaced_by,
display_order=spec.display_order,
enabled=spec.enabled,
schedule=spec.schedule,
execution_mode=spec.execution_mode,
config=spec.config or {},
)
)
else:
# 始终同步 execution_mode(确保代码中的定义生效)
existing.execution_mode = spec.execution_mode or "batch"
# 同步 display_name 和 description
existing.display_name = spec.display_name or existing.display_name
existing.description = spec.description or existing.description
existing.kind = spec.kind
existing.visible = bool(spec.visible)
existing.lifecycle_status = spec.lifecycle_status or "active"
existing.replaced_by = spec.replaced_by or ""
existing.display_order = int(spec.display_order or 0)
# capability 强制不参与调度,避免旧配置继续触发。
if spec.kind != AGENT_KIND_WORKFLOW:
existing.enabled = False
existing.schedule = ""
# 仅在用户未配置时补齐默认 config
if spec.config and (not existing.config):
existing.config = spec.config
# 对已存在配置做“向前兼容”的字段补齐(不覆盖用户已有值)
if existing.name == "intraday_monitor":
cfg = existing.config or {}
if isinstance(cfg, dict) and "event_only" not in cfg:
cfg["event_only"] = True
existing.config = cfg
db.commit()
db.close()
def seed_data_sources():
"""初始化预置数据源"""
db = SessionLocal()
sources = [
# 新闻类数据源
{
"name": "雪球资讯",
"type": "news",
"provider": "xueqiu",
"config": {
"cookies": "",
"description": "雪球个股新闻聚合,需要登录 cookie",
},
"enabled": False,
"priority": 0,
"supports_batch": True,
"test_symbols": ["601127", "600519"],
},
{
"name": "东方财富资讯",
"type": "news",
"provider": "eastmoney_news",
"config": {},
"enabled": True,
"priority": 1,
"supports_batch": False, # 每只股票单独请求
"test_symbols": ["601127", "600519"],
},
{
"name": "东方财富公告",
"type": "news",
"provider": "eastmoney",
"config": {},
"enabled": True,
"priority": 2,
"supports_batch": True, # 支持批量查询
"test_symbols": ["601127", "600519"],
},
# K线数据源
{
"name": "腾讯K线",
"type": "kline",
"provider": "tencent",
"config": {},
"enabled": True,
"priority": 0,
"supports_batch": False,
"test_symbols": ["601127", "600519", "300750"],
},
# 资金流向数据源
{
"name": "东方财富资金流",
"type": "capital_flow",
"provider": "eastmoney",
"config": {},
"enabled": True,
"priority": 0,
"supports_batch": False,
"test_symbols": ["601127", "600519"],
},
# 实时行情数据源
{
"name": "腾讯行情",
"type": "quote",
"provider": "tencent",
"config": {},
"enabled": True,
"priority": 0,
"supports_batch": True,
"test_symbols": ["601127", "600519", "300750"],
},
# 事件日历数据源(基于公告结构化)
{
"name": "东方财富事件日历",
"type": "events",
"provider": "eastmoney",
"config": {},
"enabled": True,
"priority": 0,
"supports_batch": True,
"test_symbols": ["601127", "600519"],
},
# K线截图数据源
{
"name": "雪球K线截图",
"type": "chart",
"provider": "xueqiu",
"config": {
"viewport": {"width": 1280, "height": 900},
"extra_wait_ms": 3000,
},
"enabled": True,
"priority": 0,
"supports_batch": False,
"test_symbols": ["601127"],
},
{
"name": "东方财富K线截图",
"type": "chart",
"provider": "eastmoney",
"config": {
"viewport": {"width": 1280, "height": 900},
"extra_wait_ms": 2000,
},
"enabled": False,
"priority": 1,
"supports_batch": False,
"test_symbols": ["601127"],
},
]
for source_data in sources:
existing = (
db.query(DataSource)
.filter(
DataSource.name == source_data["name"],
DataSource.provider == source_data["provider"],
)
.first()
)
if existing:
# 更新已存在记录的新字段(保留用户可能修改的配置)
if existing.supports_batch != source_data.get("supports_batch", False):
existing.supports_batch = source_data.get("supports_batch", False)
if not existing.test_symbols: # 只在空时更新
existing.test_symbols = source_data.get("test_symbols", [])
else:
db.add(DataSource(**source_data))
db.commit()
db.close()
def seed_strategies():
"""初始化策略目录。"""
ensure_strategy_catalog()
logger.info("策略目录初始化完成")
def load_watchlist_for_agent(agent_name: str) -> list[StockConfig]:
"""从数据库加载某个 Agent 关联的自选股"""
db = SessionLocal()
try:
stock_agents = (
db.query(StockAgent).filter(StockAgent.agent_name == agent_name).all()
)
stock_ids = [sa.stock_id for sa in stock_agents]
if not stock_ids:
return []
# 绑定优先:只要绑定了 Agent,就纳入执行范围
stocks = db.query(Stock).filter(Stock.id.in_(stock_ids)).all()
result = []
for s in stocks:
try:
market = MarketCode(s.market)
except ValueError:
market = MarketCode.CN
result.append(
StockConfig(
symbol=s.symbol,
name=s.name,
market=market,
)
)
return result
finally:
db.close()
def load_portfolio_for_agent(agent_name: str) -> PortfolioInfo:
"""从数据库加载某个 Agent 关联股票的持仓信息(包括多账户)"""
from src.web.models import Account, Position
db = SessionLocal()
try:
# 获取 Agent 关联的股票 ID
stock_agents = (
db.query(StockAgent).filter(StockAgent.agent_name == agent_name).all()
)
stock_ids = set(sa.stock_id for sa in stock_agents)
if not stock_ids:
return PortfolioInfo()
# 获取所有启用的账户
accounts = db.query(Account).filter(Account.enabled == True).all()
account_infos = []
for acc in accounts:
# 获取该账户中属于关联股票的持仓
positions = (
db.query(Position)
.filter(
Position.account_id == acc.id,
Position.stock_id.in_(stock_ids),
)
.all()
)
position_infos = []
for pos in positions:
stock = pos.stock
if not stock:
continue
try:
market = MarketCode(stock.market)
except ValueError:
market = MarketCode.CN
position_infos.append(
PositionInfo(
account_id=acc.id,
account_name=acc.name,
stock_id=stock.id,
symbol=stock.symbol,
name=stock.name,
market=market,
cost_price=pos.cost_price,
quantity=pos.quantity,
invested_amount=pos.invested_amount,
trading_style=pos.trading_style or "swing",
)
)
account_infos.append(
AccountInfo(
id=acc.id,
name=acc.name,
available_funds=acc.available_funds,
positions=position_infos,
)
)
return PortfolioInfo(accounts=account_infos)
finally:
db.close()
def load_portfolio_for_stock(stock_id: int) -> PortfolioInfo:
"""从数据库加载单只股票的持仓信息"""
from src.web.models import Account, Position
db = SessionLocal()
try:
stock = db.query(Stock).filter(Stock.id == stock_id).first()
if not stock:
return PortfolioInfo()
try:
market = MarketCode(stock.market)
except ValueError:
market = MarketCode.CN
accounts = db.query(Account).filter(Account.enabled == True).all()
account_infos = []
for acc in accounts:
pos = (
db.query(Position)
.filter(
Position.account_id == acc.id,
Position.stock_id == stock_id,
)
.first()
)
position_infos = []
if pos:
position_infos.append(
PositionInfo(
account_id=acc.id,
account_name=acc.name,
stock_id=stock.id,
symbol=stock.symbol,
name=stock.name,
market=market,
cost_price=pos.cost_price,
quantity=pos.quantity,
invested_amount=pos.invested_amount,
trading_style=pos.trading_style or "swing",
)
)
account_infos.append(
AccountInfo(
id=acc.id,
name=acc.name,
available_funds=acc.available_funds,
positions=position_infos,
)
)
return PortfolioInfo(accounts=account_infos)
finally:
db.close()
def _get_proxy() -> str:
"""从 app_settings 获取 http_proxy"""
db = SessionLocal()
try:
setting = db.query(AppSettings).filter(AppSettings.key == "http_proxy").first()
return setting.value if setting and setting.value else ""
finally:
db.close()
def _get_app_setting(key: str) -> str:
"""从 app_settings 获取配置(不存在返回空字符串)"""
db = SessionLocal()
try:
setting = db.query(AppSettings).filter(AppSettings.key == key).first()
return setting.value if setting and setting.value else ""
finally:
db.close()
def resolve_ai_model(
agent_name: str, stock_agent_id: int | None = None
) -> tuple[AIModel | None, AIService | None]:
"""解析 AI 模型: stock_agent 覆盖 → agent 默认 → 系统默认(is_default=True)
返回 (model, service) 元组"""
db = SessionLocal()
try:
model_id = None
# 1. stock_agent 级别覆盖
if stock_agent_id:
sa = db.query(StockAgent).filter(StockAgent.id == stock_agent_id).first()
if sa and sa.ai_model_id:
model_id = sa.ai_model_id
# 2. agent 级别默认
if not model_id:
agent = db.query(AgentConfig).filter(AgentConfig.name == agent_name).first()
if agent and agent.ai_model_id:
model_id = agent.ai_model_id
# 3. 系统默认
if not model_id:
default_model = db.query(AIModel).filter(AIModel.is_default == True).first()
if default_model:
model_id = default_model.id
# 4. 回退:取第一个
if not model_id:
first_model = db.query(AIModel).first()
if first_model:
model_id = first_model.id
if not model_id:
return None, None
model = db.query(AIModel).filter(AIModel.id == model_id).first()
if not model:
return None, None
service = db.query(AIService).filter(AIService.id == model.service_id).first()
if model:
db.expunge(model)
if service:
db.expunge(service)
return model, service
finally:
db.close()
def resolve_notify_channels(
agent_name: str, stock_agent_id: int | None = None
) -> list[NotifyChannel]:
"""解析通知渠道: stock_agent 覆盖 → agent 默认 → 系统默认(is_default=True)"""
db = SessionLocal()
try:
channel_ids = None
# 1. stock_agent 级别覆盖
if stock_agent_id:
sa = db.query(StockAgent).filter(StockAgent.id == stock_agent_id).first()
if sa and sa.notify_channel_ids:
channel_ids = sa.notify_channel_ids
# 2. agent 级别默认
if channel_ids is None:
agent = db.query(AgentConfig).filter(AgentConfig.name == agent_name).first()
if agent and agent.notify_channel_ids:
channel_ids = agent.notify_channel_ids
# 3. 按 id 列表查询或取系统默认
if channel_ids:
channels = (
db.query(NotifyChannel)
.filter(
NotifyChannel.id.in_(channel_ids),
NotifyChannel.enabled == True,
)
.all()
)
else:
channels = (
db.query(NotifyChannel)
.filter(
NotifyChannel.is_default == True,
NotifyChannel.enabled == True,
)
.all()
)
for ch in channels:
db.expunge(ch)
return channels
finally:
db.close()
def _build_notifier(channels: list[NotifyChannel]) -> NotifierManager:
"""根据解析后的渠道列表构建 NotifierManager"""
settings = Settings()
# allow UI override via app_settings
quiet_hours = _get_app_setting("notify_quiet_hours") or settings.notify_quiet_hours
retry_attempts_raw = _get_app_setting("notify_retry_attempts")
backoff_raw = _get_app_setting("notify_retry_backoff_seconds")
overrides_raw = (
_get_app_setting("notify_dedupe_ttl_overrides")
or settings.notify_dedupe_ttl_overrides
)
try:
retry_attempts = (
int(retry_attempts_raw)
if retry_attempts_raw
else settings.notify_retry_attempts
)
except Exception:
retry_attempts = settings.notify_retry_attempts
try:
retry_backoff_seconds = (
float(backoff_raw) if backoff_raw else settings.notify_retry_backoff_seconds
)
except Exception:
retry_backoff_seconds = settings.notify_retry_backoff_seconds
from src.core.notify_policy import NotifyPolicy, parse_dedupe_overrides
policy = NotifyPolicy(
timezone=settings.app_timezone,
quiet_hours=quiet_hours,
retry_attempts=retry_attempts,
retry_backoff_seconds=retry_backoff_seconds,
dedupe_ttl_overrides=parse_dedupe_overrides(overrides_raw),
)
notifier = NotifierManager(policy=policy)
for ch in channels:
notifier.add_channel(ch.type, ch.config or {})
return notifier
def _build_ai_client(
model: AIModel | None, service: AIService | None, proxy: str
) -> AIClient:
"""根据解析后的 model+service 构建 AIClient"""
if model and service:
return AIClient(
base_url=service.base_url,
api_key=service.api_key,
model=model.model,
proxy=proxy,
)
# 回退到环境变量配置
settings = Settings()
return AIClient(
base_url=settings.ai_base_url,
api_key=settings.ai_api_key,
model=settings.ai_model,
proxy=proxy,
)
def build_context(agent_name: str, stock_agent_id: int | None = None) -> AgentContext:
"""为指定 Agent 构建运行上下文"""
settings = Settings()
watchlist = load_watchlist_for_agent(agent_name)
portfolio = load_portfolio_for_agent(agent_name)
proxy = _get_proxy() or settings.http_proxy
model, service = resolve_ai_model(agent_name, stock_agent_id)
ai_client = _build_ai_client(model, service, proxy)
channels = resolve_notify_channels(agent_name, stock_agent_id)
notifier = _build_notifier(channels)
model_label = f"{service.name}/{model.model}" if model and service else ""
config = AppConfig(settings=settings, watchlist=watchlist)
return AgentContext(
ai_client=ai_client,
notifier=notifier,
config=config,
portfolio=portfolio,
model_label=model_label,
notify_policy=getattr(notifier, "policy", None),
)
# Agent 注册表
AGENT_REGISTRY: dict[str, type] = {
"daily_report": DailyReportAgent,
"premarket_outlook": PremarketOutlookAgent,
"news_digest": NewsDigestAgent,
"chart_analyst": ChartAnalystAgent,
"intraday_monitor": IntradayMonitorAgent,
}
def build_scheduler() -> AgentScheduler:
"""构建调度器并注册已启用的 Agent"""
settings = Settings()
sched = AgentScheduler(timezone=settings.app_timezone)
# 设置 context 构建函数(每次执行时动态获取最新配置)
sched.set_context_builder(build_context)
db = SessionLocal()
try:
agent_configs = (
db.query(AgentConfig)
.filter(
AgentConfig.enabled == True,
AgentConfig.kind == AGENT_KIND_WORKFLOW,
)
.all()
)
for cfg in agent_configs:
agent_cls = AGENT_REGISTRY.get(cfg.name)
if not agent_cls:
logger.warning(f"Agent {cfg.name} 未在 AGENT_REGISTRY 中注册")
continue
if not cfg.schedule:
logger.info(f"Agent {cfg.name} 未设置调度计划,跳过")
continue
agent_kwargs = cfg.config or {}
try:
agent_instance = (
agent_cls(**agent_kwargs) if agent_kwargs else agent_cls()
)
except TypeError:
agent_instance = agent_cls()
sched.register(
agent_instance,
schedule=cfg.schedule,
execution_mode=cfg.execution_mode or "batch",
)
finally:
db.close()
return sched
def reload_scheduler() -> bool:
"""重载调度器(用于配置导入/批量修改后立即生效)"""
global scheduler
try:
current = globals().get("scheduler")
if current:
try:
current.shutdown()
except Exception:
pass
scheduler = build_scheduler()
scheduler.start()
logger.info("Agent 调度器已重载")
return True
except Exception as e:
logger.error(f"Agent 调度器重载失败: {e}")
return False
def _log_trigger_info(
agent_name: str,
stocks: list,
model: AIModel | None,
service: AIService | None,
channels: list[NotifyChannel],
):
"""打印 Agent 触发时的上下文信息"""
stock_names = ", ".join(
f"{s.name}({s.symbol})" if hasattr(s, "symbol") else str(s) for s in stocks
)
ai_info = f"{service.name}/{model.model}" if model and service else "未配置"
channel_info = ", ".join(ch.name for ch in channels) if channels else "无"
logger.info(
f"[触发] Agent={agent_name} | 股票=[{stock_names}] | AI={ai_info} | 通知=[{channel_info}]"
)
def get_agent_execution_mode(agent_name: str) -> str:
"""获取 Agent 的执行模式"""
db = SessionLocal()
try:
agent = db.query(AgentConfig).filter(AgentConfig.name == agent_name).first()
return agent.execution_mode if agent and agent.execution_mode else "batch"
finally:
db.close()
def get_agent_config(agent_name: str) -> dict:
"""获取 Agent 的配置参数"""
db = SessionLocal()
try:
agent = db.query(AgentConfig).filter(AgentConfig.name == agent_name).first()
return agent.config if agent and agent.config else {}
finally:
db.close()
async def trigger_agent(agent_name: str) -> str:
"""手动触发 Agent 执行(根据执行模式处理)"""
start = time.monotonic()
trace_id = f"man-{agent_name}-{int(time.time() * 1000)}"
agent_cls = AGENT_REGISTRY.get(agent_name)
if not agent_cls:
raise ValueError(f"Agent {agent_name} 未注册实际实现")
with log_context(
trace_id=trace_id,
run_id=trace_id,
agent_name=agent_name,
event="trigger_agent",
tags={"trigger_source": "manual"},
):
watchlist = load_watchlist_for_agent(agent_name)
logger.info(
f"[watchlist] Agent={agent_name} count={len(watchlist)} symbols={[s.symbol for s in watchlist]}"
)
if not watchlist:
return f"Agent {agent_name} 没有关联的自选股"
model, service = resolve_ai_model(agent_name)
channels = resolve_notify_channels(agent_name)
_log_trigger_info(agent_name, watchlist, model, service, channels)
context = build_context(agent_name)
execution_mode = get_agent_execution_mode(agent_name)
agent_config = get_agent_config(agent_name)
# 根据配置初始化 Agent
if agent_config:
agent = agent_cls(**agent_config)
else:
agent = agent_cls()
try:
if execution_mode == "single" and hasattr(agent, "run_single"):
# 单只模式:逐只股票分析
results = []
for stock in watchlist:
result = await agent.run_single(context, stock.symbol)
if result:
results.append(f"{stock.name}: {result.content[:100]}...")
msg = "\n\n".join(results) if results else "无异动"
record_agent_run(
agent_name=agent_name,
status="success",
result=msg,
duration_ms=int((time.monotonic() - start) * 1000),
trace_id=trace_id,
trigger_source="manual",
model_label=context.model_label,
)
return msg
else:
# 批量模式:所有股票一起分析
result = await agent.run(context)
raw = result.raw_data or {}
record_agent_run(
agent_name=agent_name,
status="success",
result=result.content,
duration_ms=int((time.monotonic() - start) * 1000),
trace_id=trace_id,
trigger_source="manual",
notify_attempted=(
"notified" in raw
or "notify_error" in raw
or "notify_skipped" in raw
),
notify_sent=bool(raw.get("notified", False)),
model_label=context.model_label,
)
return result.content
except Exception as e:
record_agent_run(
agent_name=agent_name,
status="failed",
error=str(e),
duration_ms=int((time.monotonic() - start) * 1000),
trace_id=trace_id,
trigger_source="manual",
model_label=context.model_label,
)
raise
async def trigger_agent_for_stock(
agent_name: str,
stock,
stock_agent_id: int | None = None,
bypass_throttle: bool = False,
bypass_market_hours: bool = False,
suppress_notify: bool = False,
) -> dict:
"""手动触发 Agent 执行(单只股票)"""
start = time.monotonic()
trace_id = f"man-{agent_name}-{stock.symbol}-{int(time.time() * 1000)}"
agent_cls = AGENT_REGISTRY.get(agent_name)
if not agent_cls:
raise ValueError(f"Agent {agent_name} 未注册实际实现")
settings = Settings()
proxy = _get_proxy() or settings.http_proxy
try:
market = MarketCode(stock.market)
except ValueError:
market = MarketCode.CN
stock_config = StockConfig(
symbol=stock.symbol,
name=stock.name,
market=market,
)
# 加载该股票的持仓信息
portfolio = load_portfolio_for_stock(stock.id)
model, service = resolve_ai_model(agent_name, stock_agent_id)
channels = [] if suppress_notify else resolve_notify_channels(agent_name, stock_agent_id)
_log_trigger_info(agent_name, [stock], model, service, channels)
ai_client = _build_ai_client(model, service, proxy)
notifier = _build_notifier(channels)
model_label = f"{service.name}/{model.model}" if model and service else ""
config = AppConfig(settings=settings, watchlist=[stock_config])
context = AgentContext(