import type { Register } from 'claude-code' import { resolveConfig } from './config' import { INDEXES } from './data/index' import { buildInjection } from './inject' import { buildRules, SECTION_ID } from './rules' // 規則段落(W8)加到系統提示最後;每則使用者訊息再依功能標籤檢索角色的對白當語氣參考(W7)。 export const register: Register = (on, options) => { const config = resolveConfig(options) const showTiming = options.showTiming === true let turn = 0 on('prompt.compose', async ($, e, next) => { const result = await next(e) const text = buildRules(config) if (text === null) return result return { sections: [...result.sections, { id: SECTION_ID, text, scope: 'session' }] } }) on('prompt.submit', async ($, e, next) => { if (e.command || e.isMeta) return next(e) const r = await buildInjection( { config, indexes: INDEXES, complete: req => $.model.complete(req), now: Date.now, turn: turn++ }, e.text, ) if (r === null) return next(e) if (showTiming) $.ui.status(`anime-companion 分類 ${r.ms}ms → ${r.fn ?? '失敗'}`) // 附加在使用者訊息之前,只增不改,不影響對話的快取。 if (r.text !== null) await $.session.append({ message: { type: 'user', content: [{ type: 'text', text: r.text }] } }) return next(e) }) }