An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:zap: Answer field type questions from a precomputed index
Jeffrey Chen5118a6bf09ec
diff --git a/app/src/protyle/render/av/capabilities.test.ts b/app/src/protyle/render/av/capabilities.test.tsindex e9044056df..e1711bdf28 100644--- a/app/src/protyle/render/av/capabilities.test.ts+++ b/app/src/protyle/render/av/capabilities.test.ts@@ -78,3 +78,24 @@ test("field membership decisions use declared capabilities", () => {inspectDirectory(root);assert.deepEqual(violations, []);});++test("field type predicates read the precomputed index", () => {+ // 渲染按单元格反复判断字段类型,逐次读取能力表对象比集合查找慢一倍以上。+ // 这里锁定这五个常用谓词的实现,避免为了「集中来源」退回逐次属性链。+ const predicates = ["isAVDateType", "isAVTimestampType", "isAVSelectType", "isAVTextType", "isAVReadonlyType"];+ const file = path.join(__dirname, "capabilities.ts");+ const source = ts.createSourceFile(file, readFileSync(file, "utf8"), ts.ScriptTarget.Latest, true);+ const found: string[] = [];+ const visit = (node: ts.Node) => {+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && predicates.includes(node.name.text)) {+ found.push(node.name.text);+ const body = node.initializer?.getText(source) || "";+ assert.ok(!body.includes("getAVKeyCapability"),+ `${node.name.text} 不应逐次读取能力表`);+ assert.ok(body.includes(".has("), `${node.name.text} 应查询预计算索引`);+ }+ ts.forEachChild(node, visit);+ };+ visit(source);+ assert.deepEqual(found.sort(), [...predicates].sort());+});diff --git a/app/src/protyle/render/av/capabilities.ts b/app/src/protyle/render/av/capabilities.tsindex c614d18c12..f65c2571cd 100644--- a/app/src/protyle/render/av/capabilities.ts+++ b/app/src/protyle/render/av/capabilities.ts@@ -7,6 +7,28 @@ export const isAVKeyType = (type: string): type is AVKeyType =>export const getAVKeyCapability = (type: string | undefined | null): AVKeyCapability | undefined =>typeof type === "string" && isAVKeyType(type) ? AV_KEY_CAPABILITIES[type] : undefined;+// 渲染按单元格反复判断字段类型,逐次读取能力表对象比直接比较字符串慢一个数量级。+// 这里在加载时把能力表转成索引,谓词只需要一次集合查找。+const typesOfValueKind = (kind: string): ReadonlySet<string> =>+ new Set(AV_KEY_TYPES.filter(type => AV_KEY_CAPABILITIES[type].valueKind === kind));++const dateLikeTypes: ReadonlySet<string> = new Set(AV_KEY_TYPES.filter(type => {+ const kind = AV_KEY_CAPABILITIES[type].valueKind;+ return kind === "date" || kind === "timestamp";+}));+const timestampTypes = typesOfValueKind("timestamp");+const optionTypes = typesOfValueKind("options");+const textTypes = typesOfValueKind("text");+const readonlyTypes: ReadonlySet<string> = new Set(AV_KEY_TYPES.filter(type => AV_KEY_CAPABILITIES[type].editable === false));+const groupMembers = new Map<AVKeyGroup, Set<string>>();+for (const type of AV_KEY_TYPES) {+ for (const group of AV_KEY_CAPABILITIES[type].groups || []) {+ const members = groupMembers.get(group) || new Set<string>();+ members.add(type);+ groupMembers.set(group, members);+ }+}+export const hasAVCapability = (type: string | undefined | null,capability: "editable" | "filterable" | "sortable" | "groupable") =>getAVKeyCapability(type)?.[capability] === true;@@ -14,25 +36,25 @@ export const hasAVCapability = (type: string | undefined | null,export const getAVTypesByCapability = (capability: "editable" | "filterable" | "sortable" | "groupable") =>AV_KEY_TYPES.filter(type => hasAVCapability(type, capability));-export const isAVDateType = (type: string | undefined | null) => {- const kind = getAVKeyCapability(type)?.valueKind;- return kind === "date" || kind === "timestamp";-};+export const isAVDateType = (type: string | undefined | null): boolean =>+ typeof type === "string" && dateLikeTypes.has(type);-export const isAVTimestampType = (type: string | undefined | null) =>- getAVKeyCapability(type)?.valueKind === "timestamp";+export const isAVTimestampType = (type: string | undefined | null): boolean =>+ typeof type === "string" && timestampTypes.has(type);-export const isAVSelectType = (type: string | undefined | null) =>- getAVKeyCapability(type)?.valueKind === "options";+export const isAVSelectType = (type: string | undefined | null): boolean =>+ typeof type === "string" && optionTypes.has(type);-export const isAVTextType = (type: string | undefined | null) =>- getAVKeyCapability(type)?.valueKind === "text";+export const isAVTextType = (type: string | undefined | null): boolean =>+ typeof type === "string" && textTypes.has(type);-export const isAVReadonlyType = (type: string | undefined | null) =>- getAVKeyCapability(type)?.editable === false;+export const isAVReadonlyType = (type: string | undefined | null): boolean =>+ typeof type === "string" && readonlyTypes.has(type);-export const hasAVKeyGroup = (type: string | undefined | null, group: AVKeyGroup) =>- getAVKeyCapability(type)?.groups?.includes(group) === true;+export const hasAVKeyGroup = (type: string | undefined | null, group: AVKeyGroup): boolean => {+ const members = groupMembers.get(group);+ return !!members && typeof type === "string" && members.has(type);+};export const isAVRichTextType = (type: string | undefined | null) => hasAVKeyGroup(type, "richText");