import type { FunctionTag } from './tags' // 對白索引的一筆;由建置流水線產生,見 data/。text 不超過 80 字。 export type IndexEntry = { id: string function: FunctionTag emotion: string mode: string | null text: string } export const DEFAULT_K = 3 // 取同功能標籤的對白 k 句。依 id 排序後從 turn 起輪流取,讓每一輪撈到的句子不同, // 同一輪結果固定(便於測試)。沒有同標籤的對白就回傳空陣列,不退而求其次。 export function retrieve( index: readonly IndexEntry[], fn: FunctionTag, { k = DEFAULT_K, mode = null, turn = 0 }: { k?: number; mode?: string | null; turn?: number } = {}, ): IndexEntry[] { const pool = index .filter(e => e.function === fn && (mode === null || e.mode === mode)) .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) if (pool.length === 0) return [] const n = Math.min(k, pool.length) const start = ((turn % pool.length) + pool.length) % pool.length return Array.from({ length: n }, (_, i) => pool[(start + i) % pool.length]!) }