From 74d91883525ef1950628e7f02d7126586db983c7 Mon Sep 17 00:00:00 2001 From: Drake Stapleton <213298340+drakestapleton@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:42:52 -0600 Subject: [PATCH] feat(plugins): add isolated m3/plugin-host --- apps/server/src/m3PluginRoute.ts | 56 ++ apps/server/src/server.ts | 2 + apps/web/src/components/M3PluginSlotHost.tsx | 806 +++++++++++++++++++ apps/web/src/i18n/translations/de.json | 79 +- apps/web/src/i18n/translations/en.json | 79 +- apps/web/src/i18n/translations/es.json | 79 +- apps/web/src/i18n/translations/fr.json | 79 +- apps/web/src/i18n/translations/ja.json | 79 +- apps/web/src/i18n/translations/ko.json | 79 +- apps/web/src/i18n/translations/pt-BR.json | 79 +- apps/web/src/i18n/translations/ru.json | 79 +- apps/web/src/i18n/translations/sv.json | 79 +- apps/web/src/i18n/translations/zh.json | 79 +- apps/web/src/routes/_chat.tsx | 2 + m3/plugin-host/PluginHostManager.test.ts | 171 ++++ m3/plugin-host/bridgeProtocol.ts | 112 +++ m3/plugin-host/manifest.ts | 175 ++++ m3/plugin-host/sdk/m3-plugin-sdk.ts | 93 +++ m3/plugin-host/server/PluginHostManager.ts | 524 ++++++++++++ m3/plugin-host/server/httpHandler.ts | 202 +++++ 20 files changed, 2813 insertions(+), 120 deletions(-) create mode 100644 apps/server/src/m3PluginRoute.ts create mode 100644 apps/web/src/components/M3PluginSlotHost.tsx create mode 100644 m3/plugin-host/PluginHostManager.test.ts create mode 100644 m3/plugin-host/bridgeProtocol.ts create mode 100644 m3/plugin-host/manifest.ts create mode 100644 m3/plugin-host/sdk/m3-plugin-sdk.ts create mode 100644 m3/plugin-host/server/PluginHostManager.ts create mode 100644 m3/plugin-host/server/httpHandler.ts diff --git a/apps/server/src/m3PluginRoute.ts b/apps/server/src/m3PluginRoute.ts new file mode 100644 index 0000000000000..0560ec963c71f --- /dev/null +++ b/apps/server/src/m3PluginRoute.ts @@ -0,0 +1,56 @@ +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http"; + +import { handleM3PluginHttpRequest } from "../../../m3/plugin-host/server/httpHandler.ts"; +import { PluginHostManager } from "../../../m3/plugin-host/server/PluginHostManager.ts"; +import * as ServerConfig from "./config.ts"; + +/** + * Thin Effect HttpRouter bridge to the standalone `m3/plugin-host` server. + * Kept in its own file so upstream `apps/server/src/server.ts` only needs one line. + */ +export const routeLayer = Layer.unwrap( + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const manager = new PluginHostManager({ m3HomeDir: config.baseDir }); + + const handler = Effect.fn("M3PluginRoute.handle")(function* () { + const request = yield* HttpServerRequest.HttpServerRequest; + const hasBody = request.method !== "GET" && request.method !== "HEAD"; + const rawBytes = hasBody + ? new Uint8Array( + yield* request.arrayBuffer.pipe(Effect.orElseSucceed(() => new ArrayBuffer(0))), + ) + : new Uint8Array(0); + const response = yield* Effect.promise(() => + handleM3PluginHttpRequest(manager, { + method: request.method, + url: request.url, + headers: request.headers, + readText: async () => new TextDecoder().decode(rawBytes), + readBytes: async () => rawBytes, + }), + ); + return typeof response.body === "string" + ? HttpServerResponse.text(response.body, { + status: response.status, + headers: response.headers, + }) + : HttpServerResponse.uint8Array(response.body, { + status: response.status, + headers: response.headers, + }); + }); + + const shutdownLayer = Layer.effectDiscard( + Effect.addFinalizer(() => Effect.promise(() => manager.shutdown())), + ); + + return Layer.mergeAll( + HttpRouter.add("GET", "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/api/m3-plugins", handler()), + HttpRouter.add("*", "/api/m3-plugins/*", handler()), + shutdownLayer, + ); + }), +); diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 5cc4290e48bdd..1f0526e26b314 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -106,6 +106,7 @@ import * as DeviceHubProxy from "./device/DeviceHubProxy.ts"; import * as PreviewManager from "./preview/Manager.ts"; import * as PortScanner from "./preview/PortScanner.ts"; import * as PreviewTunnel from "./preview/PreviewTunnel.ts"; +import * as M3PluginHost from "./m3PluginRoute.ts"; import * as ServerBrowser from "./preview/ServerBrowser.ts"; import * as PreviewAgentActivity from "./preview/PreviewAgentActivity.ts"; import * as DesktopBrowserChannel from "./preview/DesktopBrowserChannel.ts"; @@ -966,6 +967,7 @@ const layerMakeRoutes = Layer.mergeAll( DeviceHubProxy.layer, ServerBrowserStream.routeLayer, PreviewTunnel.routeLayer, + M3PluginHost.routeLayer, ServerHttp.layerStaticAndDevRoute, Ws.layer, speechStreamRouteLayer, diff --git a/apps/web/src/components/M3PluginSlotHost.tsx b/apps/web/src/components/M3PluginSlotHost.tsx new file mode 100644 index 0000000000000..be404b4bf04d9 --- /dev/null +++ b/apps/web/src/components/M3PluginSlotHost.tsx @@ -0,0 +1,806 @@ +import { T, Var, useGT } from "gt-react"; +import { useEffect, useRef, useState } from "react"; + +const M3_PLUGIN_BRIDGE_VERSION = 1 as const; +const API_BASE = "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/api/m3-plugins"; + +type M3PluginSlot = "sidebar-tab" | "right-panel" | "thread-toolbar" | "modal"; + +interface M3PluginManifest { + readonly id: string; + readonly name: string; + readonly version: string; + readonly description?: string; + readonly runtime: { readonly type: "static" | "sidecar" }; + readonly slots: ReadonlyArray; + readonly subscriptions: ReadonlyArray; +} + +interface M3PluginRecord { + readonly manifest: M3PluginManifest; + readonly enabled: boolean; + readonly status: "active" | "disabled" | "starting" | "error"; + readonly entryUrl: string | null; + readonly sidecarPort: number | null; + readonly errorMessage?: string; +} + +interface PluginListResponse { + readonly pluginsDir: string; + readonly plugins: ReadonlyArray; +} + +interface BridgeCommandMessage { + readonly version: typeof M3_PLUGIN_BRIDGE_VERSION; + readonly type: "m3:command"; + readonly id: string; + readonly pluginId: string; + readonly command: + | "plugin:ready" + | "thread:create" + | "prompt:inject" + | "file:open" + | "toast:show" + | "plugins:list"; + readonly payload?: Record; +} + +const VALID_COMMANDS = new Set([ + "plugin:ready", + "thread:create", + "prompt:inject", + "file:open", + "toast:show", + "plugins:list", +]); + +function isBridgeCommand(value: unknown): value is BridgeCommandMessage { + if (typeof value !== "object" || value === null || Array.isArray(value)) return false; + const rec = value as Record; + return ( + rec.version === M3_PLUGIN_BRIDGE_VERSION && + rec.type === "m3:command" && + typeof rec.id === "string" && + typeof rec.pluginId === "string" && + typeof rec.command === "string" && + VALID_COMMANDS.has(rec.command) + ); +} + +function buildBridgeEvent(eventName: string, payload: Record) { + return { + version: M3_PLUGIN_BRIDGE_VERSION, + type: "m3:event", + event: eventName, + payload, + timestamp: new Date().toISOString(), + }; +} + +/** + * Single-line integration seam for `apps/web/src/routes/_chat.tsx`. + * Renders sandboxed `