import * as React from 'react' import { applyMnemon, mnemonServices, applyRuntime, applyDocuments, applyMemorySpaces, applySkills } from 'brain:mnemon-client' import { CardBoundary } from './components' const BRAIN_PACKAGE = 'dsh-brain' const FORK_PACKAGE = 'dsh-brain-mnemon' const SETTINGS_ID = 'dsh-brain:mnemon' const SETTINGS_SECTION = 'settings.section' const BUNDLE_CONFIG = 'plugins.bundle.config' const STANDALONE_PACKAGES = new Set(['dsh-mnemon', FORK_PACKAGE]) interface SlotOptions { name: string key?: string id?: string locale?: string priority?: number } function withOverrides(object: T, overrides: Partial): T { return new Proxy(object, { get(target, key) { if (Object.hasOwn(overrides, key)) return Reflect.get(overrides, key) const value = Reflect.get(target, key, target) return typeof value === 'function' ? value.bind(target) : value }, }) } function embeddedContext(ctx: any) { const localeNamespace = (name: string) => name === 'mnemon' ? FORK_PACKAGE : name const locale = withOverrides(ctx.locale, { bind: (name: string) => ctx.locale.bind(localeNamespace(name)), register: (name: string, ...args: unknown[]) => ctx.locale.register(localeNamespace(name), ...args), }) const slots = withOverrides(ctx.slots, { inject: (name: string, setup: () => () => void) => ctx.slots.inject(name === BUNDLE_CONFIG ? SETTINGS_SECTION : name, setup), register: (options: SlotOptions, Component: React.ComponentType) => { // A standalone client can activate before its config slot announces it. // Keep its locale dictionary independent and give its shared seats priority. const embedded = { ...options, priority: (options.priority ?? 0) + 1 } if (options.locale !== undefined) embedded.locale = localeNamespace(options.locale) if (options.name === BUNDLE_CONFIG && options.key === FORK_PACKAGE) { const { key: _key, ...section } = embedded return ctx.slots.register({ ...section, name: SETTINGS_SECTION, id: SETTINGS_ID, order: 101, label: () => 'Mnemon' }, (props: object) => React.createElement(CardBoundary as any, null, React.createElement(Component, { ...props, view: 'page' }))) } if (options.name === 'plugins.row.config' && options.key?.startsWith(FORK_PACKAGE + '#mnemon-')) { const rowId = BRAIN_PACKAGE + '-' + options.key.slice(FORK_PACKAGE.length + 1) embedded.key = BRAIN_PACKAGE + '#' + rowId } if (options.name === 'plugins.detail.actions' && options.id === FORK_PACKAGE + '/open-workspace') { return ctx.slots.register(embedded, (props: any) => { const subject = props.subject if (subject?.kind !== 'bundle' || subject.pkg.name !== BRAIN_PACKAGE) return null return React.createElement(Component, { ...props, subject: { ...subject, pkg: { ...subject.pkg, name: FORK_PACKAGE } } }) }) } return ctx.slots.register(embedded, Component) }, }) // The real child owns optional injections and all effects. Adapt only the // upstream plugin-manager navigation target to this package's bundle page. return withOverrides(ctx, { slots, locale, inject: (names: string[], setup: (inner: any) => void) => ctx.inject(names, (inner: any) => { if (!names.includes('pluginNavigation')) return setup(inner) const pluginNavigation = withOverrides(inner.pluginNavigation, { openBundle: () => inner.pluginNavigation.openBundle(BRAIN_PACKAGE) }) return setup(withOverrides(inner, { pluginNavigation })) }), }) } // The workbench's configuration action opens the Brain plugin detail page. // Read the same live settings contribution that the Settings pane renders. export function MnemonBundleSettings({ ctx }: { ctx: any }) { const subscribe = React.useCallback((listener: () => void) => ctx.slots.subscribe?.(SETTINGS_SECTION, listener) ?? (() => {}), [ctx]) const getSnapshot = React.useCallback(() => ctx.slots.entriesOfSlot?.(SETTINGS_SECTION).find((entry: any) => entry.options.id === SETTINGS_ID), [ctx]) const entry = React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot) return entry === undefined ? null : React.createElement(entry.component, entry.inject?.()) } export function mountMnemonWorkbench(ctx: any): void { if (typeof ctx.inject !== 'function') return ctx.inject(mnemonServices, (host: any) => { const standalone = (): boolean => host.slots.entriesOfSlot(BUNDLE_CONFIG).some((entry: { options: SlotOptions }) => STANDALONE_PACKAGES.has(entry.options.key ?? '')) let active: { dispose(): Promise } | undefined let stopping: Promise | undefined let disposed = false const stop = (after?: () => void): Promise | undefined => { if (active === undefined) return stopping const previous = active active = undefined stopping = previous.dispose().catch(error => console.error('[dsh-brain] Mnemon workbench cleanup failed', error)).then(() => { stopping = undefined after?.() }) return stopping } const reconcile = (): void => { if (disposed || stopping !== undefined) return if (standalone()) { void stop(reconcile) } else if (active === undefined) { active = host.plugin({ name: 'dsh-brain: Mnemon workbench', apply(child: any) { const embedded = embeddedContext(child) applyMnemon(embedded) applyRuntime(embedded) applyDocuments(embedded) applyMemorySpaces(embedded) applySkills(embedded) } }) // Cordis reports activation failures through the child fiber. Dispose // its partial registrations while keeping Brain's independent UI up. const started = active void Promise.resolve(started).catch(error => { console.error('[dsh-brain] Mnemon workbench registration failed', error) if (active === started) void stop() }) } } host.effect(() => { const unsubscribe = host.slots.subscribe(BUNDLE_CONFIG, reconcile) reconcile() return async () => { disposed = true unsubscribe() await stop() } }, 'dsh-brain: Mnemon workbench availability') }) }