2024-03-16 22:29:24 +08:00
|
|
|
import type { CommandDef } from "../../lib/command.ts";
|
2024-02-29 22:23:05 +08:00
|
|
|
import type { SyscallMeta } from "../types.ts";
|
|
|
|
import type { ParseTree } from "../lib/tree.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { syscall } from "../syscall.ts";
|
2024-08-07 02:11:38 +08:00
|
|
|
import type { Config } from "../../type/config.ts";
|
2022-04-01 23:07:08 +08:00
|
|
|
|
2024-08-09 15:27:58 +08:00
|
|
|
/**
|
|
|
|
* System level syscalls
|
|
|
|
* @module
|
|
|
|
*/
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Invoke a plug function
|
|
|
|
* @param name a string representing the name of the function to invoke ("plug.functionName")
|
|
|
|
* @param args arguments to pass to the function
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-10-10 20:50:21 +08:00
|
|
|
export function invokeFunction(
|
2022-04-01 23:07:08 +08:00
|
|
|
name: string,
|
|
|
|
...args: any[]
|
|
|
|
): Promise<any> {
|
2023-08-28 23:12:15 +08:00
|
|
|
return syscall("system.invokeFunction", name, ...args);
|
2022-04-01 23:07:08 +08:00
|
|
|
}
|
2022-04-27 01:04:36 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Invoke a client command by name
|
|
|
|
* Note: only available on the client
|
|
|
|
* @param name name of the command
|
|
|
|
* @param args arguments to pass to the command
|
|
|
|
*/
|
2023-11-26 01:57:00 +08:00
|
|
|
export function invokeCommand(name: string, args?: string[]): Promise<any> {
|
|
|
|
return syscall("system.invokeCommand", name, args);
|
2022-07-11 15:08:22 +08:00
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Lists all commands available
|
|
|
|
* @returns a map of all available commands
|
|
|
|
*/
|
|
|
|
export function listCommands(): Promise<Record<string, CommandDef>> {
|
2022-09-06 20:36:06 +08:00
|
|
|
return syscall("system.listCommands");
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Lists all syscalls available
|
|
|
|
* @returns a list of all available syscalls
|
|
|
|
*/
|
2024-02-06 23:51:04 +08:00
|
|
|
export function listSyscalls(): Promise<SyscallMeta[]> {
|
|
|
|
return syscall("system.listSyscalls");
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Invoke a space function by name
|
|
|
|
* @param name a string representing the name of the function to invoke
|
|
|
|
* @param args arguments to pass to the function
|
|
|
|
* @returns the value returned by the function
|
|
|
|
*/
|
2024-02-28 03:05:12 +08:00
|
|
|
export function invokeSpaceFunction(
|
|
|
|
name: string,
|
|
|
|
...args: any[]
|
|
|
|
): Promise<any> {
|
|
|
|
return syscall("system.invokeSpaceFunction", name, ...args);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Applies attribute extractors to a ParseTree
|
|
|
|
*/
|
2024-02-28 03:05:12 +08:00
|
|
|
export function applyAttributeExtractors(
|
|
|
|
tags: string[],
|
|
|
|
text: string,
|
|
|
|
tree: ParseTree,
|
|
|
|
): Promise<Record<string, any>[]> {
|
|
|
|
return syscall("system.applyAttributeExtractors", tags, text, tree);
|
|
|
|
}
|
|
|
|
|
2024-08-02 22:47:36 +08:00
|
|
|
/**
|
|
|
|
* Loads a particular space configuration key (or all of them when no key is spacified)
|
|
|
|
* @param key the key to load, when not specified, all keys are loaded
|
2024-08-07 19:27:25 +08:00
|
|
|
* @param defaultValue the default value to return when the key is not found
|
2024-08-02 22:47:36 +08:00
|
|
|
* @returns either the value of the key or all keys as a Record<string, any>
|
|
|
|
*/
|
|
|
|
export async function getSpaceConfig(
|
|
|
|
key?: string,
|
|
|
|
defaultValue?: any,
|
|
|
|
): Promise<any> {
|
|
|
|
return (await syscall("system.getSpaceConfig", key)) ?? defaultValue;
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Trigger a reload of all plugs
|
|
|
|
* @returns
|
|
|
|
*/
|
2024-07-30 23:24:17 +08:00
|
|
|
export function reloadPlugs(): Promise<void> {
|
2024-02-28 03:05:12 +08:00
|
|
|
return syscall("system.reloadPlugs");
|
2022-04-27 01:04:36 +08:00
|
|
|
}
|
2023-01-15 01:51:00 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Trigger an explicit reload of the configuration
|
|
|
|
* @returns the new configuration
|
|
|
|
*/
|
2024-08-02 22:47:36 +08:00
|
|
|
export function reloadConfig(): Promise<Config> {
|
|
|
|
return syscall("system.reloadConfig");
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Returns what runtime environment this plug is run in, e.g. "server" or "client" can be undefined, which would mean a hybrid environment (such as mobile)
|
|
|
|
*/
|
2023-01-15 01:51:00 +08:00
|
|
|
export function getEnv(): Promise<string | undefined> {
|
|
|
|
return syscall("system.getEnv");
|
|
|
|
}
|
2024-01-27 00:05:10 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Returns the current mode of the system, either "ro" (read-only) or "rw" (read-write)
|
|
|
|
*/
|
2024-01-27 00:05:10 +08:00
|
|
|
export function getMode(): Promise<"ro" | "rw"> {
|
|
|
|
return syscall("system.getMode");
|
|
|
|
}
|
2024-02-18 06:32:09 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Returns the SilverBullet version
|
|
|
|
*/
|
2024-02-18 06:32:09 +08:00
|
|
|
export function getVersion(): Promise<string> {
|
|
|
|
return syscall("system.getVersion");
|
|
|
|
}
|