Files
aichat/src/utils/strategy-classes.ts
T
2026-11-05 10:46:36 +08:00

104 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 解析 native 出的**技能清单 md**,得到「9 个类型 + 分组 + skill 名」。
*
* ── 为什么 app 侧要解析清单 ──────────────────────────────────────────────
* UI 的「类型」区(生成任务 ⇒ 9 个类型;解释·改写 ⇒ 分组 2 项)需要
* 类型名 / 分组 / skill 名,而这些**只有 native 出的清单里才有**。
* ⇒ app **不硬编码**任何类名 / 分组 / skill 名 —— 清单是唯一事实来源,
* 格式见 `constants/policy-skills.ts` 头部的「清单的格式规范」。
*
* ── 解析规则 ────────────────────────────────────────────────────────────
* 按 `^## ` 切节;每节:
* ## 常规打法技能 ⇒ 类型 = 去掉结尾「技能」→「常规打法」
* - **分组**:打法策略 ⇒ 分组
* - **概述**:…… ⇒ description(缺失 ⇒ 空串,不跳过该节)
* - **英文名**:`normalBet`(…) ⇒ 取第一个反引号里的英文名 = skill 名
*
* ── 容错 ────────────────────────────────────────────────────────────────
* 缺行(无「分组」/ 无「英文名」/ 标题不以「技能」结尾)就**跳过该节**,
* **绝不抛异常**。清单取不到 / 全不合规 ⇒ 返回空数组 ⇒ 界面隐藏类型区。
* ⚠️ 例外:「概述」缺失或为空 ⇒ description = 空串、**不跳过该节** ——
* description 只用于悬停提示,不能因为缺它就让类型按钮消失。
* ⚠️ `## 总体规则` 一节没有「技能」结尾的标题,天然被跳过 ——
* 本函数喂清单正文或整段拼装结果都安全。
*/
export interface StrategyClass {
/** 类型,如「常规注码」(节标题去掉结尾「技能」) */
className: string
/** 分组,如「注码策略」 */
category: string
/** 如 'normalChip'(「英文名」行第一个反引号里的值) */
skillName: string
/** 「概述」行的值(悬停提示用);缺失或为空 ⇒ 空串(不因此跳过该节) */
description: string
}
/** 节标题:`## XX技能` → XX;不以「技能」结尾 / 剥完为空 ⇒ null。 */
function extractClassName(title: string): string | null {
const trimmed = title.trim()
if (!trimmed.endsWith('技能')) return null
const className = trimmed.slice(0, -'技能'.length).trim()
return className || null
}
/** 取 `- **分组**:xxx` 行的值;没有该行 ⇒ null。 */
function extractCategory(sectionText: string): string | null {
const line = sectionText
.split('\n')
.find((l) => /^- \*\*分组\*\*[::]/.test(l.trim()))
if (!line) return null
const value = line.trim().replace(/^- \*\*分组\*\*[::]/, '').trim()
return value || null
}
/** 取 `- **概述**:xxx` 行的值;没有该行 / 值为空 ⇒ ''(不跳过该节,见容错说明)。 */
function extractOverview(sectionText: string): string {
const line = sectionText
.split('\n')
.find((l) => /^- \*\*概述\*\*[::]/.test(l.trim()))
if (!line) return ''
// 只替换行首标签(概述正文里可能含「:」,不能贪婪截断)
const value = line.trim().replace(/^- \*\*概述\*\*[::]/, '').trim()
return value
}
/** 取「英文名」行里第一个反引号值(`[A-Za-z][A-Za-z0-9]*`);没有 ⇒ null。 */
function extractSkillName(sectionText: string): string | null {
const line = sectionText
.split('\n')
.find((l) => /^- \*\*英文名\*\*[::]/.test(l.trim()))
if (!line) return null
const name = line.match(/`([A-Za-z][A-Za-z0-9]*)`/)?.[1]
return name || null
}
/**
* 解析技能清单 md → 类型条目数组。**不抛异常**;不合规的节一律跳过。
*
* @param skillListText native 给的清单正文(md 文本);空 / 非字符串 ⇒ 空数组
*/
export function parseStrategyClasses(skillListText: string): StrategyClass[] {
if (typeof skillListText !== 'string' || !skillListText.trim()) {
return []
}
const results: StrategyClass[] = []
// 按 `^## ` 切节:`(?=## )` 是「两个井号+空格」—— `# `(单井号)匹配不到
// `## XX技能` 标题(`# ` 后面跟的是空格,而标题第二字符是 #)。
// split 保留每节含标题行的整块;`# 技能清单` 等单井号块不以 `## ` 开头 ⇒ 天然跳过。
const sections = skillListText.split(/^(?=## )/m)
for (const section of sections) {
if (!section.startsWith('## ')) continue
const titleLine = section.split('\n', 1)[0]
const className = extractClassName(titleLine.replace(/^## /, ''))
if (!className) continue
const category = extractCategory(section)
if (!category) continue
const skillName = extractSkillName(section)
if (!skillName) continue
results.push({ className, category, skillName, description: extractOverview(section) })
}
return results
}