skybridge 0.0.0-dev.f881c56 → 0.0.0-dev.faf07e2
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 +10 -397
- package/dist/src/server/server.d.ts +21 -12
- package/dist/src/server/server.js +2 -2
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/test/utils.js +6 -6
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/test/widget.test.js +3 -3
- package/dist/src/test/widget.test.js.map +1 -1
- package/dist/src/web/data-llm.d.ts +0 -1
- package/dist/src/web/data-llm.js +1 -2
- package/dist/src/web/data-llm.js.map +1 -1
- package/dist/src/web/hooks/use-openai-global.d.ts +2 -2
- package/dist/src/web/hooks/use-widget-state.js +24 -2
- package/dist/src/web/hooks/use-widget-state.js.map +1 -1
- package/dist/src/web/index.d.ts +0 -1
- package/dist/src/web/index.js +0 -1
- package/dist/src/web/index.js.map +1 -1
- package/dist/src/web/types.d.ts +4 -4
- package/package.json +6 -7
- package/dist/src/web/create-store.d.ts +0 -3
- package/dist/src/web/create-store.js +0 -25
- package/dist/src/web/create-store.js.map +0 -1
- package/dist/src/web/create-store.test.d.ts +0 -1
- package/dist/src/web/create-store.test.js +0 -70
- package/dist/src/web/create-store.test.js.map +0 -1
- package/dist/src/web/helpers/state.d.ts +0 -7
- package/dist/src/web/helpers/state.js +0 -40
- package/dist/src/web/helpers/state.js.map +0 -1
- package/dist/src/web/helpers/state.test.d.ts +0 -1
- package/dist/src/web/helpers/state.test.js +0 -53
- package/dist/src/web/helpers/state.test.js.map +0 -1
package/README.md
CHANGED
|
@@ -12,404 +12,17 @@
|
|
|
12
12
|
|
|
13
13
|
</div>
|
|
14
14
|
|
|
15
|
-
Skybridge comes with
|
|
15
|
+
Skybridge is a fullstack library. It comes with modular packages aiming at simplifying the development of ChatGPT apps:
|
|
16
16
|
|
|
17
|
-
-
|
|
18
|
-
-
|
|
17
|
+
- HMR server for development and build pipeline for production including end-to-end (tools-to-widget) type safety
|
|
18
|
+
- collection of React hooks for state management, async data fetching
|
|
19
|
+
- attribute for widget-to-context synchronization
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
### <a href="https://skybridge.tech">Read the docs →</b></a>
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
## Get Involved
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Concepts
|
|
31
|
-
|
|
32
|
-
### Widgets
|
|
33
|
-
|
|
34
|
-
> A widget is a UI component that turns structured tool results into a human-friendly UI. Those are built using React components. They are rendered inside an iframe inline with the conversation on ChatGPT.
|
|
35
|
-
|
|
36
|
-
Each widget in your app must have a unique name. The name is used to bridge the tool invocation result with the widget React component.
|
|
37
|
-
|
|
38
|
-
For example, in order to register a new widget named `pokemon` on your ChatGPT app. You should have the following file structure and file contents:
|
|
39
|
-
|
|
40
|
-
_Project structure_
|
|
41
|
-
|
|
42
|
-
```
|
|
43
|
-
server/
|
|
44
|
-
└── src/
|
|
45
|
-
└── index.ts // Register the widget with McpServer.widget()
|
|
46
|
-
web/
|
|
47
|
-
└── src/
|
|
48
|
-
└── widgets/
|
|
49
|
-
└── pokemon.tsx // Use the same widget name as the file name
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
_server/src/index.ts_
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
import { McpServer } from "skybridge/server";
|
|
56
|
-
|
|
57
|
-
const server = new McpServer();
|
|
58
|
-
|
|
59
|
-
server.widget(
|
|
60
|
-
"pokemon"
|
|
61
|
-
// Remaining arguments...
|
|
62
|
-
);
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
_web/src/widgets/pokemon.tsx_
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
import { mountWidget } from "skybridge/web";
|
|
69
|
-
|
|
70
|
-
const Pokemon: React.FunctionComponent = () => {
|
|
71
|
-
// Your React component code goes here...
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
mountWidget(<Pokemon />);
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Packages
|
|
78
|
-
|
|
79
|
-
### skybridge/server
|
|
80
|
-
|
|
81
|
-
The `skybridge/server` package is a drop-in replacement of the `@modelcontextprotocol/sdk` official `McpServer` class with extra features for widget development. If you're already using the `@modelcontextprotocol/sdk`, you can simply replace your `McpServer` import with `skybridge/server` and you're good to go.
|
|
82
|
-
|
|
83
|
-
### skybridge/web
|
|
84
|
-
|
|
85
|
-
The `skybridge/web` package is a react library with hooks and components to build widgets on the underlying _OpenAI iFrame skybridge_ runtime.
|
|
86
|
-
|
|
87
|
-
**Vite plugin**
|
|
88
|
-
|
|
89
|
-
The `skybridge/web` package comes with a Vite plugin that allows you to build your widgets as regular Vite apps.
|
|
90
|
-
|
|
91
|
-
```ts
|
|
92
|
-
import { defineConfig } from "vite";
|
|
93
|
-
import { skybridge } from "skybridge/web";
|
|
94
|
-
|
|
95
|
-
export default defineConfig({
|
|
96
|
-
plugins: [skybridge()],
|
|
97
|
-
});
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
**Typed Hooks**
|
|
101
|
-
|
|
102
|
-
Skybridge provides fully typed hooks that give you autocomplete for tool names and type inference for inputs/outputs - similar to tRPC. This is opt-in and requires exporting your server type.
|
|
103
|
-
|
|
104
|
-
> **Tip:** For the best TypeScript experience, use typed hooks throughout your application. They provide autocomplete, type safety, and better IDE support.
|
|
105
|
-
|
|
106
|
-
> **Important:** For `generateHelpers` to work correctly, your MCP server must be defined using method chaining (e.g., `server.widget(...).widget(...).registerTool(...)`). This ensures TypeScript can properly infer the tool registry type from the chained calls.
|
|
107
|
-
|
|
108
|
-
**Examples:**
|
|
109
|
-
|
|
110
|
-
✅ **Works** - Using method chaining:
|
|
111
|
-
|
|
112
|
-
```ts
|
|
113
|
-
import { McpServer } from "skybridge/server";
|
|
114
|
-
import { z } from "zod";
|
|
115
|
-
|
|
116
|
-
const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
117
|
-
.widget("search-voyage", {}, {
|
|
118
|
-
inputSchema: { destination: z.string() },
|
|
119
|
-
}, async ({ destination }) => {
|
|
120
|
-
return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
|
|
121
|
-
})
|
|
122
|
-
.registerTool("calculate-price", {
|
|
123
|
-
inputSchema: { tripId: z.string() },
|
|
124
|
-
}, async ({ tripId }) => {
|
|
125
|
-
return { content: [{ type: "text", text: `Price for ${tripId}` }] };
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
export type AppType = typeof server; // ✅ Type inference works correctly
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
❌ **Doesn't work** - Without method chaining:
|
|
132
|
-
|
|
133
|
-
```ts
|
|
134
|
-
import { McpServer } from "skybridge/server";
|
|
135
|
-
import { z } from "zod";
|
|
136
|
-
|
|
137
|
-
const server = new McpServer({ name: "my-app", version: "1.0" }, {});
|
|
138
|
-
|
|
139
|
-
server.widget("search-voyage", {}, {
|
|
140
|
-
inputSchema: { destination: z.string() },
|
|
141
|
-
}, async ({ destination }) => {
|
|
142
|
-
return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
server.registerTool("calculate-price", {
|
|
146
|
-
inputSchema: { tripId: z.string() },
|
|
147
|
-
}, async ({ tripId }) => {
|
|
148
|
-
return { content: [{ type: "text", text: `Price for ${tripId}` }] };
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
export type AppType = typeof server; // ❌ Type inference fails - tool registry is empty
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
_Server setup (server/src/index.ts)_
|
|
155
|
-
|
|
156
|
-
```ts
|
|
157
|
-
import { McpServer } from "skybridge/server";
|
|
158
|
-
import { z } from "zod";
|
|
159
|
-
|
|
160
|
-
const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
161
|
-
.widget("search-voyage", {}, {
|
|
162
|
-
description: "Search for trips",
|
|
163
|
-
inputSchema: {
|
|
164
|
-
destination: z.string(),
|
|
165
|
-
departureDate: z.string().optional(),
|
|
166
|
-
},
|
|
167
|
-
outputSchema: {
|
|
168
|
-
results: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
169
|
-
totalCount: z.number(),
|
|
170
|
-
},
|
|
171
|
-
}, async ({ destination }) => {
|
|
172
|
-
// Your tool logic here...
|
|
173
|
-
return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
|
|
174
|
-
})
|
|
175
|
-
.widget("get-details", {}, {
|
|
176
|
-
inputSchema: { tripId: z.string() },
|
|
177
|
-
}, async ({ tripId }) => {
|
|
178
|
-
return { content: [{ type: "text", text: `Details for ${tripId}` }] };
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
// Export the server type for the client
|
|
182
|
-
export type AppType = typeof server;
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
_One-time setup (web/src/skybridge.ts)_
|
|
186
|
-
|
|
187
|
-
Create typed hooks once and export them for use across your app. This file acts as a bridge between your server types and your widgets:
|
|
188
|
-
|
|
189
|
-
```ts
|
|
190
|
-
import type { AppType } from "../server"; // type-only import
|
|
191
|
-
import { generateHelpers } from "skybridge/web";
|
|
192
|
-
|
|
193
|
-
export const { useCallTool, useToolInfo } = generateHelpers<AppType>();
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
_Usage in widgets (web/src/widgets/search.tsx)_
|
|
197
|
-
|
|
198
|
-
```tsx
|
|
199
|
-
import { useCallTool, useToolInfo } from "../skybridge"; // import typed hooks
|
|
200
|
-
|
|
201
|
-
export function SearchWidget() {
|
|
202
|
-
const { callTool, data, isPending } = useCallTool("search-voyage");
|
|
203
|
-
// ^ autocomplete for tool names
|
|
204
|
-
const toolInfo = useToolInfo<"search-voyage">();
|
|
205
|
-
// ^ autocomplete for widget names
|
|
206
|
-
|
|
207
|
-
const handleSearch = () => {
|
|
208
|
-
callTool({ destination: "Spain" });
|
|
209
|
-
// ^ autocomplete for input fields
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
return (
|
|
213
|
-
<div>
|
|
214
|
-
<button onClick={handleSearch} disabled={isPending}>
|
|
215
|
-
Search
|
|
216
|
-
</button>
|
|
217
|
-
{toolInfo.isSuccess && (
|
|
218
|
-
<div>Found {toolInfo.output.structuredContent.totalCount} results</div>
|
|
219
|
-
// ^ typed output
|
|
220
|
-
)}
|
|
221
|
-
</div>
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
**Hooks**
|
|
227
|
-
|
|
228
|
-
The `skybridge/web` package comes with a set of hooks to help you build your widgets :
|
|
229
|
-
|
|
230
|
-
- `useOpenAiGlobal`: A generic hook to get any global data from the OpenAI iFrame skybridge runtime (in `window.openai`).
|
|
231
|
-
- `useToolOutput`: A hook to get the initial tool `structuredContent` returned when rendering the widget for the first time. The data inside this hook is not updated when the tool is called again.
|
|
232
|
-
- `useToolResponseMetadata`: A hook to get the initial tool `meta` returned when rendering the widget for the first time. The data inside this hook is not updated when the tool is called again.
|
|
233
|
-
- `useToolInfo`: A hook to get the tool input, output, and response metadata with type inference. Provides a discriminated union based on status (pending/success).
|
|
234
|
-
- `useCallTool`: A @tanstack/react-query inspired hook to send make additional tool calls inside a widget.
|
|
235
|
-
- `generateHelpers`: A factory that creates typed versions of `useCallTool` and `useToolInfo` with full type inference from your server type.
|
|
236
|
-
|
|
237
|
-
_useOpenAiGlobal_
|
|
238
|
-
|
|
239
|
-
```ts
|
|
240
|
-
import { useOpenAiGlobal } from "skybridge/web";
|
|
241
|
-
|
|
242
|
-
const theme = useOpenAiGlobal("theme");
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
_useToolOutput_
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
import { useToolOutput } from "skybridge/web";
|
|
249
|
-
|
|
250
|
-
const toolOutput = useToolOutput();
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
_useToolResponseMetadata_
|
|
254
|
-
|
|
255
|
-
```ts
|
|
256
|
-
import { useToolResponseMetadata } from "skybridge/web";
|
|
257
|
-
|
|
258
|
-
const toolResponseMetadata = useToolResponseMetadata();
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
_useToolInfo_
|
|
262
|
-
|
|
263
|
-
```ts
|
|
264
|
-
import { useToolInfo } from "skybridge/web";
|
|
265
|
-
|
|
266
|
-
const toolInfo = useToolInfo<{
|
|
267
|
-
input: { query: string };
|
|
268
|
-
output: { results: string[] };
|
|
269
|
-
responseMetadata: { id: number };
|
|
270
|
-
}>();
|
|
271
|
-
|
|
272
|
-
// toolInfo.input is typed based on the input type
|
|
273
|
-
// toolInfo.output.structuredContent is typed based on the output type (undefined when pending)
|
|
274
|
-
// toolInfo.status narrows correctly: "pending" | "success"
|
|
275
|
-
|
|
276
|
-
if (toolInfo.isPending) {
|
|
277
|
-
// toolInfo.output is undefined here (pending state)
|
|
278
|
-
console.log(toolInfo.input.query);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
if (toolInfo.isSuccess) {
|
|
282
|
-
// toolInfo.output.structuredContent is typed here
|
|
283
|
-
console.log(toolInfo.output.structuredContent.results);
|
|
284
|
-
}
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
_useToolInfo_ with typed hooks (recommended)
|
|
288
|
-
|
|
289
|
-
```tsx
|
|
290
|
-
import { useToolInfo } from "../skybridge"; // import typed hooks
|
|
291
|
-
|
|
292
|
-
export function SearchWidget() {
|
|
293
|
-
const toolInfo = useToolInfo<"search-voyage">();
|
|
294
|
-
// ^ autocomplete for widget names
|
|
295
|
-
// toolInfo.input is typed as { destination: string; departureDate?: string; ... }
|
|
296
|
-
// toolInfo.output.structuredContent is typed as { results: Array<...>; totalCount: number; }
|
|
297
|
-
|
|
298
|
-
if (toolInfo.isSuccess) {
|
|
299
|
-
return <div>Found {toolInfo.output.structuredContent.totalCount} results</div>;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return <div>Searching for {toolInfo.input.destination}...</div>;
|
|
303
|
-
}
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
_useCallTool_ in synchronous mode
|
|
307
|
-
|
|
308
|
-
```ts
|
|
309
|
-
import { useCallTool } from "skybridge/web";
|
|
310
|
-
|
|
311
|
-
export const TestTool: React.FunctionComponent = () => {
|
|
312
|
-
const { callTool, isPending } = useCallTool("myToolName");
|
|
313
|
-
|
|
314
|
-
return (
|
|
315
|
-
<div>
|
|
316
|
-
<button
|
|
317
|
-
disabled={isPending}
|
|
318
|
-
onClick={() => {
|
|
319
|
-
callTool({ input: "test input" }, {
|
|
320
|
-
onSuccess: (data) => {
|
|
321
|
-
alert("Tool returned: " + data);
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
>
|
|
325
|
-
Call Tool inside a widget
|
|
326
|
-
</button>
|
|
327
|
-
</div>
|
|
328
|
-
);
|
|
329
|
-
};
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
_useCallTool_ in asynchronous mode
|
|
333
|
-
|
|
334
|
-
```ts
|
|
335
|
-
import { useCallTool } from "skybridge/web";
|
|
336
|
-
|
|
337
|
-
export const TestTool: React.FunctionComponent = () => {
|
|
338
|
-
const { callToolAsync, isPending } = useCallTool("myToolName");
|
|
339
|
-
|
|
340
|
-
return (
|
|
341
|
-
<div>
|
|
342
|
-
<button
|
|
343
|
-
disabled={isPending}
|
|
344
|
-
onClick={async () => {
|
|
345
|
-
const data = await callToolAsync({ input: "test input" });
|
|
346
|
-
alert("Tool returned: " + data);
|
|
347
|
-
}}
|
|
348
|
-
>
|
|
349
|
-
Call Tool inside a widget
|
|
350
|
-
</button>
|
|
351
|
-
</div>
|
|
352
|
-
);
|
|
353
|
-
};
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
**State Management**
|
|
357
|
-
|
|
358
|
-
The `skybridge/web` package provides `createStore` for creating Zustand stores with automatic persistence to `window.openai.widgetState`. This is useful when you need more advanced state management than what `useWidgetState` provides.
|
|
359
|
-
|
|
360
|
-
_createStore_
|
|
361
|
-
|
|
362
|
-
```ts
|
|
363
|
-
import { createStore } from "skybridge/web";
|
|
364
|
-
|
|
365
|
-
type CounterState = {
|
|
366
|
-
count: number;
|
|
367
|
-
increment: () => void;
|
|
368
|
-
decrement: () => void;
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
const useCounterStore = createStore<CounterState>(
|
|
372
|
-
(set) => ({
|
|
373
|
-
count: 0,
|
|
374
|
-
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
375
|
-
decrement: () => set((state) => ({ count: state.count - 1 })),
|
|
376
|
-
}),
|
|
377
|
-
{ count: 0 } // Optional default state
|
|
378
|
-
);
|
|
379
|
-
|
|
380
|
-
// Use in your component
|
|
381
|
-
function CounterWidget() {
|
|
382
|
-
const { count, increment, decrement } = useCounterStore();
|
|
383
|
-
|
|
384
|
-
return (
|
|
385
|
-
<div>
|
|
386
|
-
<p>Count: {count}</p>
|
|
387
|
-
<button onClick={increment}>Increment</button>
|
|
388
|
-
<button onClick={decrement}>Decrement</button>
|
|
389
|
-
</div>
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
The `createStore` function:
|
|
395
|
-
|
|
396
|
-
- Automatically persists state to `window.openai.widgetState`
|
|
397
|
-
- Syncs with external state changes from the host
|
|
398
|
-
- Only serializes state data, not methods
|
|
399
|
-
|
|
400
|
-
## Migrate your existing MCP server to a ChatGPT app
|
|
401
|
-
|
|
402
|
-
If you're already using the `@modelcontextprotocol/sdk` to build a MCP server, you can migrate to a ChatGPT app by following these steps:
|
|
403
|
-
|
|
404
|
-
1. Replace your `McpServer` import from `@modelcontextprotocol/sdk` with the same import from `skybridge/server`
|
|
405
|
-
2. Create a new vite project in a folder named `web` and install the `skybridge` package
|
|
406
|
-
3. Replace the `vite.config.ts` file with the following:
|
|
407
|
-
|
|
408
|
-
```ts
|
|
409
|
-
import { defineConfig } from "vite";
|
|
410
|
-
import { skybridge } from "skybridge/web";
|
|
411
|
-
|
|
412
|
-
export default defineConfig({
|
|
413
|
-
plugins: [skybridge()],
|
|
414
|
-
});
|
|
415
|
-
```
|
|
25
|
+
We welcome issues and pull requests!</br>
|
|
26
|
+
Participate in [GitHub discussions](https://github.com/alpic-ai/skybridge/discussions)</br>
|
|
27
|
+
Chat with the community on [Discord](https://discord.com/invite/gNAazGueab)</br>
|
|
28
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { McpServer as McpServerBase
|
|
1
|
+
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { RegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
3
|
import type { Resource, ToolAnnotations, CallToolResult, ServerRequest, ServerNotification } from "@modelcontextprotocol/sdk/types.js";
|
|
3
4
|
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
4
|
-
import type {
|
|
5
|
+
import type { ZodRawShapeCompat, AnySchema, SchemaOutput } from "@modelcontextprotocol/sdk/server/zod-compat.js";
|
|
5
6
|
export type ToolDef<TInput = unknown, TOutput = unknown> = {
|
|
6
7
|
input: TInput;
|
|
7
8
|
output: TOutput;
|
|
8
9
|
};
|
|
9
10
|
type McpServerOriginalResourceConfig = Omit<Resource, "uri" | "name" | "mimeType">;
|
|
10
|
-
type McpServerOriginalToolConfig = Omit<Parameters<McpServerBase
|
|
11
|
+
type McpServerOriginalToolConfig = Omit<Parameters<typeof McpServerBase.prototype.registerTool<ZodRawShapeCompat, ZodRawShapeCompat>>[1], "inputSchema" | "outputSchema">;
|
|
11
12
|
type ExtractStructuredContent<T> = T extends {
|
|
12
13
|
structuredContent: infer SC;
|
|
13
14
|
} ? SC : never;
|
|
@@ -23,26 +24,34 @@ type ExtractStructuredContent<T> = T extends {
|
|
|
23
24
|
export interface McpServerTypes<TTools extends Record<string, ToolDef> = {}> {
|
|
24
25
|
readonly tools: TTools;
|
|
25
26
|
}
|
|
26
|
-
type
|
|
27
|
-
[K in
|
|
27
|
+
type Simplify<T> = {
|
|
28
|
+
[K in keyof T]: T[K];
|
|
29
|
+
};
|
|
30
|
+
type ShapeOutput<Shape extends ZodRawShapeCompat> = Simplify<{
|
|
31
|
+
[K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? never : K]: SchemaOutput<Shape[K]>;
|
|
32
|
+
} & {
|
|
33
|
+
[K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? K : never]?: SchemaOutput<Shape[K]>;
|
|
34
|
+
}>;
|
|
35
|
+
type AddTool<TTools, TName extends string, TInput extends ZodRawShapeCompat, TOutput> = McpServer<TTools & {
|
|
36
|
+
[K in TName]: ToolDef<ShapeOutput<TInput>, TOutput>;
|
|
28
37
|
}>;
|
|
29
|
-
type ToolConfig<TInput extends
|
|
38
|
+
type ToolConfig<TInput extends ZodRawShapeCompat | AnySchema> = {
|
|
30
39
|
title?: string;
|
|
31
40
|
description?: string;
|
|
32
41
|
inputSchema?: TInput;
|
|
33
|
-
outputSchema?:
|
|
42
|
+
outputSchema?: ZodRawShapeCompat | AnySchema;
|
|
34
43
|
annotations?: ToolAnnotations;
|
|
35
44
|
_meta?: Record<string, unknown>;
|
|
36
45
|
};
|
|
37
|
-
type ToolHandler<TInput extends
|
|
46
|
+
type ToolHandler<TInput extends ZodRawShapeCompat, TReturn extends CallToolResult = CallToolResult> = (args: ShapeOutput<TInput>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => TReturn | Promise<TReturn>;
|
|
38
47
|
export declare class McpServer<TTools extends Record<string, ToolDef> = {}> extends McpServerBase {
|
|
39
48
|
readonly $types: McpServerTypes<TTools>;
|
|
40
|
-
|
|
49
|
+
registerWidget<TName extends string, TInput extends ZodRawShapeCompat, TReturn extends CallToolResult>(name: TName, resourceConfig: McpServerOriginalResourceConfig, toolConfig: McpServerOriginalToolConfig & {
|
|
41
50
|
inputSchema?: TInput;
|
|
42
|
-
outputSchema?:
|
|
51
|
+
outputSchema?: ZodRawShapeCompat | AnySchema;
|
|
43
52
|
}, toolCallback: ToolHandler<TInput, TReturn>): AddTool<TTools, TName, TInput, ExtractStructuredContent<TReturn>>;
|
|
44
|
-
registerTool<TName extends string, InputArgs extends
|
|
45
|
-
registerTool<InputArgs extends
|
|
53
|
+
registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends CallToolResult>(name: TName, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs, TReturn>): AddTool<TTools, TName, InputArgs, ExtractStructuredContent<TReturn>>;
|
|
54
|
+
registerTool<InputArgs extends ZodRawShapeCompat>(name: string, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs>): RegisteredTool;
|
|
46
55
|
private lookupDistFile;
|
|
47
56
|
}
|
|
48
57
|
export {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { McpServer as McpServerBase
|
|
1
|
+
import { McpServer as McpServerBase } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { templateHelper } from "./templateHelper.js";
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
export class McpServer extends McpServerBase {
|
|
6
|
-
|
|
6
|
+
registerWidget(name, resourceConfig, toolConfig, toolCallback) {
|
|
7
7
|
const uri = `ui://widgets/${name}.html`;
|
|
8
8
|
const resourceMetadata = {
|
|
9
9
|
...(resourceConfig._meta ?? {}),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAkBrF,OAAO,EAAE,cAAc,EAAE,MAAM,qB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,OAAO;gBACL,QAAQ,EAAE;oBACR;wBACE,GAAG;wBACH,QAAQ,EAAE,qBAAqB;wBAC/B,IAAI,EAAE,IAAI;qBACX;iBACF;aACF,CAAC;QACJ,CAAC,CACF,CAAC;QAEF,MAAM,QAAQ,GAAa;YACzB,GAAG,UAAU,CAAC,KAAK;YACnB,uBAAuB,EAAE,GAAG;YAC5B,gBAAgB,EAAE,GAAG;SACtB,CAAC;QAEF,IAAI,CAAC,YAAY,CACf,IAAI,EACJ;YACE,GAAG,UAAU;YACb,KAAK,EAAE,QAAQ;SAChB,EACD,YAAY,CACb,CAAC;QAEF,OAAO,IAKN,CAAC;IACJ,CAAC;IAkBQ,YAAY,CACnB,IAAY,EACZ,MAA6B,EAC7B,EAA2B;QAE3B,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,cAAc,CAAC,GAAW;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CACzB,YAAY,CACV,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,eAAe,CAAC,EACpE,OAAO,CACR,CACF,CAAC;QAEF,OAAO,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC;IAC7B,CAAC;CACF"}
|
package/dist/src/test/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { vi } from "vitest";
|
|
2
2
|
import { McpServer } from "../server/server.js";
|
|
3
|
-
import
|
|
3
|
+
import * as z from "zod";
|
|
4
4
|
/**
|
|
5
5
|
* Creates a real McpServer instance for testing
|
|
6
6
|
*/
|
|
@@ -21,7 +21,7 @@ export function createMockMcpServer() {
|
|
|
21
21
|
}
|
|
22
22
|
export function createTestServer() {
|
|
23
23
|
return new McpServer({ name: "test-app", version: "1.0.0" }, {})
|
|
24
|
-
.
|
|
24
|
+
.registerWidget("search-voyage", {}, {
|
|
25
25
|
description: "Search for voyages",
|
|
26
26
|
inputSchema: {
|
|
27
27
|
destination: z.string(),
|
|
@@ -45,7 +45,7 @@ export function createTestServer() {
|
|
|
45
45
|
},
|
|
46
46
|
};
|
|
47
47
|
})
|
|
48
|
-
.
|
|
48
|
+
.registerWidget("get-trip-details", {}, {
|
|
49
49
|
description: "Get trip details",
|
|
50
50
|
inputSchema: {
|
|
51
51
|
tripId: z.string(),
|
|
@@ -65,7 +65,7 @@ export function createTestServer() {
|
|
|
65
65
|
},
|
|
66
66
|
};
|
|
67
67
|
})
|
|
68
|
-
.
|
|
68
|
+
.registerWidget("no-input-widget", {}, {
|
|
69
69
|
description: "Widget with no input",
|
|
70
70
|
inputSchema: {},
|
|
71
71
|
outputSchema: {},
|
|
@@ -75,7 +75,7 @@ export function createTestServer() {
|
|
|
75
75
|
structuredContent: {},
|
|
76
76
|
};
|
|
77
77
|
})
|
|
78
|
-
.
|
|
78
|
+
.registerWidget("inferred-output-widget", {}, {
|
|
79
79
|
description: "Widget with output inferred from callback",
|
|
80
80
|
inputSchema: {
|
|
81
81
|
query: z.string(),
|
|
@@ -124,7 +124,7 @@ export function createTestServer() {
|
|
|
124
124
|
});
|
|
125
125
|
}
|
|
126
126
|
export function createMinimalTestServer() {
|
|
127
|
-
return new McpServer({ name: "test-app", version: "1.0.0" }, {}).
|
|
127
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {}).registerWidget("search-voyage", {}, {
|
|
128
128
|
description: "Search for voyages",
|
|
129
129
|
inputSchema: {
|
|
130
130
|
destination: z.string(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/test/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAqB,MAAM,QAAQ,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.js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AAC,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"}
|
|
@@ -21,7 +21,7 @@ vi.mock("node:fs", async () => {
|
|
|
21
21
|
},
|
|
22
22
|
};
|
|
23
23
|
});
|
|
24
|
-
describe("McpServer.
|
|
24
|
+
describe("McpServer.registerWidget", () => {
|
|
25
25
|
let server;
|
|
26
26
|
let mockResource;
|
|
27
27
|
let mockRegisterTool;
|
|
@@ -38,7 +38,7 @@ describe("McpServer.widget", () => {
|
|
|
38
38
|
const mockToolCallback = vi.fn();
|
|
39
39
|
const mockResourceConfig = { description: "Test widget" };
|
|
40
40
|
const mockToolConfig = { description: "Test tool" };
|
|
41
|
-
server.
|
|
41
|
+
server.registerWidget("my-widget", mockResourceConfig, mockToolConfig, mockToolCallback);
|
|
42
42
|
// Get the resource callback function
|
|
43
43
|
const resourceCallback = mockResource.mock.calls[0]?.[3];
|
|
44
44
|
expect(resourceCallback).toBeDefined();
|
|
@@ -64,7 +64,7 @@ describe("McpServer.widget", () => {
|
|
|
64
64
|
const mockToolCallback = vi.fn();
|
|
65
65
|
const mockResourceConfig = { description: "Test widget" };
|
|
66
66
|
const mockToolConfig = { description: "Test tool" };
|
|
67
|
-
server.
|
|
67
|
+
server.registerWidget("my-widget", mockResourceConfig, mockToolConfig, mockToolCallback);
|
|
68
68
|
// Get the resource callback function
|
|
69
69
|
const resourceCallback = mockResource.mock.calls[0]?.[3];
|
|
70
70
|
expect(resourceCallback).toBeDefined();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"widget.test.js","sourceRoot":"","sources":["../../../src/test/widget.test.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,MAAM,EACN,EAAE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,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,SAAS,GAAG,cAAc,CAAC,CAAC;QAC3E,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,SAAS,CACxC,SAAS,GAAG,sBAAsB,CACnC,CAAC;QACF,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,GAAG,mBAAmB,CAAC,CAAC;IAC9E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/src/web/data-llm.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { createContext, useContext, useEffect, useId, } from "react";
|
|
3
|
-
export const WIDGET_CONTEXT_KEY = "__widget_context";
|
|
4
3
|
const nodes = new Map();
|
|
5
4
|
function setNode(node) {
|
|
6
5
|
nodes.set(node.id, node);
|
|
@@ -14,7 +13,7 @@ function onChange() {
|
|
|
14
13
|
const description = getLLMDescriptionString();
|
|
15
14
|
window.openai.setWidgetState({
|
|
16
15
|
...window.openai.widgetState,
|
|
17
|
-
|
|
16
|
+
__widget_context: description,
|
|
18
17
|
});
|
|
19
18
|
}
|
|
20
19
|
const ParentIdContext = createContext(null);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-llm.js","sourceRoot":"","sources":["../../../src/web/data-llm.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,aAAa,EACb,UAAU,EACV,SAAS,EACT,KAAK,GAEN,MAAM,OAAO,CAAC;AAUf,MAAM,
|
|
1
|
+
{"version":3,"file":"data-llm.js","sourceRoot":"","sources":["../../../src/web/data-llm.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,aAAa,EACb,UAAU,EACV,SAAS,EACT,KAAK,GAEN,MAAM,OAAO,CAAC;AAUf,MAA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AI,EAAE,CAAC,CAAC,CAAC;IAEtB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
export declare function useOpenAiGlobal<K extends keyof
|
|
1
|
+
import { type OpenAiProperties } from "../types.js";
|
|
2
|
+
export declare function useOpenAiGlobal<K extends keyof OpenAiProperties>(key: K): OpenAiProperties[K] | undefined;
|
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
import { useCallback, useEffect, useState } from "react";
|
|
2
2
|
import { useOpenAiGlobal } from "./use-openai-global.js";
|
|
3
|
-
|
|
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 preserveWidgetContext(newState) {
|
|
12
|
+
if (newState === null) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const currentWindowState = window.openai?.widgetState;
|
|
16
|
+
if (currentWindowState !== null &&
|
|
17
|
+
currentWindowState !== undefined &&
|
|
18
|
+
WIDGET_CONTEXT_KEY in currentWindowState) {
|
|
19
|
+
return {
|
|
20
|
+
...newState,
|
|
21
|
+
[WIDGET_CONTEXT_KEY]: currentWindowState[WIDGET_CONTEXT_KEY],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return newState;
|
|
25
|
+
}
|
|
4
26
|
export function useWidgetState(defaultState) {
|
|
5
27
|
const widgetStateFromWindow = useOpenAiGlobal("widgetState");
|
|
6
28
|
const [widgetState, _setWidgetState] = useState(() => {
|
|
@@ -20,7 +42,7 @@ export function useWidgetState(defaultState) {
|
|
|
20
42
|
const setWidgetState = useCallback((state) => {
|
|
21
43
|
_setWidgetState((prevState) => {
|
|
22
44
|
const newState = typeof state === "function" ? state(prevState) : state;
|
|
23
|
-
const stateToSet =
|
|
45
|
+
const stateToSet = preserveWidgetContext(newState);
|
|
24
46
|
if (stateToSet !== null) {
|
|
25
47
|
window.openai.setWidgetState(stateToSet);
|
|
26
48
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-widget-state.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-widget-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAuB,MAAM,OAAO,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-widget-state.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-widget-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAuB,MAAM,OAAO,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,MAAM,kBAAkB,GAAG,kBAA2B,CAAC;AAEvD,SAAS,mBAAmB,CAC1B,KAAgB;IAEhB,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,EAAE,CAAC,kBAAkB,CAAC,EAAE,CAAC,EAAE,GAAG,aAAa,EAAE,GAAG,KAErD,CAAC;IAEF,OAAO,aAAkB,CAAC;AAC5B,CAAC;AAED,SAAS,qBAAqB,CAC5B,QAAkB;IAElB,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,WAG7B,CAAC;IAEd,IACE,kBAAkB,KAAK,IAAI;QAC3B,kBAAkB,KAAK,SAAS;QAChC,kBAAkB,IAAI,kBAAkB,EACxC,CAAC;QACD,OAAO;YACL,GAAG,QAAQ;YACX,CA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C;IAEP,OAAO,CAAC,WAAW,EAAE,cAAc,CAAU,CAAC;AAChD,CAAC"}
|
package/dist/src/web/index.d.ts
CHANGED
|
@@ -2,6 +2,5 @@ export * from "./types.js";
|
|
|
2
2
|
export * from "./data-llm.js";
|
|
3
3
|
export { mountWidget } from "./mount-widget.js";
|
|
4
4
|
export { skybridge } from "./plugin/plugin.js";
|
|
5
|
-
export { createStore } from "./create-store.js";
|
|
6
5
|
export * from "./hooks/index.js";
|
|
7
6
|
export { generateHelpers } from "./generate-helpers.js";
|
package/dist/src/web/index.js
CHANGED
|
@@ -2,7 +2,6 @@ export * from "./types.js";
|
|
|
2
2
|
export * from "./data-llm.js";
|
|
3
3
|
export { mountWidget } from "./mount-widget.js";
|
|
4
4
|
export { skybridge } from "./plugin/plugin.js";
|
|
5
|
-
export { createStore } from "./create-store.js";
|
|
6
5
|
export * from "./hooks/index.js";
|
|
7
6
|
export { generateHelpers } from "./generate-helpers.js";
|
|
8
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/web/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/web/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/src/web/types.d.ts
CHANGED
|
@@ -28,13 +28,13 @@ export declare class ToolResponseEvent extends CustomEvent<{
|
|
|
28
28
|
}
|
|
29
29
|
declare global {
|
|
30
30
|
interface Window {
|
|
31
|
-
openai:
|
|
31
|
+
openai: OpenAiMethods<WidgetState> & OpenAiProperties;
|
|
32
32
|
}
|
|
33
33
|
interface WindowEventMap {
|
|
34
34
|
[SET_GLOBALS_EVENT_TYPE]: SetGlobalsEvent;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
export type
|
|
37
|
+
export type OpenAiProperties<ToolInput extends UnknownObject = {}, ToolOutput extends UnknownObject = UnknownObject, ToolResponseMetadata extends UnknownObject = UnknownObject, WidgetState extends UnknownObject = UnknownObject> = {
|
|
38
38
|
theme: Theme;
|
|
39
39
|
userAgent: UserAgent;
|
|
40
40
|
locale: string;
|
|
@@ -59,7 +59,7 @@ export type CallToolResponse = {
|
|
|
59
59
|
result: string;
|
|
60
60
|
meta: Record<string, unknown>;
|
|
61
61
|
};
|
|
62
|
-
type
|
|
62
|
+
export type OpenAiMethods<WidgetState extends UnknownObject = UnknownObject> = {
|
|
63
63
|
/** Calls a tool on your MCP. Returns the full response. */
|
|
64
64
|
callTool: <ToolArgs extends CallToolArgs = null, ToolResponse extends CallToolResponse = CallToolResponse>(name: string, args: ToolArgs) => Promise<ToolResponse>;
|
|
65
65
|
/** Triggers a followup turn in the ChatGPT conversation */
|
|
@@ -104,7 +104,7 @@ type API<WidgetState extends UnknownObject> = {
|
|
|
104
104
|
};
|
|
105
105
|
export declare const SET_GLOBALS_EVENT_TYPE = "openai:set_globals";
|
|
106
106
|
export declare class SetGlobalsEvent extends CustomEvent<{
|
|
107
|
-
globals: Partial<
|
|
107
|
+
globals: Partial<OpenAiProperties>;
|
|
108
108
|
}> {
|
|
109
109
|
readonly type = "openai:set_globals";
|
|
110
110
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "skybridge",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.faf07e2",
|
|
4
4
|
"description": "Skybridge is a framework for building ChatGPT apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -35,21 +35,19 @@
|
|
|
35
35
|
"author": "Frédéric Barthelet",
|
|
36
36
|
"license": "ISC",
|
|
37
37
|
"peerDependencies": {
|
|
38
|
+
"@modelcontextprotocol/sdk": ">=1.0.0",
|
|
38
39
|
"react": ">=18.0.0",
|
|
39
40
|
"react-dom": ">=18.0.0"
|
|
40
41
|
},
|
|
41
42
|
"dependencies": {
|
|
42
43
|
"@babel/core": "^7.28.5",
|
|
43
|
-
"@modelcontextprotocol/sdk": "^1.20.0",
|
|
44
44
|
"cors": "^2.8.5",
|
|
45
45
|
"express": "^5.1.0",
|
|
46
46
|
"handlebars": "^4.7.8",
|
|
47
|
-
"
|
|
48
|
-
"vite": "^7.1.11",
|
|
49
|
-
"zod": "^3.25.51",
|
|
50
|
-
"zustand": "^5.0.9"
|
|
47
|
+
"vite": "^7.1.11"
|
|
51
48
|
},
|
|
52
49
|
"devDependencies": {
|
|
50
|
+
"@modelcontextprotocol/sdk": "^1.24.3",
|
|
53
51
|
"@testing-library/dom": "^10.4.1",
|
|
54
52
|
"@testing-library/react": "^16.3.0",
|
|
55
53
|
"@total-typescript/tsconfig": "^1.0.4",
|
|
@@ -63,7 +61,8 @@
|
|
|
63
61
|
"@vitest/ui": "^2.1.8",
|
|
64
62
|
"jsdom": "^25.0.1",
|
|
65
63
|
"typescript": "^5.9.3",
|
|
66
|
-
"vitest": "^2.1.8"
|
|
64
|
+
"vitest": "^2.1.8",
|
|
65
|
+
"zod": "^4.1.13"
|
|
67
66
|
},
|
|
68
67
|
"packageManager": "pnpm@10.17.1"
|
|
69
68
|
}
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { type StateCreator } from "zustand";
|
|
2
|
-
import type { UnknownObject } from "./types.js";
|
|
3
|
-
export declare function createStore<State extends UnknownObject>(storeCreator: StateCreator<State, [], [], State>, defaultState?: State | (() => State)): import("zustand").UseBoundStore<import("zustand").StoreApi<State>>;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { create } from "zustand";
|
|
2
|
-
import { getInitialState, injectWidgetContext, serializeState } from "./helpers/state.js";
|
|
3
|
-
export function createStore(storeCreator, defaultState) {
|
|
4
|
-
const initialState = getInitialState(defaultState);
|
|
5
|
-
const store = create()((...args) => {
|
|
6
|
-
const baseStore = storeCreator(...args);
|
|
7
|
-
if (initialState !== null) {
|
|
8
|
-
return { ...baseStore, ...initialState };
|
|
9
|
-
}
|
|
10
|
-
return baseStore;
|
|
11
|
-
});
|
|
12
|
-
store.subscribe((state) => {
|
|
13
|
-
if (window.openai) {
|
|
14
|
-
const serializedState = serializeState(state);
|
|
15
|
-
if (serializedState !== null && serializedState !== undefined) {
|
|
16
|
-
const stateToPersist = injectWidgetContext(serializedState);
|
|
17
|
-
if (stateToPersist !== null) {
|
|
18
|
-
window.openai.setWidgetState(stateToPersist);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
return store;
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=create-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,EACf,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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach, } from "vitest";
|
|
2
|
-
import { createStore } from "./create-store.js";
|
|
3
|
-
import { WIDGET_CONTEXT_KEY } from "./data-llm.js";
|
|
4
|
-
describe("createStore", () => {
|
|
5
|
-
let OpenaiMock;
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
OpenaiMock = {
|
|
8
|
-
widgetState: null,
|
|
9
|
-
setWidgetState: vi.fn().mockResolvedValue(undefined),
|
|
10
|
-
};
|
|
11
|
-
vi.stubGlobal("openai", OpenaiMock);
|
|
12
|
-
});
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
vi.unstubAllGlobals();
|
|
15
|
-
vi.resetAllMocks();
|
|
16
|
-
});
|
|
17
|
-
it("should create a store without default state", () => {
|
|
18
|
-
const storeCreator = () => ({
|
|
19
|
-
count: 0,
|
|
20
|
-
});
|
|
21
|
-
const store = createStore(storeCreator);
|
|
22
|
-
expect(store.getState()).toEqual({ count: 0 });
|
|
23
|
-
});
|
|
24
|
-
it("should create a store with default state", () => {
|
|
25
|
-
const storeCreator = () => ({
|
|
26
|
-
count: 0,
|
|
27
|
-
name: "initial",
|
|
28
|
-
});
|
|
29
|
-
const defaultState = { count: 5, name: "default" };
|
|
30
|
-
const store = createStore(storeCreator, defaultState);
|
|
31
|
-
expect(store.getState()).toEqual({ count: 5, name: "default" });
|
|
32
|
-
});
|
|
33
|
-
it("should initialize from window.openai.widgetState when available", () => {
|
|
34
|
-
const storeCreator = () => ({
|
|
35
|
-
count: 0,
|
|
36
|
-
name: "initial",
|
|
37
|
-
});
|
|
38
|
-
const windowState = { count: 20, name: "window" };
|
|
39
|
-
OpenaiMock.widgetState = windowState;
|
|
40
|
-
const store = createStore(storeCreator);
|
|
41
|
-
expect(store.getState()).toEqual({ count: 20, name: "window" });
|
|
42
|
-
});
|
|
43
|
-
it("should persist state changes to window.openai.setWidgetState", async () => {
|
|
44
|
-
const storeCreator = (set) => ({
|
|
45
|
-
count: 0,
|
|
46
|
-
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
47
|
-
});
|
|
48
|
-
const store = createStore(storeCreator);
|
|
49
|
-
store.getState().increment();
|
|
50
|
-
await vi.waitFor(() => {
|
|
51
|
-
expect(OpenaiMock.setWidgetState).toHaveBeenCalled();
|
|
52
|
-
});
|
|
53
|
-
const callArgs = OpenaiMock.setWidgetState.mock.calls[0]?.[0];
|
|
54
|
-
expect(callArgs).toEqual({ count: 1 });
|
|
55
|
-
});
|
|
56
|
-
it("should filter widget context from initial state", () => {
|
|
57
|
-
const storeCreator = () => ({
|
|
58
|
-
count: 0,
|
|
59
|
-
});
|
|
60
|
-
const windowState = {
|
|
61
|
-
count: 5,
|
|
62
|
-
[WIDGET_CONTEXT_KEY]: "context-value",
|
|
63
|
-
};
|
|
64
|
-
OpenaiMock.widgetState = windowState;
|
|
65
|
-
const store = createStore(storeCreator);
|
|
66
|
-
expect(store.getState()).toEqual({ count: 5 });
|
|
67
|
-
expect(store.getState()[WIDGET_CONTEXT_KEY]).toBeUndefined();
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
//# sourceMappingURL=create-store.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-store.test.js","sourceRoot":"","sources":["../../../src/web/create-store.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,EAAE,EACF,MAAM,EACN,EAAE,EACF,UAAU,EACV,SAAS,GAEV,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,IAAI,UAGH,CAAC;IAEF,UAAU,CAAC,GAAG,EAAE;QACd,UAAU,GAAG;YACX,WAAW,EAAE,IAAI;YACjB,cAAc,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;SACrD,CAAC;QACF,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,EAAE,CAAC,gBAAgB,EAAE,CAAC;QACtB,EAAE,CAAC,aAAa,EAAE,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QAErD,MAAM,YAAY,GAA+C,GAAG,EAAE,CAAC,CAAC;YACtE,KAAK,EAAE,CAAC;SACT,CAAC,CAAC;QAEH,MAAM,KAAK,GAAG,WAAW,CAAC,YAAY,CAAC,CAAC;QAExC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;IACjD,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declare function filterWidgetContext<T extends UnknownObject>(state?: T | null): T | null;
|
|
4
|
-
export declare function injectWidgetContext<T extends UnknownObject>(newState: T | null): T | null;
|
|
5
|
-
export declare function serializeState(value: UnknownObject): unknown;
|
|
6
|
-
export declare function deserializeState(value: SuperJSONResult): unknown;
|
|
7
|
-
export declare function getInitialState<State extends UnknownObject>(defaultState?: State | (() => State)): State | null;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import superjson, {} from "superjson";
|
|
2
|
-
import { WIDGET_CONTEXT_KEY } from "../data-llm.js";
|
|
3
|
-
export function filterWidgetContext(state) {
|
|
4
|
-
if (state === null || state === undefined) {
|
|
5
|
-
return null;
|
|
6
|
-
}
|
|
7
|
-
const { [WIDGET_CONTEXT_KEY]: _, ...filteredState } = state;
|
|
8
|
-
return filteredState;
|
|
9
|
-
}
|
|
10
|
-
export function injectWidgetContext(newState) {
|
|
11
|
-
if (newState === null) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
const currentWindowState = window.openai?.widgetState;
|
|
15
|
-
if (currentWindowState !== null &&
|
|
16
|
-
currentWindowState !== undefined &&
|
|
17
|
-
WIDGET_CONTEXT_KEY in currentWindowState) {
|
|
18
|
-
return {
|
|
19
|
-
...newState,
|
|
20
|
-
[WIDGET_CONTEXT_KEY]: currentWindowState[WIDGET_CONTEXT_KEY],
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
return newState;
|
|
24
|
-
}
|
|
25
|
-
export function serializeState(value) {
|
|
26
|
-
return superjson.parse(superjson.stringify(value)); // Strips functions
|
|
27
|
-
}
|
|
28
|
-
export function deserializeState(value) {
|
|
29
|
-
return superjson.deserialize(value);
|
|
30
|
-
}
|
|
31
|
-
export function getInitialState(defaultState) {
|
|
32
|
-
const widgetStateFromWindow = window.openai?.widgetState;
|
|
33
|
-
if (widgetStateFromWindow !== null && widgetStateFromWindow !== undefined) {
|
|
34
|
-
return filterWidgetContext(widgetStateFromWindow);
|
|
35
|
-
}
|
|
36
|
-
return typeof defaultState === "function"
|
|
37
|
-
? defaultState()
|
|
38
|
-
: defaultState ?? null;
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=state.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../../src/web/helpers/state.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,EAAE,EAAwB,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAGpD,MAAM,UAAU,mBAAmB,CACjC,KAAgB;IAEhB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,EAAE,CAAC,kBAAkB,CAAC,EAAE,CAAC,EAAE,GAAG,aAAa,EAAE,GAAG,KAErD,CAAC;IAEF,OAAO,aAAkB,CAAC;AAC5B,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,QAAkB;IAElB,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,WAG7B,CAAC;IAEd,IACE,kBAAkB,KAAK,IAAI;QAC3B,kBAAkB,KAAK,SAAS;QAChC,kBAAkB,IAAI,kBAAkB,EACxC,CAAC;QACD,OAAO;YACL,GAAG,QAAQ;YACX,CAAC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC,kBAAkB,CAAC;SACxD,CAAC;IACT,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,KAAoB;IACjD,OAAO,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,mBAAmB;AACzE,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,KAAsB;IACrD,OAAO,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,eAAe,CAC7B,YAAoC;IAEpC,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,WAGhC,CAAC;IAEd,IAAI,qBAAqB,KAAK,IAAI,IAAI,qBAAqB,KAAK,SAAS,EAAE,CAAC;QAC1E,OAAO,mBAAmB,CAAC,qBAAqB,CAAC,CAAC;IACpD,CAAC;IAED,OAAO,OAAO,YAAY,KAAK,UAAU;QACvC,CAAC,CAAC,YAAY,EAAE;QAChB,CAAC,CAAC,YAAY,IAAI,IAAI,CAAC;AAC3B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach, } from "vitest";
|
|
2
|
-
import { filterWidgetContext, injectWidgetContext, serializeState, getInitialState, } from "./state.js";
|
|
3
|
-
import { WIDGET_CONTEXT_KEY } from "../data-llm.js";
|
|
4
|
-
describe("state helpers", () => {
|
|
5
|
-
describe("filterWidgetContext", () => {
|
|
6
|
-
it("should return null when state is null", () => {
|
|
7
|
-
expect(filterWidgetContext(null)).toBe(null);
|
|
8
|
-
});
|
|
9
|
-
it("should return null when state is undefined", () => {
|
|
10
|
-
expect(filterWidgetContext(undefined)).toBe(null);
|
|
11
|
-
});
|
|
12
|
-
it("should correctly filter WIDGET_CONTEXT_KEY and preserve other properties", () => {
|
|
13
|
-
const stateWithContextAndOthers = {
|
|
14
|
-
a: 1,
|
|
15
|
-
b: "two",
|
|
16
|
-
c: { nested: true },
|
|
17
|
-
[WIDGET_CONTEXT_KEY]: "context",
|
|
18
|
-
};
|
|
19
|
-
const filteredWithContextAndOthers = filterWidgetContext(stateWithContextAndOthers);
|
|
20
|
-
expect(filteredWithContextAndOthers).toEqual({
|
|
21
|
-
a: 1,
|
|
22
|
-
b: "two",
|
|
23
|
-
c: { nested: true },
|
|
24
|
-
});
|
|
25
|
-
const stateNoContext = { count: 5, name: "test" };
|
|
26
|
-
const filteredNoContext = filterWidgetContext(stateNoContext);
|
|
27
|
-
expect(filteredNoContext).toEqual(stateNoContext);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe("serializeState", () => {
|
|
31
|
-
it("should serialize plain objects", () => {
|
|
32
|
-
const array = [1, "two", { three: 3 }];
|
|
33
|
-
const date = new Date("2023-01-01T00:00:00Z");
|
|
34
|
-
const object = {
|
|
35
|
-
a: 1,
|
|
36
|
-
b: "test",
|
|
37
|
-
c: true,
|
|
38
|
-
array,
|
|
39
|
-
date,
|
|
40
|
-
function: () => "test",
|
|
41
|
-
};
|
|
42
|
-
const result = serializeState(object);
|
|
43
|
-
expect(result).toEqual({
|
|
44
|
-
a: 1,
|
|
45
|
-
b: "test",
|
|
46
|
-
c: true,
|
|
47
|
-
array: [1, "two", { three: 3 }],
|
|
48
|
-
date: new Date("2023-01-01T00:00:00.000Z"),
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
//# sourceMappingURL=state.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"state.test.js","sourceRoot":"","sources":["../../../../src/web/helpers/state.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,EAAE,EACF,MAAM,EACN,EAAE,EACF,UAAU,EACV,SAAS,GAEV,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;QACnC,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;YAC/C,MAAM,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,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