// Entity 正規化 — 純 exact match 版(零 AI / 零 Vectorize)。 // // 鐵律:embedding / 語意相似度合併【不是插件職責】,屬基本盤 optional embed 模組。 // 插件只做 exact match(小寫去空白比對 canonical / aliases): // 命中 → 回 canonical;未命中 → 建新 entity 後回 rawName。 // 原本的 cosine 門檻(0.92 merge / 0.75 pending)與 pending 流程, // 待基本盤 embed 模組上線後在 base 層處理;插件不綁 AI/Vectorize。 import { findEntityByName, createEntity } from './entity-crud'; import type { KbdbClient } from '../lib/kbdb-client'; /** * 正規化 rawName(純 exact): * 1. exact match 命中 → 回傳 canonical * 2. 未命中 → 建新 entity → 回傳 rawName * 任何錯誤靜默降級,回傳 rawName。 */ export async function normalizeEntity( client: KbdbClient, rawName: string, owner: string, // 必經:未命中會建新 entity,必帶 owner ): Promise { try { const exact = await findEntityByName(client, rawName, owner); if (exact) return exact.canonical; await createEntity(client, rawName, owner); return rawName; } catch (err) { console.error('[normalizeEntity] error, fallback to rawName:', err); return rawName; } }