silverbullet/web/client_system.ts

244 lines
7.9 KiB
TypeScript
Raw Normal View History

2023-07-14 22:48:35 +08:00
import { PlugNamespaceHook } from "../common/hooks/plug_namespace.ts";
2024-01-24 20:34:12 +08:00
import { SilverBulletHooks } from "../common/manifest.ts";
2023-07-14 19:44:30 +08:00
import { CronHook } from "../plugos/hooks/cron.ts";
import { EventHook } from "../plugos/hooks/event.ts";
2024-01-14 20:38:39 +08:00
import { createSandbox } from "../plugos/sandboxes/web_worker_sandbox.ts";
2023-07-14 19:44:30 +08:00
import assetSyscalls from "../plugos/syscalls/asset.ts";
import { eventSyscalls } from "../plugos/syscalls/event.ts";
import { System } from "../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";
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";
import { markdownSyscalls } from "../common/syscalls/markdown.ts";
2023-07-14 19:44:30 +08:00
import { shellSyscalls } from "./syscalls/shell.ts";
import { spaceReadSyscalls, spaceWriteSyscalls } from "./syscalls/space.ts";
2023-07-14 19:44:30 +08:00
import { syncSyscalls } from "./syscalls/sync.ts";
import { systemSyscalls } from "../common/syscalls/system.ts";
import { yamlSyscalls } from "../common/syscalls/yaml.ts";
2023-07-14 19:44:30 +08:00
import { Space } from "./space.ts";
2023-08-11 00:32:41 +08:00
import { MQHook } from "../plugos/hooks/mq.ts";
import { mqSyscalls } from "../plugos/syscalls/mq.ts";
2023-08-28 23:12:15 +08:00
import { mqProxySyscalls } from "./syscalls/mq.proxy.ts";
import { dataStoreProxySyscalls } from "./syscalls/datastore.proxy.ts";
import {
dataStoreReadSyscalls,
dataStoreWriteSyscalls,
} from "../plugos/syscalls/datastore.ts";
import { DataStore } from "../plugos/lib/datastore.ts";
import { MessageQueue } from "../plugos/lib/mq.ts";
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";
2023-12-12 21:15:21 +08:00
import { KVPrimitivesManifestCache } from "../plugos/manifest_cache.ts";
import { deepObjectMerge } from "$sb/lib/json.ts";
2023-12-22 01:37:50 +08:00
import { Query } from "$sb/types.ts";
import { PanelWidgetHook } from "./hooks/panel_widget.ts";
import { createKeyBindings } from "./editor_state.ts";
import { CommonSystem } from "../common/common_system.ts";
import { DataStoreMQ } from "../plugos/lib/mq.datastore.ts";
const plugNameExtractRegex = /\/(.+)\.plug\.js$/;
2023-07-14 19:44:30 +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,
mq: DataStoreMQ,
ds: DataStore,
eventHook: EventHook,
readOnlyMode: boolean,
2023-07-14 19:44:30 +08:00
) {
super(
mq,
ds,
eventHook,
readOnlyMode,
window.silverBulletConfig.enableSpaceScript,
);
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)
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);
// 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
this.commandHook = new CommandHook(
this.readOnlyMode,
this.spaceScriptCommands,
);
2023-07-14 19:44:30 +08:00
this.commandHook.on({
commandsUpdated: (commandMap) => {
this.client.ui?.viewDispatch({
2023-07-14 19:44:30 +08:00
type: "update-commands",
commands: commandMap,
});
// 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-12 21:15:21 +08:00
this.system.on({
plugLoaded: (plug) => {
// Apply plug overrides
const manifestOverrides = this.client.settings.plugOverrides;
if (manifestOverrides && manifestOverrides[plug.manifest!.name]) {
plug.manifest = deepObjectMerge(
plug.manifest,
manifestOverrides[plug.manifest!.name],
);
2023-12-12 21:16:57 +08:00
// console.log("New manifest", plug.manifest);
2023-12-12 21:15:21 +08:00
}
},
});
this.eventHook.addLocalListener(
"file:changed",
async (path: string, _selfUpdate, _oldHash, newHash) => {
if (path.startsWith("_plug/") && path.endsWith(".plug.js")) {
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(
plugName,
createSandbox(new URL(`/${path}`, location.href)),
newHash,
);
2023-08-30 03:17:29 +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),
spaceReadSyscalls(this.client),
systemSyscalls(this.system, false, this, this.client),
2024-01-24 20:34:12 +08:00
markdownSyscalls(),
2023-07-14 19:44:30 +08:00
assetSyscalls(this.system),
yamlSyscalls(),
templateSyscalls(this.ds),
2023-10-31 17:33:38 +08:00
codeWidgetSyscalls(this.codeWidgetHook),
2023-11-15 17:08:21 +08:00
clientCodeWidgetSyscalls(),
languageSyscalls(),
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),
...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),
clientStoreSyscalls(this.ds),
2023-07-14 19:44:30 +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");
// await space.updatePageList();
2023-07-14 19:44:30 +08:00
await this.system.unloadAll();
console.log("(Re)loading plugs");
await Promise.all((await space.listPlugs()).map(async (plugMeta) => {
2023-07-14 19:44:30 +08:00
try {
const plugName = plugNameExtractRegex.exec(plugMeta.name)![1];
2023-07-14 19:44:30 +08:00
await this.system.load(
plugName,
createSandbox(new URL(plugMeta.name, location.origin)),
plugMeta.lastModified,
2023-07-14 19:44:30 +08:00
);
} catch (e: any) {
console.error(
"Could not load plug",
plugMeta.name,
"error:",
e.message,
);
2023-07-14 19:44:30 +08:00
}
}));
}
localSyscall(name: string, args: any[]) {
return this.system.localSyscall(name, args);
2023-12-22 01:37:50 +08:00
}
queryObjects<T>(tag: string, query: Query): Promise<T[]> {
return this.localSyscall(
2023-12-22 01:37:50 +08:00
"system.invokeFunction",
["index.queryObjects", tag, query],
2023-12-22 01:37:50 +08:00
);
2023-07-14 19:44:30 +08:00
}
}