silverbullet/common/syscalls/system.ts

169 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-07-30 23:33:33 +08:00
import type { SyscallMeta } from "../../plug-api/types.ts";
import type { SysCallMapping, System } from "../../lib/plugos/system.ts";
import type { Client } from "../../web/client.ts";
2024-07-30 23:33:33 +08:00
import type { CommandDef } from "$lib/command.ts";
import { proxySyscall } from "../../web/syscalls/util.ts";
import type { CommonSystem } from "../common_system.ts";
import { version } from "../../version.ts";
2024-07-30 23:33:33 +08:00
import type { ParseTree } from "../../plug-api/lib/tree.ts";
import type { ConfigContainer } from "../config.ts";
import type { Config } from "$type/config.ts";
2022-03-25 19:03:06 +08:00
export function systemSyscalls(
system: System<any>,
readOnlyMode: boolean,
commonSystem: CommonSystem,
configContainer: ConfigContainer,
client?: Client,
): SysCallMapping {
2023-08-27 20:13:18 +08:00
const api: SysCallMapping = {
2022-10-14 21:11:33 +08:00
"system.invokeFunction": (
2023-08-27 20:13:18 +08:00
ctx,
fullName: string, // plug.function
2023-08-27 20:13:18 +08:00
...args: any[]
) => {
const [plugName, functionName] = fullName.split(".");
if (!plugName || !functionName) {
throw Error(`Invalid function name ${fullName}`);
2023-08-28 23:12:15 +08:00
}
const plug = system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
const functionDef = plug.manifest!.functions[functionName];
2023-08-27 20:13:18 +08:00
if (!functionDef) {
throw Error(`Function ${functionName} not found`);
2023-08-27 20:13:18 +08:00
}
2023-08-30 03:17:29 +08:00
if (
client && functionDef.env && system.env &&
functionDef.env !== system.env
) {
2023-08-27 20:13:18 +08:00
// Proxy to another environment
return proxySyscall(
ctx,
client.httpSpacePrimitives,
"system.invokeFunction",
[fullName, ...args],
);
2023-08-27 20:13:18 +08:00
}
return plug.invoke(functionName, args);
2022-04-27 01:04:36 +08:00
},
"system.invokeFunctionOnServer": (
ctx,
fullName: string, // plug.function
...args: any[]
) => {
const [plugName, functionName] = fullName.split(".");
if (!plugName || !functionName) {
throw Error(`Invalid function name ${fullName}`);
}
const plug = system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
const functionDef = plug.manifest!.functions[functionName];
if (!functionDef) {
throw Error(`Function ${functionName} not found`);
}
if (!client) {
throw new Error("Not supported");
}
// Proxy to system env
return proxySyscall(
ctx,
client.httpSpacePrimitives,
"system.invokeFunction",
[fullName, ...args],
);
},
"system.invokeCommand": (_ctx, name: string, args?: string[]) => {
2023-08-30 03:17:29 +08:00
if (!client) {
throw new Error("Not supported");
}
return client.runCommandByName(name, args);
2022-07-11 15:08:22 +08:00
},
2022-10-14 21:11:33 +08:00
"system.listCommands": (): { [key: string]: CommandDef } => {
const commandHook = commonSystem!.commandHook;
2022-10-14 21:11:33 +08:00
const allCommands: { [key: string]: CommandDef } = {};
for (const [cmd, def] of commandHook.editorCommands) {
2022-09-06 20:36:06 +08:00
allCommands[cmd] = def.command;
}
return allCommands;
},
"system.listSyscalls": (): SyscallMeta[] => {
const syscalls: SyscallMeta[] = [];
for (const [name, info] of system.registeredSyscalls) {
syscalls.push({
name,
requiredPermissions: info.requiredPermissions,
argCount: Math.max(0, info.callback.length - 1),
});
}
return syscalls;
},
2022-10-14 21:11:33 +08:00
"system.reloadPlugs": () => {
2023-08-30 03:17:29 +08:00
if (!client) {
throw new Error("Not supported");
}
return client.loadPlugs();
2022-03-25 19:03:06 +08:00
},
"system.reloadConfig": async (): Promise<Config> => {
await configContainer.loadConfig();
return configContainer.config;
},
"system.loadSpaceScripts": async () => {
// Reload scripts locally
await commonSystem.loadSpaceScripts();
if (client) {
// And we are in a hybrud mode, tell the server to do the same
if (system.env === "client") {
console.info(
"Sending syscall to server to trigger space script reload",
);
await proxySyscall(
{},
client.httpSpacePrimitives,
"system.loadSpaceScripts",
[],
);
}
}
},
"system.loadSpaceStyles": async () => {
if (!client) {
throw new Error("Not supported on server");
}
await client.loadCustomStyles();
},
"system.invokeSpaceFunction": (_ctx, name: string, ...args: any[]) => {
return commonSystem.invokeSpaceFunction(name, args);
},
"system.applyAttributeExtractors": (
_ctx,
tags: string[],
text: string,
tree: ParseTree,
): Promise<Record<string, any>> => {
return commonSystem.applyAttributeExtractors(tags, text, tree);
},
"system.getSpaceConfig": (_ctx, key?: string): any => {
const config: any = configContainer.config;
if (key) {
return config[key];
} else {
return config;
}
},
2023-01-15 01:51:00 +08:00
"system.getEnv": () => {
return system.env;
},
"system.getMode": () => {
return readOnlyMode ? "ro" : "rw";
},
"system.getVersion": () => {
return version;
},
2022-03-25 19:03:06 +08:00
};
2023-08-27 20:13:18 +08:00
return api;
2022-03-25 19:03:06 +08:00
}