Files
plainleaf/client/plugos/syscalls/index.ts
T
Zef Hemel 10898f2580 Add index.previewProcessedObjects syscall
Read access to the index pipeline validation, multi-tag  expansion, tagDefinition.transform()).
2026-05-09 12:30:08 +02:00

119 lines
3.3 KiB
TypeScript

import type { SysCallMapping } from "../system.ts";
import type {
LuaCollectionQuery,
LuaQueryCollection,
} from "../../space_lua/query_collection.ts";
import {
type ObjectIndex,
ObjectValidationError,
} from "../../data/object_index.ts";
import type { ObjectValue } from "@silverbulletmd/silverbullet/type/index";
import type { Client } from "../../client.ts";
import type { LuaTable } from "../../space_lua/runtime.ts";
export function indexSyscalls(
objectIndex: ObjectIndex,
client: Client,
): SysCallMapping {
return {
"index.tag": (_ctx, tagName: string): LuaQueryCollection => {
return objectIndex.tag(tagName);
},
"index.contentPages": (): LuaQueryCollection => {
return objectIndex.contentPages();
},
"index.metaPages": (): LuaQueryCollection => {
return objectIndex.metaPages();
},
"index.aggregates": (): LuaQueryCollection => {
return objectIndex.aggregates();
},
"index.ensureFullIndex": () => {
return objectIndex.ensureFullIndex(client.space);
},
"index.reindexSpace": () => {
return objectIndex.reindexSpace(client.space);
},
"index.indexObjects": (
_ctx,
page: string,
objects: ObjectValue[],
): Promise<void> => {
return objectIndex.indexObjects(page, objects);
},
"index.validateObjects": async (
_ctx,
page: string,
objects: ObjectValue[],
): Promise<{ error: string; object: ObjectValue } | null> => {
try {
await objectIndex.validateObjects(page, objects);
return null;
} catch (e: any) {
if (e instanceof ObjectValidationError) {
return {
error: e.message,
object: e.object,
};
} else {
throw e;
}
}
},
"index.previewProcessedObjects": (
_ctx,
page: string,
objects: ObjectValue[],
): Promise<{ tag: string; object: ObjectValue }[]> => {
return objectIndex.previewProcessedObjects(page, objects);
},
"index.getObjectByRef": (
_ctx,
page: string,
tag: string,
ref: string,
): Promise<ObjectValue | undefined> => {
return objectIndex.getObjectByRef(page, tag, ref);
},
"index.queryLuaObjects": (
_ctx,
tag: string,
query: LuaCollectionQuery,
scopedVariables?: Record<string, any>,
): Promise<ObjectValue[]> => {
return objectIndex.queryLuaObjects(
client.clientSystem.spaceLuaEnv.env,
tag,
query,
scopedVariables,
);
},
"index.deleteObject": (
_ctx,
page: string,
tag: string,
ref: string,
): Promise<void> => {
return objectIndex.deleteObject(page, tag, ref);
},
"lua:index.defineTag": (_ctx, tagDef: LuaTable) => {
// Using 'lua:' prefix to _not_ convert tagDef to a JS version (but keep original LuaTable)
if (!tagDef.has("name")) {
throw new Error("A tag name is required");
}
const currentTag = client.config.get(["tags", tagDef.get("name")], null);
if (!currentTag) {
client.config.set(["tags", tagDef.get("name")], {
name: tagDef.get("name"),
});
}
client.config.set(
["tags", tagDef.get("name"), "metatable"],
tagDef.get("metatable"),
);
},
};
}