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";
|
2024-01-27 00:05:10 +08:00
|
|
|
import type { Client } from "../../web/client.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { CommandDef } from "$lib/command.ts";
|
2024-01-27 00:05:10 +08:00
|
|
|
import { proxySyscall } from "../../web/syscalls/util.ts";
|
2024-02-07 21:50:01 +08:00
|
|
|
import type { CommonSystem } from "../common_system.ts";
|
2024-02-18 06:32:09 +08:00
|
|
|
import { version } from "../../version.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { ParseTree } from "../../plug-api/lib/tree.ts";
|
2024-08-02 22:47:36 +08:00
|
|
|
import type { ConfigContainer } from "../config.ts";
|
|
|
|
import type { Config } from "$type/config.ts";
|
2022-03-25 19:03:06 +08:00
|
|
|
|
2022-10-28 22:17:40 +08:00
|
|
|
export function systemSyscalls(
|
|
|
|
system: System<any>,
|
2024-01-27 00:05:10 +08:00
|
|
|
readOnlyMode: boolean,
|
2024-02-07 21:50:01 +08:00
|
|
|
commonSystem: CommonSystem,
|
2024-08-02 22:47:36 +08:00
|
|
|
configContainer: ConfigContainer,
|
2024-02-07 21:50:01 +08:00
|
|
|
client?: Client,
|
2022-10-28 22:17:40 +08:00
|
|
|
): 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,
|
2024-01-15 23:43:12 +08:00
|
|
|
fullName: string, // plug.function
|
2023-08-27 20:13:18 +08:00
|
|
|
...args: any[]
|
2022-04-04 00:42:12 +08:00
|
|
|
) => {
|
2024-01-15 23:43:12 +08:00
|
|
|
const [plugName, functionName] = fullName.split(".");
|
|
|
|
if (!plugName || !functionName) {
|
|
|
|
throw Error(`Invalid function name ${fullName}`);
|
2023-08-28 23:12:15 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
const plug = system.loadedPlugs.get(plugName);
|
|
|
|
if (!plug) {
|
|
|
|
throw Error(`Plug ${plugName} not found`);
|
2022-10-28 22:17:40 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
const functionDef = plug.manifest!.functions[functionName];
|
2023-08-27 20:13:18 +08:00
|
|
|
if (!functionDef) {
|
2024-01-15 23:43:12 +08:00
|
|
|
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
|
2023-10-03 20:16:33 +08:00
|
|
|
return proxySyscall(
|
|
|
|
ctx,
|
2023-12-17 18:46:18 +08:00
|
|
|
client.httpSpacePrimitives,
|
2023-10-03 20:16:33 +08:00
|
|
|
"system.invokeFunction",
|
|
|
|
[fullName, ...args],
|
|
|
|
);
|
2023-08-27 20:13:18 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
return plug.invoke(functionName, args);
|
2022-04-27 01:04:36 +08:00
|
|
|
},
|
2024-07-24 17:26:12 +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],
|
|
|
|
);
|
|
|
|
},
|
2023-11-26 01:57:00 +08:00
|
|
|
"system.invokeCommand": (_ctx, name: string, args?: string[]) => {
|
2023-08-30 03:17:29 +08:00
|
|
|
if (!client) {
|
|
|
|
throw new Error("Not supported");
|
|
|
|
}
|
2023-11-26 01:57:00 +08:00
|
|
|
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 } => {
|
2024-02-07 21:50:01 +08:00
|
|
|
const commandHook = commonSystem!.commandHook;
|
2022-10-14 21:11:33 +08:00
|
|
|
const allCommands: { [key: string]: CommandDef } = {};
|
2024-02-06 23:51:04 +08:00
|
|
|
for (const [cmd, def] of commandHook.editorCommands) {
|
2022-09-06 20:36:06 +08:00
|
|
|
allCommands[cmd] = def.command;
|
|
|
|
}
|
|
|
|
return allCommands;
|
|
|
|
},
|
2024-02-06 23:51:04 +08:00
|
|
|
"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
|
|
|
},
|
2024-08-02 22:47:36 +08:00
|
|
|
"system.reloadConfig": async (): Promise<Config> => {
|
|
|
|
await configContainer.loadConfig();
|
|
|
|
return configContainer.config;
|
|
|
|
},
|
2024-02-06 23:51:04 +08:00
|
|
|
"system.loadSpaceScripts": async () => {
|
2024-02-07 21:50:01 +08:00
|
|
|
// Reload scripts locally
|
|
|
|
await commonSystem.loadSpaceScripts();
|
2024-02-06 23:51:04 +08:00
|
|
|
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",
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-03-09 19:26:58 +08:00
|
|
|
"system.loadSpaceStyles": async () => {
|
|
|
|
if (!client) {
|
|
|
|
throw new Error("Not supported on server");
|
|
|
|
}
|
|
|
|
await client.loadCustomStyles();
|
|
|
|
},
|
2024-02-28 03:05:12 +08:00
|
|
|
"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);
|
|
|
|
},
|
2024-08-02 22:47:36 +08:00
|
|
|
"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;
|
|
|
|
},
|
2024-01-27 00:05:10 +08:00
|
|
|
"system.getMode": () => {
|
|
|
|
return readOnlyMode ? "ro" : "rw";
|
|
|
|
},
|
2024-02-18 06:32:09 +08:00
|
|
|
"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
|
|
|
}
|