histoire
Version:
A fast story book powered by Vite
55 lines (54 loc) • 2.63 kB
JavaScript
import { fileURLToPath } from 'node:url';
import { getMcpObserver, onMcpObserverChange } from '../../mcp/observer/context.js';
import { McpDomainError, toMcpError } from '../../mcp/protocol/errors.js';
import { boundMcpSnapshot } from './mcp-snapshot.js';
import { uiMcpCancelSchema } from './validation.js';
/** Installed CLI wrapper has the same relative position in source and emitted modules. */
export function uiStdioConfig(ctx) {
const args = [fileURLToPath(new URL('../../../../bin.mjs', import.meta.url)), 'mcp', '--root', ctx.root];
if (ctx.configFile)
args.push('--config', ctx.configFile);
return { command: process.execPath, args };
}
/** Project's dev observer arrives after Vite startup; attach without polling. */
export function registerMcpChannel(ctx, channel) {
let observer;
let offClients;
let offOperations;
/** Missing observer is a disabled server until runtime supplies policy. */
function snapshot() {
return boundMcpSnapshot({ ...(observer?.snapshot() ?? { status: 'disabled', clients: [], operations: [] }), stdio: uiStdioConfig(ctx) });
}
/** Fresh snapshots replace running states from a superseded runtime. */
function sendSnapshot(client) {
channel.send('histoire:ui:mcp-snapshot', snapshot(), client);
}
/** Replace subscriptions before exposing the next runtime generation. */
function attach(next) {
offClients?.();
offOperations?.();
observer = next;
offClients = next?.onClientChange(() => sendSnapshot());
offOperations = next?.onOperationChange(value => channel.send('histoire:ui:mcp-operation', value));
sendSnapshot();
}
attach(getMcpObserver(ctx));
channel.addCleanup(onMcpObserverChange(ctx, attach));
channel.addCleanup(() => {
offClients?.();
offOperations?.();
});
channel.onReady(sendSnapshot);
channel.on('histoire:ui:mcp-cancel', value => uiMcpCancelSchema.parse(value), async ({ operationId }, client) => {
try {
if (!observer)
throw new McpDomainError('CAPABILITY_UNAVAILABLE', 'MCP status unavailable.');
await observer.cancelFromUi(operationId);
channel.send('histoire:ui:mcp-cancel-result', { operationId }, client);
}
catch (error) {
const value = toMcpError(error);
channel.send('histoire:ui:mcp-cancel-result', { operationId, error: { code: value.code === 'OPERATION_NOT_FOUND' || value.code === 'CAPABILITY_UNAVAILABLE' ? 'unavailable' : 'failed', message: value.message } }, client);
}
});
}