skybridge 0.0.0-dev.87acdcf → 0.0.0-dev.896f829
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 -674
- package/README.md +73 -148
- package/dist/src/server/devtoolsStaticServer.d.ts +15 -0
- package/dist/src/server/devtoolsStaticServer.js +38 -0
- package/dist/src/server/devtoolsStaticServer.js.map +1 -0
- package/dist/src/server/index.d.ts +3 -0
- package/dist/src/server/index.js +1 -0
- package/dist/src/server/index.js.map +1 -1
- package/dist/src/server/inferUtilityTypes.d.ts +64 -0
- package/dist/src/server/inferUtilityTypes.js +2 -0
- package/dist/src/server/inferUtilityTypes.js.map +1 -0
- package/dist/src/server/server.d.ts +66 -9
- package/dist/src/server/server.js +9 -4
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/server/templateHelper.js +5 -4
- package/dist/src/server/templateHelper.js.map +1 -1
- package/dist/src/server/templates/development.hbs +1 -1
- package/dist/src/server/widgetsDevServer.js +3 -5
- package/dist/src/server/widgetsDevServer.js.map +1 -1
- package/dist/src/test/utils.d.ts +108 -1
- package/dist/src/test/utils.js +202 -3
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/test/widget.test.js +26 -21
- package/dist/src/test/widget.test.js.map +1 -1
- package/dist/src/web/create-store.d.ts +1 -1
- package/dist/src/web/create-store.js +3 -81
- package/dist/src/web/create-store.js.map +1 -1
- package/dist/src/web/create-store.test.d.ts +1 -0
- package/dist/src/web/create-store.test.js +70 -0
- package/dist/src/web/create-store.test.js.map +1 -0
- package/dist/src/web/data-llm.d.ts +14 -0
- package/dist/src/web/{llm-describe.js → data-llm.js} +16 -10
- package/dist/src/web/data-llm.js.map +1 -0
- package/dist/src/web/data-llm.test.d.ts +1 -0
- package/dist/src/web/data-llm.test.js +76 -0
- package/dist/src/web/data-llm.test.js.map +1 -0
- package/dist/src/web/generate-helpers.d.ts +115 -0
- package/dist/src/web/generate-helpers.js +111 -0
- package/dist/src/web/generate-helpers.js.map +1 -0
- package/dist/src/web/generate-helpers.test-d.d.ts +1 -0
- package/dist/src/web/generate-helpers.test-d.js +206 -0
- package/dist/src/web/generate-helpers.test-d.js.map +1 -0
- package/dist/src/web/generate-helpers.test.d.ts +1 -0
- package/dist/src/web/generate-helpers.test.js +17 -0
- package/dist/src/web/generate-helpers.test.js.map +1 -0
- package/dist/src/web/helpers/state.d.ts +7 -0
- package/dist/src/web/helpers/state.js +40 -0
- package/dist/src/web/helpers/state.js.map +1 -0
- package/dist/src/web/helpers/state.test.d.ts +1 -0
- package/dist/src/web/helpers/state.test.js +53 -0
- package/dist/src/web/helpers/state.test.js.map +1 -0
- package/dist/src/web/hooks/index.d.ts +2 -3
- package/dist/src/web/hooks/index.js +2 -3
- package/dist/src/web/hooks/index.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.d.ts +58 -20
- package/dist/src/web/hooks/use-call-tool.js +7 -15
- package/dist/src/web/hooks/use-call-tool.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.test-d.d.ts +1 -0
- package/dist/src/web/hooks/use-call-tool.test-d.js +104 -0
- package/dist/src/web/hooks/use-call-tool.test-d.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.test.js +9 -15
- package/dist/src/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/src/web/hooks/use-display-mode.js.map +1 -1
- package/dist/src/web/hooks/use-display-mode.test.js +2 -2
- package/dist/src/web/hooks/use-display-mode.test.js.map +1 -1
- package/dist/src/web/hooks/use-files.d.ts +10 -0
- package/dist/src/web/hooks/use-files.js +7 -0
- package/dist/src/web/hooks/use-files.js.map +1 -0
- package/dist/src/web/hooks/use-files.test.d.ts +1 -0
- package/dist/src/web/hooks/use-files.test.js +29 -0
- package/dist/src/web/hooks/use-files.test.js.map +1 -0
- package/dist/src/web/hooks/use-locale.js.map +1 -1
- package/dist/src/web/hooks/use-locale.test.js +2 -2
- package/dist/src/web/hooks/use-locale.test.js.map +1 -1
- package/dist/src/web/hooks/use-open-external.test.js +2 -2
- package/dist/src/web/hooks/use-open-external.test.js.map +1 -1
- package/dist/src/web/hooks/use-openai-global.d.ts +2 -2
- package/dist/src/web/hooks/use-openai-global.js +7 -5
- package/dist/src/web/hooks/use-openai-global.js.map +1 -1
- package/dist/src/web/hooks/use-request-modal.d.ts +3 -2
- package/dist/src/web/hooks/use-request-modal.js.map +1 -1
- package/dist/src/web/hooks/use-request-modal.test.js +2 -2
- package/dist/src/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/src/web/hooks/use-theme.js.map +1 -1
- package/dist/src/web/hooks/use-theme.test.js +2 -2
- package/dist/src/web/hooks/use-theme.test.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.d.ts +4 -4
- package/dist/src/web/hooks/use-tool-info.js +4 -6
- package/dist/src/web/hooks/use-tool-info.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test-d.js +0 -1
- package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test.js +2 -2
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/src/web/hooks/use-user-agent.js.map +1 -1
- package/dist/src/web/hooks/use-user-agent.test.js +2 -2
- package/dist/src/web/hooks/use-user-agent.test.js.map +1 -1
- package/dist/src/web/hooks/use-widget-state.js +3 -25
- package/dist/src/web/hooks/use-widget-state.js.map +1 -1
- package/dist/src/web/hooks/use-widget-state.test.js +2 -2
- package/dist/src/web/hooks/use-widget-state.test.js.map +1 -1
- package/dist/src/web/index.d.ts +5 -4
- package/dist/src/web/index.js +5 -4
- package/dist/src/web/index.js.map +1 -1
- package/dist/src/web/mount-widget.js +5 -0
- package/dist/src/web/mount-widget.js.map +1 -1
- package/dist/src/web/plugin/data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/data-llm.test.js +81 -0
- package/dist/src/web/plugin/data-llm.test.js.map +1 -0
- package/dist/src/web/{plugin.js → plugin/plugin.js} +5 -0
- package/dist/src/web/plugin/plugin.js.map +1 -0
- package/dist/src/web/plugin/transform-data-llm.d.ts +12 -0
- package/dist/src/web/plugin/transform-data-llm.js +93 -0
- package/dist/src/web/plugin/transform-data-llm.js.map +1 -0
- package/dist/src/web/plugin/transform-data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/transform-data-llm.test.js +81 -0
- package/dist/src/web/plugin/transform-data-llm.test.js.map +1 -0
- package/dist/src/web/proxy.d.ts +1 -0
- package/dist/src/web/proxy.js +48 -0
- package/dist/src/web/proxy.js.map +1 -0
- package/dist/src/web/types.d.ts +38 -22
- package/dist/src/web/types.js.map +1 -1
- package/package.json +10 -5
- package/dist/src/server/describe-plugin.d.ts +0 -2
- package/dist/src/server/describe-plugin.js +0 -101
- package/dist/src/server/describe-plugin.js.map +0 -1
- package/dist/src/web/hooks/use-tool-output.d.ts +0 -4
- package/dist/src/web/hooks/use-tool-output.js +0 -9
- package/dist/src/web/hooks/use-tool-output.js.map +0 -1
- package/dist/src/web/hooks/use-tool-response-metadata.d.ts +0 -4
- package/dist/src/web/hooks/use-tool-response-metadata.js +0 -8
- package/dist/src/web/hooks/use-tool-response-metadata.js.map +0 -1
- package/dist/src/web/llm-describe.d.ts +0 -13
- package/dist/src/web/llm-describe.js.map +0 -1
- package/dist/src/web/plugin.js.map +0 -1
- /package/dist/src/web/{plugin.d.ts → plugin/plugin.d.ts} +0 -0
package/dist/src/test/utils.d.ts
CHANGED
|
@@ -5,9 +5,116 @@ import { McpServer } from "../server/server.js";
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function createMockMcpServer(): {
|
|
7
7
|
server: McpServer;
|
|
8
|
-
|
|
8
|
+
mockRegisterResource: MockInstance<McpServer["registerResource"]>;
|
|
9
9
|
mockRegisterTool: MockInstance<McpServer["registerTool"]>;
|
|
10
10
|
};
|
|
11
|
+
export declare function createTestServer(): McpServer<Record<never, import("../server/server.js").ToolDef<unknown, unknown, unknown>> & {
|
|
12
|
+
"search-voyage": import("../server/server.js").ToolDef<{
|
|
13
|
+
destination: string;
|
|
14
|
+
departureDate?: string | undefined;
|
|
15
|
+
maxPrice?: number | undefined;
|
|
16
|
+
}, {
|
|
17
|
+
results: {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
price: number;
|
|
21
|
+
}[];
|
|
22
|
+
totalCount: number;
|
|
23
|
+
}, unknown>;
|
|
24
|
+
} & {
|
|
25
|
+
"get-trip-details": import("../server/server.js").ToolDef<{
|
|
26
|
+
tripId: string;
|
|
27
|
+
}, {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
images: string[];
|
|
31
|
+
}, unknown>;
|
|
32
|
+
} & {
|
|
33
|
+
"no-input-widget": import("../server/server.js").ToolDef<{}, {}, unknown>;
|
|
34
|
+
} & {
|
|
35
|
+
"inferred-output-widget": import("../server/server.js").ToolDef<{
|
|
36
|
+
query: string;
|
|
37
|
+
}, {
|
|
38
|
+
inferredResults: {
|
|
39
|
+
id: string;
|
|
40
|
+
score: number;
|
|
41
|
+
}[];
|
|
42
|
+
inferredCount: number;
|
|
43
|
+
}, unknown>;
|
|
44
|
+
} & {
|
|
45
|
+
"calculate-price": import("../server/server.js").ToolDef<{
|
|
46
|
+
tripId: string;
|
|
47
|
+
passengers: number;
|
|
48
|
+
}, {
|
|
49
|
+
totalPrice: number;
|
|
50
|
+
currency: string;
|
|
51
|
+
}, unknown>;
|
|
52
|
+
} & {
|
|
53
|
+
"inferred-tool": import("../server/server.js").ToolDef<{
|
|
54
|
+
itemId: string;
|
|
55
|
+
}, {
|
|
56
|
+
itemDetails: {
|
|
57
|
+
name: string;
|
|
58
|
+
available: boolean;
|
|
59
|
+
};
|
|
60
|
+
fetchedAt: string;
|
|
61
|
+
}, unknown>;
|
|
62
|
+
} & {
|
|
63
|
+
"widget-with-metadata": import("../server/server.js").ToolDef<{
|
|
64
|
+
resourceId: string;
|
|
65
|
+
}, {
|
|
66
|
+
data: {
|
|
67
|
+
id: string;
|
|
68
|
+
loaded: boolean;
|
|
69
|
+
};
|
|
70
|
+
}, {
|
|
71
|
+
requestId: string;
|
|
72
|
+
timestamp: number;
|
|
73
|
+
cached: boolean;
|
|
74
|
+
}>;
|
|
75
|
+
} & {
|
|
76
|
+
"tool-with-metadata": import("../server/server.js").ToolDef<{
|
|
77
|
+
query: string;
|
|
78
|
+
}, {
|
|
79
|
+
results: string[];
|
|
80
|
+
}, {
|
|
81
|
+
executionTime: number;
|
|
82
|
+
source: string;
|
|
83
|
+
}>;
|
|
84
|
+
} & {
|
|
85
|
+
"widget-with-mixed-returns": import("../server/server.js").ToolDef<{
|
|
86
|
+
shouldSucceed: boolean;
|
|
87
|
+
}, {
|
|
88
|
+
error: string;
|
|
89
|
+
data?: undefined;
|
|
90
|
+
} | {
|
|
91
|
+
data: string;
|
|
92
|
+
error?: undefined;
|
|
93
|
+
}, {
|
|
94
|
+
processedAt: number;
|
|
95
|
+
region: string;
|
|
96
|
+
}>;
|
|
97
|
+
}>;
|
|
98
|
+
export declare function createMinimalTestServer(): McpServer<Record<never, import("../server/server.js").ToolDef<unknown, unknown, unknown>> & {
|
|
99
|
+
"search-voyage": import("../server/server.js").ToolDef<{
|
|
100
|
+
destination: string;
|
|
101
|
+
}, {
|
|
102
|
+
results: {
|
|
103
|
+
id: string;
|
|
104
|
+
}[];
|
|
105
|
+
}, unknown>;
|
|
106
|
+
}>;
|
|
107
|
+
export declare function createInterfaceTestServer(): McpServer<Record<never, import("../server/server.js").ToolDef<unknown, unknown, unknown>> & {
|
|
108
|
+
"interface-widget": import("../server/server.js").ToolDef<{
|
|
109
|
+
id: string;
|
|
110
|
+
}, {
|
|
111
|
+
itemName: string;
|
|
112
|
+
quantity: number;
|
|
113
|
+
}, {
|
|
114
|
+
processedBy: string;
|
|
115
|
+
version: number;
|
|
116
|
+
}>;
|
|
117
|
+
}>;
|
|
11
118
|
/**
|
|
12
119
|
* Mock extra parameter for resource callback
|
|
13
120
|
*/
|
package/dist/src/test/utils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { vi } from "vitest";
|
|
2
|
-
import
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import { McpServer } from "../server/server.js";
|
|
3
4
|
/**
|
|
4
5
|
* Creates a real McpServer instance for testing
|
|
5
6
|
*/
|
|
@@ -10,14 +11,212 @@ export function createMockMcpServer() {
|
|
|
10
11
|
version: "0.0.1",
|
|
11
12
|
}, { capabilities: {} });
|
|
12
13
|
// Mock the underlying methods to track calls
|
|
13
|
-
const
|
|
14
|
+
const mockRegisterResource = vi.spyOn(server, "registerResource");
|
|
14
15
|
const mockRegisterTool = vi.spyOn(server, "registerTool");
|
|
15
16
|
return {
|
|
16
17
|
server,
|
|
17
|
-
|
|
18
|
+
mockRegisterResource,
|
|
18
19
|
mockRegisterTool,
|
|
19
20
|
};
|
|
20
21
|
}
|
|
22
|
+
export function createTestServer() {
|
|
23
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {})
|
|
24
|
+
.registerWidget("search-voyage", {}, {
|
|
25
|
+
description: "Search for voyages",
|
|
26
|
+
inputSchema: {
|
|
27
|
+
destination: z.string(),
|
|
28
|
+
departureDate: z.string().optional(),
|
|
29
|
+
maxPrice: z.number().optional(),
|
|
30
|
+
},
|
|
31
|
+
outputSchema: {
|
|
32
|
+
results: z.array(z.object({
|
|
33
|
+
id: z.string(),
|
|
34
|
+
name: z.string(),
|
|
35
|
+
price: z.number(),
|
|
36
|
+
})),
|
|
37
|
+
totalCount: z.number(),
|
|
38
|
+
},
|
|
39
|
+
}, async ({ destination }) => {
|
|
40
|
+
return {
|
|
41
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
42
|
+
structuredContent: {
|
|
43
|
+
results: [{ id: "1", name: "Trip", price: 1000 }],
|
|
44
|
+
totalCount: 1,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
})
|
|
48
|
+
.registerWidget("get-trip-details", {}, {
|
|
49
|
+
description: "Get trip details",
|
|
50
|
+
inputSchema: {
|
|
51
|
+
tripId: z.string(),
|
|
52
|
+
},
|
|
53
|
+
outputSchema: {
|
|
54
|
+
name: z.string(),
|
|
55
|
+
description: z.string(),
|
|
56
|
+
images: z.array(z.string()),
|
|
57
|
+
},
|
|
58
|
+
}, async ({ tripId }) => {
|
|
59
|
+
return {
|
|
60
|
+
content: [{ type: "text", text: `Details for ${tripId}` }],
|
|
61
|
+
structuredContent: {
|
|
62
|
+
name: "Trip",
|
|
63
|
+
description: "A great trip",
|
|
64
|
+
images: ["image1.jpg"],
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
})
|
|
68
|
+
.registerWidget("no-input-widget", {}, {
|
|
69
|
+
description: "Widget with no input",
|
|
70
|
+
inputSchema: {},
|
|
71
|
+
outputSchema: {},
|
|
72
|
+
}, async () => {
|
|
73
|
+
return {
|
|
74
|
+
content: [{ type: "text", text: "No input needed" }],
|
|
75
|
+
structuredContent: {},
|
|
76
|
+
};
|
|
77
|
+
})
|
|
78
|
+
.registerWidget("inferred-output-widget", {}, {
|
|
79
|
+
description: "Widget with output inferred from callback",
|
|
80
|
+
inputSchema: {
|
|
81
|
+
query: z.string(),
|
|
82
|
+
},
|
|
83
|
+
}, async ({ query }) => {
|
|
84
|
+
return {
|
|
85
|
+
content: [{ type: "text", text: `Query: ${query}` }],
|
|
86
|
+
structuredContent: {
|
|
87
|
+
inferredResults: [{ id: "inferred-1", score: 0.95 }],
|
|
88
|
+
inferredCount: 1,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
})
|
|
92
|
+
.registerTool("calculate-price", {
|
|
93
|
+
description: "Calculate trip price",
|
|
94
|
+
inputSchema: {
|
|
95
|
+
tripId: z.string(),
|
|
96
|
+
passengers: z.number(),
|
|
97
|
+
},
|
|
98
|
+
outputSchema: {
|
|
99
|
+
totalPrice: z.number(),
|
|
100
|
+
currency: z.string(),
|
|
101
|
+
},
|
|
102
|
+
}, async ({ tripId, passengers }) => {
|
|
103
|
+
return {
|
|
104
|
+
content: [{ type: "text", text: `Price for ${tripId}` }],
|
|
105
|
+
structuredContent: {
|
|
106
|
+
totalPrice: 1000 * passengers,
|
|
107
|
+
currency: "USD",
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
})
|
|
111
|
+
.registerTool("inferred-tool", {
|
|
112
|
+
description: "Tool with output inferred from callback",
|
|
113
|
+
inputSchema: {
|
|
114
|
+
itemId: z.string(),
|
|
115
|
+
},
|
|
116
|
+
}, async ({ itemId }) => {
|
|
117
|
+
return {
|
|
118
|
+
content: [{ type: "text", text: `Item: ${itemId}` }],
|
|
119
|
+
structuredContent: {
|
|
120
|
+
itemDetails: { name: "Inferred Item", available: true },
|
|
121
|
+
fetchedAt: "2024-01-01",
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
})
|
|
125
|
+
.registerWidget("widget-with-metadata", {}, {
|
|
126
|
+
description: "Widget that returns response metadata",
|
|
127
|
+
inputSchema: {
|
|
128
|
+
resourceId: z.string(),
|
|
129
|
+
},
|
|
130
|
+
}, async ({ resourceId }) => {
|
|
131
|
+
return {
|
|
132
|
+
content: [{ type: "text", text: `Resource: ${resourceId}` }],
|
|
133
|
+
structuredContent: {
|
|
134
|
+
data: { id: resourceId, loaded: true },
|
|
135
|
+
},
|
|
136
|
+
_meta: {
|
|
137
|
+
requestId: "req-123",
|
|
138
|
+
timestamp: 1704067200000,
|
|
139
|
+
cached: false,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
})
|
|
143
|
+
.registerTool("tool-with-metadata", {
|
|
144
|
+
description: "Tool that returns response metadata",
|
|
145
|
+
inputSchema: {
|
|
146
|
+
query: z.string(),
|
|
147
|
+
},
|
|
148
|
+
}, async ({ query }) => {
|
|
149
|
+
return {
|
|
150
|
+
content: [{ type: "text", text: `Query: ${query}` }],
|
|
151
|
+
structuredContent: {
|
|
152
|
+
results: [query],
|
|
153
|
+
},
|
|
154
|
+
_meta: {
|
|
155
|
+
executionTime: 150,
|
|
156
|
+
source: "cache",
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
})
|
|
160
|
+
.registerWidget("widget-with-mixed-returns", {}, {
|
|
161
|
+
description: "Widget with mixed return paths (some with _meta, some without)",
|
|
162
|
+
inputSchema: {
|
|
163
|
+
shouldSucceed: z.boolean(),
|
|
164
|
+
},
|
|
165
|
+
}, async ({ shouldSucceed }) => {
|
|
166
|
+
if (!shouldSucceed) {
|
|
167
|
+
// Error path - no _meta
|
|
168
|
+
return {
|
|
169
|
+
content: [{ type: "text", text: "Error occurred" }],
|
|
170
|
+
structuredContent: { error: "Something went wrong" },
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
// Success path - has _meta
|
|
174
|
+
return {
|
|
175
|
+
content: [{ type: "text", text: "Success" }],
|
|
176
|
+
structuredContent: { data: "result" },
|
|
177
|
+
_meta: {
|
|
178
|
+
processedAt: 1704067200000,
|
|
179
|
+
region: "eu-west-1",
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
export function createMinimalTestServer() {
|
|
185
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {}).registerWidget("search-voyage", {}, {
|
|
186
|
+
description: "Search for voyages",
|
|
187
|
+
inputSchema: {
|
|
188
|
+
destination: z.string(),
|
|
189
|
+
},
|
|
190
|
+
outputSchema: {
|
|
191
|
+
results: z.array(z.object({ id: z.string() })),
|
|
192
|
+
},
|
|
193
|
+
}, async ({ destination }) => {
|
|
194
|
+
return {
|
|
195
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
196
|
+
structuredContent: { results: [{ id: "1" }] },
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
export function createInterfaceTestServer() {
|
|
201
|
+
return new McpServer({ name: "interface-test-app", version: "1.0.0" }, {}).registerWidget("interface-widget", {}, {
|
|
202
|
+
description: "Widget with interface-typed output",
|
|
203
|
+
inputSchema: {
|
|
204
|
+
id: z.string(),
|
|
205
|
+
},
|
|
206
|
+
}, async ({ id }) => {
|
|
207
|
+
return {
|
|
208
|
+
content: [{ type: "text", text: `Item ${id}` }],
|
|
209
|
+
structuredContent: {
|
|
210
|
+
itemName: "Test Item",
|
|
211
|
+
quantity: 42,
|
|
212
|
+
},
|
|
213
|
+
_meta: {
|
|
214
|
+
processedBy: "test",
|
|
215
|
+
version: 1,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
}
|
|
21
220
|
/**
|
|
22
221
|
* Mock extra parameter for resource callback
|
|
23
222
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/test/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/test/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC/C,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD;;GAEG;AACH,MAAM,UAAU,mBAAmB;IAKjC,mCAAmC;IACnC,MAAM,MAAM,GAAG,IAAI,SAAS,CAC1B;QACE,IAAI,EAAE,kBAAkB;QACxB,OAAO,EAAE,OAAO;KACjB,EACD,EAAE,YAAY,EAAE,EAAE,EAAE,CACrB,CAAC;IAEF,6CAA6C;IAC7C,MAAM,oBAAoB,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAClE,MAAM,gBAAgB,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IAE1D,OAAO;QACL,MAAM;QACN,oBAAoB;QACpB,gBAAgB;KACjB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,gBAAgB;IAC9B,OAAO,IAAI,SAAS,CAAC,EAAE,IAAI,EAAE,UA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ACL,WAAW,EAAE,MAAM;gBACnB,OAAO,EAAE,CAAC;aACX;SACF,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY;IAC1C,OAAO;QACL,WAAW,EAAE;YACX,OAAO,EAAE,EAAE,IAAI,EAAE;SAClB;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,GAA2B;IACpD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;AAC9B,CAAC"}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
1
|
import { afterEach, beforeEach, describe, expect, it, vi, } from "vitest";
|
|
3
|
-
import { McpServer } from "../server/server.js";
|
|
4
2
|
import { createMockExtra, createMockMcpServer, resetTestEnv, setTestEnv, } from "./utils.js";
|
|
5
3
|
const mockManifest = {
|
|
6
4
|
"src/widgets/my-widget.tsx": { file: "my-widget.js" },
|
|
@@ -8,12 +6,16 @@ const mockManifest = {
|
|
|
8
6
|
};
|
|
9
7
|
vi.mock("node:fs", async () => {
|
|
10
8
|
const actual = await vi.importActual("node:fs");
|
|
11
|
-
const
|
|
9
|
+
const readFileSyncImpl = (path, ...args) => {
|
|
12
10
|
if (typeof path === "string" && path.includes("manifest.json")) {
|
|
13
11
|
return JSON.stringify(mockManifest);
|
|
14
12
|
}
|
|
13
|
+
// Type assertion needed because readFileSync has overloads with different parameter types
|
|
14
|
+
// Using @ts-expect-error because the overloads are complex and we're forwarding args
|
|
15
|
+
// @ts-expect-error - readFileSync overloads require complex type handling
|
|
15
16
|
return actual.readFileSync(path, ...args);
|
|
16
|
-
}
|
|
17
|
+
};
|
|
18
|
+
const readFileSync = vi.fn(readFileSyncImpl);
|
|
17
19
|
return {
|
|
18
20
|
readFileSync,
|
|
19
21
|
default: {
|
|
@@ -21,13 +23,13 @@ vi.mock("node:fs", async () => {
|
|
|
21
23
|
},
|
|
22
24
|
};
|
|
23
25
|
});
|
|
24
|
-
describe("McpServer.
|
|
26
|
+
describe("McpServer.registerWidget", () => {
|
|
25
27
|
let server;
|
|
26
|
-
let
|
|
28
|
+
let mockRegisterResource;
|
|
27
29
|
let mockRegisterTool;
|
|
28
|
-
let readFileSyncSpy = null;
|
|
29
30
|
beforeEach(() => {
|
|
30
|
-
({ server,
|
|
31
|
+
({ server, mockRegisterResource, mockRegisterTool } =
|
|
32
|
+
createMockMcpServer());
|
|
31
33
|
});
|
|
32
34
|
afterEach(() => {
|
|
33
35
|
vi.clearAllMocks();
|
|
@@ -36,15 +38,17 @@ describe("McpServer.widget", () => {
|
|
|
36
38
|
it("should generate correct HTML for development mode", async () => {
|
|
37
39
|
setTestEnv({ NODE_ENV: "development" });
|
|
38
40
|
const mockToolCallback = vi.fn();
|
|
39
|
-
const
|
|
41
|
+
const mockRegisterResourceConfig = { description: "Test widget" };
|
|
40
42
|
const mockToolConfig = { description: "Test tool" };
|
|
41
|
-
server.
|
|
43
|
+
server.registerWidget("my-widget", mockRegisterResourceConfig, mockToolConfig, mockToolCallback);
|
|
42
44
|
// Get the resource callback function
|
|
43
|
-
const resourceCallback =
|
|
45
|
+
const resourceCallback = mockRegisterResource.mock
|
|
46
|
+
.calls[0]?.[3];
|
|
44
47
|
expect(resourceCallback).toBeDefined();
|
|
45
48
|
const serverUrl = "http://localhost:3000";
|
|
46
49
|
const mockExtra = createMockExtra("__not_used__");
|
|
47
50
|
const result = await resourceCallback(new URL("ui://widgets/my-widget.html"), mockExtra);
|
|
51
|
+
expect(mockRegisterTool).toHaveBeenCalled();
|
|
48
52
|
expect(result).toEqual({
|
|
49
53
|
contents: [
|
|
50
54
|
{
|
|
@@ -55,18 +59,19 @@ describe("McpServer.widget", () => {
|
|
|
55
59
|
],
|
|
56
60
|
});
|
|
57
61
|
// Check development-specific content
|
|
58
|
-
expect(result.contents[0]?.text).toContain(serverUrl
|
|
59
|
-
expect(result.contents[0]?.text).toContain(serverUrl
|
|
60
|
-
expect(result.contents[0]?.text).toContain(serverUrl
|
|
62
|
+
expect(result.contents[0]?.text).toContain(`${serverUrl}/@react-refresh`);
|
|
63
|
+
expect(result.contents[0]?.text).toContain(`${serverUrl}/@vite/client`);
|
|
64
|
+
expect(result.contents[0]?.text).toContain(`${serverUrl}/src/widgets/my-widget.tsx`);
|
|
61
65
|
});
|
|
62
66
|
it("should generate correct HTML for production mode", async () => {
|
|
63
67
|
setTestEnv({ NODE_ENV: "production" });
|
|
64
68
|
const mockToolCallback = vi.fn();
|
|
65
|
-
const
|
|
69
|
+
const mockRegisterResourceConfig = { description: "Test widget" };
|
|
66
70
|
const mockToolConfig = { description: "Test tool" };
|
|
67
|
-
server.
|
|
71
|
+
server.registerWidget("my-widget", mockRegisterResourceConfig, mockToolConfig, mockToolCallback);
|
|
68
72
|
// Get the resource callback function
|
|
69
|
-
const resourceCallback =
|
|
73
|
+
const resourceCallback = mockRegisterResource.mock
|
|
74
|
+
.calls[0]?.[3];
|
|
70
75
|
expect(resourceCallback).toBeDefined();
|
|
71
76
|
const serverUrl = "https://myapp.com";
|
|
72
77
|
const mockExtra = createMockExtra(serverUrl);
|
|
@@ -81,10 +86,10 @@ describe("McpServer.widget", () => {
|
|
|
81
86
|
],
|
|
82
87
|
});
|
|
83
88
|
// Check production-specific content
|
|
84
|
-
expect(result.contents[0]?.text).not.toContain(serverUrl
|
|
85
|
-
expect(result.contents[0]?.text).not.toContain(serverUrl
|
|
86
|
-
expect(result.contents[0]?.text).toContain(serverUrl
|
|
87
|
-
expect(result.contents[0]?.text).toContain(serverUrl
|
|
89
|
+
expect(result.contents[0]?.text).not.toContain(`${serverUrl}@react-refresh`);
|
|
90
|
+
expect(result.contents[0]?.text).not.toContain(`${serverUrl}@vite/client`);
|
|
91
|
+
expect(result.contents[0]?.text).toContain(`${serverUrl}/assets/my-widget.js`);
|
|
92
|
+
expect(result.contents[0]?.text).toContain(`${serverUrl}/assets/style.css`);
|
|
88
93
|
});
|
|
89
94
|
});
|
|
90
95
|
//# sourceMappingURL=widget.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"widget.test.js","sourceRoot":"","sources":["../../../src/test/widget.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"widget.test.js","sourceRoot":"","sources":["../../../src/test/widget.test.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,MAAM,EACN,EAAE,EAEF,EAAE,GACH,MAAM,QAAQ,CAAC;AAEhB,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,YAAY,EACZ,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,MAAM,YAAY,GAAG;IACnB,2BAA2B,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE;IACrD,WAAW,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;CACnC,CAAC;AAEF,EAAE,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE;IAC5B,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,YAAY,CAA2B,SAAS,CAAC,CAAC;IAC1E,MAAM,gBAAgB,GAAG,CACvB,IAA+C,EAC/C,GAAG,IAAe,EACsB,EAAE;QAC1C,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;YAC/D,OAAO,IAAI,CAAC,SAAS,CAAC,YAAY,CAEjC,CAAC;QACJ,CAAC;QACD,0FAA0F;QAC1F,qFAAqF;QACrF,0EAA0E;QAC1E,OAAO,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC,CAAC;IACF,MAAM,YAAY,GAAG,EAAE,CAAC,EAAE,CAAC,gBAAgB,CAA+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AAC,WAAW,EAAE,CAAC;QAEvC,MAAM,SAAS,GAAG,mBAAmB,CAAC;QACtC,MAAM,SAAS,GAAG,eAAe,CAC/B,SAAS,CAC2D,CAAC;QACvE,MAAM,MAAM,GAAG,MAAM,gBAAgB,CACnC,IAAI,GAAG,CAAC,6BAA6B,CAAC,EACtC,SAAS,CACV,CAAC;QAEF,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC;YACrB,QAAQ,EAAE;gBACR;oBACE,GAAG,EAAE,6BAA6B;oBAClC,QAAQ,EAAE,qBAAqB;oBAC/B,IAAI,EAAE,MAAM,CAAC,gBAAgB,CAAC,uBAAuB,CAAC;iBACvD;aACF;SACF,CAAC,CAAC;QAEH,oCAAoC;QACpC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,SAAS,CAC5C,GAAG,SAAS,gBAAgB,CAC7B,CAAC;QACF,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,SAAS,cAAc,CAAC,CAAC;QAC3E,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,SAAS,CACxC,GAAG,SAAS,sBAAsB,CACnC,CAAC;QACF,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,SAAS,CAAC,GAAG,SAAS,mBAAmB,CAAC,CAAC;IAC9E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type StateCreator } from "zustand";
|
|
2
2
|
import type { UnknownObject } from "./types.js";
|
|
3
|
-
export declare function createStore<
|
|
3
|
+
export declare function createStore<State extends UnknownObject>(storeCreator: StateCreator<State, [], [], State>, defaultState?: State | (() => State)): import("zustand").UseBoundStore<import("zustand").StoreApi<State>>;
|
|
@@ -1,103 +1,25 @@
|
|
|
1
1
|
import { create } from "zustand";
|
|
2
|
-
import {
|
|
3
|
-
const WIDGET_CONTEXT_KEY = "__widget_context";
|
|
4
|
-
function filterWidgetContext(state) {
|
|
5
|
-
if (state === null) {
|
|
6
|
-
return null;
|
|
7
|
-
}
|
|
8
|
-
const { [WIDGET_CONTEXT_KEY]: _, ...filteredState } = state;
|
|
9
|
-
return filteredState;
|
|
10
|
-
}
|
|
11
|
-
function serializeState(value) {
|
|
12
|
-
// Skip functions (methods)
|
|
13
|
-
if (typeof value === "function") {
|
|
14
|
-
return undefined;
|
|
15
|
-
}
|
|
16
|
-
// Handle arrays
|
|
17
|
-
if (Array.isArray(value)) {
|
|
18
|
-
return value
|
|
19
|
-
.map((item) => serializeState(item))
|
|
20
|
-
.filter((item) => item !== undefined);
|
|
21
|
-
}
|
|
22
|
-
// Handle objects
|
|
23
|
-
if (value !== null && typeof value === "object") {
|
|
24
|
-
const serialized = {};
|
|
25
|
-
for (const [key, val] of Object.entries(value)) {
|
|
26
|
-
const serializedValue = serializeState(val);
|
|
27
|
-
if (serializedValue !== undefined) {
|
|
28
|
-
serialized[key] = serializedValue;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return serialized;
|
|
32
|
-
}
|
|
33
|
-
// Primitive values
|
|
34
|
-
return value;
|
|
35
|
-
}
|
|
36
|
-
function preserveWidgetContext(newState) {
|
|
37
|
-
if (newState === null) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
const currentWindowState = window.openai?.widgetState;
|
|
41
|
-
if (currentWindowState !== null &&
|
|
42
|
-
currentWindowState !== undefined &&
|
|
43
|
-
WIDGET_CONTEXT_KEY in currentWindowState) {
|
|
44
|
-
return {
|
|
45
|
-
...newState,
|
|
46
|
-
[WIDGET_CONTEXT_KEY]: currentWindowState[WIDGET_CONTEXT_KEY],
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
return newState;
|
|
50
|
-
}
|
|
51
|
-
function getInitialState(defaultState) {
|
|
52
|
-
const widgetStateFromWindow = window.openai?.widgetState;
|
|
53
|
-
if (widgetStateFromWindow !== null && widgetStateFromWindow !== undefined) {
|
|
54
|
-
return filterWidgetContext(widgetStateFromWindow);
|
|
55
|
-
}
|
|
56
|
-
return typeof defaultState === "function"
|
|
57
|
-
? defaultState()
|
|
58
|
-
: defaultState ?? null;
|
|
59
|
-
}
|
|
2
|
+
import { getInitialState, injectWidgetContext, serializeState, } from "./helpers/state.js";
|
|
60
3
|
export function createStore(storeCreator, defaultState) {
|
|
61
4
|
const initialState = getInitialState(defaultState);
|
|
62
5
|
const store = create()((...args) => {
|
|
63
6
|
const baseStore = storeCreator(...args);
|
|
64
|
-
// Merge with initial state from window.openai.widgetState or defaultState
|
|
65
7
|
if (initialState !== null) {
|
|
66
8
|
return { ...baseStore, ...initialState };
|
|
67
9
|
}
|
|
68
10
|
return baseStore;
|
|
69
11
|
});
|
|
70
|
-
// Subscribe to store changes and persist to window.openai.setWidgetState
|
|
71
|
-
let isInternalUpdate = false;
|
|
72
12
|
store.subscribe((state) => {
|
|
73
|
-
if (
|
|
74
|
-
// Only serialize the state data, not the methods
|
|
13
|
+
if (window.openai) {
|
|
75
14
|
const serializedState = serializeState(state);
|
|
76
15
|
if (serializedState !== null && serializedState !== undefined) {
|
|
77
|
-
const stateToPersist =
|
|
16
|
+
const stateToPersist = injectWidgetContext(serializedState);
|
|
78
17
|
if (stateToPersist !== null) {
|
|
79
18
|
window.openai.setWidgetState(stateToPersist);
|
|
80
19
|
}
|
|
81
20
|
}
|
|
82
21
|
}
|
|
83
22
|
});
|
|
84
|
-
// Listen to external widgetState changes and update the store
|
|
85
|
-
if (typeof window !== "undefined") {
|
|
86
|
-
const handleSetGlobals = (event) => {
|
|
87
|
-
const widgetState = event.detail.globals.widgetState;
|
|
88
|
-
if (widgetState !== undefined) {
|
|
89
|
-
const filteredState = filterWidgetContext(widgetState);
|
|
90
|
-
if (filteredState !== null) {
|
|
91
|
-
isInternalUpdate = true;
|
|
92
|
-
store.setState(filteredState);
|
|
93
|
-
isInternalUpdate = false;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobals, {
|
|
98
|
-
passive: true,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
23
|
return store;
|
|
102
24
|
}
|
|
103
25
|
//# sourceMappingURL=create-store.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-store.js","sourceRoot":"","sources":["../../../src/web/create-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAqB,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"create-store.js","sourceRoot":"","sources":["../../../src/web/create-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAqB,MAAM,SAAS,CAAC;AACpD,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,cAAc,GACf,MAAM,oBAAoB,CAAC;AAG5B,MAAM,UAAU,WAAW,CACzB,YAAgD,EAChD,YAAoC;IAEpC,MAAM,YAAY,GAAG,eAAe,CAAC,YAAY,CAAC,CAAC;IAEnD,MAAM,KAAK,GAAG,MAAM,EAAS,CAC3B,CAAC,GAAG,IAAoD,EAAE,EAAE;QAC1D,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,IAAI,CAAC,CAAC;QAExC,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YAC1B,OAAO,EAAE,GAAG,SAAS,EAAE,GAAG,YAAY,EAAE,CAAC;QAC3C,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC,CACF,CAAC;IAEF,KAAK,CAAC,SAAS,CAAC,CAAC,KAAY,EAAE,EAAE;QAC/B,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAClB,MAAM,eAAe,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;YAC9C,IAAI,eAAe,KAAK,IAAI,IAAI,eAAe,KAAK,SAAS,EAAE,CAAC;gBAC9D,MAAM,cAAc,GAAG,mBAAmB,CAAC,eAAwB,CAAC,CAAC;gBACrE,IAAI,cAAc,KAAK,IAAI,EAAE,CAAC;oBAC5B,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;gBAC/C,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|