|
| 1 | +"""Human-readable formatters for AI tool output. |
| 2 | +
|
| 3 | +Each function takes a typed model and returns a markdown string |
| 4 | +optimized for LLM consumption. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from .models import ( |
| 10 | + Hotmoney, |
| 11 | + LadderDetail, |
| 12 | + Market, |
| 13 | + HotTheme, |
| 14 | + NewsItem, |
| 15 | + SectorGroup, |
| 16 | + Snapshot, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def format_snapshot(snap: Snapshot) -> str: |
| 21 | + """Format the full snapshot as a concise AI-readable summary.""" |
| 22 | + parts = [f"# A 股日报快照 — {snap.date or '未知日期'}"] |
| 23 | + parts.append(f"\n> {snap.disclaimer}") |
| 24 | + |
| 25 | + s = snap.ai_summary |
| 26 | + parts.append(f"\n## 市场概况\n- {s.market_state}") |
| 27 | + parts.append(f"- {s.focus_direction}") |
| 28 | + parts.append(f"- {s.theme_focus}") |
| 29 | + parts.append(f"- {s.hotmoney_state}") |
| 30 | + parts.append(f"- {s.news_highlight}") |
| 31 | + |
| 32 | + if snap.market: |
| 33 | + parts.append(f"\n{format_market(snap.market)}") |
| 34 | + if snap.hot_themes: |
| 35 | + parts.append(f"\n{format_hot_themes(snap.hot_themes)}") |
| 36 | + if snap.sectors: |
| 37 | + parts.append(f"\n{format_sectors(snap.sectors)}") |
| 38 | + if snap.ladder_detail: |
| 39 | + parts.append(f"\n{format_ladder(snap.ladder_detail)}") |
| 40 | + if snap.hotmoney: |
| 41 | + parts.append(f"\n{format_hotmoney(snap.hotmoney)}") |
| 42 | + if snap.focus_news: |
| 43 | + parts.append(f"\n{format_news(snap.focus_news)}") |
| 44 | + |
| 45 | + parts.append(f"\n---\n数据来源: [恢恢量化](https://hhxg.top)") |
| 46 | + return "\n".join(parts) |
| 47 | + |
| 48 | + |
| 49 | +def format_market(m: Market) -> str: |
| 50 | + """Format market overview.""" |
| 51 | + lines = [ |
| 52 | + "## 市场赚钱效应", |
| 53 | + f"- 日期: {m.date}", |
| 54 | + f"- 赚钱效应: {m.sentiment_index}% ({m.sentiment_label})", |
| 55 | + ] |
| 56 | + if m.total: |
| 57 | + lines.append(f"- 个股总数: {m.total}") |
| 58 | + if m.struct_diff is not None: |
| 59 | + lines.append(f"- 结构差值: {m.struct_diff}") |
| 60 | + if m.limit_up is not None: |
| 61 | + lines.append(f"- 涨停: {m.limit_up} 炸板: {m.fried} 跌停: {m.limit_down}") |
| 62 | + if m.promotion_rate: |
| 63 | + lines.append(f"- 晋级率: {m.promotion_rate}") |
| 64 | + |
| 65 | + if m.buckets: |
| 66 | + lines.append("\n| 分类 | 今日 | 昨日 | 方向 |") |
| 67 | + lines.append("|------|------|------|------|") |
| 68 | + for b in m.buckets: |
| 69 | + lines.append(f"| {b.name} | {b.count} | {b.prev} | {b.dir} |") |
| 70 | + |
| 71 | + return "\n".join(lines) |
| 72 | + |
| 73 | + |
| 74 | +def format_hot_themes(themes: list[HotTheme]) -> str: |
| 75 | + """Format hot themes table.""" |
| 76 | + lines = [ |
| 77 | + "## 热门题材", |
| 78 | + "| 题材 | 涨停数 | 净流入(亿) | 龙头股 |", |
| 79 | + "|------|--------|-----------|--------|", |
| 80 | + ] |
| 81 | + for t in themes: |
| 82 | + leaders = ", ".join(s.name for s in t.top_stocks[:2]) |
| 83 | + net = f"{t.net_yi:.2f}" if t.net_yi is not None else "-" |
| 84 | + lines.append(f"| {t.name} | {t.limitup_count} | {net} | {leaders} |") |
| 85 | + return "\n".join(lines) |
| 86 | + |
| 87 | + |
| 88 | +def format_sectors(groups: list[SectorGroup]) -> str: |
| 89 | + """Format sector fund-flow data.""" |
| 90 | + lines = ["## 行业/板块资金流向"] |
| 91 | + for g in groups: |
| 92 | + lines.append(f"\n### {g.label}") |
| 93 | + if g.strong: |
| 94 | + lines.append("\n**强势:**") |
| 95 | + lines.append("| 名称 | 净流入(亿) | 领涨 | 偏离% |") |
| 96 | + lines.append("|------|-----------|------|-------|") |
| 97 | + for s in g.strong: |
| 98 | + net = f"{s.net_yi:.1f}" if s.net_yi is not None else "-" |
| 99 | + bias = f"{s.bias_pct:.1f}" if s.bias_pct is not None else "-" |
| 100 | + lines.append(f"| {s.name} | {net} | {s.leader or '-'} | {bias} |") |
| 101 | + if g.weak: |
| 102 | + lines.append("\n**弱势:**") |
| 103 | + lines.append("| 名称 | 净流入(亿) | 领涨 | 偏离% |") |
| 104 | + lines.append("|------|-----------|------|-------|") |
| 105 | + for s in g.weak: |
| 106 | + net = f"{s.net_yi:.1f}" if s.net_yi is not None else "-" |
| 107 | + bias = f"{s.bias_pct:.1f}" if s.bias_pct is not None else "-" |
| 108 | + lines.append(f"| {s.name} | {net} | {s.leader or '-'} | {bias} |") |
| 109 | + return "\n".join(lines) |
| 110 | + |
| 111 | + |
| 112 | +def format_ladder(detail: LadderDetail) -> str: |
| 113 | + """Format the consecutive limit-up ladder.""" |
| 114 | + lines = ["## 连板天梯"] |
| 115 | + |
| 116 | + if detail.lb_rates_map: |
| 117 | + rates = ", ".join(f"{k}板→{v}" for k, v in sorted(detail.lb_rates_map.items())) |
| 118 | + lines.append(f"晋级率: {rates}") |
| 119 | + |
| 120 | + for level in detail.levels: |
| 121 | + names = ", ".join( |
| 122 | + f"{s.name}({s.code})" if s.code else s.name |
| 123 | + for s in level.stocks[:5] |
| 124 | + ) |
| 125 | + suffix = f" +{level.count - 5}只" if level.count > 5 else "" |
| 126 | + lines.append(f"- **{level.boards}板** ({level.count}只): {names}{suffix}") |
| 127 | + |
| 128 | + if detail.area_counts: |
| 129 | + top_areas = sorted(detail.area_counts.items(), key=lambda x: -x[1])[:5] |
| 130 | + lines.append(f"\n地域分布: {', '.join(f'{k}({v})' for k, v in top_areas)}") |
| 131 | + if detail.concept_counts: |
| 132 | + top_concepts = sorted(detail.concept_counts.items(), key=lambda x: -x[1])[:5] |
| 133 | + lines.append(f"概念分布: {', '.join(f'{k}({v})' for k, v in top_concepts)}") |
| 134 | + |
| 135 | + return "\n".join(lines) |
| 136 | + |
| 137 | + |
| 138 | +def format_hotmoney(hm: Hotmoney) -> str: |
| 139 | + """Format hotmoney / Dragon-Tiger board data.""" |
| 140 | + lines = [ |
| 141 | + "## 游资龙虎榜", |
| 142 | + f"- 日期: {hm.date}", |
| 143 | + ] |
| 144 | + if hm.total_net_yi is not None: |
| 145 | + lines.append(f"- 合计净买入: {hm.total_net_yi:.2f} 亿") |
| 146 | + |
| 147 | + if hm.top_net_buy: |
| 148 | + lines.append("\n**净买入 TOP:**") |
| 149 | + lines.append("| 股票 | 净买入(亿) | 占比% |") |
| 150 | + lines.append("|------|-----------|-------|") |
| 151 | + for b in hm.top_net_buy: |
| 152 | + ratio = f"{b.ratio_pct:.1f}" if b.ratio_pct is not None else "-" |
| 153 | + lines.append(f"| {b.name} | {b.net_yi:.2f} | {ratio} |") |
| 154 | + |
| 155 | + if hm.seats: |
| 156 | + lines.append("\n**知名游资席位:**") |
| 157 | + for seat in hm.seats: |
| 158 | + stocks_str = ", ".join( |
| 159 | + f"{s.name}({'+' if s.net_yi >= 0 else ''}{s.net_yi:.2f}亿)" |
| 160 | + for s in seat.stocks[:5] |
| 161 | + ) |
| 162 | + lines.append(f"- **{seat.name}**: {stocks_str}") |
| 163 | + |
| 164 | + return "\n".join(lines) |
| 165 | + |
| 166 | + |
| 167 | +def format_news(items: list[NewsItem]) -> str: |
| 168 | + """Format news items.""" |
| 169 | + lines = ["## 焦点新闻"] |
| 170 | + for n in items: |
| 171 | + lines.append(f"- [{n.cat}] {n.title}") |
| 172 | + return "\n".join(lines) |
0 commit comments