forked from ZhuLinsen/daily_stock_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
471 lines (393 loc) · 15.6 KB
/
Copy pathagent.py
File metadata and controls
471 lines (393 loc) · 15.6 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
# -*- coding: utf-8 -*-
"""
Agent API endpoints.
"""
import asyncio
import json
import logging
import uuid
from typing import Any, Dict, List, Optional
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import StreamingResponse
from pydantic import AliasChoices, BaseModel, ConfigDict, Field
from src.config import get_config
from src.services.agent_model_service import list_agent_model_deployments
# Tool name -> Chinese display name mapping
TOOL_DISPLAY_NAMES: Dict[str, str] = {
"get_realtime_quote": "获取实时行情",
"get_daily_history": "获取历史K线",
"get_chip_distribution": "分析筹码分布",
"get_analysis_context": "获取分析上下文",
"get_stock_info": "获取股票基本面",
"search_stock_news": "搜索股票新闻",
"search_comprehensive_intel": "搜索综合情报",
"analyze_trend": "分析技术趋势",
"calculate_ma": "计算均线系统",
"get_volume_analysis": "分析量能变化",
"analyze_pattern": "识别K线形态",
"get_market_indices": "获取市场指数",
"get_sector_rankings": "分析行业板块",
"get_skill_backtest_summary": "获取技能回测概览",
"get_strategy_backtest_summary": "获取策略回测概览",
"get_stock_backtest_summary": "获取个股回测数据",
}
logger = logging.getLogger(__name__)
router = APIRouter()
class ChatRequest(BaseModel):
model_config = ConfigDict(populate_by_name=True)
message: str
session_id: Optional[str] = None
skills: Optional[List[str]] = Field(
default=None,
validation_alias=AliasChoices("skills", "strategies"),
)
context: Optional[Dict[str, Any]] = None # Previous analysis context for data reuse
@property
def effective_skills(self) -> Optional[List[str]]:
"""Return skill ids from the unified request shape."""
return self.skills
class ChatResponse(BaseModel):
success: bool
content: str
session_id: str
error: Optional[str] = None
class SkillInfo(BaseModel):
id: str
name: str
description: str
class SkillsResponse(BaseModel):
skills: List[SkillInfo]
default_skill_id: str = ""
class StrategiesResponse(BaseModel):
strategies: List[SkillInfo]
default_strategy_id: str = ""
class AgentModelDeployment(BaseModel):
deployment_id: str
model: str
provider: str
source: str
api_base: Optional[str] = None
deployment_name: Optional[str] = None
is_primary: bool = False
is_fallback: bool = False
class AgentModelsResponse(BaseModel):
models: List[AgentModelDeployment]
@router.get("/models", response_model=AgentModelsResponse)
async def get_agent_models():
"""Get configured Agent model deployments for frontend selection."""
config = get_config()
return AgentModelsResponse(
models=[AgentModelDeployment(**item) for item in list_agent_model_deployments(config)]
)
def _build_skills_response(config) -> SkillsResponse:
from src.agent.factory import get_skill_manager
from src.agent.skills.defaults import get_primary_default_skill_id
skill_manager = get_skill_manager(config)
available_skills = sorted(
[
skill
for skill in skill_manager.list_skills()
if getattr(skill, "user_invocable", True)
],
key=lambda skill: (
int(getattr(skill, "default_priority", 100)),
skill.display_name,
skill.name,
),
)
skills = [
SkillInfo(id=skill.name, name=skill.display_name, description=skill.description)
for skill in available_skills
]
return SkillsResponse(
skills=skills,
default_skill_id=get_primary_default_skill_id(available_skills),
)
@router.get("/skills", response_model=SkillsResponse)
async def get_skills():
"""
Get available agent strategy skills.
"""
return _build_skills_response(get_config())
@router.get("/strategies", response_model=StrategiesResponse, include_in_schema=False)
async def get_strategies():
"""Compatibility alias for legacy clients."""
payload = _build_skills_response(get_config())
return StrategiesResponse(
strategies=payload.skills,
default_strategy_id=payload.default_skill_id,
)
@router.post("/chat", response_model=ChatResponse)
async def agent_chat(request: ChatRequest):
"""
Chat with the AI Agent.
"""
config = get_config()
if not config.is_agent_available():
raise HTTPException(status_code=400, detail="Agent mode is not enabled")
session_id = request.session_id or str(uuid.uuid4())
try:
skills = request.effective_skills
executor = _build_executor(config, skills or None)
# Pass explicit skills into context for the orchestrator.
# Direct assignment so caller-provided skills always take precedence
# over any stale value carried in the context dict.
ctx = dict(request.context or {})
if skills is not None:
ctx["skills"] = skills
# Offload the blocking call to a thread to avoid blocking the event loop.
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(
None,
lambda: executor.chat(message=request.message, session_id=session_id,
context=ctx),
)
return ChatResponse(
success=result.success,
content=result.content,
session_id=session_id,
error=result.error
)
except Exception as e:
logger.error(f"Agent chat API failed: {e}")
logger.exception("Agent chat error details:")
raise HTTPException(status_code=500, detail=str(e))
class SessionItem(BaseModel):
session_id: str
title: str
message_count: int
created_at: Optional[str] = None
last_active: Optional[str] = None
class SessionsResponse(BaseModel):
sessions: List[SessionItem]
class SessionMessagesResponse(BaseModel):
session_id: str
messages: List[Dict[str, Any]]
@router.get("/chat/sessions", response_model=SessionsResponse)
async def list_chat_sessions(request: Request, limit: int = 50, user_id: Optional[str] = None):
"""获取聊天会话列表
Args:
request: HTTP request (used to extract authenticated user_id).
limit: Maximum number of sessions to return.
user_id: Optional platform-prefixed user identifier for session
isolation. When provided, only sessions whose session_id
starts with this prefix are returned. The value must
include the platform prefix, e.g. ``telegram_12345``,
``feishu_ou_abc``.
"""
from src.storage import get_db
auth_user_id = getattr(request.state, "user_id", None)
sessions = get_db().get_chat_sessions(
limit=limit,
session_prefix=user_id,
extra_session_ids=[user_id] if user_id else None,
user_id=auth_user_id,
)
return SessionsResponse(sessions=sessions)
@router.get("/chat/sessions/{session_id}", response_model=SessionMessagesResponse)
async def get_chat_session_messages(request: Request, session_id: str, limit: int = 100):
"""获取单个会话的完整消息"""
from src.storage import get_db
auth_user_id = getattr(request.state, "user_id", None)
messages = get_db().get_conversation_messages(session_id, limit=limit, user_id=auth_user_id)
return SessionMessagesResponse(session_id=session_id, messages=messages)
@router.delete("/chat/sessions/{session_id}")
async def delete_chat_session(request: Request, session_id: str):
"""删除指定会话"""
from src.storage import get_db
auth_user_id = getattr(request.state, "user_id", None)
count = get_db().delete_conversation_session(session_id, user_id=auth_user_id)
return {"deleted": count}
class SendChatRequest(BaseModel):
"""Request body for sending chat content to notification channels."""
content: str = Field(..., min_length=1, max_length=50000)
title: Optional[str] = None
@router.post("/chat/send")
async def send_chat_to_notification(request: SendChatRequest):
"""
Send chat session content to configured notification channels.
Uses run_in_executor to avoid blocking the event loop.
"""
from src.notification import NotificationService
loop = asyncio.get_running_loop()
success = await loop.run_in_executor(
None,
lambda: NotificationService().send(request.content),
)
if not success:
return {
"success": False,
"error": "no_channels",
"message": "未配置通知渠道,请先在设置中配置",
}
return {"success": True}
def _build_executor(config, skills: Optional[List[str]] = None):
"""Build and return a configured AgentExecutor (sync helper)."""
from src.agent.factory import build_agent_executor
return build_agent_executor(config, skills=skills)
async def _run_research_in_background(
agent,
question: str,
context: Optional[Dict[str, Any]],
*,
timeout: int,
):
"""Run deep research off the event loop with an internal overall timeout."""
return await asyncio.to_thread(
agent.research,
question,
context,
timeout_seconds=timeout,
)
# ============================================================
# Deep research endpoint
# ============================================================
class ResearchRequest(BaseModel):
question: str
stock_code: Optional[str] = None
class ResearchResponse(BaseModel):
success: bool
content: str
sources: List[str] = Field(default_factory=list)
token_usage: int = 0
error: Optional[str] = None
@router.post("/research", response_model=ResearchResponse)
async def agent_research(request: ResearchRequest):
"""Run a deep-research query via the ResearchAgent.
Similar to the ``/research`` bot command but exposed as a REST endpoint.
"""
config = get_config()
if not config.is_agent_available():
raise HTTPException(status_code=400, detail="Agent mode is not enabled")
question = request.question
context: Optional[Dict[str, Any]] = None
if request.stock_code:
question = f"[Stock: {request.stock_code}] {question}"
context = {"stock_code": request.stock_code}
try:
from src.agent.research import ResearchAgent
from src.agent.factory import get_tool_registry
from src.agent.llm_adapter import LLMToolAdapter
registry = get_tool_registry()
llm_adapter = LLMToolAdapter(config)
budget = getattr(config, "agent_deep_research_budget", 30000)
agent = ResearchAgent(
tool_registry=registry,
llm_adapter=llm_adapter,
token_budget=budget,
)
research_timeout = getattr(config, "agent_deep_research_timeout", 180)
result = await _run_research_in_background(
agent,
question,
context,
timeout=research_timeout,
)
if getattr(result, "timed_out", False):
logger.warning("Agent research API timed out after %ss", research_timeout)
return ResearchResponse(
success=False,
content="",
sources=[],
token_usage=0,
error=f"Deep research timed out after {research_timeout}s",
)
return ResearchResponse(
success=result.success,
content=result.report,
sources=[f"Sub-question {i+1}: {q}" for i, q in enumerate(result.sub_questions)],
token_usage=result.total_tokens,
error=result.error if not result.success else None,
)
except Exception as e:
logger.error("Agent research API failed: %s", e)
logger.exception("Agent research error details:")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/chat/stream")
async def agent_chat_stream(request: ChatRequest):
"""
Chat with the AI Agent, streaming progress via SSE.
Each SSE event is a JSON object with a 'type' field:
- thinking: AI is deciding next action
- tool_start: a tool call has begun
- tool_done: a tool call finished
- generating: final answer being generated
- done: analysis complete, contains 'content' and 'success'
- error: error occurred, contains 'message'
"""
config = get_config()
if not config.is_agent_available():
raise HTTPException(status_code=400, detail="Agent mode is not enabled")
session_id = request.session_id or str(uuid.uuid4())
loop = asyncio.get_running_loop()
queue: asyncio.Queue = asyncio.Queue()
# Pass explicit skills into context for the orchestrator.
# Direct assignment so caller-provided skills always take precedence.
skills = request.effective_skills
stream_ctx = dict(request.context or {})
if skills is not None:
stream_ctx["skills"] = skills
def progress_callback(event: dict):
# Enrich tool events with display names
if event.get("type") in ("tool_start", "tool_done"):
tool = event.get("tool", "")
event["display_name"] = TOOL_DISPLAY_NAMES.get(tool, tool)
asyncio.run_coroutine_threadsafe(queue.put(event), loop)
def run_sync():
try:
executor = _build_executor(config, skills or None)
result = executor.chat(
message=request.message,
session_id=session_id,
progress_callback=progress_callback,
context=stream_ctx,
)
asyncio.run_coroutine_threadsafe(
queue.put({
"type": "done",
"success": result.success,
"content": result.content,
"error": result.error,
"total_steps": result.total_steps,
"session_id": session_id,
}),
loop,
)
except Exception as exc:
logger.error(f"Agent stream error: {exc}")
asyncio.run_coroutine_threadsafe(
queue.put({"type": "error", "message": str(exc)}),
loop,
)
async def event_generator():
# Start executor in a thread so we don't block the event loop
fut = loop.run_in_executor(None, run_sync)
try:
while True:
try:
event = await asyncio.wait_for(queue.get(), timeout=300.0)
except asyncio.TimeoutError:
yield "data: " + json.dumps({"type": "error", "message": "分析超时"}, ensure_ascii=False) + "\n\n"
break
yield "data: " + json.dumps(event, ensure_ascii=False) + "\n\n"
if event.get("type") in ("done", "error"):
break
finally:
try:
await asyncio.wait_for(fut, timeout=5.0)
except asyncio.CancelledError:
pass
except asyncio.TimeoutError:
# Cleanup taking longer than 5s is treated as an expected timeout; no warning.
logger.debug("agent executor cleanup timed out after 5s for session %s", session_id)
except Exception as exc:
logger.warning("agent executor cleanup error (ignored): %s", exc, exc_info=True)
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"Connection": "keep-alive",
},
)