Files
aichat/src/services/policy-lookup.ts
T
2026-11-05 10:46:36 +08:00

31 lines
1011 B
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.
import type { StrategyInstance } from '../bridge/contracts'
/**
* get_policy 的纯函数实现(分组分组已在 chat store 缓存完成)。
*
* @param bucket 已按分组分好组后的 StrategyInstance[](全量或某一类)
* @param name 可选:模糊匹配(name 子串,不区分大小写)
* @param id 可选:精确匹配(StrategyInstance.id)
*
* 命中规则:
* - 传 id ⇒ 在 bucket 里按 id 精确取(0 或 1 条)
* - 传 name ⇒ 在 bucket 里按 name 子串取(0..N 条;命中多条全返回,模型自行判断)
* - 都不传 ⇒ 空数组
*
* 不抛异常;不解析 `data` 内部。
*/
export function getPolicy(
bucket: StrategyInstance[],
name?: string,
id?: number,
): StrategyInstance[] {
if (id !== undefined) {
return bucket.filter((s) => s.id === id)
}
if (name && name.trim()) {
const q = name.trim().toLowerCase()
return bucket.filter((s) => s.name?.toLowerCase().includes(q))
}
return []
}