import { CHARACTERS, type CharacterCode } from './characters' import { classify, type Complete } from './classify' import type { Config } from './config' import { formatExamples } from './examples' import { retrieve, type IndexEntry } from './retrieval' export type InjectDeps = { config: Config indexes: Readonly> complete: Complete append: (text: string) => Promise now: () => number turn: number } // 回傳 null 表示這一輪不注入(沒選角色、強度 0、索引為空、分類失敗或沒有同標籤的對白)。 // 索引為空時不呼叫模型,省下一次無用的分類。 export async function buildInjection( { config, indexes, complete, now, turn }: Omit, userText: string, ): Promise<{ text: string | null; fn: string | null; ms: number } | null> { const character = config.character if (character === null || config.intensity === 0) return null const index = indexes[character] if (index.length === 0) return null const started = now() const fn = await classify(complete, userText) const ms = now() - started if (fn === null) return { text: null, fn: null, ms } return { text: formatExamples(CHARACTERS[character].name, retrieve(index, fn, { turn })), fn, ms } }