Files
kbdb-graph-plugin/src/actions/entity-normalize.ts
T
Claude 4190fbcf90 feat(graph): owner_id 寫入鏈必經(根治無主資料)——配合 base owner-mandatory(D28)
- kbdb-client requireOwner 守衛:漏 owner 插件端即 throw,不再靜默送 owner:''
- owner 一路 thread:persistNodes/ingestEnvelope/entity-crud/triplet-crud 全必填
- 病灶修:POST /triplets/ingest 原本沒傳 owner→加 owner_id query+400
- 寫入 route 缺 owner→400;vitest 23→27
- 註:gloss-bridge 分支的 backfill/gloss-entry 另需套 owner 必填(附說明),不跨支疊改
2026-07-05 11:00:30 +00:00

34 lines
1.3 KiB
TypeScript

// 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<string> {
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;
}
}