2024-02-09 04:00:45 +08:00
|
|
|
import { PlugNamespaceHook } from "$common/hooks/plug_namespace.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { SilverBulletHooks } from "../lib/manifest.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { CronHook } from "../lib/plugos/hooks/cron.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { EventHook } from "../common/hooks/event.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { createSandbox } from "../lib/plugos/sandboxes/web_worker_sandbox.ts";
|
2023-07-14 19:44:30 +08:00
|
|
|
|
2024-02-09 04:00:45 +08:00
|
|
|
import assetSyscalls from "../lib/plugos/syscalls/asset.ts";
|
|
|
|
import { eventSyscalls } from "../lib/plugos/syscalls/event.ts";
|
|
|
|
import { System } from "../lib/plugos/system.ts";
|
2023-07-14 22:56:20 +08:00
|
|
|
import type { Client } from "./client.ts";
|
2023-07-14 19:44:30 +08:00
|
|
|
import { CodeWidgetHook } from "./hooks/code_widget.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { CommandHook } from "$common/hooks/command.ts";
|
2023-07-14 19:44:30 +08:00
|
|
|
import { SlashCommandHook } from "./hooks/slash_command.ts";
|
|
|
|
import { clientStoreSyscalls } from "./syscalls/clientStore.ts";
|
|
|
|
import { debugSyscalls } from "./syscalls/debug.ts";
|
|
|
|
import { editorSyscalls } from "./syscalls/editor.ts";
|
|
|
|
import { sandboxFetchSyscalls } from "./syscalls/fetch.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { markdownSyscalls } from "$common/syscalls/markdown.ts";
|
2023-07-14 19:44:30 +08:00
|
|
|
import { shellSyscalls } from "./syscalls/shell.ts";
|
2024-01-27 00:05:10 +08:00
|
|
|
import { spaceReadSyscalls, spaceWriteSyscalls } from "./syscalls/space.ts";
|
2023-07-14 19:44:30 +08:00
|
|
|
import { syncSyscalls } from "./syscalls/sync.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { systemSyscalls } from "$common/syscalls/system.ts";
|
|
|
|
import { yamlSyscalls } from "$common/syscalls/yaml.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { Space } from "../common/space.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { MQHook } from "../lib/plugos/hooks/mq.ts";
|
|
|
|
import { mqSyscalls } from "../lib/plugos/syscalls/mq.ts";
|
2023-08-28 23:12:15 +08:00
|
|
|
import { mqProxySyscalls } from "./syscalls/mq.proxy.ts";
|
2023-10-03 20:16:33 +08:00
|
|
|
import { dataStoreProxySyscalls } from "./syscalls/datastore.proxy.ts";
|
2024-01-27 00:05:10 +08:00
|
|
|
import {
|
|
|
|
dataStoreReadSyscalls,
|
|
|
|
dataStoreWriteSyscalls,
|
2024-02-09 04:00:45 +08:00
|
|
|
} from "../lib/plugos/syscalls/datastore.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { DataStore } from "$lib/data/datastore.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { languageSyscalls } from "$common/syscalls/language.ts";
|
|
|
|
import { templateSyscalls } from "$common/syscalls/template.ts";
|
2023-10-31 17:33:38 +08:00
|
|
|
import { codeWidgetSyscalls } from "./syscalls/code_widget.ts";
|
2023-11-15 17:08:21 +08:00
|
|
|
import { clientCodeWidgetSyscalls } from "./syscalls/client_code_widget.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { KVPrimitivesManifestCache } from "$lib/plugos/manifest_cache.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { Query } from "../plug-api/types.ts";
|
2023-12-28 01:05:47 +08:00
|
|
|
import { PanelWidgetHook } from "./hooks/panel_widget.ts";
|
2024-01-21 02:16:07 +08:00
|
|
|
import { createKeyBindings } from "./editor_state.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { CommonSystem } from "$common/common_system.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { DataStoreMQ } from "$lib/data/mq.datastore.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { plugPrefix } from "$common/spaces/constants.ts";
|
2024-08-02 22:47:36 +08:00
|
|
|
import { jsonschemaSyscalls } from "$common/syscalls/jsonschema.ts";
|
2024-10-03 23:55:51 +08:00
|
|
|
import { luaSyscalls } from "$common/syscalls/lua.ts";
|
2023-12-07 01:44:48 +08:00
|
|
|
|
|
|
|
const plugNameExtractRegex = /\/(.+)\.plug\.js$/;
|
2023-07-14 19:44:30 +08:00
|
|
|
|
2024-02-07 21:50:01 +08:00
|
|
|
/**
|
|
|
|
* Wrapper around a System, used by the client
|
|
|
|
*/
|
|
|
|
export class ClientSystem extends CommonSystem {
|
2023-07-14 19:44:30 +08:00
|
|
|
constructor(
|
2023-08-26 14:31:51 +08:00
|
|
|
private client: Client,
|
2024-02-07 21:50:01 +08:00
|
|
|
mq: DataStoreMQ,
|
|
|
|
ds: DataStore,
|
|
|
|
eventHook: EventHook,
|
|
|
|
readOnlyMode: boolean,
|
2023-07-14 19:44:30 +08:00
|
|
|
) {
|
2024-02-07 21:50:01 +08:00
|
|
|
super(
|
|
|
|
mq,
|
|
|
|
ds,
|
|
|
|
eventHook,
|
|
|
|
readOnlyMode,
|
2024-10-10 18:52:28 +08:00
|
|
|
globalThis.silverBulletConfig.enableSpaceScript,
|
2024-02-07 21:50:01 +08:00
|
|
|
);
|
2023-08-27 20:13:18 +08:00
|
|
|
// Only set environment to "client" when running in thin client mode, otherwise we run everything locally (hybrid)
|
2023-12-07 01:44:48 +08:00
|
|
|
this.system = new System(
|
|
|
|
client.syncMode ? undefined : "client",
|
|
|
|
{
|
|
|
|
manifestCache: new KVPrimitivesManifestCache<SilverBulletHooks>(
|
|
|
|
ds.kv,
|
|
|
|
"manifest",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
);
|
2023-08-27 20:13:18 +08:00
|
|
|
|
2023-07-14 19:44:30 +08:00
|
|
|
this.system.addHook(this.eventHook);
|
|
|
|
|
2023-07-14 22:48:35 +08:00
|
|
|
// Plug page namespace hook
|
|
|
|
this.namespaceHook = new PlugNamespaceHook();
|
|
|
|
this.system.addHook(this.namespaceHook);
|
2023-07-14 19:44:30 +08:00
|
|
|
|
|
|
|
// Cron hook
|
|
|
|
const cronHook = new CronHook(this.system);
|
|
|
|
this.system.addHook(cronHook);
|
|
|
|
|
|
|
|
// Code widget hook
|
|
|
|
this.codeWidgetHook = new CodeWidgetHook();
|
|
|
|
this.system.addHook(this.codeWidgetHook);
|
|
|
|
|
2023-12-28 01:05:47 +08:00
|
|
|
// Panel widget hook
|
|
|
|
this.panelWidgetHook = new PanelWidgetHook();
|
|
|
|
this.system.addHook(this.panelWidgetHook);
|
|
|
|
|
2023-08-11 00:32:41 +08:00
|
|
|
// MQ hook
|
2023-08-30 03:17:29 +08:00
|
|
|
if (client.syncMode) {
|
|
|
|
// Process MQ messages locally
|
2023-08-28 23:12:15 +08:00
|
|
|
this.system.addHook(new MQHook(this.system, this.mq));
|
|
|
|
}
|
2023-08-11 00:32:41 +08:00
|
|
|
|
2023-07-14 19:44:30 +08:00
|
|
|
// Command hook
|
2024-02-07 21:50:01 +08:00
|
|
|
this.commandHook = new CommandHook(
|
|
|
|
this.readOnlyMode,
|
|
|
|
this.spaceScriptCommands,
|
|
|
|
);
|
2023-07-14 19:44:30 +08:00
|
|
|
this.commandHook.on({
|
|
|
|
commandsUpdated: (commandMap) => {
|
2023-10-03 20:16:33 +08:00
|
|
|
this.client.ui?.viewDispatch({
|
2023-07-14 19:44:30 +08:00
|
|
|
type: "update-commands",
|
|
|
|
commands: commandMap,
|
|
|
|
});
|
2024-01-21 02:16:07 +08:00
|
|
|
// Replace the key mapping compartment (keybindings)
|
|
|
|
this.client.editorView.dispatch({
|
|
|
|
effects: this.client.keyHandlerCompartment?.reconfigure(
|
|
|
|
createKeyBindings(this.client),
|
|
|
|
),
|
|
|
|
});
|
2023-07-14 19:44:30 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
this.system.addHook(this.commandHook);
|
|
|
|
|
|
|
|
// Slash command hook
|
2023-08-26 14:31:51 +08:00
|
|
|
this.slashCommandHook = new SlashCommandHook(this.client);
|
2023-07-14 19:44:30 +08:00
|
|
|
this.system.addHook(this.slashCommandHook);
|
|
|
|
|
2023-12-07 01:44:48 +08:00
|
|
|
this.eventHook.addLocalListener(
|
|
|
|
"file:changed",
|
|
|
|
async (path: string, _selfUpdate, _oldHash, newHash) => {
|
2024-02-09 04:00:45 +08:00
|
|
|
if (path.startsWith(plugPrefix) && path.endsWith(".plug.js")) {
|
2023-12-07 01:44:48 +08:00
|
|
|
const plugName = plugNameExtractRegex.exec(path)![1];
|
|
|
|
console.log("Plug updated, reloading", plugName, "from", path);
|
|
|
|
this.system.unload(path);
|
2024-01-24 20:34:12 +08:00
|
|
|
await this.system.load(
|
2023-12-07 01:44:48 +08:00
|
|
|
plugName,
|
2024-01-15 23:43:12 +08:00
|
|
|
createSandbox(new URL(`/${path}`, location.href)),
|
2023-12-07 01:44:48 +08:00
|
|
|
newHash,
|
|
|
|
);
|
2023-08-30 03:17:29 +08:00
|
|
|
}
|
2023-12-07 01:44:48 +08:00
|
|
|
},
|
|
|
|
);
|
2023-07-14 19:44:30 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 20:34:12 +08:00
|
|
|
init() {
|
2023-07-14 19:44:30 +08:00
|
|
|
// Slash command hook
|
2023-08-26 14:31:51 +08:00
|
|
|
this.slashCommandHook = new SlashCommandHook(this.client);
|
2023-07-14 19:44:30 +08:00
|
|
|
this.system.addHook(this.slashCommandHook);
|
|
|
|
|
|
|
|
// Syscalls available to all plugs
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
eventSyscalls(this.eventHook),
|
2023-08-26 14:31:51 +08:00
|
|
|
editorSyscalls(this.client),
|
2024-01-27 00:05:10 +08:00
|
|
|
spaceReadSyscalls(this.client),
|
2024-08-02 22:47:36 +08:00
|
|
|
systemSyscalls(this.system, false, this, this.client, this.client),
|
2024-01-24 20:34:12 +08:00
|
|
|
markdownSyscalls(),
|
2023-07-14 19:44:30 +08:00
|
|
|
assetSyscalls(this.system),
|
|
|
|
yamlSyscalls(),
|
2024-02-06 23:51:04 +08:00
|
|
|
templateSyscalls(this.ds),
|
2023-10-31 17:33:38 +08:00
|
|
|
codeWidgetSyscalls(this.codeWidgetHook),
|
2023-11-15 17:08:21 +08:00
|
|
|
clientCodeWidgetSyscalls(),
|
2023-10-03 20:16:33 +08:00
|
|
|
languageSyscalls(),
|
2024-08-02 22:47:36 +08:00
|
|
|
jsonschemaSyscalls(),
|
2024-10-03 23:55:51 +08:00
|
|
|
luaSyscalls(),
|
2023-08-30 03:17:29 +08:00
|
|
|
this.client.syncMode
|
|
|
|
// In sync mode handle locally
|
|
|
|
? mqSyscalls(this.mq)
|
|
|
|
// In non-sync mode proxy to server
|
|
|
|
: mqProxySyscalls(this.client),
|
2024-01-27 00:05:10 +08:00
|
|
|
...this.client.syncMode
|
|
|
|
? [dataStoreReadSyscalls(this.ds), dataStoreWriteSyscalls(this.ds)]
|
|
|
|
: [dataStoreProxySyscalls(this.client)],
|
|
|
|
debugSyscalls(this.client),
|
2023-08-26 14:31:51 +08:00
|
|
|
syncSyscalls(this.client),
|
2023-10-03 20:16:33 +08:00
|
|
|
clientStoreSyscalls(this.ds),
|
2023-07-14 19:44:30 +08:00
|
|
|
);
|
|
|
|
|
2024-01-27 00:05:10 +08:00
|
|
|
if (!this.readOnlyMode) {
|
|
|
|
// Write syscalls
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
spaceWriteSyscalls(this.client),
|
|
|
|
);
|
|
|
|
// Syscalls that require some additional permissions
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
["fetch"],
|
|
|
|
sandboxFetchSyscalls(this.client),
|
|
|
|
);
|
|
|
|
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
["shell"],
|
|
|
|
shellSyscalls(this.client),
|
|
|
|
);
|
|
|
|
}
|
2023-07-14 19:44:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async reloadPlugsFromSpace(space: Space) {
|
|
|
|
console.log("Loading plugs");
|
2023-12-22 18:27:07 +08:00
|
|
|
// await space.updatePageList();
|
2023-07-14 19:44:30 +08:00
|
|
|
await this.system.unloadAll();
|
|
|
|
console.log("(Re)loading plugs");
|
2023-12-07 01:44:48 +08:00
|
|
|
await Promise.all((await space.listPlugs()).map(async (plugMeta) => {
|
2023-07-14 19:44:30 +08:00
|
|
|
try {
|
2023-12-07 01:44:48 +08:00
|
|
|
const plugName = plugNameExtractRegex.exec(plugMeta.name)![1];
|
2023-07-14 19:44:30 +08:00
|
|
|
await this.system.load(
|
2023-12-07 01:44:48 +08:00
|
|
|
plugName,
|
2024-01-15 23:43:12 +08:00
|
|
|
createSandbox(new URL(plugMeta.name, location.origin)),
|
2023-12-07 01:44:48 +08:00
|
|
|
plugMeta.lastModified,
|
2023-07-14 19:44:30 +08:00
|
|
|
);
|
|
|
|
} catch (e: any) {
|
2023-12-07 01:44:48 +08:00
|
|
|
console.error(
|
|
|
|
"Could not load plug",
|
|
|
|
plugMeta.name,
|
|
|
|
"error:",
|
|
|
|
e.message,
|
|
|
|
);
|
2023-07-14 19:44:30 +08:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
localSyscall(name: string, args: any[]) {
|
2024-01-15 23:43:12 +08:00
|
|
|
return this.system.localSyscall(name, args);
|
2023-12-22 01:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
queryObjects<T>(tag: string, query: Query): Promise<T[]> {
|
2024-01-15 23:43:12 +08:00
|
|
|
return this.localSyscall(
|
2023-12-22 01:37:50 +08:00
|
|
|
"system.invokeFunction",
|
2024-01-15 23:43:12 +08:00
|
|
|
["index.queryObjects", tag, query],
|
2023-12-22 01:37:50 +08:00
|
|
|
);
|
2023-07-14 19:44:30 +08:00
|
|
|
}
|
2024-07-13 20:55:35 +08:00
|
|
|
|
|
|
|
getObjectByRef<T>(page: string, tag: string, ref: string) {
|
|
|
|
return this.localSyscall(
|
|
|
|
"system.invokeFunction",
|
|
|
|
["index.getObjectByRef", page, tag, ref],
|
|
|
|
);
|
|
|
|
}
|
2023-07-14 19:44:30 +08:00
|
|
|
}
|