skybridge 0.0.0-dev.fe23f20 → 0.0.0-dev.fe35f75
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/README.md +153 -0
- package/bin/run.js +0 -4
- package/dist/cli/detect-port.d.ts +18 -0
- package/dist/cli/detect-port.js +61 -0
- package/dist/cli/detect-port.js.map +1 -0
- package/dist/cli/header.js +1 -1
- package/dist/cli/header.js.map +1 -1
- package/dist/cli/run-command.js.map +1 -1
- package/dist/cli/telemetry.d.ts +7 -0
- package/dist/cli/telemetry.js +123 -0
- package/dist/cli/telemetry.js.map +1 -0
- package/dist/cli/tunnel-control-server.d.ts +9 -0
- package/dist/cli/tunnel-control-server.js +31 -0
- package/dist/cli/tunnel-control-server.js.map +1 -0
- package/dist/cli/tunnel-control-server.test.js +39 -0
- package/dist/cli/tunnel-control-server.test.js.map +1 -0
- package/dist/cli/tunnel-handler.d.ts +3 -0
- package/dist/cli/tunnel-handler.js +48 -0
- package/dist/cli/tunnel-handler.js.map +1 -0
- package/dist/cli/tunnel-handler.test.js +105 -0
- package/dist/cli/tunnel-handler.test.js.map +1 -0
- package/dist/cli/tunnel.d.ts +57 -0
- package/dist/cli/tunnel.js +154 -0
- package/dist/cli/tunnel.js.map +1 -0
- package/dist/cli/tunnel.test.d.ts +1 -0
- package/dist/cli/tunnel.test.js +190 -0
- package/dist/cli/tunnel.test.js.map +1 -0
- package/dist/cli/types.d.ts +5 -0
- package/dist/cli/types.js +2 -0
- package/dist/cli/types.js.map +1 -0
- package/dist/cli/use-execute-steps.d.ts +3 -2
- package/dist/cli/use-execute-steps.js +6 -1
- package/dist/cli/use-execute-steps.js.map +1 -1
- package/dist/cli/use-messages.d.ts +3 -0
- package/dist/cli/use-messages.js +11 -0
- package/dist/cli/use-messages.js.map +1 -0
- package/dist/cli/use-nodemon.d.ts +2 -0
- package/dist/cli/use-nodemon.js +73 -0
- package/dist/cli/use-nodemon.js.map +1 -0
- package/dist/cli/use-open-browser.d.ts +1 -0
- package/dist/cli/use-open-browser.js +44 -0
- package/dist/cli/use-open-browser.js.map +1 -0
- package/dist/cli/use-tunnel.d.ts +14 -0
- package/dist/cli/use-tunnel.js +131 -0
- package/dist/cli/use-tunnel.js.map +1 -0
- package/dist/cli/use-typescript-check.d.ts +9 -0
- package/dist/cli/use-typescript-check.js +94 -0
- package/dist/cli/use-typescript-check.js.map +1 -0
- package/dist/commands/build.js +64 -6
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/dev.d.ts +6 -1
- package/dist/commands/dev.js +69 -9
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/start.d.ts +3 -1
- package/dist/commands/start.js +31 -15
- package/dist/commands/start.js.map +1 -1
- package/dist/commands/telemetry/disable.d.ts +5 -0
- package/dist/commands/telemetry/disable.js +14 -0
- package/dist/commands/telemetry/disable.js.map +1 -0
- package/dist/commands/telemetry/enable.d.ts +5 -0
- package/dist/commands/telemetry/enable.js +14 -0
- package/dist/commands/telemetry/enable.js.map +1 -0
- package/dist/commands/telemetry/status.d.ts +5 -0
- package/dist/commands/telemetry/status.js +14 -0
- package/dist/commands/telemetry/status.js.map +1 -0
- package/dist/server/asset-base-url-transform-plugin.d.ts +10 -0
- package/dist/server/asset-base-url-transform-plugin.js +33 -0
- package/dist/server/asset-base-url-transform-plugin.js.map +1 -0
- package/dist/server/asset-base-url-transform-plugin.test.d.ts +1 -0
- package/dist/server/asset-base-url-transform-plugin.test.js +84 -0
- package/dist/server/asset-base-url-transform-plugin.test.js.map +1 -0
- package/dist/server/content-helpers.d.ts +27 -0
- package/dist/server/content-helpers.js +46 -0
- package/dist/server/content-helpers.js.map +1 -0
- package/dist/server/content-helpers.test.d.ts +1 -0
- package/dist/server/content-helpers.test.js +70 -0
- package/dist/server/content-helpers.test.js.map +1 -0
- package/dist/server/express.d.ts +11 -0
- package/dist/server/express.js +101 -0
- package/dist/server/express.js.map +1 -0
- package/dist/server/express.test.d.ts +1 -0
- package/dist/server/express.test.js +430 -0
- package/dist/server/express.test.js.map +1 -0
- package/dist/server/index.d.ts +5 -3
- package/dist/server/index.js +3 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/inferUtilityTypes.d.ts +6 -6
- package/dist/server/inferUtilityTypes.js.map +1 -1
- package/dist/server/metric.d.ts +14 -0
- package/dist/server/metric.js +62 -0
- package/dist/server/metric.js.map +1 -0
- package/dist/server/middleware.d.ts +124 -0
- package/dist/server/middleware.js +93 -0
- package/dist/server/middleware.js.map +1 -0
- package/dist/server/middleware.test-d.d.ts +1 -0
- package/dist/server/middleware.test-d.js +75 -0
- package/dist/server/middleware.test-d.js.map +1 -0
- package/dist/server/middleware.test.d.ts +1 -0
- package/dist/server/middleware.test.js +493 -0
- package/dist/server/middleware.test.js.map +1 -0
- package/dist/server/server.d.ts +160 -63
- package/dist/server/server.js +393 -63
- package/dist/server/server.js.map +1 -1
- package/dist/server/templateHelper.d.ts +5 -7
- package/dist/server/templateHelper.js +3 -22
- package/dist/server/templateHelper.js.map +1 -1
- package/dist/server/templates.generated.d.ts +4 -0
- package/dist/server/templates.generated.js +47 -0
- package/dist/server/templates.generated.js.map +1 -0
- package/dist/server/tunnel-proxy-router.d.ts +7 -0
- package/dist/server/tunnel-proxy-router.js +110 -0
- package/dist/server/tunnel-proxy-router.js.map +1 -0
- package/dist/server/tunnel-proxy-router.test.d.ts +1 -0
- package/dist/server/tunnel-proxy-router.test.js +229 -0
- package/dist/server/tunnel-proxy-router.test.js.map +1 -0
- package/dist/server/viewsDevServer.d.ts +14 -0
- package/dist/server/viewsDevServer.js +45 -0
- package/dist/server/viewsDevServer.js.map +1 -0
- package/dist/test/utils.d.ts +13 -21
- package/dist/test/utils.js +42 -37
- package/dist/test/utils.js.map +1 -1
- package/dist/test/view.test.d.ts +1 -0
- package/dist/test/view.test.js +523 -0
- package/dist/test/view.test.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/web/bridges/apps-sdk/adaptor.d.ts +18 -6
- package/dist/web/bridges/apps-sdk/adaptor.js +71 -8
- package/dist/web/bridges/apps-sdk/adaptor.js.map +1 -1
- package/dist/web/bridges/apps-sdk/bridge.d.ts +1 -1
- package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
- package/dist/web/bridges/apps-sdk/index.d.ts +1 -1
- package/dist/web/bridges/apps-sdk/index.js.map +1 -1
- package/dist/web/bridges/apps-sdk/types.d.ts +39 -27
- package/dist/web/bridges/apps-sdk/types.js.map +1 -1
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
- package/dist/web/bridges/get-adaptor.js.map +1 -1
- package/dist/web/bridges/index.js.map +1 -1
- package/dist/web/bridges/mcp-app/adaptor.d.ts +39 -8
- package/dist/web/bridges/mcp-app/adaptor.js +182 -56
- package/dist/web/bridges/mcp-app/adaptor.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.d.ts +13 -30
- package/dist/web/bridges/mcp-app/bridge.js +43 -196
- package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
- package/dist/web/bridges/mcp-app/index.js.map +1 -1
- package/dist/web/bridges/mcp-app/types.js.map +1 -1
- package/dist/web/bridges/mcp-app/use-mcp-app-context.d.ts +5 -3
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js +2 -2
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js.map +1 -1
- package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js +1 -41
- package/dist/web/bridges/mcp-app/use-mcp-app-context.test.js.map +1 -1
- package/dist/web/bridges/types.d.ts +55 -12
- package/dist/web/bridges/types.js.map +1 -1
- package/dist/web/bridges/use-host-context.js.map +1 -1
- package/dist/web/components/modal-provider.d.ts +4 -0
- package/dist/web/components/modal-provider.js +45 -0
- package/dist/web/components/modal-provider.js.map +1 -0
- package/dist/web/create-store.js +17 -3
- package/dist/web/create-store.js.map +1 -1
- package/dist/web/create-store.test.js +23 -20
- package/dist/web/create-store.test.js.map +1 -1
- package/dist/web/data-llm.d.ts +1 -1
- package/dist/web/data-llm.js +3 -3
- package/dist/web/data-llm.js.map +1 -1
- package/dist/web/data-llm.test.js +33 -30
- package/dist/web/data-llm.test.js.map +1 -1
- package/dist/web/generate-helpers.d.ts +20 -18
- package/dist/web/generate-helpers.js +20 -18
- package/dist/web/generate-helpers.js.map +1 -1
- package/dist/web/generate-helpers.test-d.js +26 -26
- package/dist/web/generate-helpers.test-d.js.map +1 -1
- package/dist/web/generate-helpers.test.js.map +1 -1
- package/dist/web/helpers/state.d.ts +2 -2
- package/dist/web/helpers/state.js +11 -11
- package/dist/web/helpers/state.js.map +1 -1
- package/dist/web/helpers/state.test.js +9 -9
- package/dist/web/helpers/state.test.js.map +1 -1
- package/dist/web/hooks/index.d.ts +5 -2
- package/dist/web/hooks/index.js +4 -1
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/test/utils.js +4 -0
- package/dist/web/hooks/test/utils.js.map +1 -1
- package/dist/web/hooks/use-call-tool.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test-d.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test.js +0 -4
- package/dist/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/web/hooks/use-display-mode.d.ts +3 -3
- package/dist/web/hooks/use-display-mode.js.map +1 -1
- package/dist/web/hooks/use-display-mode.test-d.d.ts +1 -0
- package/dist/web/hooks/use-display-mode.test-d.js +8 -0
- package/dist/web/hooks/use-display-mode.test-d.js.map +1 -0
- package/dist/web/hooks/use-display-mode.test.js.map +1 -1
- package/dist/web/hooks/use-files.d.ts +3 -6
- package/dist/web/hooks/use-files.js +5 -2
- package/dist/web/hooks/use-files.js.map +1 -1
- package/dist/web/hooks/use-files.test.js +28 -3
- package/dist/web/hooks/use-files.test.js.map +1 -1
- package/dist/web/hooks/use-layout.d.ts +1 -1
- package/dist/web/hooks/use-layout.js.map +1 -1
- package/dist/web/hooks/use-layout.test.js +3 -3
- package/dist/web/hooks/use-layout.test.js.map +1 -1
- package/dist/web/hooks/use-open-external.d.ts +3 -1
- package/dist/web/hooks/use-open-external.js +1 -1
- package/dist/web/hooks/use-open-external.js.map +1 -1
- package/dist/web/hooks/use-open-external.test.js +26 -11
- package/dist/web/hooks/use-open-external.test.js.map +1 -1
- package/dist/web/hooks/use-request-close.d.ts +2 -0
- package/dist/web/hooks/use-request-close.js +8 -0
- package/dist/web/hooks/use-request-close.js.map +1 -0
- package/dist/web/hooks/use-request-close.test.d.ts +1 -0
- package/dist/web/hooks/use-request-close.test.js +52 -0
- package/dist/web/hooks/use-request-close.test.js.map +1 -0
- package/dist/web/hooks/use-request-modal.d.ts +3 -3
- package/dist/web/hooks/use-request-modal.js +10 -8
- package/dist/web/hooks/use-request-modal.js.map +1 -1
- package/dist/web/hooks/use-request-modal.test.js +5 -1
- package/dist/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/web/hooks/use-request-size.d.ts +3 -0
- package/dist/web/hooks/use-request-size.js +8 -0
- package/dist/web/hooks/use-request-size.js.map +1 -0
- package/dist/web/hooks/use-request-size.test.d.ts +1 -0
- package/dist/web/hooks/use-request-size.test.js +65 -0
- package/dist/web/hooks/use-request-size.test.js.map +1 -0
- package/dist/web/hooks/use-send-follow-up-message.d.ts +2 -1
- package/dist/web/hooks/use-send-follow-up-message.js +2 -2
- package/dist/web/hooks/use-send-follow-up-message.js.map +1 -1
- package/dist/web/hooks/use-set-open-in-app-url.d.ts +1 -0
- package/dist/web/hooks/use-set-open-in-app-url.js +8 -0
- package/dist/web/hooks/use-set-open-in-app-url.js.map +1 -0
- package/dist/web/hooks/use-set-open-in-app-url.test.d.ts +1 -0
- package/dist/web/hooks/use-set-open-in-app-url.test.js +43 -0
- package/dist/web/hooks/use-set-open-in-app-url.test.js.map +1 -0
- package/dist/web/hooks/use-tool-info.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/web/hooks/use-tool-info.test.js +1 -1
- package/dist/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/web/hooks/use-user.js +18 -2
- package/dist/web/hooks/use-user.js.map +1 -1
- package/dist/web/hooks/use-user.test.js +29 -1
- package/dist/web/hooks/use-user.test.js.map +1 -1
- package/dist/web/hooks/use-view-state.d.ts +4 -0
- package/dist/web/hooks/use-view-state.js +32 -0
- package/dist/web/hooks/use-view-state.js.map +1 -0
- package/dist/web/hooks/use-view-state.test.d.ts +1 -0
- package/dist/web/hooks/use-view-state.test.js +177 -0
- package/dist/web/hooks/use-view-state.test.js.map +1 -0
- package/dist/web/index.d.ts +1 -2
- package/dist/web/index.js +1 -2
- package/dist/web/index.js.map +1 -1
- package/dist/web/mount-view.d.ts +1 -0
- package/dist/web/{mount-widget.js → mount-view.js} +11 -3
- package/dist/web/mount-view.js.map +1 -0
- package/dist/web/plugin/data-llm.test.js.map +1 -1
- package/dist/web/plugin/plugin.d.ts +4 -1
- package/dist/web/plugin/plugin.js +135 -18
- package/dist/web/plugin/plugin.js.map +1 -1
- package/dist/web/plugin/scan-views.d.ts +16 -0
- package/dist/web/plugin/scan-views.js +88 -0
- package/dist/web/plugin/scan-views.js.map +1 -0
- package/dist/web/plugin/scan-views.test.d.ts +1 -0
- package/dist/web/plugin/scan-views.test.js +99 -0
- package/dist/web/plugin/scan-views.test.js.map +1 -0
- package/dist/web/plugin/transform-data-llm.js +1 -1
- package/dist/web/plugin/transform-data-llm.js.map +1 -1
- package/dist/web/plugin/transform-data-llm.test.js.map +1 -1
- package/dist/web/plugin/validate-view.d.ts +1 -0
- package/dist/web/plugin/validate-view.js +9 -0
- package/dist/web/plugin/validate-view.js.map +1 -0
- package/dist/web/plugin/validate-view.test.d.ts +1 -0
- package/dist/web/plugin/validate-view.test.js +24 -0
- package/dist/web/plugin/validate-view.test.js.map +1 -0
- package/dist/web/proxy.js +0 -1
- package/dist/web/proxy.js.map +1 -1
- package/dist/web/types.js.map +1 -1
- package/package.json +51 -30
- package/tsconfig.base.json +33 -0
- package/dist/server/templates/development.hbs +0 -66
- package/dist/server/templates/production.hbs +0 -7
- package/dist/server/widgetsDevServer.d.ts +0 -12
- package/dist/server/widgetsDevServer.js +0 -47
- package/dist/server/widgetsDevServer.js.map +0 -1
- package/dist/test/widget.test.js +0 -255
- package/dist/test/widget.test.js.map +0 -1
- package/dist/web/hooks/use-widget-state.d.ts +0 -4
- package/dist/web/hooks/use-widget-state.js +0 -32
- package/dist/web/hooks/use-widget-state.js.map +0 -1
- package/dist/web/hooks/use-widget-state.test.js +0 -61
- package/dist/web/hooks/use-widget-state.test.js.map +0 -1
- package/dist/web/mount-widget.d.ts +0 -1
- package/dist/web/mount-widget.js.map +0 -1
- /package/dist/{test/widget.test.d.ts → cli/tunnel-control-server.test.d.ts} +0 -0
- /package/dist/{web/hooks/use-widget-state.test.d.ts → cli/tunnel-handler.test.d.ts} +0 -0
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import { dequal } from "dequal/lite";
|
|
2
2
|
import { McpAppBridge } from "./bridge.js";
|
|
3
|
+
const STORAGE_PREFIX = "sb:";
|
|
4
|
+
const MAX_STORAGE_ENTRIES = 200;
|
|
5
|
+
function findStorageKey(viewUUID) {
|
|
6
|
+
const suffix = `:${viewUUID}`;
|
|
7
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
8
|
+
const key = localStorage.key(i);
|
|
9
|
+
if (key?.startsWith(STORAGE_PREFIX) && key.endsWith(suffix)) {
|
|
10
|
+
return key;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
3
15
|
export class McpAppAdaptor {
|
|
4
16
|
static instance = null;
|
|
5
17
|
stores;
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
_viewState = null;
|
|
19
|
+
viewStateListeners = new Set();
|
|
20
|
+
_viewUUID = null;
|
|
21
|
+
_displayState = {
|
|
22
|
+
mode: "inline",
|
|
23
|
+
};
|
|
24
|
+
displayListeners = new Set();
|
|
8
25
|
constructor() {
|
|
9
26
|
this.stores = this.initializeStores();
|
|
27
|
+
this.subscribeToViewUUID();
|
|
10
28
|
}
|
|
11
29
|
static getInstance() {
|
|
12
30
|
if (!McpAppAdaptor.instance) {
|
|
@@ -21,56 +39,51 @@ export class McpAppAdaptor {
|
|
|
21
39
|
return this.stores[key];
|
|
22
40
|
}
|
|
23
41
|
callTool = async (name, args) => {
|
|
24
|
-
const
|
|
25
|
-
const response = await
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
name,
|
|
29
|
-
arguments: args ?? undefined,
|
|
30
|
-
},
|
|
42
|
+
const app = await McpAppBridge.getInstance().getApp();
|
|
43
|
+
const response = await app.callServerTool({
|
|
44
|
+
name,
|
|
45
|
+
arguments: args ?? undefined,
|
|
31
46
|
});
|
|
32
|
-
const result = response.content
|
|
33
|
-
.filter((content) => content.type === "text")
|
|
34
|
-
.map(({ text }) => text)
|
|
35
|
-
.join("\n");
|
|
36
47
|
return {
|
|
37
48
|
content: response.content,
|
|
38
49
|
structuredContent: response.structuredContent ?? {},
|
|
39
50
|
isError: response.isError ?? false,
|
|
40
|
-
result,
|
|
41
51
|
meta: response._meta ?? {},
|
|
42
52
|
};
|
|
43
53
|
};
|
|
44
|
-
requestDisplayMode = (mode) => {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
return bridge.request({
|
|
48
|
-
method: "ui/request-display-mode",
|
|
49
|
-
params: { mode },
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
throw new Error("Modal display mode is not accessible in MCP App.");
|
|
54
|
+
requestDisplayMode = async (mode) => {
|
|
55
|
+
const app = await McpAppBridge.getInstance().getApp();
|
|
56
|
+
return app.requestDisplayMode({ mode });
|
|
53
57
|
};
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
await
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
requestClose = async () => {
|
|
59
|
+
const app = await McpAppBridge.getInstance().getApp();
|
|
60
|
+
await app.requestTeardown();
|
|
61
|
+
};
|
|
62
|
+
requestSize = async (size) => {
|
|
63
|
+
const app = await McpAppBridge.getInstance().getApp();
|
|
64
|
+
await app.sendSizeChanged(size);
|
|
65
|
+
};
|
|
66
|
+
sendFollowUpMessage = async (prompt, _options) => {
|
|
67
|
+
const app = await McpAppBridge.getInstance().getApp();
|
|
68
|
+
await app.sendMessage({
|
|
69
|
+
role: "user",
|
|
70
|
+
content: [
|
|
71
|
+
{
|
|
72
|
+
type: "text",
|
|
73
|
+
text: prompt,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
67
76
|
});
|
|
68
77
|
};
|
|
69
|
-
openExternal(href) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
openExternal(href, options) {
|
|
79
|
+
if (options?.redirectUrl === false) {
|
|
80
|
+
console.warn("[skybridge] redirectUrl option is not supported by the MCP ui/open-link protocol and will be ignored.");
|
|
81
|
+
}
|
|
82
|
+
McpAppBridge.getInstance()
|
|
83
|
+
.getApp()
|
|
84
|
+
.then((app) => app.openLink({ url: href }))
|
|
85
|
+
.catch((err) => {
|
|
86
|
+
console.error("Failed to open external link:", err);
|
|
74
87
|
});
|
|
75
88
|
}
|
|
76
89
|
initializeStores() {
|
|
@@ -83,9 +96,9 @@ export class McpAppAdaptor {
|
|
|
83
96
|
displayMode: this.createHostContextStore(["displayMode"], ({ displayMode }) => displayMode ?? "inline"),
|
|
84
97
|
maxHeight: this.createHostContextStore(["containerDimensions"], ({ containerDimensions }) => {
|
|
85
98
|
if (containerDimensions && "maxHeight" in containerDimensions) {
|
|
86
|
-
return containerDimensions.maxHeight
|
|
99
|
+
return containerDimensions.maxHeight;
|
|
87
100
|
}
|
|
88
|
-
return
|
|
101
|
+
return undefined;
|
|
89
102
|
}),
|
|
90
103
|
userAgent: this.createHostContextStore(["platform", "deviceCapabilities"], ({ platform, deviceCapabilities }) => ({
|
|
91
104
|
device: {
|
|
@@ -100,31 +113,144 @@ export class McpAppAdaptor {
|
|
|
100
113
|
toolInput: this.createHostContextStore(["toolInput"], ({ toolInput }) => toolInput ?? null),
|
|
101
114
|
toolOutput: this.createHostContextStore(["toolResult"], ({ toolResult }) => toolResult?.structuredContent ?? null),
|
|
102
115
|
toolResponseMetadata: this.createHostContextStore(["toolResult"], ({ toolResult }) => toolResult?._meta ?? null),
|
|
103
|
-
|
|
116
|
+
display: {
|
|
117
|
+
subscribe: (onChange) => {
|
|
118
|
+
this.displayListeners.add(onChange);
|
|
119
|
+
return () => {
|
|
120
|
+
this.displayListeners.delete(onChange);
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
getSnapshot: () => this._displayState,
|
|
124
|
+
},
|
|
125
|
+
viewState: {
|
|
104
126
|
subscribe: (onChange) => {
|
|
105
|
-
this.
|
|
127
|
+
this.viewStateListeners.add(onChange);
|
|
106
128
|
return () => {
|
|
107
|
-
this.
|
|
129
|
+
this.viewStateListeners.delete(onChange);
|
|
108
130
|
};
|
|
109
131
|
},
|
|
110
|
-
getSnapshot: () => this.
|
|
132
|
+
getSnapshot: () => this._viewState,
|
|
111
133
|
},
|
|
112
134
|
};
|
|
113
135
|
}
|
|
114
|
-
|
|
136
|
+
setViewState = async (stateOrUpdater) => {
|
|
115
137
|
const newState = typeof stateOrUpdater === "function"
|
|
116
|
-
? stateOrUpdater(this.
|
|
138
|
+
? stateOrUpdater(this._viewState)
|
|
117
139
|
: stateOrUpdater;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
});
|
|
123
|
-
this._widgetState = newState;
|
|
124
|
-
this.widgetStateListeners.forEach((listener) => {
|
|
140
|
+
// must happen before the async bridge call to ensure the state is updated immediately for the UI,
|
|
141
|
+
// otherwise successive calls to setViewState may have stale state
|
|
142
|
+
this._viewState = newState;
|
|
143
|
+
this.viewStateListeners.forEach((listener) => {
|
|
125
144
|
listener();
|
|
126
145
|
});
|
|
146
|
+
this.persistToLocalStorage(newState);
|
|
147
|
+
try {
|
|
148
|
+
const app = await McpAppBridge.getInstance().getApp();
|
|
149
|
+
await app.updateModelContext({
|
|
150
|
+
structuredContent: newState,
|
|
151
|
+
content: [{ type: "text", text: JSON.stringify(newState) }],
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
console.error("Failed to update view state in MCP App.", error);
|
|
156
|
+
}
|
|
127
157
|
};
|
|
158
|
+
/**
|
|
159
|
+
* @throws File upload is not supported in MCP App.
|
|
160
|
+
*/
|
|
161
|
+
uploadFile() {
|
|
162
|
+
throw new Error("File upload is not supported in MCP App.");
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @throws File download is not supported in MCP App.
|
|
166
|
+
*/
|
|
167
|
+
getFileDownloadUrl() {
|
|
168
|
+
throw new Error("File download is not supported in MCP App.");
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @throws File selection is not supported in MCP App.
|
|
172
|
+
*/
|
|
173
|
+
selectFiles() {
|
|
174
|
+
throw new Error("File selection is not supported in MCP App.");
|
|
175
|
+
}
|
|
176
|
+
openModal(options) {
|
|
177
|
+
this._displayState = { mode: "modal", params: options.params };
|
|
178
|
+
this.displayListeners.forEach((listener) => {
|
|
179
|
+
listener();
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
closeModal() {
|
|
183
|
+
this._displayState = { mode: "inline" };
|
|
184
|
+
this.displayListeners.forEach((listener) => {
|
|
185
|
+
listener();
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
setOpenInAppUrl(_href) {
|
|
189
|
+
throw new Error("setOpenInAppUrl is not implemented in MCP App.");
|
|
190
|
+
}
|
|
191
|
+
subscribeToViewUUID() {
|
|
192
|
+
const bridge = McpAppBridge.getInstance();
|
|
193
|
+
bridge.subscribe("toolResult")(() => {
|
|
194
|
+
const toolResult = bridge.getSnapshot("toolResult");
|
|
195
|
+
const viewUUID = toolResult?._meta?.viewUUID;
|
|
196
|
+
if (viewUUID && viewUUID !== this._viewUUID) {
|
|
197
|
+
this._viewUUID = viewUUID;
|
|
198
|
+
this.restoreFromLocalStorage(viewUUID);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
// localStorage keys: sb:{unix_ms}:{viewUUID}
|
|
203
|
+
// Timestamp is updated on every write (LRU); eviction drops the least recently used entries.
|
|
204
|
+
restoreFromLocalStorage(viewUUID) {
|
|
205
|
+
try {
|
|
206
|
+
const existingKey = findStorageKey(viewUUID);
|
|
207
|
+
if (existingKey) {
|
|
208
|
+
const stored = localStorage.getItem(existingKey);
|
|
209
|
+
if (stored !== null) {
|
|
210
|
+
this._viewState = JSON.parse(stored);
|
|
211
|
+
this.viewStateListeners.forEach((listener) => {
|
|
212
|
+
listener();
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
console.error(err);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
persistToLocalStorage(state) {
|
|
222
|
+
if (!this._viewUUID || state === null) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
// Remove old key for this view, write with fresh timestamp (LRU)
|
|
227
|
+
const oldKey = findStorageKey(this._viewUUID);
|
|
228
|
+
if (oldKey) {
|
|
229
|
+
localStorage.removeItem(oldKey);
|
|
230
|
+
}
|
|
231
|
+
const newKey = `${STORAGE_PREFIX}${Date.now()}:${this._viewUUID}`;
|
|
232
|
+
localStorage.setItem(newKey, JSON.stringify(state));
|
|
233
|
+
// lru cleanup
|
|
234
|
+
const keys = [];
|
|
235
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
236
|
+
const key = localStorage.key(i);
|
|
237
|
+
if (key?.startsWith(STORAGE_PREFIX)) {
|
|
238
|
+
keys.push(key);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (keys.length <= MAX_STORAGE_ENTRIES) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
keys.sort();
|
|
245
|
+
const toRemove = keys.slice(0, keys.length - MAX_STORAGE_ENTRIES);
|
|
246
|
+
for (const key of toRemove) {
|
|
247
|
+
localStorage.removeItem(key);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
catch (err) {
|
|
251
|
+
console.error(err);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
128
254
|
createHostContextStore(keys, computeSnapshot) {
|
|
129
255
|
const bridge = McpAppBridge.getInstance();
|
|
130
256
|
let cachedValue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../../../../src/web/bridges/mcp-app/adaptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AASrC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAO3C,MAAM,OAAO,aAAa;IAChB,MAAM,CAAC,QAAQ,GAAyB,IAAI,CAAC;IAC7C,MAAM,CAEZ;IACM,YAAY,GAA+B,IAAI,CAAC;IAChD,oBAAoB,GAAG,IAAI,GAAG,EAAc,CAAC;IAErD;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAC;IACxC,CAAC;IAEM,MAAM,CAAC,WAAW;QACvB,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE,CAAC;YAC5B,aAAa,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAC;QAC/C,CAAC;QACD,OAAO,aAAa,CAAC,QAAQ,CAAC;IAChC,CAAC;IAEM,MAAM,CAAC,aAAa;QACzB,aAAa,CAAC,QAAQ,GAAG,IAAI,CAAC;IAChC,CAAC;IAEM,mBAAmB,CACxB,GAAM;QAEN,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAEM,QAAQ,GAAG,KAAK,EAIrB,IAAY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,GAAG,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;IAEO,sBAAsB,CAG5B,IAAU,EAAE,eAAkD;QAC9D,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,EAAE,CAAC;QAC1C,IAAI,WAA0B,CAAC;QAE/B,OAAO;YACL,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC;YACjC,WAAW,EAAE,GAAG,EAAE;gBAChB,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,CAChC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CACvB,CAAC;gBACvB,MAAM,QAAQ,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;gBAE1C,IAAI,WAAW,KAAK,SAAS,IAAI,MAAM,CAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,CAAC;oBAC/D,OAAO,WAAW,CAAC;gBACrB,CAAC;gBAED,WAAW,GAAG,QAAQ,CAAC;gBACvB,OAAO,QAAQ,CAAC;YAClB,CAAC;SACF,CAAC;IACJ,CAAC","sourcesContent":["import { dequal } from \"dequal/lite\";\nimport type {\n Adaptor,\n CallToolResponse,\n HostContext,\n HostContextStore,\n OpenExternalOptions,\n RequestDisplayMode,\n RequestModalOptions,\n RequestSizeOptions,\n SendFollowUpMessageOptions,\n SetViewStateAction,\n} from \"../types.js\";\nimport { McpAppBridge } from \"./bridge.js\";\nimport type { McpAppContext, McpAppContextKey } from \"./types.js\";\n\ntype PickContext<K extends readonly McpAppContextKey[]> = {\n [P in K[number]]: McpAppContext[P];\n};\n\nconst STORAGE_PREFIX = \"sb:\";\nconst MAX_STORAGE_ENTRIES = 200;\n\nfunction findStorageKey(viewUUID: string): string | undefined {\n const suffix = `:${viewUUID}`;\n for (let i = 0; i < localStorage.length; i++) {\n const key = localStorage.key(i);\n if (key?.startsWith(STORAGE_PREFIX) && key.endsWith(suffix)) {\n return key;\n }\n }\n return undefined;\n}\n\nexport class McpAppAdaptor implements Adaptor {\n private static instance: McpAppAdaptor | null = null;\n private stores: {\n [K in keyof HostContext]: HostContextStore<K>;\n };\n private _viewState: HostContext[\"viewState\"] = null;\n private viewStateListeners = new Set<() => void>();\n private _viewUUID: string | null = null;\n\n private _displayState: HostContext[\"display\"] = {\n mode: \"inline\",\n };\n private displayListeners = new Set<() => void>();\n\n private constructor() {\n this.stores = this.initializeStores();\n this.subscribeToViewUUID();\n }\n\n public static getInstance(): McpAppAdaptor {\n if (!McpAppAdaptor.instance) {\n McpAppAdaptor.instance = new McpAppAdaptor();\n }\n return McpAppAdaptor.instance;\n }\n\n public static resetInstance(): void {\n McpAppAdaptor.instance = null;\n }\n\n public getHostContextStore<K extends keyof HostContext>(\n key: K,\n ): HostContextStore<K> {\n return this.stores[key];\n }\n\n public callTool = async <\n ToolArgs extends Record<string, unknown> | null = null,\n ToolResponse extends CallToolResponse = CallToolResponse,\n >(\n name: string,\n args: ToolArgs,\n ): Promise<ToolResponse> => {\n const app = await McpAppBridge.getInstance().getApp();\n const response = await app.callServerTool({\n name,\n arguments: args ?? undefined,\n });\n\n return {\n content: response.content,\n structuredContent: response.structuredContent ?? {},\n isError: response.isError ?? false,\n meta: response._meta ?? {},\n } as ToolResponse;\n };\n\n public requestDisplayMode = async (mode: RequestDisplayMode) => {\n const app = await McpAppBridge.getInstance().getApp();\n return app.requestDisplayMode({ mode });\n };\n\n public requestClose = async (): Promise<void> => {\n const app = await McpAppBridge.getInstance().getApp();\n await app.requestTeardown();\n };\n\n public requestSize = async (size: RequestSizeOptions): Promise<void> => {\n const app = await McpAppBridge.getInstance().getApp();\n await app.sendSizeChanged(size);\n };\n\n public sendFollowUpMessage = async (\n prompt: string,\n _options?: SendFollowUpMessageOptions,\n ) => {\n const app = await McpAppBridge.getInstance().getApp();\n await app.sendMessage({\n role: \"user\",\n content: [\n {\n type: \"text\",\n text: prompt,\n },\n ],\n });\n };\n\n public openExternal(href: string, options?: OpenExternalOptions): void {\n if (options?.redirectUrl === false) {\n console.warn(\n \"[skybridge] redirectUrl option is not supported by the MCP ui/open-link protocol and will be ignored.\",\n );\n }\n\n McpAppBridge.getInstance()\n .getApp()\n .then((app) => app.openLink({ url: href }))\n .catch((err) => {\n console.error(\"Failed to open external link:\", err);\n });\n }\n\n private initializeStores(): {\n [K in keyof HostContext]: HostContextStore<K>;\n } {\n return {\n theme: this.createHostContextStore(\n [\"theme\"],\n ({ theme }) => theme ?? \"light\",\n ),\n locale: this.createHostContextStore(\n [\"locale\"],\n ({ locale }) => locale ?? \"en-US\",\n ),\n safeArea: this.createHostContextStore(\n [\"safeAreaInsets\"],\n ({ safeAreaInsets }) => ({\n insets: safeAreaInsets ?? { top: 0, right: 0, bottom: 0, left: 0 },\n }),\n ),\n displayMode: this.createHostContextStore(\n [\"displayMode\"],\n ({ displayMode }) => displayMode ?? \"inline\",\n ),\n maxHeight: this.createHostContextStore(\n [\"containerDimensions\"],\n ({ containerDimensions }) => {\n if (containerDimensions && \"maxHeight\" in containerDimensions) {\n return containerDimensions.maxHeight;\n }\n\n return undefined;\n },\n ),\n userAgent: this.createHostContextStore(\n [\"platform\", \"deviceCapabilities\"],\n ({ platform, deviceCapabilities }) => ({\n device: {\n type: platform === \"web\" ? \"desktop\" : (platform ?? \"unknown\"),\n },\n capabilities: {\n hover: true,\n touch: true,\n ...deviceCapabilities,\n },\n }),\n ),\n toolInput: this.createHostContextStore(\n [\"toolInput\"],\n ({ toolInput }) => toolInput ?? null,\n ),\n toolOutput: this.createHostContextStore(\n [\"toolResult\"],\n ({ toolResult }) => toolResult?.structuredContent ?? null,\n ),\n toolResponseMetadata: this.createHostContextStore(\n [\"toolResult\"],\n ({ toolResult }) => toolResult?._meta ?? null,\n ),\n display: {\n subscribe: (onChange: () => void) => {\n this.displayListeners.add(onChange);\n return () => {\n this.displayListeners.delete(onChange);\n };\n },\n getSnapshot: () => this._displayState,\n },\n viewState: {\n subscribe: (onChange: () => void) => {\n this.viewStateListeners.add(onChange);\n return () => {\n this.viewStateListeners.delete(onChange);\n };\n },\n getSnapshot: () => this._viewState,\n },\n };\n }\n\n public setViewState = async (\n stateOrUpdater: SetViewStateAction,\n ): Promise<void> => {\n const newState =\n typeof stateOrUpdater === \"function\"\n ? stateOrUpdater(this._viewState)\n : stateOrUpdater;\n\n // must happen before the async bridge call to ensure the state is updated immediately for the UI,\n // otherwise successive calls to setViewState may have stale state\n this._viewState = newState;\n this.viewStateListeners.forEach((listener) => {\n listener();\n });\n\n this.persistToLocalStorage(newState);\n\n try {\n const app = await McpAppBridge.getInstance().getApp();\n await app.updateModelContext({\n structuredContent: newState,\n content: [{ type: \"text\", text: JSON.stringify(newState) }],\n });\n } catch (error) {\n console.error(\"Failed to update view state in MCP App.\", error);\n }\n };\n\n /**\n * @throws File upload is not supported in MCP App.\n */\n public uploadFile(): Promise<{ fileId: string }> {\n throw new Error(\"File upload is not supported in MCP App.\");\n }\n\n /**\n * @throws File download is not supported in MCP App.\n */\n public getFileDownloadUrl(): Promise<{ downloadUrl: string }> {\n throw new Error(\"File download is not supported in MCP App.\");\n }\n\n /**\n * @throws File selection is not supported in MCP App.\n */\n public selectFiles(): Promise<{ fileId: string }[]> {\n throw new Error(\"File selection is not supported in MCP App.\");\n }\n\n public openModal(options: RequestModalOptions) {\n this._displayState = { mode: \"modal\", params: options.params };\n this.displayListeners.forEach((listener) => {\n listener();\n });\n }\n\n public closeModal() {\n this._displayState = { mode: \"inline\" };\n this.displayListeners.forEach((listener) => {\n listener();\n });\n }\n\n public setOpenInAppUrl(_href: string): Promise<void> {\n throw new Error(\"setOpenInAppUrl is not implemented in MCP App.\");\n }\n\n private subscribeToViewUUID(): void {\n const bridge = McpAppBridge.getInstance();\n bridge.subscribe(\"toolResult\")(() => {\n const toolResult = bridge.getSnapshot(\"toolResult\");\n const viewUUID = (\n toolResult?._meta as Record<string, unknown> | undefined\n )?.viewUUID as string | undefined;\n\n if (viewUUID && viewUUID !== this._viewUUID) {\n this._viewUUID = viewUUID;\n this.restoreFromLocalStorage(viewUUID);\n }\n });\n }\n\n // localStorage keys: sb:{unix_ms}:{viewUUID}\n // Timestamp is updated on every write (LRU); eviction drops the least recently used entries.\n private restoreFromLocalStorage(viewUUID: string): void {\n try {\n const existingKey = findStorageKey(viewUUID);\n if (existingKey) {\n const stored = localStorage.getItem(existingKey);\n if (stored !== null) {\n this._viewState = JSON.parse(stored);\n this.viewStateListeners.forEach((listener) => {\n listener();\n });\n }\n }\n } catch (err) {\n console.error(err);\n }\n }\n\n private persistToLocalStorage(state: Record<string, unknown> | null): void {\n if (!this._viewUUID || state === null) {\n return;\n }\n try {\n // Remove old key for this view, write with fresh timestamp (LRU)\n const oldKey = findStorageKey(this._viewUUID);\n if (oldKey) {\n localStorage.removeItem(oldKey);\n }\n const newKey = `${STORAGE_PREFIX}${Date.now()}:${this._viewUUID}`;\n localStorage.setItem(newKey, JSON.stringify(state));\n\n // lru cleanup\n const keys: string[] = [];\n for (let i = 0; i < localStorage.length; i++) {\n const key = localStorage.key(i);\n if (key?.startsWith(STORAGE_PREFIX)) {\n keys.push(key);\n }\n }\n if (keys.length <= MAX_STORAGE_ENTRIES) {\n return;\n }\n keys.sort();\n const toRemove = keys.slice(0, keys.length - MAX_STORAGE_ENTRIES);\n for (const key of toRemove) {\n localStorage.removeItem(key);\n }\n } catch (err) {\n console.error(err);\n }\n }\n\n private createHostContextStore<\n const Keys extends readonly McpAppContextKey[],\n R,\n >(keys: Keys, computeSnapshot: (context: PickContext<Keys>) => R) {\n const bridge = McpAppBridge.getInstance();\n let cachedValue: R | undefined;\n\n return {\n subscribe: bridge.subscribe(keys),\n getSnapshot: () => {\n const context = Object.fromEntries(\n keys.map((k) => [k, bridge.getSnapshot(k)]),\n ) as PickContext<Keys>;\n const newValue = computeSnapshot(context);\n\n if (cachedValue !== undefined && dequal(cachedValue, newValue)) {\n return cachedValue;\n }\n\n cachedValue = newValue;\n return newValue;\n },\n };\n }\n}\n"]}
|
|
@@ -1,43 +1,26 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { App } from "@modelcontextprotocol/ext-apps";
|
|
2
|
+
import type { Implementation } from "@modelcontextprotocol/sdk/types.js";
|
|
2
3
|
import type { Bridge, Subscribe } from "../types.js";
|
|
3
4
|
import type { McpAppContext, McpAppContextKey } from "./types.js";
|
|
4
|
-
|
|
5
|
-
export declare class McpAppBridge implements Bridge<McpUiHostContext> {
|
|
5
|
+
export declare class McpAppBridge implements Bridge<McpAppContext> {
|
|
6
6
|
private static instance;
|
|
7
7
|
context: McpAppContext;
|
|
8
8
|
private listeners;
|
|
9
|
-
private
|
|
10
|
-
private
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
private
|
|
15
|
-
|
|
16
|
-
static getInstance(options?: Partial<
|
|
9
|
+
private app;
|
|
10
|
+
private connectPromise;
|
|
11
|
+
constructor(options: {
|
|
12
|
+
appInfo: Implementation;
|
|
13
|
+
});
|
|
14
|
+
private connect;
|
|
15
|
+
getApp(): Promise<App>;
|
|
16
|
+
static getInstance(options?: Partial<{
|
|
17
|
+
appInfo: Implementation;
|
|
18
|
+
}>): McpAppBridge;
|
|
17
19
|
subscribe(key: McpAppContextKey): Subscribe;
|
|
18
20
|
subscribe(keys: readonly McpAppContextKey[]): Subscribe;
|
|
19
21
|
getSnapshot<K extends keyof McpAppContext>(key: K): McpAppContext[K];
|
|
20
22
|
cleanup: () => void;
|
|
21
23
|
static resetInstance(): void;
|
|
22
|
-
request<R extends {
|
|
23
|
-
method: string;
|
|
24
|
-
params?: unknown;
|
|
25
|
-
}, T>({ method, params, }: R): Promise<T>;
|
|
26
24
|
private emit;
|
|
27
25
|
private updateContext;
|
|
28
|
-
private init;
|
|
29
|
-
private handleMessage;
|
|
30
|
-
private handleResponse;
|
|
31
|
-
private handleNotification;
|
|
32
|
-
private handleRequest;
|
|
33
|
-
private connect;
|
|
34
|
-
private notify;
|
|
35
|
-
private sendSizeChanged;
|
|
36
|
-
/**
|
|
37
|
-
* Set up automatic size change notifications using ResizeObserver.
|
|
38
|
-
* Based on @modelcontextprotocol/ext-apps App.setupSizeChangedNotifications
|
|
39
|
-
* @see https://github.com/modelcontextprotocol/ext-apps/blob/main/src/app.ts#L940-L989
|
|
40
|
-
*/
|
|
41
|
-
private setupSizeChangedNotifications;
|
|
42
26
|
}
|
|
43
|
-
export {};
|