🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { useEffect, useState } from 'react';
import { mcpAppActions } from '@/store/mcpApp/action';
import { mcpAppContext } from '@/store/mcpApp/context';
import { mcpAppSelectors, useMcpAppStore } from '@/store/mcpApp/store';
import { serverConfigSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useToolStore } from '@/store/tool';
import { resolveDisplayMode } from './displayMode';
import type { McpAppViewProps } from './McpAppView';
import { pickAppResource } from './utils';
export interface AppPresentationOptions {
agentId?: string;
/** The tool call's arguments: they name the account it ran as. */
arguments?: string;
icon?: string;
identifier: string;
location: 'inline' | 'fullscreen';
messageId?: string;
onThisDevice?: boolean;
preferred?: 'inline' | 'fullscreen';
resourceUri: string;
scope: string;
server?: string;
title?: string;
toolCallId: string;
}
// Numbers host instances, so a frame knows which one mounted it
let lastHostInstance = 0;
/** Ownership and protocol callbacks shared by the inline and sidebar hosts. */
export const useMcpAppPresentation = ({
agentId,
arguments: toolArguments,
icon,
identifier,
location,
messageId,
onThisDevice = false,
preferred,
resourceUri,
scope,
server,
title,
toolCallId,
}: AppPresentationOptions) => {
const enabled = useServerConfigStore(serverConfigSelectors.enableMcpApps);
const view = useMcpAppStore(mcpAppSelectors.view(toolCallId));
const panelAvailable = useMcpAppStore(mcpAppSelectors.panelAvailable(scope));
useEffect(() => {
if (enabled)
mcpAppActions.registerView(toolCallId, {
identifier,
messageId,
scope,
title: title || identifier,
icon,
...(location === 'fullscreen' && { inPanel: true }),
});
}, [enabled, toolCallId, identifier, messageId, scope, title, icon, location]);
// Where the view lives follows what the tool and its resource declare,
// decided before any frame mounts. Either host decides: a side panel tab
// restored after a reload may have no message mounted. Same request as the
// view's own.
const useFetchMcpAppResource = useToolStore((s) => s.useFetchMcpAppResource);
const deciding = enabled && !!view && !view.placed;
const { data, isLoading } = useFetchMcpAppResource(identifier, resourceUri, {
agentId,
onThisDevice,
server,
toolArguments,
});
useEffect(() => {
if (!deciding || isLoading) return;
// After this commit's effects: the side panel registers itself in one
const timer = setTimeout(() => {
const hasPanel = mcpAppSelectors.panelAvailable(scope)(useMcpAppStore.getState());
mcpAppActions.place(
toolCallId,
resolveDisplayMode({
hostModes: hasPanel ? ['inline', 'fullscreen'] : ['inline'],
resourceMeta: pickAppResource(data, resourceUri)?.meta,
toolPreferred: preferred,
}) === 'fullscreen'
? 'fullscreen'
: 'inline',
);
}, 0);
return () => clearTimeout(timer);
}, [data, deciding, isLoading, preferred, resourceUri, scope, toolCallId]);
const mode = view?.mode ?? 'inline';
const supported = !view?.availableModes || view.availableModes.includes(mode);
// The host whose location the view is in mounts its frame and owns it
const [instance] = useState(() => ++lastHostInstance);
const runsFrame = enabled && !!view?.placed && mode === location;
useEffect(() => {
if (runsFrame) mcpAppActions.ownFrame(toolCallId, instance);
}, [instance, runsFrame, toolCallId]);
const presentation: NonNullable<McpAppViewProps['presentation']> = {
hostModes: panelAvailable ? ['inline', 'fullscreen'] : ['inline'],
mode,
onInitialized: (modes, resolved) =>
mcpAppActions.settle(toolCallId, modes, resolved === 'fullscreen' ? 'fullscreen' : 'inline'),
// Only the app moves itself; the host offers no switch of its own
onMode: (requested) => {
if (requested === 'pip') return mode;
setTimeout(() => mcpAppActions.setMode(toolCallId, requested), 0);
return requested;
},
// An app that finishes leaving after its frame was reopened elsewhere (tab
// closed, then View) must not end the instance that replaced it
onTeardown: () => {
if (mcpAppActions.ownsFrame(toolCallId, instance)) mcpAppActions.dismiss(toolCallId);
},
onUpdateModelContext: (params) => mcpAppContext.update(toolCallId, params),
onDispose: () => mcpAppContext.clear(toolCallId),
preferred,
retireOnRemove: () => {
const current = useMcpAppStore.getState().views[toolCallId];
return (
(current?.mode ?? 'inline') === location &&
(!current?.availableModes || current.availableModes.includes(location))
);
},
};
return {
enabled,
mode,
panelAvailable,
placed: !!view?.placed,
presentation,
supported,
view,
};
};