Files
jsc-plugins/plugins/anime-companion/hooks/retrieval.ts
T
Jeffery (jiantw83)andClaude Sonnet 5.5 d9e50d196b feat: W7 功能標籤分類、對白檢索與注入 (#8)
用 $.model.complete(Haiku)判斷回應的功能標籤,從角色索引取同標籤對白,
以只增不改的訊息附在使用者訊息之前;失敗一律退回不注入。

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 17:44:09 +00:00

29 lines
1.1 KiB
TypeScript

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]!)
}