skybridge 0.0.0-dev.de4a871 → 0.0.0-dev.deff1f1
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/dist/src/server/devtoolsStaticServer.d.ts +4 -0
- package/dist/src/server/devtoolsStaticServer.js +22 -12
- package/dist/src/server/devtoolsStaticServer.js.map +1 -1
- package/dist/src/server/server.d.ts +9 -1
- package/dist/src/server/server.js +42 -15
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/server/templates/development.hbs +55 -2
- package/dist/src/server/templates/production.hbs +1 -1
- package/dist/src/server/widgetsDevServer.js +11 -3
- package/dist/src/server/widgetsDevServer.js.map +1 -1
- package/dist/src/test/widget.test.js +65 -12
- package/dist/src/test/widget.test.js.map +1 -1
- package/dist/src/web/bridges/adaptors/apps-sdk-adaptor.d.ts +13 -0
- package/dist/src/web/bridges/adaptors/apps-sdk-adaptor.js +33 -0
- package/dist/src/web/bridges/adaptors/apps-sdk-adaptor.js.map +1 -0
- package/dist/src/web/bridges/adaptors/mcp-app-adaptor.d.ts +16 -0
- package/dist/src/web/bridges/adaptors/mcp-app-adaptor.js +115 -0
- package/dist/src/web/bridges/adaptors/mcp-app-adaptor.js.map +1 -0
- package/dist/src/web/bridges/apps-sdk-bridge.d.ts +4 -2
- package/dist/src/web/bridges/apps-sdk-bridge.js +19 -14
- package/dist/src/web/bridges/apps-sdk-bridge.js.map +1 -1
- package/dist/src/web/bridges/hooks/use-adaptor.d.ts +2 -0
- package/dist/src/web/bridges/hooks/use-adaptor.js +8 -0
- package/dist/src/web/bridges/hooks/use-adaptor.js.map +1 -0
- package/dist/src/web/bridges/hooks/use-bridge.d.ts +2 -3
- package/dist/src/web/bridges/hooks/use-bridge.js +5 -24
- package/dist/src/web/bridges/hooks/use-bridge.js.map +1 -1
- package/dist/src/web/bridges/hooks/use-mcp-app-bridge.d.ts +3 -2
- package/dist/src/web/bridges/hooks/use-mcp-app-bridge.js +1 -1
- package/dist/src/web/bridges/hooks/use-mcp-app-bridge.js.map +1 -1
- package/dist/src/web/bridges/hooks/use-mcp-app-bridge.test.js +9 -26
- package/dist/src/web/bridges/hooks/use-mcp-app-bridge.test.js.map +1 -1
- package/dist/src/web/bridges/index.d.ts +1 -0
- package/dist/src/web/bridges/index.js +1 -0
- package/dist/src/web/bridges/index.js.map +1 -1
- package/dist/src/web/bridges/mcp-app-bridge.d.ts +19 -7
- package/dist/src/web/bridges/mcp-app-bridge.js +58 -26
- package/dist/src/web/bridges/mcp-app-bridge.js.map +1 -1
- package/dist/src/web/bridges/types.d.ts +57 -0
- package/dist/src/web/bridges/types.js +2 -0
- package/dist/src/web/bridges/types.js.map +1 -0
- package/dist/src/web/data-llm.js +4 -2
- package/dist/src/web/data-llm.js.map +1 -1
- package/dist/src/web/generate-helpers.test-d.js +4 -1
- package/dist/src/web/generate-helpers.test-d.js.map +1 -1
- package/dist/src/web/hooks/test/utils.d.ts +10 -0
- package/dist/src/web/hooks/test/utils.js +40 -0
- package/dist/src/web/hooks/test/utils.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.d.ts +1 -1
- package/dist/src/web/hooks/use-call-tool.js +12 -4
- package/dist/src/web/hooks/use-call-tool.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.test-d.js +1 -1
- package/dist/src/web/hooks/use-call-tool.test-d.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.test.js +28 -3
- package/dist/src/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/src/web/hooks/use-display-mode.d.ts +1 -1
- package/dist/src/web/hooks/use-display-mode.js +6 -3
- package/dist/src/web/hooks/use-display-mode.js.map +1 -1
- package/dist/src/web/hooks/use-layout.js +3 -3
- package/dist/src/web/hooks/use-layout.js.map +1 -1
- package/dist/src/web/hooks/use-layout.test.js +87 -38
- package/dist/src/web/hooks/use-layout.test.js.map +1 -1
- package/dist/src/web/hooks/use-open-external.js +5 -3
- package/dist/src/web/hooks/use-open-external.js.map +1 -1
- package/dist/src/web/hooks/use-open-external.test.js +41 -15
- package/dist/src/web/hooks/use-open-external.test.js.map +1 -1
- package/dist/src/web/hooks/use-request-modal.d.ts +6 -3
- package/dist/src/web/hooks/use-request-modal.js +6 -1
- package/dist/src/web/hooks/use-request-modal.js.map +1 -1
- package/dist/src/web/hooks/use-request-modal.test.js +35 -2
- package/dist/src/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/src/web/hooks/use-send-follow-up-message.js +3 -6
- package/dist/src/web/hooks/use-send-follow-up-message.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.d.ts +12 -1
- package/dist/src/web/hooks/use-tool-info.js +15 -9
- package/dist/src/web/hooks/use-tool-info.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test-d.js +40 -4
- package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test.js +117 -47
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/src/web/hooks/use-user.js +2 -2
- package/dist/src/web/hooks/use-user.js.map +1 -1
- package/dist/src/web/hooks/use-user.test.js +83 -34
- package/dist/src/web/hooks/use-user.test.js.map +1 -1
- package/dist/src/web/plugin/transform-data-llm.js +6 -3
- package/dist/src/web/plugin/transform-data-llm.js.map +1 -1
- package/dist/src/web/types.d.ts +20 -9
- package/dist/src/web/types.js.map +1 -1
- package/package.json +28 -26
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { dequal } from "dequal/lite";
|
|
2
|
+
import { McpAppBridge, } from "../mcp-app-bridge.js";
|
|
3
|
+
export class McpAppAdaptor {
|
|
4
|
+
static instance = null;
|
|
5
|
+
stores;
|
|
6
|
+
constructor() {
|
|
7
|
+
this.stores = this.initializeStores();
|
|
8
|
+
}
|
|
9
|
+
static getInstance() {
|
|
10
|
+
if (!McpAppAdaptor.instance) {
|
|
11
|
+
McpAppAdaptor.instance = new McpAppAdaptor();
|
|
12
|
+
}
|
|
13
|
+
return McpAppAdaptor.instance;
|
|
14
|
+
}
|
|
15
|
+
static resetInstance() {
|
|
16
|
+
McpAppAdaptor.instance = null;
|
|
17
|
+
}
|
|
18
|
+
getExternalStore(key) {
|
|
19
|
+
return this.stores[key];
|
|
20
|
+
}
|
|
21
|
+
callTool = async (name, args) => {
|
|
22
|
+
const bridge = McpAppBridge.getInstance();
|
|
23
|
+
const response = await bridge.request({
|
|
24
|
+
method: "tools/call",
|
|
25
|
+
params: {
|
|
26
|
+
name,
|
|
27
|
+
arguments: args ?? undefined,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
const result = response.content
|
|
31
|
+
.filter((content) => content.type === "text")
|
|
32
|
+
.map(({ text }) => text)
|
|
33
|
+
.join("\n");
|
|
34
|
+
return {
|
|
35
|
+
content: response.content,
|
|
36
|
+
structuredContent: response.structuredContent ?? {},
|
|
37
|
+
isError: response.isError ?? false,
|
|
38
|
+
result,
|
|
39
|
+
_meta: response._meta ?? {},
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
requestDisplayMode = (mode) => {
|
|
43
|
+
const bridge = McpAppBridge.getInstance();
|
|
44
|
+
if (mode !== "modal") {
|
|
45
|
+
return bridge.request({
|
|
46
|
+
method: "ui/request-display-mode",
|
|
47
|
+
params: { mode },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
throw new Error("Modal display mode is not accessible in MCP App.");
|
|
51
|
+
};
|
|
52
|
+
sendFollowUpMessage = async (prompt) => {
|
|
53
|
+
const bridge = McpAppBridge.getInstance();
|
|
54
|
+
await bridge.request({
|
|
55
|
+
method: "ui/message",
|
|
56
|
+
params: {
|
|
57
|
+
role: "user",
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: "text",
|
|
61
|
+
text: prompt,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
openExternal(href) {
|
|
68
|
+
const bridge = McpAppBridge.getInstance();
|
|
69
|
+
bridge.request({
|
|
70
|
+
method: "ui/open-link",
|
|
71
|
+
params: { url: href },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
initializeStores() {
|
|
75
|
+
return {
|
|
76
|
+
theme: this.createExternalStore(["theme"], ({ theme }) => theme ?? "light"),
|
|
77
|
+
locale: this.createExternalStore(["locale"], ({ locale }) => locale ?? "en-US"),
|
|
78
|
+
safeArea: this.createExternalStore(["safeAreaInsets"], ({ safeAreaInsets }) => ({
|
|
79
|
+
insets: safeAreaInsets ?? { top: 0, right: 0, bottom: 0, left: 0 },
|
|
80
|
+
})),
|
|
81
|
+
displayMode: this.createExternalStore(["displayMode"], ({ displayMode }) => displayMode ?? "inline"),
|
|
82
|
+
maxHeight: this.createExternalStore(["viewport"], ({ viewport }) => viewport?.maxHeight ?? window.innerHeight),
|
|
83
|
+
userAgent: this.createExternalStore(["platform", "deviceCapabilities"], ({ platform, deviceCapabilities }) => ({
|
|
84
|
+
device: {
|
|
85
|
+
type: platform === "web" ? "desktop" : (platform ?? "unknown"),
|
|
86
|
+
},
|
|
87
|
+
capabilities: {
|
|
88
|
+
hover: true,
|
|
89
|
+
touch: true,
|
|
90
|
+
...deviceCapabilities,
|
|
91
|
+
},
|
|
92
|
+
})),
|
|
93
|
+
toolInput: this.createExternalStore(["toolInput"], ({ toolInput }) => toolInput ?? null),
|
|
94
|
+
toolOutput: this.createExternalStore(["toolResult"], ({ toolResult }) => toolResult?.structuredContent ?? null),
|
|
95
|
+
toolResponseMetadata: this.createExternalStore(["toolResult"], ({ toolResult }) => toolResult?._meta ?? null),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
createExternalStore(keys, computeSnapshot) {
|
|
99
|
+
const bridge = McpAppBridge.getInstance();
|
|
100
|
+
let cachedValue;
|
|
101
|
+
return {
|
|
102
|
+
subscribe: bridge.subscribe(keys),
|
|
103
|
+
getSnapshot: () => {
|
|
104
|
+
const context = Object.fromEntries(keys.map((k) => [k, bridge.getSnapshot(k)]));
|
|
105
|
+
const newValue = computeSnapshot(context);
|
|
106
|
+
if (cachedValue !== undefined && dequal(cachedValue, newValue)) {
|
|
107
|
+
return cachedValue;
|
|
108
|
+
}
|
|
109
|
+
cachedValue = newValue;
|
|
110
|
+
return newValue;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=mcp-app-adaptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-app-adaptor.js","sourceRoot":"","sources":["../../../../../src/web/bridges/adaptors/mcp-app-adaptor.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EACL,YAAY,GAGb,MAAM,sBAAsB,CAAC;AAa9B,MAAM,OAAO,aAAa;IAChB,MAAM,CAAC,QAAQ,GAAyB,IAAI,CAAC;IAC7C,MAAM,CAEZ;IACF;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,gBAAgB,CACrB,GAAM;QAEN,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAEM,QAAQ,GAAG,KAAK,EAIrB,IAAY,EACZ,IAAc,EACS,EAAE;QACzB,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,EAAE,CAAC;QAC1C,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAkC;YACrE,MAAM,EAAE,YAAY;YACpB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E,CAAC;oBAC/D,OAAO,WAAW,CAAC;gBACrB,CAAC;gBAED,WAAW,GAAG,QAAQ,CAAC;gBACvB,OAAO,QAAQ,CAAC;YAClB,CAAC;SACF,CAAC;IACJ,CAAC"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type OpenAiProperties } from "../types.js";
|
|
2
|
-
|
|
2
|
+
import type { Bridge, Subscribe } from "./types.js";
|
|
3
|
+
export declare class AppsSdkBridge implements Bridge<OpenAiProperties> {
|
|
3
4
|
private static instance;
|
|
4
5
|
static getInstance(): AppsSdkBridge;
|
|
5
6
|
static resetInstance(): void;
|
|
6
|
-
subscribe
|
|
7
|
+
subscribe(key: keyof OpenAiProperties): Subscribe;
|
|
8
|
+
subscribe(keys: readonly (keyof OpenAiProperties)[]): Subscribe;
|
|
7
9
|
getSnapshot: <K extends keyof OpenAiProperties>(key: K) => (import("../types.js").OpenAiMethods<import("../types.js").UnknownObject> & OpenAiProperties<Record<never, unknown>, import("../types.js").UnknownObject, import("../types.js").UnknownObject, import("../types.js").UnknownObject>)[K];
|
|
8
10
|
}
|
|
@@ -16,21 +16,26 @@ export class AppsSdkBridge {
|
|
|
16
16
|
AppsSdkBridge.instance = null;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
subscribe
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
subscribe(keyOrKeys) {
|
|
20
|
+
const keys = Array.isArray(keyOrKeys)
|
|
21
|
+
? keyOrKeys
|
|
22
|
+
: [keyOrKeys];
|
|
23
|
+
return (onChange) => {
|
|
24
|
+
const handleSetGlobal = (event) => {
|
|
25
|
+
const hasRelevantChange = keys.some((key) => event.detail.globals[key] !== undefined);
|
|
26
|
+
if (!hasRelevantChange) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
onChange();
|
|
30
|
+
};
|
|
31
|
+
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobal, {
|
|
32
|
+
passive: true,
|
|
33
|
+
});
|
|
34
|
+
return () => {
|
|
35
|
+
window.removeEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobal);
|
|
36
|
+
};
|
|
26
37
|
};
|
|
27
|
-
|
|
28
|
-
passive: true,
|
|
29
|
-
});
|
|
30
|
-
return () => {
|
|
31
|
-
window.removeEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobal);
|
|
32
|
-
};
|
|
33
|
-
};
|
|
38
|
+
}
|
|
34
39
|
getSnapshot = (key) => {
|
|
35
40
|
if (window.openai === undefined) {
|
|
36
41
|
throw new Error(`window.openai is not available. Make sure you're calling the hook requiring ${key} within the OpenAI iFrame skybridge runtime.`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apps-sdk-bridge.js","sourceRoot":"","sources":["../../../../src/web/bridges/apps-sdk-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,GAEvB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"apps-sdk-bridge.js","sourceRoot":"","sources":["../../../../src/web/bridges/apps-sdk-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,GAEvB,MAAM,aAAa,CAAC;AAGrB,MAAM,OAAO,aAAa;IAChB,MAAM,CAAC,QAAQ,GAAyB,IAAI,CAAC;IAE9C,MAAM,CAAC,WAAW;QACvB,IACE,MAAM,CAAC,SAAS,CAAC,QAAQ,KAAK,UAAU;YACxC,MAAM,CAAC,MAAM,KAAK,SAAS,EAC3B,CAAC;YACD,MAAM,IAAI,KAAK,CACb,0DAA0D,CAC3D,CAAC;QACJ,CAAC;QACD,IAAI,aAAa,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpC,aAAa,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAC;QAC/C,CAAC;QACD,OAAO,aAAa,CAAC,QAAQ,CAAC;IAChC,CAAC;IAEM,MAAM,CAAC,aA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.js";
|
|
3
|
+
export const useAdaptor = () => {
|
|
4
|
+
return window.skybridge.hostType === "apps-sdk"
|
|
5
|
+
? AppsSdkAdaptor.getInstance()
|
|
6
|
+
: McpAppAdaptor.getInstance();
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=use-adaptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-adaptor.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-adaptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAG/D,MAAM,CAAC,MAAM,UAAU,GAAG,GAAY,EAAE;IACtC,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,KAAK,UAAU;QAC7C,CAAC,CAAC,cAAc,CAAC,WAAW,EAAE;QAC9B,CAAC,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC,CAAC"}
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export
|
|
3
|
-
export declare const useBridge: <K extends keyof BridgeInterface>(key: K, defaultValue?: BridgeInterface[K]) => BridgeInterface[K];
|
|
1
|
+
import type { BridgeInterface } from "../types.js";
|
|
2
|
+
export declare const useBridge: <K extends keyof BridgeInterface>(key: K) => BridgeInterface[K];
|
|
@@ -1,27 +1,8 @@
|
|
|
1
1
|
import { useSyncExternalStore } from "react";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
const getExternalStore = (key, defaultValue = DEFAULT_VALUE_FOR_MCP_APP_BRIDGE[key]) => {
|
|
9
|
-
const hostType = window.skybridge.hostType;
|
|
10
|
-
if (hostType === "apps-sdk") {
|
|
11
|
-
const bridge = AppsSdkBridge.getInstance();
|
|
12
|
-
return {
|
|
13
|
-
subscribe: bridge.subscribe(key),
|
|
14
|
-
getSnapshot: () => bridge.getSnapshot(key),
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
const bridge = McpAppBridge.getInstance();
|
|
18
|
-
return {
|
|
19
|
-
subscribe: bridge.subscribe(key),
|
|
20
|
-
getSnapshot: () => (bridge.getSnapshot(key) ?? defaultValue),
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
export const useBridge = (key, defaultValue = DEFAULT_VALUE_FOR_MCP_APP_BRIDGE[key]) => {
|
|
24
|
-
const externalStore = getExternalStore(key, defaultValue);
|
|
25
|
-
return useSyncExternalStore(externalStore.subscribe, externalStore.getSnapshot, () => DEFAULT_VALUE_FOR_MCP_APP_BRIDGE[key]);
|
|
2
|
+
import { useAdaptor } from "./use-adaptor.js";
|
|
3
|
+
export const useBridge = (key) => {
|
|
4
|
+
const adaptor = useAdaptor();
|
|
5
|
+
const externalStore = adaptor.getExternalStore(key);
|
|
6
|
+
return useSyncExternalStore(externalStore.subscribe, externalStore.getSnapshot);
|
|
26
7
|
};
|
|
27
8
|
//# sourceMappingURL=use-bridge.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-bridge.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-bridge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-bridge.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAE7C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,GAAM,EACc,EAAE;IACtB,MAAM,OAAO,GAAG,UAAU,EAAE,CAAC;IAC7B,MAAM,aAAa,GAAG,OAAO,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAEpD,OAAO,oBAAoB,CACzB,aAAa,CAAC,SAAS,EACvB,aAAa,CAAC,WAAW,CAC1B,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { McpUiInitializeRequest } from "@modelcontextprotocol/ext-apps";
|
|
2
|
+
import { type McpAppBridgeContext } from "../mcp-app-bridge.js";
|
|
2
3
|
type McpAppInitializationOptions = Pick<McpUiInitializeRequest["params"], "appInfo">;
|
|
3
|
-
export declare function useMcpAppBridge<K extends keyof
|
|
4
|
+
export declare function useMcpAppBridge<K extends keyof McpAppBridgeContext>(key: K, options?: Partial<McpAppInitializationOptions>, requestTimeout?: number): McpAppBridgeContext[K];
|
|
4
5
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useSyncExternalStore } from "react";
|
|
2
|
-
import { McpAppBridge } from "../mcp-app-bridge";
|
|
2
|
+
import { McpAppBridge } from "../mcp-app-bridge.js";
|
|
3
3
|
export function useMcpAppBridge(key, options, requestTimeout) {
|
|
4
4
|
const bridge = McpAppBridge.getInstance(options, requestTimeout);
|
|
5
5
|
return useSyncExternalStore(bridge.subscribe(key), () => bridge.getSnapshot(key));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-mcp-app-bridge.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-mcp-app-bridge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-mcp-app-bridge.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-mcp-app-bridge.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAE,YAAY,EAA4B,MAAM,sBAAsB,CAAC;AAO9E,MAAM,UAAU,eAAe,CAC7B,GAAM,EACN,OAA8C,EAC9C,cAAuB;IAEvB,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IACjE,OAAO,oBAAoB,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CACtD,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,CACxB,CAAC;AACJ,CAAC"}
|
|
@@ -1,41 +1,22 @@
|
|
|
1
1
|
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
2
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { getMcpAppHostPostMessageMock } from "../../hooks/test/utils.js";
|
|
3
4
|
import { McpAppBridge } from "../mcp-app-bridge.js";
|
|
4
5
|
import { useMcpAppBridge } from "./use-mcp-app-bridge.js";
|
|
5
6
|
describe("useMcpAppBridge", () => {
|
|
6
|
-
let mockPostMessage;
|
|
7
7
|
beforeEach(async () => {
|
|
8
|
-
mockPostMessage = vi.fn();
|
|
9
|
-
Object.defineProperty(window, "parent", {
|
|
10
|
-
value: { postMessage: mockPostMessage },
|
|
11
|
-
writable: true,
|
|
12
|
-
configurable: true,
|
|
13
|
-
});
|
|
14
8
|
vi.stubGlobal("skybridge", { hostType: "mcp-app" });
|
|
15
9
|
McpAppBridge.resetInstance();
|
|
16
10
|
});
|
|
17
11
|
afterEach(() => {
|
|
12
|
+
vi.unstubAllGlobals();
|
|
18
13
|
vi.clearAllMocks();
|
|
19
14
|
});
|
|
20
|
-
it("should return the theme value from host context", async () => {
|
|
15
|
+
it("should return the theme value from host context and update on notification", async () => {
|
|
16
|
+
vi.stubGlobal("parent", {
|
|
17
|
+
postMessage: getMcpAppHostPostMessageMock({ theme: "light" }),
|
|
18
|
+
});
|
|
21
19
|
const { result } = renderHook(() => useMcpAppBridge("theme"));
|
|
22
|
-
const initCall = mockPostMessage.mock.calls.find((call) => call[0].method === "ui/initialize");
|
|
23
|
-
if (initCall) {
|
|
24
|
-
act(() => {
|
|
25
|
-
window.dispatchEvent(new MessageEvent("message", {
|
|
26
|
-
data: {
|
|
27
|
-
jsonrpc: "2.0",
|
|
28
|
-
id: initCall[0].id,
|
|
29
|
-
result: {
|
|
30
|
-
protocolVersion: "2025-06-18",
|
|
31
|
-
hostInfo: { name: "test-host", version: "1.0.0" },
|
|
32
|
-
hostCapabilities: {},
|
|
33
|
-
hostContext: { theme: "light" },
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
}));
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
20
|
await waitFor(() => {
|
|
40
21
|
expect(result.current).toBe("light");
|
|
41
22
|
});
|
|
@@ -45,8 +26,10 @@ describe("useMcpAppBridge", () => {
|
|
|
45
26
|
const consoleErrorSpy = vi
|
|
46
27
|
.spyOn(console, "error")
|
|
47
28
|
.mockImplementation(() => { });
|
|
29
|
+
const nonRespondingMock = vi.fn();
|
|
30
|
+
vi.stubGlobal("parent", { postMessage: nonRespondingMock });
|
|
48
31
|
renderHook(() => useMcpAppBridge("theme", undefined, 100));
|
|
49
|
-
expect(
|
|
32
|
+
expect(nonRespondingMock).toHaveBeenCalledWith(expect.objectContaining({ method: "ui/initialize" }), "*");
|
|
50
33
|
await act(async () => {
|
|
51
34
|
await vi.advanceTimersByTimeAsync(100);
|
|
52
35
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-mcp-app-bridge.test.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-mcp-app-bridge.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE1D,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,
|
|
1
|
+
{"version":3,"file":"use-mcp-app-bridge.test.js","sourceRoot":"","sources":["../../../../../src/web/bridges/hooks/use-mcp-app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,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,eAAe,GAAG,EAAE;aACvB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC;aACvB,kBAAkB,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAEhC,MAAM,iBAAiB,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAE,WAAW,EAAE,iBAAiB,EAAE,CAAC,CAAC;QAE5D,UAAU,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC,CAAC;QAE3D,MAAM,CAAC,iBAAiB,CAAC,CAAC,oBAAoB,CAC5C,MAAM,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC,EACpD,GAAG,CACJ,CAAC;QAEF,MAAM,GAAG,CAAC,KAAK,IAAI,EAAE;YACnB,MAAM,EAAE,CAAC,wBAAwB,CAAC,GAAG,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,eAAe,CAAC,CAAC,oBAAoB,CAC1C,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAC/B,CAAC;QAEF,eAAe,CAAC,WAAW,EAAE,CAAC;QAC9B,EAAE,CAAC,aAAa,EAAE,CAAC;IACrB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/web/bridges/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/web/bridges/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC"}
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import type { McpUiHostContext, McpUiInitializeRequest } from "@modelcontextprotocol/ext-apps";
|
|
1
|
+
import type { McpUiHostContext, McpUiInitializeRequest, McpUiToolCancelledNotification, McpUiToolInputNotification, McpUiToolResultNotification } from "@modelcontextprotocol/ext-apps";
|
|
2
|
+
import type { Bridge, Subscribe } from "./types.js";
|
|
2
3
|
type McpAppInitializationOptions = Pick<McpUiInitializeRequest["params"], "appInfo">;
|
|
3
|
-
export
|
|
4
|
+
export type McpToolState = {
|
|
5
|
+
toolInput: NonNullable<McpUiToolInputNotification["params"]["arguments"]> | null;
|
|
6
|
+
toolResult: McpUiToolResultNotification["params"] | null;
|
|
7
|
+
toolCancelled: McpUiToolCancelledNotification["params"] | null;
|
|
8
|
+
};
|
|
9
|
+
export type McpAppBridgeContext = McpUiHostContext & McpToolState;
|
|
10
|
+
export type McpAppBridgeKey = keyof McpAppBridgeContext;
|
|
11
|
+
export declare class McpAppBridge implements Bridge<McpUiHostContext> {
|
|
4
12
|
private static instance;
|
|
5
|
-
context:
|
|
13
|
+
context: McpAppBridgeContext;
|
|
6
14
|
private listeners;
|
|
7
15
|
private pendingRequests;
|
|
8
16
|
private nextId;
|
|
@@ -11,13 +19,17 @@ export declare class McpAppBridge {
|
|
|
11
19
|
private requestTimeout;
|
|
12
20
|
constructor(options: McpAppInitializationOptions, requestTimeout?: number);
|
|
13
21
|
static getInstance(options?: Partial<McpAppInitializationOptions>, requestTimeout?: number): McpAppBridge;
|
|
14
|
-
subscribe
|
|
15
|
-
|
|
22
|
+
subscribe(key: McpAppBridgeKey): Subscribe;
|
|
23
|
+
subscribe(keys: readonly McpAppBridgeKey[]): Subscribe;
|
|
24
|
+
getSnapshot<K extends keyof McpAppBridgeContext>(key: K): McpAppBridgeContext[K];
|
|
16
25
|
cleanup: () => void;
|
|
17
26
|
static resetInstance(): void;
|
|
18
|
-
|
|
27
|
+
request<R extends {
|
|
28
|
+
method: string;
|
|
29
|
+
params?: unknown;
|
|
30
|
+
}, T>({ method, params, }: R): Promise<T>;
|
|
19
31
|
private emit;
|
|
20
|
-
private
|
|
32
|
+
private updateContext;
|
|
21
33
|
private init;
|
|
22
34
|
private handleMessage;
|
|
23
35
|
private connect;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
const LATEST_PROTOCOL_VERSION = "2025-11-21";
|
|
2
2
|
export class McpAppBridge {
|
|
3
3
|
static instance = null;
|
|
4
|
-
context =
|
|
4
|
+
context = {
|
|
5
|
+
toolInput: null,
|
|
6
|
+
toolCancelled: null,
|
|
7
|
+
toolResult: null,
|
|
8
|
+
};
|
|
5
9
|
listeners = new Map();
|
|
6
10
|
pendingRequests = new Map();
|
|
7
11
|
nextId = 1;
|
|
@@ -32,14 +36,23 @@ export class McpAppBridge {
|
|
|
32
36
|
}
|
|
33
37
|
return McpAppBridge.instance;
|
|
34
38
|
}
|
|
35
|
-
subscribe
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
subscribe(keyOrKeys) {
|
|
40
|
+
const keys = Array.isArray(keyOrKeys) ? keyOrKeys : [keyOrKeys];
|
|
41
|
+
return (onChange) => {
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
this.listeners.set(key, new Set([...(this.listeners.get(key) || []), onChange]));
|
|
44
|
+
}
|
|
45
|
+
this.init();
|
|
46
|
+
return () => {
|
|
47
|
+
for (const key of keys) {
|
|
48
|
+
this.listeners.get(key)?.delete(onChange);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
getSnapshot(key) {
|
|
54
|
+
return this.context[key];
|
|
55
|
+
}
|
|
43
56
|
cleanup = () => {
|
|
44
57
|
window.removeEventListener("message", this.handleMessage);
|
|
45
58
|
this.pendingRequests.forEach((request) => {
|
|
@@ -73,17 +86,16 @@ export class McpAppBridge {
|
|
|
73
86
|
listener();
|
|
74
87
|
});
|
|
75
88
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return;
|
|
79
|
-
this.context = context;
|
|
89
|
+
updateContext(context) {
|
|
90
|
+
this.context = { ...this.context, ...context };
|
|
80
91
|
for (const key of Object.keys(context)) {
|
|
81
92
|
this.emit(key);
|
|
82
93
|
}
|
|
83
94
|
}
|
|
84
95
|
init() {
|
|
85
|
-
if (this.initialized)
|
|
96
|
+
if (this.initialized) {
|
|
86
97
|
return;
|
|
98
|
+
}
|
|
87
99
|
this.initialized = true;
|
|
88
100
|
if (typeof window === "undefined" || window.parent === window) {
|
|
89
101
|
return;
|
|
@@ -93,21 +105,41 @@ export class McpAppBridge {
|
|
|
93
105
|
}
|
|
94
106
|
handleMessage = (event) => {
|
|
95
107
|
const data = event.data;
|
|
96
|
-
if (data
|
|
108
|
+
if (data.jsonrpc !== "2.0") {
|
|
97
109
|
return;
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
110
|
+
}
|
|
111
|
+
if ("id" in data) {
|
|
112
|
+
const request = this.pendingRequests.get(data.id);
|
|
113
|
+
if (request) {
|
|
114
|
+
clearTimeout(request.timeout);
|
|
115
|
+
this.pendingRequests.delete(data.id);
|
|
116
|
+
if ("error" in data) {
|
|
117
|
+
request.reject(new Error(data.error.message));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
request.resolve(data.result);
|
|
105
121
|
}
|
|
106
|
-
request.resolve(data.result);
|
|
107
122
|
return;
|
|
108
123
|
}
|
|
109
|
-
|
|
110
|
-
|
|
124
|
+
switch (data.method) {
|
|
125
|
+
case "ui/notifications/host-context-changed":
|
|
126
|
+
this.updateContext(data.params);
|
|
127
|
+
return;
|
|
128
|
+
case "ui/notifications/tool-input":
|
|
129
|
+
this.updateContext({
|
|
130
|
+
toolInput: data.params.arguments ?? {},
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
case "ui/notifications/tool-result":
|
|
134
|
+
this.updateContext({
|
|
135
|
+
toolResult: data.params,
|
|
136
|
+
});
|
|
137
|
+
return;
|
|
138
|
+
case "ui/notifications/tool-cancelled":
|
|
139
|
+
this.updateContext({
|
|
140
|
+
toolCancelled: data.params,
|
|
141
|
+
});
|
|
142
|
+
return;
|
|
111
143
|
}
|
|
112
144
|
};
|
|
113
145
|
async connect() {
|
|
@@ -116,7 +148,7 @@ export class McpAppBridge {
|
|
|
116
148
|
method: "ui/initialize",
|
|
117
149
|
params: this.appInitializationOptions,
|
|
118
150
|
});
|
|
119
|
-
this.
|
|
151
|
+
this.updateContext(result.hostContext);
|
|
120
152
|
this.notify({ method: "ui/notifications/initialized" });
|
|
121
153
|
}
|
|
122
154
|
catch (err) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-app-bridge.js","sourceRoot":"","sources":["../../../../src/web/bridges/mcp-app-bridge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mcp-app-bridge.js","sourceRoot":"","sources":["../../../../src/web/bridges/mcp-app-bridge.ts"],"names":[],"mappings":"AAmCA,MAAM,uBAAuB,GAAG,YAAY,CAAC;AAqB7C,MAAM,OAAO,YAAY;IACf,MAAM,CAAC,QAAQ,GAAwB,IAAI,CAAC;IAC7C,OAAO,GAAwB;QACpC,SAAS,EAAE,IAAI;QACf,aAAa,EAAE,IAAI;QACnB,UAAU,EAAE,IAAI;KACjB,CAAC;IACM,SAAS,GAAG,IAAI,GAAG,EAAoC,CAAC;IACxD,eAAe,GAAG,IAAI,GAAG,EAA4C,CAAC;IACtE,MAAM,GAAG,CAAC,CAAC;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K,iCAAiC;gBACpC,IAAI,CAAC,aAAa,CAAC;oBACjB,aAAa,EAAE,IAAI,CAAC,MAAM;iBAC3B,CAAC,CAAC;gBACH,OAAO;QACX,CAAC;IACH,CAAC,CAAC;IAEM,KAAK,CAAC,OAAO;QACnB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAG/B;gBACA,MAAM,EAAE,eAAe;gBACvB,MAAM,EAAE,IAAI,CAAC,wBAAwB;aACtC,CAAC,CAAC;YAEH,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;YACvC,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,8BAA8B,EAAE,CAAC,CAAC;QAC1D,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;IAEO,MAAM,CAAC,YAA0C;QACvD,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,YAAY,EAAE,EAAE,GAAG,CAAC,CAAC;IACtE,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import type { useSyncExternalStore } from "react";
|
|
3
|
+
export type CallToolArgs = Record<string, unknown> | null;
|
|
4
|
+
export type CallToolResponse = {
|
|
5
|
+
content: CallToolResult["content"];
|
|
6
|
+
structuredContent: NonNullable<CallToolResult["structuredContent"]>;
|
|
7
|
+
isError: NonNullable<CallToolResult["isError"]>;
|
|
8
|
+
result: string;
|
|
9
|
+
_meta?: CallToolResult["_meta"];
|
|
10
|
+
};
|
|
11
|
+
export type DisplayMode = "pip" | "inline" | "fullscreen" | "modal";
|
|
12
|
+
export type DeviceType = "mobile" | "tablet" | "desktop" | "unknown";
|
|
13
|
+
export interface BridgeInterface {
|
|
14
|
+
theme: "light" | "dark";
|
|
15
|
+
locale: string;
|
|
16
|
+
displayMode: DisplayMode;
|
|
17
|
+
safeArea: {
|
|
18
|
+
insets: {
|
|
19
|
+
top: number;
|
|
20
|
+
right: number;
|
|
21
|
+
bottom: number;
|
|
22
|
+
left: number;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
maxHeight: number;
|
|
26
|
+
userAgent: {
|
|
27
|
+
device: {
|
|
28
|
+
type: DeviceType;
|
|
29
|
+
};
|
|
30
|
+
capabilities: {
|
|
31
|
+
hover: boolean;
|
|
32
|
+
touch: boolean;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
toolInput: Record<string, unknown> | null;
|
|
36
|
+
toolOutput: Record<string, unknown> | null;
|
|
37
|
+
toolResponseMetadata: Record<string, unknown> | null;
|
|
38
|
+
}
|
|
39
|
+
export type Subscribe = Parameters<typeof useSyncExternalStore>[0];
|
|
40
|
+
export interface Bridge<Context> {
|
|
41
|
+
subscribe(key: keyof Context): Subscribe;
|
|
42
|
+
subscribe(keys: readonly (keyof Context)[]): Subscribe;
|
|
43
|
+
getSnapshot<K extends keyof Context>(key: K): Context[K] | undefined;
|
|
44
|
+
}
|
|
45
|
+
export type ExternalStore<K extends keyof BridgeInterface> = {
|
|
46
|
+
subscribe: Subscribe;
|
|
47
|
+
getSnapshot: () => BridgeInterface[K];
|
|
48
|
+
};
|
|
49
|
+
export interface Adaptor {
|
|
50
|
+
getExternalStore<K extends keyof BridgeInterface>(key: K): ExternalStore<K>;
|
|
51
|
+
callTool<ToolArgs extends CallToolArgs = null, ToolResponse extends CallToolResponse = CallToolResponse>(name: string, args: ToolArgs): Promise<ToolResponse>;
|
|
52
|
+
requestDisplayMode(mode: DisplayMode): Promise<{
|
|
53
|
+
mode: DisplayMode;
|
|
54
|
+
}>;
|
|
55
|
+
sendFollowUpMessage(prompt: string): Promise<void>;
|
|
56
|
+
openExternal(href: string): void;
|
|
57
|
+
}
|