feat: add agent strategy chat across API, bot, and web UI #minor (#367)

* feat: add agent strategy chat across API, bot, and web UI #minor
- add /api/v1/agent endpoints for strategies and streaming chat
- route pipeline to agent mode when specific skills are configured
- persist multi-turn conversation history in database
- add web chat page, bot /ask command, built-in strategies, and tests

* fix(agent): improve session handling, progress messaging, and chat type safety #patch

Generate UUID session IDs for stream chat requests and use running loop API.

Persist user messages even on failed runs, and store assistant error notes for context continuity.

Add contextual thinking messages after tool calls in agent executor.

Fix strategy display name lookup in bot ask command.

Refine ChatPage TypeScript types and error handling for SSE parsing.

Use SQLAlchemy select+execute for conversation history query.

* docs: add Agent strategy chat to README and CHANGELOG #skip

* fix(tests): update strategy count assertions 6->11; clean up CHANGELOG [Unreleased] section #patch

* chore: update readme and fix unitest

* feat: optimize tools

* fix: Fix hard-coded test values and resolve nested mapping in Dashboard

* update changelog

* refactor(agent): extract shared factory, cache ToolRegistry/SkillManager, fix Gemini model name

* - CRITICAL: fix 4 wrong tool names in AGENT_SYSTEM_PROMPT and
  CHAT_SYSTEM_PROMPT (get_k_history→get_daily_history,
  get_technical_analysis→analyze_trend, get_chip_analysis→
  get_chip_distribution, search_news→search_stock_news)
- fix _THINKING_TOOL_LABELS: update 7 stale keys, add calculate_ma
  and get_analysis_context (13 tools now fully mapped)
- add isinstance(dict) guard in _parse_dashboard to reject non-dict
  JSON (arrays, scalars) that would crash downstream dict access
- use getattr() for config.agent_skills in pipeline.py (AttributeError safety)
- move  to top-level, remove unused imports (get_config,
  Callable, Any/Dict/Optional)
- add phased workflow directives to both system prompts to enforce
  sequential tool calling across analysis stages
- expand .env.example AGENT_SKILLS with all 11 built-in strategies
  and usage examples
- fix factory.py docstring referencing non-existent comprehensive_analysis
This commit is contained in:
mumu
2026-02-23 13:24:01 +08:00
committed by GitHub
parent c9b01fead2
commit cddb6baeda
52 changed files with 8362 additions and 76 deletions

View File

@@ -39,6 +39,7 @@
| 市場 | 全球市場 | 支援 A股、港股、美股 |
| 復盤 | 大盤復盤 | 每日市場概覽、板塊漲跌、北向資金 |
| 回測 | AI 回測驗證 | 自動評估歷史分析準確率,方向勝率、止盈止損命中率 |
| **Agent 問股** | **策略對話** | **多輪策略問答,支援 11 種內建策略Web/Bot/API** |
| 推送 | 多渠道通知 | Telegram、Discord、郵件、企業微信、飛書等 |
| 自動化 | 定時運行 | GitHub Actions 定時執行,無需伺服器 |
@@ -122,6 +123,9 @@
| `BRAVE_API_KEYS` | [Brave Search](https://brave.com/search/api/) API隱私優先美股優化多個key用逗號分隔 | 可選 |
| `SERPAPI_API_KEYS` | [SerpAPI](https://serpapi.com/baidu-search-api?utm_source=github_daily_stock_analysis) 備用搜索 | 可選 |
| `TUSHARE_TOKEN` | [Tushare Pro](https://tushare.pro/weborder/#/login?reg=834638 ) Token | 可選 |
| `AGENT_MODE` | 啟用 Agent 策略問股模式(`true`/`false`,預設 `false` | 可選 |
| `AGENT_MAX_STEPS` | Agent 最大推理步數(預設 `10` | 可選 |
| `AGENT_STRATEGY_DIR` | 自訂策略目錄(預設內建 `strategies/` | 可選 |
#### 3. 啟用 Actions
@@ -200,6 +204,7 @@
- 📝 **配置管理** - 查看/修改自選股列表
- 🚀 **快速分析** - 通過 API 接口觸發分析
- 📊 **實時進度** - 分析任務狀態實時更新,支持多任務並行
- 🤖 **Agent 策略對話** - 啟用 `AGENT_MODE=true` 後可在 `/chat` 進行多輪問答
- 📈 **回測驗證** - 評估歷史分析準確率,查詢方向勝率與模擬收益
### API 接口
@@ -214,6 +219,8 @@
| `/api/v1/backtest/results` | GET | 查詢回測結果(分頁) |
| `/api/v1/backtest/performance` | GET | 獲取整體回測表現 |
| `/api/v1/backtest/performance/{code}` | GET | 獲取單股回測表現 |
| `/api/v1/agent/strategies` | GET | 取得可用策略清單(內建/自訂) |
| `/api/v1/agent/chat/stream` | POST (SSE) | Agent 多輪策略對話(流式) |
| `/api/health` | GET | 健康檢查 |
## 項目結構