@mcpview/components 0.0.0-stage → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +575 -2
- package/esm/_dnt.polyfills.d.ts +12 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +15 -0
- package/esm/fonts.d.ts +13 -0
- package/esm/fonts.d.ts.map +1 -0
- package/esm/fonts.js +12 -0
- package/esm/layout.d.ts +14 -0
- package/esm/layout.d.ts.map +1 -0
- package/esm/layout.js +13 -0
- package/esm/mod.d.ts +19 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +14 -0
- package/esm/package.json +3 -0
- package/esm/preact-components.d.ts +22 -0
- package/esm/preact-components.d.ts.map +1 -0
- package/esm/preact-components.js +20 -0
- package/esm/preact.d.ts +10 -0
- package/esm/preact.d.ts.map +1 -0
- package/esm/preact.js +9 -0
- package/esm/src/component-primitives.d.ts +41 -0
- package/esm/src/component-primitives.d.ts.map +1 -0
- package/esm/src/component-primitives.js +121 -0
- package/esm/src/components.d.ts +50 -0
- package/esm/src/components.d.ts.map +1 -0
- package/esm/src/components.js +283 -0
- package/esm/src/font-families.d.ts +16 -0
- package/esm/src/font-families.d.ts.map +1 -0
- package/esm/src/font-families.js +11 -0
- package/esm/src/fonts-data.d.ts +2 -0
- package/esm/src/fonts-data.d.ts.map +1 -0
- package/esm/src/fonts-data.js +42 -0
- package/esm/src/fonts.d.ts +12 -0
- package/esm/src/fonts.d.ts.map +1 -0
- package/esm/src/fonts.js +19 -0
- package/esm/src/i18n.d.ts +44 -0
- package/esm/src/i18n.d.ts.map +1 -0
- package/esm/src/i18n.js +98 -0
- package/esm/src/layout/hooks.d.ts +60 -0
- package/esm/src/layout/hooks.d.ts.map +1 -0
- package/esm/src/layout/hooks.js +121 -0
- package/esm/src/layout/mod.d.ts +6 -0
- package/esm/src/layout/mod.d.ts.map +1 -0
- package/esm/src/layout/mod.js +3 -0
- package/esm/src/layout/viewer-layout.d.ts +82 -0
- package/esm/src/layout/viewer-layout.d.ts.map +1 -0
- package/esm/src/layout/viewer-layout.js +74 -0
- package/esm/src/preact/components.d.ts +215 -0
- package/esm/src/preact/components.d.ts.map +1 -0
- package/esm/src/preact/components.js +176 -0
- package/esm/src/preact/focused-view.d.ts +42 -0
- package/esm/src/preact/focused-view.d.ts.map +1 -0
- package/esm/src/preact/focused-view.js +20 -0
- package/esm/src/preact/mod.d.ts +6 -0
- package/esm/src/preact/mod.d.ts.map +1 -0
- package/esm/src/preact/mod.js +3 -0
- package/esm/src/preact/plots.d.ts +119 -0
- package/esm/src/preact/plots.d.ts.map +1 -0
- package/esm/src/preact/plots.js +152 -0
- package/esm/src/preact/semantic-element.d.ts +103 -0
- package/esm/src/preact/semantic-element.d.ts.map +1 -0
- package/esm/src/preact/semantic-element.js +103 -0
- package/esm/src/preact/structural.d.ts +178 -0
- package/esm/src/preact/structural.d.ts.map +1 -0
- package/esm/src/preact/structural.js +147 -0
- package/esm/src/preact/surface.d.ts +37 -0
- package/esm/src/preact/surface.d.ts.map +1 -0
- package/esm/src/preact/surface.js +47 -0
- package/esm/src/surface-app.d.ts +188 -0
- package/esm/src/surface-app.d.ts.map +1 -0
- package/esm/src/surface-app.js +374 -0
- package/esm/src/theme.d.ts +65 -0
- package/esm/src/theme.d.ts.map +1 -0
- package/esm/src/theme.js +1877 -0
- package/esm/surface.d.ts +12 -0
- package/esm/surface.d.ts.map +1 -0
- package/esm/surface.js +10 -0
- package/package.json +68 -4
- package/script/_dnt.polyfills.d.ts +12 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +16 -0
- package/script/fonts.d.ts +13 -0
- package/script/fonts.d.ts.map +1 -0
- package/script/fonts.js +19 -0
- package/script/layout.d.ts +14 -0
- package/script/layout.d.ts.map +1 -0
- package/script/layout.js +29 -0
- package/script/mod.d.ts +19 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +40 -0
- package/script/package.json +3 -0
- package/script/preact-components.d.ts +22 -0
- package/script/preact-components.d.ts.map +1 -0
- package/script/preact-components.js +68 -0
- package/script/preact.d.ts +10 -0
- package/script/preact.d.ts.map +1 -0
- package/script/preact.js +25 -0
- package/script/src/component-primitives.d.ts +41 -0
- package/script/src/component-primitives.d.ts.map +1 -0
- package/script/src/component-primitives.js +127 -0
- package/script/src/components.d.ts +50 -0
- package/script/src/components.d.ts.map +1 -0
- package/script/src/components.js +297 -0
- package/script/src/font-families.d.ts +16 -0
- package/script/src/font-families.d.ts.map +1 -0
- package/script/src/font-families.js +14 -0
- package/script/src/fonts-data.d.ts +2 -0
- package/script/src/fonts-data.d.ts.map +1 -0
- package/script/src/fonts-data.js +45 -0
- package/script/src/fonts.d.ts +12 -0
- package/script/src/fonts.d.ts.map +1 -0
- package/script/src/fonts.js +24 -0
- package/script/src/i18n.d.ts +44 -0
- package/script/src/i18n.d.ts.map +1 -0
- package/script/src/i18n.js +102 -0
- package/script/src/layout/hooks.d.ts +60 -0
- package/script/src/layout/hooks.d.ts.map +1 -0
- package/script/src/layout/hooks.js +126 -0
- package/script/src/layout/mod.d.ts +6 -0
- package/script/src/layout/mod.d.ts.map +1 -0
- package/script/src/layout/mod.js +17 -0
- package/script/src/layout/viewer-layout.d.ts +82 -0
- package/script/src/layout/viewer-layout.d.ts.map +1 -0
- package/script/src/layout/viewer-layout.js +83 -0
- package/script/src/preact/components.d.ts +215 -0
- package/script/src/preact/components.d.ts.map +1 -0
- package/script/src/preact/components.js +199 -0
- package/script/src/preact/focused-view.d.ts +42 -0
- package/script/src/preact/focused-view.d.ts.map +1 -0
- package/script/src/preact/focused-view.js +24 -0
- package/script/src/preact/mod.d.ts +6 -0
- package/script/src/preact/mod.d.ts.map +1 -0
- package/script/src/preact/mod.js +55 -0
- package/script/src/preact/plots.d.ts +119 -0
- package/script/src/preact/plots.d.ts.map +1 -0
- package/script/src/preact/plots.js +157 -0
- package/script/src/preact/semantic-element.d.ts +103 -0
- package/script/src/preact/semantic-element.d.ts.map +1 -0
- package/script/src/preact/semantic-element.js +115 -0
- package/script/src/preact/structural.d.ts +178 -0
- package/script/src/preact/structural.d.ts.map +1 -0
- package/script/src/preact/structural.js +158 -0
- package/script/src/preact/surface.d.ts +37 -0
- package/script/src/preact/surface.d.ts.map +1 -0
- package/script/src/preact/surface.js +53 -0
- package/script/src/surface-app.d.ts +188 -0
- package/script/src/surface-app.d.ts.map +1 -0
- package/script/src/surface-app.js +379 -0
- package/script/src/theme.d.ts +65 -0
- package/script/src/theme.d.ts.map +1 -0
- package/script/src/theme.js +1881 -0
- package/script/surface.d.ts +12 -0
- package/script/surface.d.ts.map +1 -0
- package/script/surface.js +15 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Result-driven MCP App over one component registry, renderer-neutral.
|
|
4
|
+
*
|
|
5
|
+
* Every componentized viewer repeats the same lifecycle: install the theme,
|
|
6
|
+
* show a status until the host sends a result, project that result into a
|
|
7
|
+
* data model, mount the host-selected surface, remount it when the host
|
|
8
|
+
* context changes, and make sure only the latest mount survives. Copies of
|
|
9
|
+
* that loop drifted — one lost the generation guard in its error path,
|
|
10
|
+
* another buffered recorded sessions by hand. This module is the loop, once.
|
|
11
|
+
* Renderers plug in through `renderStatus`; `startPreactSurfaceApp` binds it
|
|
12
|
+
* to the Preact `StateMessage`.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.SurfaceAppError = void 0;
|
|
16
|
+
exports.startSurfaceApp = startSurfaceApp;
|
|
17
|
+
const view_1 = require("@mcpview/view");
|
|
18
|
+
const components_js_1 = require("./components.js");
|
|
19
|
+
const theme_js_1 = require("./theme.js");
|
|
20
|
+
const i18n_js_1 = require("./i18n.js");
|
|
21
|
+
/** Options refused at the boundary. Match on `code`, not on `message`. */
|
|
22
|
+
class SurfaceAppError extends TypeError {
|
|
23
|
+
code;
|
|
24
|
+
data;
|
|
25
|
+
constructor(code, message, data = {}) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = "SurfaceAppError";
|
|
28
|
+
this.code = code;
|
|
29
|
+
this.data = Object.freeze({ ...data });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.SurfaceAppError = SurfaceAppError;
|
|
33
|
+
const DEFAULT_RUNTIME = { createApp: view_1.createMcpApp };
|
|
34
|
+
/**
|
|
35
|
+
* Start a result-driven MCP App backed by a component registry.
|
|
36
|
+
*
|
|
37
|
+
* The registry may expose a default surface or be component-only. In the
|
|
38
|
+
* latter case a host-selected surface is required and no artificial
|
|
39
|
+
* standalone composition is invented.
|
|
40
|
+
*/
|
|
41
|
+
async function startSurfaceApp(options, runtime = DEFAULT_RUNTIME) {
|
|
42
|
+
if (options.fromToolResult && options.validate) {
|
|
43
|
+
throw new SurfaceAppError("SURFACE_APP_PROJECTION_CONFLICT", "startSurfaceApp accepts either fromToolResult or validate, not both", { recovery: "Keep fromToolResult and validate inside it, or drop it and keep validate." });
|
|
44
|
+
}
|
|
45
|
+
if (options.theme !== false)
|
|
46
|
+
(0, theme_js_1.installMcpViewTheme)();
|
|
47
|
+
const state = {};
|
|
48
|
+
const project = options.fromToolResult ?? defaultProjection(options.validate);
|
|
49
|
+
const session = options.viewerSession;
|
|
50
|
+
let mounted;
|
|
51
|
+
let pendingMount;
|
|
52
|
+
let mountGeneration = 0;
|
|
53
|
+
let navigation = Promise.resolve();
|
|
54
|
+
let renderedHostContext;
|
|
55
|
+
let hostLocale;
|
|
56
|
+
let renderedStatusLocale;
|
|
57
|
+
let activeStatus = { kind: "loading" };
|
|
58
|
+
let closed = false;
|
|
59
|
+
let removeHostContextListener;
|
|
60
|
+
const reportError = (error) => {
|
|
61
|
+
if (options.onError)
|
|
62
|
+
options.onError(error);
|
|
63
|
+
else
|
|
64
|
+
console.error("[mcp-view-components] Surface App failed", error);
|
|
65
|
+
};
|
|
66
|
+
const message = (key, values) => (options.messages ?? i18n_js_1.mcpViewMessages)(hostLocale)(key, values);
|
|
67
|
+
const resolveLabel = (value) => typeof value === "function" ? value(hostLocale) : value;
|
|
68
|
+
const label = (override, key) => override === undefined ? message(key) : resolveLabel(override);
|
|
69
|
+
const resolve = (display) => {
|
|
70
|
+
switch (display.kind) {
|
|
71
|
+
case "loading":
|
|
72
|
+
return {
|
|
73
|
+
kind: "loading",
|
|
74
|
+
title: message("loadingTitle"),
|
|
75
|
+
message: label(options.loadingLabel, "loadingMessage"),
|
|
76
|
+
tone: "info",
|
|
77
|
+
busy: true,
|
|
78
|
+
};
|
|
79
|
+
case "empty":
|
|
80
|
+
return {
|
|
81
|
+
kind: "empty",
|
|
82
|
+
title: message("emptyTitle"),
|
|
83
|
+
message: label(options.emptyLabel, "emptyMessage"),
|
|
84
|
+
tone: "neutral",
|
|
85
|
+
busy: false,
|
|
86
|
+
};
|
|
87
|
+
case "error":
|
|
88
|
+
return {
|
|
89
|
+
kind: "error",
|
|
90
|
+
title: display.title === undefined
|
|
91
|
+
? message(display.rejectionTitleKey ?? "errorTitle")
|
|
92
|
+
: resolveLabel(display.title),
|
|
93
|
+
message: resolveLabel(display.message),
|
|
94
|
+
tone: "danger",
|
|
95
|
+
busy: false,
|
|
96
|
+
...(display.code === undefined ? {} : { code: display.code }),
|
|
97
|
+
};
|
|
98
|
+
case "notice":
|
|
99
|
+
return {
|
|
100
|
+
kind: "notice",
|
|
101
|
+
title: display.title === undefined ? undefined : resolveLabel(display.title),
|
|
102
|
+
message: resolveLabel(display.message),
|
|
103
|
+
tone: display.tone ?? "neutral",
|
|
104
|
+
busy: display.busy ?? false,
|
|
105
|
+
...(display.code === undefined ? {} : { code: display.code }),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const failure = (title, message) => ({
|
|
110
|
+
kind: "error",
|
|
111
|
+
title,
|
|
112
|
+
message,
|
|
113
|
+
tone: "danger",
|
|
114
|
+
busy: false,
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* Every navigation the App issues is chained here. The router serializes
|
|
118
|
+
* navigations too, but a step chained behind the last one observes the
|
|
119
|
+
* router settled — never a view mid-transition, when `currentView` throws
|
|
120
|
+
* and `currentData` still names a result on its way out.
|
|
121
|
+
*/
|
|
122
|
+
const enqueue = (step) => {
|
|
123
|
+
const next = navigation.then(step);
|
|
124
|
+
navigation = next.catch(() => { });
|
|
125
|
+
return next;
|
|
126
|
+
};
|
|
127
|
+
const show = (navigate, display) => enqueue(() => {
|
|
128
|
+
activeStatus = display.kind === "result" ? undefined : display;
|
|
129
|
+
return display.kind === "result"
|
|
130
|
+
? navigate("surface", display.result)
|
|
131
|
+
: navigate("status", resolve(display));
|
|
132
|
+
});
|
|
133
|
+
/** A projection that throws shows its failure; it never breaks the lifecycle. */
|
|
134
|
+
const settle = async (compute, rejectionTitleKey) => {
|
|
135
|
+
try {
|
|
136
|
+
return await compute();
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
reportError(error);
|
|
140
|
+
return { kind: "error", message: errorMessage(error), rejectionTitleKey };
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const hostAccess = (app) => ({
|
|
144
|
+
locale: app.ctx.hostContext.locale,
|
|
145
|
+
readServerResource: (uri) => app.ctx.app.readServerResource({ uri }),
|
|
146
|
+
});
|
|
147
|
+
const disposeSurface = async () => {
|
|
148
|
+
mountGeneration += 1;
|
|
149
|
+
await pendingMount;
|
|
150
|
+
pendingMount = undefined;
|
|
151
|
+
const active = mounted;
|
|
152
|
+
mounted = undefined;
|
|
153
|
+
await active?.dispose();
|
|
154
|
+
};
|
|
155
|
+
const status = (0, view_1.defineView)({
|
|
156
|
+
onEnter(_context, next) {
|
|
157
|
+
state.currentData = undefined;
|
|
158
|
+
return next;
|
|
159
|
+
},
|
|
160
|
+
render: (context, next) => {
|
|
161
|
+
hostLocale = context.hostContext.locale;
|
|
162
|
+
renderedStatusLocale = hostLocale;
|
|
163
|
+
return options.renderStatus(activeStatus ? resolve(activeStatus) : next);
|
|
164
|
+
},
|
|
165
|
+
onLeave: disposeSurface,
|
|
166
|
+
});
|
|
167
|
+
const surface = (0, view_1.defineView)({
|
|
168
|
+
onEnter(_context, data) {
|
|
169
|
+
state.currentData = data;
|
|
170
|
+
return data;
|
|
171
|
+
},
|
|
172
|
+
render(context, data) {
|
|
173
|
+
hostLocale = context.hostContext.locale;
|
|
174
|
+
renderedHostContext = context.hostContext;
|
|
175
|
+
const shell = document.createElement("div");
|
|
176
|
+
shell.className = options.surfaceClassName ?? "mcp-view-surface-shell";
|
|
177
|
+
let selected;
|
|
178
|
+
// Named before the call: a `surfaceFor` that throws is the data-owned
|
|
179
|
+
// path failing, not the host selection it was asked to replace.
|
|
180
|
+
let owner = options.surfaceFor ? "data-owned" : "host-selected";
|
|
181
|
+
try {
|
|
182
|
+
const owned = options.surfaceFor?.(data);
|
|
183
|
+
if (!owned)
|
|
184
|
+
owner = "host-selected";
|
|
185
|
+
selected = owned
|
|
186
|
+
? (0, components_js_1.defineComponentSurface)(owned)
|
|
187
|
+
: (0, components_js_1.activeComponentSurface)(options.registry, context.hostContext);
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
// A malformed surface must not unmount the route: the App stays on
|
|
191
|
+
// its surface view and says why nothing is composed.
|
|
192
|
+
reportError(error);
|
|
193
|
+
shell.replaceChildren(options.renderStatus(failure(message("surfaceInvalidTitle"), message("surfaceInvalidMessage", { owner, error: errorMessage(error) }))));
|
|
194
|
+
return shell;
|
|
195
|
+
}
|
|
196
|
+
if (!selected) {
|
|
197
|
+
shell.replaceChildren(options.renderStatus({
|
|
198
|
+
kind: "surface-required",
|
|
199
|
+
title: message("surfaceRequiredTitle"),
|
|
200
|
+
message: label(options.surfaceRequiredLabel, "surfaceRequiredMessage"),
|
|
201
|
+
tone: "warning",
|
|
202
|
+
busy: false,
|
|
203
|
+
}));
|
|
204
|
+
return shell;
|
|
205
|
+
}
|
|
206
|
+
const generation = ++mountGeneration;
|
|
207
|
+
pendingMount = (0, components_js_1.mountComponentSurface)({
|
|
208
|
+
root: shell,
|
|
209
|
+
registry: options.registry,
|
|
210
|
+
data,
|
|
211
|
+
appContext: context,
|
|
212
|
+
hostContext: context.hostContext,
|
|
213
|
+
surface: selected,
|
|
214
|
+
}).then(async (next) => {
|
|
215
|
+
if (generation !== mountGeneration) {
|
|
216
|
+
await next.dispose();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
mounted = next;
|
|
220
|
+
}).catch((error) => {
|
|
221
|
+
reportError(error);
|
|
222
|
+
// A superseded mount that fails must not overwrite the newer route.
|
|
223
|
+
if (generation !== mountGeneration)
|
|
224
|
+
return;
|
|
225
|
+
shell.replaceChildren(options.renderStatus(failure(message("surfaceFailedTitle"), message("surfaceFailedMessage", { error: errorMessage(error) }))));
|
|
226
|
+
});
|
|
227
|
+
return shell;
|
|
228
|
+
},
|
|
229
|
+
onLeave: disposeSurface,
|
|
230
|
+
});
|
|
231
|
+
const handle = await runtime.createApp({
|
|
232
|
+
info: options.info,
|
|
233
|
+
root: options.root,
|
|
234
|
+
...(options.strict === undefined ? {} : { strict: options.strict }),
|
|
235
|
+
views: { status, surface },
|
|
236
|
+
initialView: "status",
|
|
237
|
+
initialArgs: resolve({ kind: "loading" }),
|
|
238
|
+
initialState: state,
|
|
239
|
+
...(session
|
|
240
|
+
? {
|
|
241
|
+
viewerSession: {
|
|
242
|
+
validate: session.validate,
|
|
243
|
+
onSession: async (value, _payload, app) => {
|
|
244
|
+
hostLocale = app.ctx.hostContext.locale;
|
|
245
|
+
const next = await settle(() => session.toState(value, hostAccess(app)), "sessionRejectedTitle");
|
|
246
|
+
await show(app.navigate, next);
|
|
247
|
+
},
|
|
248
|
+
onInvalid: (payload) => session.onInvalid?.(payload.data),
|
|
249
|
+
onError: reportError,
|
|
250
|
+
},
|
|
251
|
+
}
|
|
252
|
+
: {}),
|
|
253
|
+
capabilities: {
|
|
254
|
+
experimental: (0, components_js_1.componentCatalogCapabilities)(options.registry),
|
|
255
|
+
},
|
|
256
|
+
// Either notification means a run is under way: a host that does not
|
|
257
|
+
// stream arguments sends only the complete `tool-input`.
|
|
258
|
+
onToolInput: async (_params, app) => {
|
|
259
|
+
await show(app.navigate, { kind: "loading" });
|
|
260
|
+
},
|
|
261
|
+
onToolInputPartial: async (_params, app) => {
|
|
262
|
+
await show(app.navigate, { kind: "loading" });
|
|
263
|
+
},
|
|
264
|
+
onToolResult: async (result, app) => {
|
|
265
|
+
hostLocale = app.ctx.hostContext.locale;
|
|
266
|
+
const next = await settle(() => project(result, hostAccess(app)), "resultRejectedTitle");
|
|
267
|
+
await show(app.navigate, next);
|
|
268
|
+
},
|
|
269
|
+
onTeardown: async () => {
|
|
270
|
+
// The router runs the active view's onLeave, which disposes the surface.
|
|
271
|
+
closed = true;
|
|
272
|
+
removeHostContextListener?.();
|
|
273
|
+
removeHostContextListener = undefined;
|
|
274
|
+
try {
|
|
275
|
+
await options.onTeardown?.();
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
reportError(error);
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
const surfaceHandle = {
|
|
283
|
+
show: (display) => closed
|
|
284
|
+
? Promise.reject(new SurfaceAppError("SURFACE_APP_CLOSED", "The surface App has been torn down", {
|
|
285
|
+
recovery: "Start a new App with startSurfaceApp; this handle cannot show anything.",
|
|
286
|
+
}))
|
|
287
|
+
: show(handle.navigate, display),
|
|
288
|
+
dispose: () => handle.dispose(),
|
|
289
|
+
};
|
|
290
|
+
// A host teardown buffered during the handshake has already run: there is
|
|
291
|
+
// no App left to listen to.
|
|
292
|
+
if (closed)
|
|
293
|
+
return surfaceHandle;
|
|
294
|
+
/** Remount the displayed result when the host context moved since it rendered. */
|
|
295
|
+
const remountIfStale = () => enqueue(async () => {
|
|
296
|
+
const data = state.currentData;
|
|
297
|
+
if (closed)
|
|
298
|
+
return;
|
|
299
|
+
if (data === undefined) {
|
|
300
|
+
if (activeStatus && hostLocale !== renderedStatusLocale) {
|
|
301
|
+
await handle.navigate("status", resolve(activeStatus));
|
|
302
|
+
}
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (handle.ctx.hostContext === renderedHostContext) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (options.themeUpdates === "in-place" && renderedHostContext &&
|
|
309
|
+
onlyThemeChanged(renderedHostContext, handle.ctx.hostContext)) {
|
|
310
|
+
renderedHostContext = handle.ctx.hostContext;
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
await handle.navigate("surface", data);
|
|
314
|
+
});
|
|
315
|
+
const onHostContextChanged = () => {
|
|
316
|
+
hostLocale = handle.ctx.hostContext.locale;
|
|
317
|
+
if (options.documentLanguage) {
|
|
318
|
+
document.documentElement.lang = options.documentLanguage(hostLocale);
|
|
319
|
+
}
|
|
320
|
+
(0, components_js_1.applySurfaceContext)(handle.ctx.hostContext, document.documentElement);
|
|
321
|
+
remountIfStale().catch(reportError);
|
|
322
|
+
};
|
|
323
|
+
handle.ctx.app.addEventListener("hostcontextchanged", onHostContextChanged);
|
|
324
|
+
removeHostContextListener = () => {
|
|
325
|
+
handle.ctx.app.removeEventListener("hostcontextchanged", onHostContextChanged);
|
|
326
|
+
};
|
|
327
|
+
// Host notifications replayed by `createMcpApp` may have rendered a result
|
|
328
|
+
// and then moved the host context before this listener existed.
|
|
329
|
+
onHostContextChanged();
|
|
330
|
+
return surfaceHandle;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Conservative: only `theme` is exempt. Any other field whose JSON-shaped value
|
|
334
|
+
* changed retains the normal remount behavior. Object key order is ignored;
|
|
335
|
+
* array order is kept. No stringify, no coercion.
|
|
336
|
+
*/
|
|
337
|
+
function onlyThemeChanged(previous, next) {
|
|
338
|
+
const keys = new Set([...Object.keys(previous), ...Object.keys(next)]);
|
|
339
|
+
return [...keys].every((key) => key === "theme" || jsonValueEqual(previous[key], next[key]));
|
|
340
|
+
}
|
|
341
|
+
function jsonValueEqual(left, right) {
|
|
342
|
+
if (left === right)
|
|
343
|
+
return true;
|
|
344
|
+
if (left === null || right === null)
|
|
345
|
+
return false;
|
|
346
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
347
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
return left.every((item, index) => jsonValueEqual(item, right[index]));
|
|
351
|
+
}
|
|
352
|
+
if (!isPlainJsonObject(left) || !isPlainJsonObject(right))
|
|
353
|
+
return false;
|
|
354
|
+
const leftKeys = Object.keys(left);
|
|
355
|
+
const rightKeys = Object.keys(right);
|
|
356
|
+
if (leftKeys.length !== rightKeys.length)
|
|
357
|
+
return false;
|
|
358
|
+
return leftKeys.every((key) => Object.hasOwn(right, key) && jsonValueEqual(left[key], right[key]));
|
|
359
|
+
}
|
|
360
|
+
function isPlainJsonObject(value) {
|
|
361
|
+
if (typeof value !== "object" || value === null)
|
|
362
|
+
return false;
|
|
363
|
+
const proto = Object.getPrototypeOf(value);
|
|
364
|
+
return proto === Object.prototype || proto === null;
|
|
365
|
+
}
|
|
366
|
+
function defaultProjection(validate) {
|
|
367
|
+
return (result) => {
|
|
368
|
+
const data = (0, view_1.readResultData)(result, {
|
|
369
|
+
fallback: "json-text",
|
|
370
|
+
...(validate ? { validate: (value) => validate(value) } : {}),
|
|
371
|
+
});
|
|
372
|
+
return data === undefined
|
|
373
|
+
? { kind: "empty" }
|
|
374
|
+
: { kind: "result", result: data };
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
function errorMessage(error) {
|
|
378
|
+
return error instanceof Error ? error.message : String(error);
|
|
379
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** Shared light-first visual language for composable MCP App components. */
|
|
2
|
+
export declare const MCP_VIEW_THEME_STYLE_ID = "mcp-view-theme";
|
|
3
|
+
/** Stable CSS custom-property names available to App-local themes. */
|
|
4
|
+
export type McpViewThemeTokens = Readonly<{
|
|
5
|
+
text: "--mcp-view-text";
|
|
6
|
+
muted: "--mcp-view-muted";
|
|
7
|
+
quiet: "--mcp-view-quiet";
|
|
8
|
+
border: "--mcp-view-border";
|
|
9
|
+
panel: "--mcp-view-panel";
|
|
10
|
+
subtle: "--mcp-view-subtle";
|
|
11
|
+
accent: "--mcp-view-accent";
|
|
12
|
+
brand: "--mcp-view-brand";
|
|
13
|
+
success: "--mcp-view-success";
|
|
14
|
+
warning: "--mcp-view-warning";
|
|
15
|
+
danger: "--mcp-view-danger";
|
|
16
|
+
fontHeading: "--mcp-view-font-heading";
|
|
17
|
+
fontBody: "--mcp-view-font-body";
|
|
18
|
+
fontMono: "--mcp-view-font-mono";
|
|
19
|
+
radius: "--mcp-view-radius";
|
|
20
|
+
radiusSmall: "--mcp-view-radius-sm";
|
|
21
|
+
gap: "--mcp-view-gap";
|
|
22
|
+
textSecondary: "--mcp-view-text-secondary";
|
|
23
|
+
ghost: "--mcp-view-ghost";
|
|
24
|
+
borderSoft: "--mcp-view-border-soft";
|
|
25
|
+
borderStrong: "--mcp-view-border-strong";
|
|
26
|
+
hover: "--mcp-view-hover";
|
|
27
|
+
track: "--mcp-view-track";
|
|
28
|
+
accentText: "--mcp-view-accent-text";
|
|
29
|
+
brandText: "--mcp-view-brand-text";
|
|
30
|
+
warningText: "--mcp-view-warning-text";
|
|
31
|
+
radiusControl: "--mcp-view-radius-control";
|
|
32
|
+
sizeMicro: "--mcp-view-size-micro";
|
|
33
|
+
sizeChip: "--mcp-view-size-chip";
|
|
34
|
+
sizeMeta: "--mcp-view-size-meta";
|
|
35
|
+
sizeNote: "--mcp-view-size-note";
|
|
36
|
+
sizeData: "--mcp-view-size-data";
|
|
37
|
+
sizeCell: "--mcp-view-size-cell";
|
|
38
|
+
sizeBody: "--mcp-view-size-body";
|
|
39
|
+
sizeLede: "--mcp-view-size-lede";
|
|
40
|
+
sizeCardTitle: "--mcp-view-size-card-title";
|
|
41
|
+
sizeTotal: "--mcp-view-size-total";
|
|
42
|
+
sizeTitle: "--mcp-view-size-title";
|
|
43
|
+
sizeMetric: "--mcp-view-size-metric";
|
|
44
|
+
trackingLabel: "--mcp-view-tracking-label";
|
|
45
|
+
trackingChip: "--mcp-view-tracking-chip";
|
|
46
|
+
trackingEyebrow: "--mcp-view-tracking-eyebrow";
|
|
47
|
+
trackingTitle: "--mcp-view-tracking-title";
|
|
48
|
+
trackingMetric: "--mcp-view-tracking-metric";
|
|
49
|
+
}>;
|
|
50
|
+
export declare const MCP_VIEW_THEME_TOKENS: McpViewThemeTokens;
|
|
51
|
+
export type McpViewThemeToken = McpViewThemeTokens[keyof McpViewThemeTokens];
|
|
52
|
+
/**
|
|
53
|
+
* Compact, container-friendly MCP View v2 defaults. Domain components keep
|
|
54
|
+
* their own semantics while sharing tokens, cards, metrics, tables, badges
|
|
55
|
+
* and explicit system states. Every token can be overridden by the host.
|
|
56
|
+
*/
|
|
57
|
+
export declare const MCP_VIEW_THEME_CSS: string;
|
|
58
|
+
export interface McpViewThemeDocument {
|
|
59
|
+
readonly head: Pick<HTMLElement, "append">;
|
|
60
|
+
getElementById(id: string): HTMLElement | null;
|
|
61
|
+
createElement(tagName: "style"): HTMLStyleElement;
|
|
62
|
+
}
|
|
63
|
+
/** Install the shared theme once in the current MCP App document. */
|
|
64
|
+
export declare function installMcpViewTheme(target?: McpViewThemeDocument): HTMLStyleElement;
|
|
65
|
+
//# sourceMappingURL=theme.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../../src/src/theme.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAE5E,eAAO,MAAM,uBAAuB,mBAAmB,CAAC;AAExD,sEAAsE;AACtE,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IACxC,IAAI,EAAE,iBAAiB,CAAC;IACxB,K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