skybridge 0.0.0-dev.e2cf49b → 0.0.0-dev.e3e0986
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 +122 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/inferUtilityTypes.d.ts +33 -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 +13 -7
- package/dist/src/server/server.js +5 -1
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/test/utils.js +1 -1
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/web/hooks/index.d.ts +2 -1
- package/dist/src/web/hooks/index.js +2 -1
- package/dist/src/web/hooks/index.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.d.ts +16 -16
- package/dist/src/web/hooks/use-call-tool.js +12 -10
- package/dist/src/web/hooks/use-call-tool.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.test.js +61 -2
- package/dist/src/web/hooks/use-call-tool.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.js +29 -0
- package/dist/src/web/hooks/use-files.test.js.map +1 -0
- package/dist/src/web/hooks/use-openai-global.js +3 -1
- 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-tool-info.d.ts +24 -4
- package/dist/src/web/hooks/use-tool-info.js +17 -4
- package/dist/src/web/hooks/use-tool-info.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test-d.d.ts +1 -0
- package/dist/src/web/hooks/use-tool-info.test-d.js +74 -0
- package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -0
- package/dist/src/web/hooks/use-tool-info.test.js +36 -15
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/src/web/hooks/use-tool-output.d.ts +1 -1
- package/dist/src/web/hooks/use-widget-state.test.js +0 -1
- package/dist/src/web/hooks/use-widget-state.test.js.map +1 -1
- package/dist/src/web/index.d.ts +1 -0
- package/dist/src/web/index.js +1 -0
- 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/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/typed-hooks.d.ts +61 -0
- package/dist/src/web/typed-hooks.js +61 -0
- package/dist/src/web/typed-hooks.js.map +1 -0
- package/dist/src/web/typed-hooks.test-d.d.ts +1 -0
- package/dist/src/web/typed-hooks.test-d.js +72 -0
- package/dist/src/web/typed-hooks.test-d.js.map +1 -0
- package/dist/src/web/typed-hooks.test.d.ts +1 -0
- package/dist/src/web/typed-hooks.test.js +10 -0
- package/dist/src/web/typed-hooks.test.js.map +1 -0
- package/dist/src/web/types.d.ts +29 -20
- package/dist/src/web/types.js.map +1 -1
- package/dist/vitest.config.js +0 -1
- package/dist/vitest.config.js.map +1 -1
- package/package.json +4 -2
- package/dist/src/test/setup.js +0 -9
- package/dist/src/test/setup.js.map +0 -1
- /package/dist/src/{test/setup.d.ts → web/hooks/use-files.test.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -97,6 +97,81 @@ export default defineConfig({
|
|
|
97
97
|
});
|
|
98
98
|
```
|
|
99
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
|
+
_Server setup (server/src/index.ts)_
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { McpServer } from "skybridge/server";
|
|
108
|
+
import { z } from "zod";
|
|
109
|
+
|
|
110
|
+
const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
111
|
+
.widget("search-voyage", {}, {
|
|
112
|
+
description: "Search for trips",
|
|
113
|
+
inputSchema: {
|
|
114
|
+
destination: z.string(),
|
|
115
|
+
departureDate: z.string().optional(),
|
|
116
|
+
},
|
|
117
|
+
outputSchema: {
|
|
118
|
+
results: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
119
|
+
},
|
|
120
|
+
}, async ({ destination }) => {
|
|
121
|
+
// Your tool logic here...
|
|
122
|
+
return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
|
|
123
|
+
})
|
|
124
|
+
.widget("get-details", {}, {
|
|
125
|
+
inputSchema: { tripId: z.string() },
|
|
126
|
+
}, async ({ tripId }) => {
|
|
127
|
+
return { content: [{ type: "text", text: `Details for ${tripId}` }] };
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Export the server type for the client
|
|
131
|
+
export type AppType = typeof server;
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
_One-time setup (web/src/skybridge.ts)_
|
|
135
|
+
|
|
136
|
+
Create typed hooks once and export them for use across your app:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import type { AppType } from "../server"; // type-only import
|
|
140
|
+
import { createTypedHooks } from "skybridge/web";
|
|
141
|
+
|
|
142
|
+
export const { useCallTool, useToolInfo } = createTypedHooks<AppType>();
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
_Usage in widgets (web/src/widgets/search.tsx)_
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
import { useCallTool, useToolInfo } from "../skybridge"; // import typed hooks
|
|
149
|
+
|
|
150
|
+
export function SearchWidget() {
|
|
151
|
+
const { callTool, data, isPending } = useCallTool("search-voyage");
|
|
152
|
+
// ^ autocomplete for tool names
|
|
153
|
+
const toolInfo = useToolInfo<"search-voyage">();
|
|
154
|
+
// ^ autocomplete for widget names
|
|
155
|
+
|
|
156
|
+
const handleSearch = () => {
|
|
157
|
+
callTool({ destination: "Spain" });
|
|
158
|
+
// ^ autocomplete for input fields
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div>
|
|
163
|
+
<button onClick={handleSearch} disabled={isPending}>
|
|
164
|
+
Search
|
|
165
|
+
</button>
|
|
166
|
+
{toolInfo.isSuccess && (
|
|
167
|
+
<div>Found {toolInfo.output.totalCount} results</div>
|
|
168
|
+
// ^ typed output
|
|
169
|
+
)}
|
|
170
|
+
</div>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
100
175
|
**Hooks**
|
|
101
176
|
|
|
102
177
|
The `skybridge/web` package comes with a set of hooks to help you build your widgets :
|
|
@@ -104,7 +179,9 @@ The `skybridge/web` package comes with a set of hooks to help you build your wid
|
|
|
104
179
|
- `useOpenAiGlobal`: A generic hook to get any global data from the OpenAI iFrame skybridge runtime (in `window.openai`).
|
|
105
180
|
- `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.
|
|
106
181
|
- `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.
|
|
182
|
+
- `useToolInfo`: A hook to get the tool input, output, and response metadata with type inference. Provides a discriminated union based on status (pending/success).
|
|
107
183
|
- `useCallTool`: A @tanstack/react-query inspired hook to send make additional tool calls inside a widget.
|
|
184
|
+
- `createTypedHooks`: A factory that creates typed versions of `useCallTool` and `useToolInfo` with full type inference from your server type.
|
|
108
185
|
|
|
109
186
|
_useOpenAiGlobal_
|
|
110
187
|
|
|
@@ -130,6 +207,51 @@ import { useToolResponseMetadata } from "skybridge/web";
|
|
|
130
207
|
const toolResponseMetadata = useToolResponseMetadata();
|
|
131
208
|
```
|
|
132
209
|
|
|
210
|
+
_useToolInfo_
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import { useToolInfo } from "skybridge/web";
|
|
214
|
+
|
|
215
|
+
const toolInfo = useToolInfo<{
|
|
216
|
+
input: { query: string };
|
|
217
|
+
output: { results: string[] };
|
|
218
|
+
responseMetadata: { id: number };
|
|
219
|
+
}>();
|
|
220
|
+
|
|
221
|
+
// toolInfo.input is typed based on the input type
|
|
222
|
+
// toolInfo.output is typed based on the output type (undefined when pending)
|
|
223
|
+
// toolInfo.status narrows correctly: "pending" | "success"
|
|
224
|
+
|
|
225
|
+
if (toolInfo.isPending) {
|
|
226
|
+
// toolInfo.output is undefined here
|
|
227
|
+
console.log(toolInfo.input.query);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (toolInfo.isSuccess) {
|
|
231
|
+
// toolInfo.output is typed here
|
|
232
|
+
console.log(toolInfo.output.results);
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
_useToolInfo_ with typed hooks (recommended)
|
|
237
|
+
|
|
238
|
+
```tsx
|
|
239
|
+
import { useToolInfo } from "../skybridge"; // import typed hooks
|
|
240
|
+
|
|
241
|
+
export function SearchWidget() {
|
|
242
|
+
const toolInfo = useToolInfo<"search-voyage">();
|
|
243
|
+
// ^ autocomplete for widget names
|
|
244
|
+
// toolInfo.input is typed as { destination: string; departureDate?: string; ... }
|
|
245
|
+
// toolInfo.output is typed as { results: Array<...>; totalCount: number; } | undefined
|
|
246
|
+
|
|
247
|
+
if (toolInfo.isSuccess) {
|
|
248
|
+
return <div>Found {toolInfo.output.totalCount} results</div>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return <div>Searching for {toolInfo.input.destination}...</div>;
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
133
255
|
_useCallTool_ in synchronous mode
|
|
134
256
|
|
|
135
257
|
```ts
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { McpServer, WidgetDef } from "./server.js";
|
|
2
|
+
/** Any widget registry shape */
|
|
3
|
+
export type AnyWidgetRegistry = Record<string, WidgetDef>;
|
|
4
|
+
/**
|
|
5
|
+
* Extract the widget registry type from an McpServer instance.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* type MyWidgets = InferWidgets<MyServer>;
|
|
10
|
+
* // { "search": WidgetDef<...>, "details": WidgetDef<...> }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export type InferWidgets<T> = T extends McpServer<infer W extends AnyWidgetRegistry> ? W : T extends McpServer<any> ? never : "Error: T must be an McpServer instance";
|
|
14
|
+
/** Get a union of all widget names from an McpServer instance */
|
|
15
|
+
export type WidgetNames<T> = keyof InferWidgets<T> & string;
|
|
16
|
+
/**
|
|
17
|
+
* Get the input type for a specific widget.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* type SearchInput = WidgetInput<MyServer, "search">;
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type WidgetInput<T, K extends WidgetNames<T>> = InferWidgets<T> extends Record<string, WidgetDef> ? K extends keyof InferWidgets<T> ? InferWidgets<T>[K]["input"] : never : never;
|
|
25
|
+
/**
|
|
26
|
+
* Get the output type for a specific widget.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* type SearchOutput = WidgetOutput<MyServer, "search">;
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export type WidgetOutput<T, K extends WidgetNames<T>> = InferWidgets<T> extends Record<string, WidgetDef> ? K extends keyof InferWidgets<T> ? InferWidgets<T>[K]["output"] : never : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inferUtilityTypes.js","sourceRoot":"","sources":["../../../src/server/inferUtilityTypes.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { McpServer as McpServerBase, type ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import type { Resource } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
-
import type { ZodRawShape } from "zod";
|
|
3
|
+
import type { ZodRawShape, ZodObject, infer as Infer } from "zod";
|
|
4
|
+
export type WidgetDef<TInput = unknown, TOutput = unknown> = {
|
|
5
|
+
input: TInput;
|
|
6
|
+
output: TOutput;
|
|
7
|
+
};
|
|
4
8
|
type McpServerOriginalResourceConfig = Omit<Resource, "uri" | "name" | "mimeType">;
|
|
5
|
-
type McpServerOriginalToolConfig = Omit<Parameters<
|
|
6
|
-
export declare class McpServer extends McpServerBase {
|
|
7
|
-
widget<
|
|
8
|
-
inputSchema?:
|
|
9
|
-
outputSchema?:
|
|
10
|
-
}, toolCallback: ToolCallback<
|
|
9
|
+
type McpServerOriginalToolConfig = Omit<Parameters<McpServerBase["registerTool"]>[1], "inputSchema" | "outputSchema">;
|
|
10
|
+
export declare class McpServer<TWidgets extends Record<string, WidgetDef> = {}> extends McpServerBase {
|
|
11
|
+
widget<TName extends string, TInput extends ZodRawShape, TOutput extends ZodRawShape = {}>(name: TName, resourceConfig: McpServerOriginalResourceConfig, toolConfig: McpServerOriginalToolConfig & {
|
|
12
|
+
inputSchema?: TInput;
|
|
13
|
+
outputSchema?: TOutput;
|
|
14
|
+
}, toolCallback: ToolCallback<TInput>): McpServer<TWidgets & {
|
|
15
|
+
[K in TName]: WidgetDef<Infer<ZodObject<TInput>>, Infer<ZodObject<TOutput>>>;
|
|
16
|
+
}>;
|
|
11
17
|
private lookupDistFile;
|
|
12
18
|
}
|
|
13
19
|
export {};
|
|
@@ -5,7 +5,9 @@ import path from "node:path";
|
|
|
5
5
|
export class McpServer extends McpServerBase {
|
|
6
6
|
widget(name, resourceConfig, toolConfig, toolCallback) {
|
|
7
7
|
const uri = `ui://widgets/${name}.html`;
|
|
8
|
-
const resourceMetadata = {
|
|
8
|
+
const resourceMetadata = {
|
|
9
|
+
...(resourceConfig._meta ?? {}),
|
|
10
|
+
};
|
|
9
11
|
if (toolConfig.description !== undefined) {
|
|
10
12
|
resourceMetadata["openai/widgetDescription"] = toolConfig.description;
|
|
11
13
|
}
|
|
@@ -40,11 +42,13 @@ export class McpServer extends McpServerBase {
|
|
|
40
42
|
const toolMeta = {
|
|
41
43
|
...toolConfig._meta,
|
|
42
44
|
"openai/outputTemplate": uri,
|
|
45
|
+
"ui/resourceUri": uri,
|
|
43
46
|
};
|
|
44
47
|
this.registerTool(name, {
|
|
45
48
|
...toolConfig,
|
|
46
49
|
_meta: toolMeta,
|
|
47
50
|
}, toolCallback);
|
|
51
|
+
return this;
|
|
48
52
|
}
|
|
49
53
|
lookupDistFile(key) {
|
|
50
54
|
const manifest = JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,aAAa,GAE3B,MAAM,yCAAyC,CAAC;AAGjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,aAAa,GAE3B,MAAM,yCAAyC,CAAC;AAGjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAuD7B,MAAM,OAAO,SAEX,SAAQ,aAAa;IACrB,MAAM,CAKJ,IAAW,EACX,cAA+C,EAC/C,UAGC,EACD,YAAkC;QASlC,MAAM,GAAG,GAAG,gBAAgB,IAAI,OAAO,CAAC;QACxC,MAAM,gBAAgB,GAAiB;YACrC,GAAG,CAAC,cAAc,CAAC,KAAK,I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,UAAU;YACb,KAAK,EAAE,QAAQ;SAChB,EACD,YAAY,CACb,CAAC;QAEF,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 +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,
|
|
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;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,YAAY,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAClD,MAAM,gBAAgB,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IAE1D,OAAO;QACL,MAAM;QACN,YAAY;QACZ,gBAAgB;KACjB,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,8 +1,9 @@
|
|
|
1
1
|
export { useCallTool } from "./use-call-tool.js";
|
|
2
2
|
export { useDisplayMode } from "./use-display-mode.js";
|
|
3
|
+
export { useFiles } from "./use-files.js";
|
|
3
4
|
export { useLocale } from "./use-locale.js";
|
|
4
|
-
export { useOpenExternal } from "./use-open-external.js";
|
|
5
5
|
export { useOpenAiGlobal } from "./use-openai-global.js";
|
|
6
|
+
export { useOpenExternal } from "./use-open-external.js";
|
|
6
7
|
export { useRequestModal } from "./use-request-modal.js";
|
|
7
8
|
export { useSendFollowUpMessage } from "./use-send-follow-up-message.js";
|
|
8
9
|
export { useTheme } from "./use-theme.js";
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export { useCallTool } from "./use-call-tool.js";
|
|
2
2
|
export { useDisplayMode } from "./use-display-mode.js";
|
|
3
|
+
export { useFiles } from "./use-files.js";
|
|
3
4
|
export { useLocale } from "./use-locale.js";
|
|
4
|
-
export { useOpenExternal } from "./use-open-external.js";
|
|
5
5
|
export { useOpenAiGlobal } from "./use-openai-global.js";
|
|
6
|
+
export { useOpenExternal } from "./use-open-external.js";
|
|
6
7
|
export { useRequestModal } from "./use-request-modal.js";
|
|
7
8
|
export { useSendFollowUpMessage } from "./use-send-follow-up-message.js";
|
|
8
9
|
export { useTheme } from "./use-theme.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/web/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/web/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type { CallToolArgs, CallToolResponse
|
|
2
|
-
type ResolvedToolArgs<TArgs extends CallToolArgs> = TArgs extends null ? null : TArgs;
|
|
1
|
+
import type { CallToolArgs, CallToolResponse } from "../types.js";
|
|
3
2
|
type SideEffects<ToolArgs, ToolResponse> = {
|
|
4
3
|
onSuccess?: (data: ToolResponse, toolArgs: ToolArgs) => void;
|
|
5
4
|
onError?: (error: unknown, toolArgs: ToolArgs) => void;
|
|
6
5
|
onSettled?: (data: ToolResponse | undefined, error: unknown | undefined, toolArgs: ToolArgs) => void;
|
|
7
6
|
};
|
|
8
|
-
type CallToolAsyncFn<
|
|
9
|
-
|
|
7
|
+
type CallToolAsyncFn<ToolArgs, ToolResponse> = ToolArgs extends null ? () => Promise<ToolResponse> : (toolArgs: ToolArgs) => Promise<ToolResponse>;
|
|
8
|
+
type ToolResponseSignature = Pick<CallToolResponse, "structuredContent" | "meta">;
|
|
9
|
+
export declare const useCallTool: <ToolArgs extends CallToolArgs = null, ToolResponse extends Partial<ToolResponseSignature> = {}>(name: string) => {
|
|
10
10
|
callTool: {
|
|
11
|
-
(sideEffects?: SideEffects<ToolArgs, ToolResponse>): void;
|
|
12
|
-
(toolArgs:
|
|
11
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
12
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
13
13
|
};
|
|
14
|
-
callToolAsync: CallToolAsyncFn<
|
|
14
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
15
15
|
status: "idle";
|
|
16
16
|
isIdle: true;
|
|
17
17
|
isPending: false;
|
|
@@ -21,10 +21,10 @@ export declare const useCallTool: <ToolArgs extends CallToolArgs = null, ToolRes
|
|
|
21
21
|
error: undefined;
|
|
22
22
|
} | {
|
|
23
23
|
callTool: {
|
|
24
|
-
(sideEffects?: SideEffects<ToolArgs, ToolResponse>): void;
|
|
25
|
-
(toolArgs:
|
|
24
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
25
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
26
26
|
};
|
|
27
|
-
callToolAsync: CallToolAsyncFn<
|
|
27
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
28
28
|
status: "pending";
|
|
29
29
|
isIdle: false;
|
|
30
30
|
isPending: true;
|
|
@@ -34,10 +34,10 @@ export declare const useCallTool: <ToolArgs extends CallToolArgs = null, ToolRes
|
|
|
34
34
|
error: undefined;
|
|
35
35
|
} | {
|
|
36
36
|
callTool: {
|
|
37
|
-
(sideEffects?: SideEffects<ToolArgs, ToolResponse>): void;
|
|
38
|
-
(toolArgs:
|
|
37
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
38
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
39
39
|
};
|
|
40
|
-
callToolAsync: CallToolAsyncFn<
|
|
40
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
41
41
|
status: "error";
|
|
42
42
|
isIdle: false;
|
|
43
43
|
isPending: false;
|
|
@@ -47,10 +47,10 @@ export declare const useCallTool: <ToolArgs extends CallToolArgs = null, ToolRes
|
|
|
47
47
|
error: unknown;
|
|
48
48
|
} | {
|
|
49
49
|
callTool: {
|
|
50
|
-
(sideEffects?: SideEffects<ToolArgs, ToolResponse>): void;
|
|
51
|
-
(toolArgs:
|
|
50
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
51
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
52
52
|
};
|
|
53
|
-
callToolAsync: CallToolAsyncFn<
|
|
53
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
54
54
|
status: "success";
|
|
55
55
|
isIdle: false;
|
|
56
56
|
isPending: false;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState } from "react";
|
|
2
2
|
export const useCallTool = (name) => {
|
|
3
3
|
const [{ status, data, error }, setCallToolState] = useState({ status: "idle", data: undefined, error: undefined });
|
|
4
|
-
const
|
|
4
|
+
const execute = async (toolArgs) => {
|
|
5
5
|
setCallToolState({ status: "pending", data: undefined, error: undefined });
|
|
6
6
|
try {
|
|
7
7
|
const data = await window.openai.callTool(name, toolArgs);
|
|
@@ -15,22 +15,24 @@ export const useCallTool = (name) => {
|
|
|
15
15
|
};
|
|
16
16
|
const callToolAsync = (async (toolArgs) => {
|
|
17
17
|
if (toolArgs === undefined) {
|
|
18
|
-
return
|
|
18
|
+
return execute(null);
|
|
19
19
|
}
|
|
20
|
-
return
|
|
20
|
+
return execute(toolArgs);
|
|
21
21
|
});
|
|
22
|
-
function callTool(
|
|
22
|
+
function callTool(firstArg, sideEffects) {
|
|
23
23
|
let toolArgs;
|
|
24
|
-
if (
|
|
25
|
-
typeof
|
|
26
|
-
("onSuccess" in
|
|
24
|
+
if (firstArg &&
|
|
25
|
+
typeof firstArg === "object" &&
|
|
26
|
+
("onSuccess" in firstArg ||
|
|
27
|
+
"onError" in firstArg ||
|
|
28
|
+
"onSettled" in firstArg)) {
|
|
27
29
|
toolArgs = null; // no toolArgs provided
|
|
28
|
-
sideEffects =
|
|
30
|
+
sideEffects = firstArg;
|
|
29
31
|
}
|
|
30
32
|
else {
|
|
31
|
-
toolArgs =
|
|
33
|
+
toolArgs = (firstArg === undefined ? null : firstArg);
|
|
32
34
|
}
|
|
33
|
-
|
|
35
|
+
execute(toolArgs)
|
|
34
36
|
.then((data) => {
|
|
35
37
|
if (sideEffects?.onSuccess) {
|
|
36
38
|
sideEffects.onSuccess(data, toolArgs);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-call-tool.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-call-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"use-call-tool.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-call-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAoEjC,MAAM,CAAC,MAAM,WAAW,GAAG,CAIzB,IAAY,EACZ,EAAE;IAGF,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAK1D,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAEzD,MAAM,OAAO,GAAG,KAAK,EACnB,QAAkB,EACiB,EAAE;QACrC,gBAAgB,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;QAC3E,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,CAGvC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAClB,gBAAgB,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC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D,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,IAAI,WAAW,EAAE,OAAO,EAAE,CAAC;gBACzB,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;YACvC,CAAC;YACD,IAAI,WAAW,EAAE,SAAS,EAAE,CAAC;gBAC3B,WAAW,CAAC,SAAS,CAAC,SAAS,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;YACpD,CAAC;QACH,CAAC,CAAC,CAAC;IACP,CAAC;IAED,MAAM,aAAa,GAAG;QACpB,MAAM;QACN,IAAI;QACJ,KAAK;QACL,MAAM,EAAE,MAAM,KAAK,MAAM;QACzB,SAAS,EAAE,MAAM,KAAK,SAAS;QAC/B,SAAS,EAAE,MAAM,KAAK,SAAS;QAC/B,OAAO,EAAE,MAAM,KAAK,OAAO;KACe,CAAC;IAE7C,OAAO;QACL,GAAG,aAAa;QAChB,QAAQ;QACR,aAAa;KACd,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi, expectTypeOf, } from "vitest";
|
|
1
3
|
import { useCallTool } from "./use-call-tool.js";
|
|
2
|
-
import { describe, it, expect, vi, beforeEach, afterEach, } from "vitest";
|
|
3
|
-
import { renderHook, act, waitFor } from "@testing-library/react";
|
|
4
4
|
describe("useCallTool - onSuccess callback", () => {
|
|
5
5
|
let OpenaiMock;
|
|
6
6
|
beforeEach(() => {
|
|
@@ -101,4 +101,63 @@ describe("useCallTool - onSuccess callback", () => {
|
|
|
101
101
|
});
|
|
102
102
|
});
|
|
103
103
|
});
|
|
104
|
+
describe("useCallTool - TypeScript typing", () => {
|
|
105
|
+
let OpenaiMock;
|
|
106
|
+
beforeEach(() => {
|
|
107
|
+
OpenaiMock = {
|
|
108
|
+
callTool: vi.fn(),
|
|
109
|
+
};
|
|
110
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
111
|
+
});
|
|
112
|
+
afterEach(() => {
|
|
113
|
+
vi.unstubAllGlobals();
|
|
114
|
+
vi.resetAllMocks();
|
|
115
|
+
});
|
|
116
|
+
it("should have correct return types when ToolArgs is null and ToolResponse is specified", () => {
|
|
117
|
+
const { result } = renderHook(() => useCallTool("test-tool"));
|
|
118
|
+
const data = {
|
|
119
|
+
content: [{ type: "text", text: "test" }],
|
|
120
|
+
structuredContent: { result: "test" },
|
|
121
|
+
isError: false,
|
|
122
|
+
result: "test",
|
|
123
|
+
meta: { id: 123 },
|
|
124
|
+
};
|
|
125
|
+
OpenaiMock.callTool.mockResolvedValueOnce(data);
|
|
126
|
+
act(() => {
|
|
127
|
+
result.current.callTool();
|
|
128
|
+
});
|
|
129
|
+
expect(OpenaiMock.callTool).toHaveBeenCalledWith("test-tool", null);
|
|
130
|
+
expectTypeOf(result.current.data);
|
|
131
|
+
});
|
|
132
|
+
it("should correctly type callToolAsync return value", async () => {
|
|
133
|
+
const { result } = renderHook(() => useCallTool("test-tool"));
|
|
134
|
+
const testArgs = { query: "test" };
|
|
135
|
+
const mockResponse = {
|
|
136
|
+
content: [{ type: "text", text: "answer" }],
|
|
137
|
+
structuredContent: { answer: "test answer" },
|
|
138
|
+
isError: false,
|
|
139
|
+
result: "answer",
|
|
140
|
+
meta: {},
|
|
141
|
+
};
|
|
142
|
+
OpenaiMock.callTool.mockResolvedValueOnce(mockResponse);
|
|
143
|
+
const promise = result.current.callToolAsync(testArgs);
|
|
144
|
+
expectTypeOf(promise);
|
|
145
|
+
const resolvedValue = await promise;
|
|
146
|
+
expect(resolvedValue).toEqual(mockResponse);
|
|
147
|
+
});
|
|
148
|
+
it("should correctly type callToolAsync when ToolArgs is null", async () => {
|
|
149
|
+
const { result } = renderHook(() => useCallTool("test-tool"));
|
|
150
|
+
const mockResponse = {
|
|
151
|
+
content: [{ type: "text", text: "data" }],
|
|
152
|
+
structuredContent: { data: "test data" },
|
|
153
|
+
isError: false,
|
|
154
|
+
result: "data",
|
|
155
|
+
};
|
|
156
|
+
OpenaiMock.callTool.mockResolvedValueOnce(mockResponse);
|
|
157
|
+
const promise = result.current.callToolAsync();
|
|
158
|
+
expectTypeOf(promise);
|
|
159
|
+
const resolvedValue = await promise;
|
|
160
|
+
expect(resolvedValue).toEqual(mockResponse);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
104
163
|
//# sourceMappingURL=use-call-tool.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-call-tool.test.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-call-tool.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"use-call-tool.test.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-call-tool.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,MAAM,EACN,EAAE,EACF,EAAE,EAEF,YAAY,GACb,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,QAAQ,CAAC,kCAAkC,EAAE,GAAG,EAAE;IAChD,IAAI,UAA8B,CAAC;IAEnC,UAAU,CAAC,GAAG,EAAE;QACd,UAAU,GAAG;YACX,QAAQ,EAAE,EAAE,CAAC,EAAE,EAAE;SAClB,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,MAAM,QAAQ,GAAG,WAAW,CAAC;IAC7B,MAAM,IAAI,GAAG,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC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M;SACf,CAAC;QAEF,UAAU,CAAC,QAAQ,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC;QAExD,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;QAC/C,YAAY,CAA+B,OAAO,CAAC,CAAC;QAEpD,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC;QACpC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-files.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-files.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ;IACtB,OAAO;QACL,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,UAAU;QAChC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,YAAY;KACrC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { renderHook } from "@testing-library/react";
|
|
3
|
+
import { useFiles } from "./use-files.js";
|
|
4
|
+
describe("useLocale", () => {
|
|
5
|
+
const OpenaiMock = {
|
|
6
|
+
uploadFile: vi.fn(),
|
|
7
|
+
downloadFile: vi.fn(),
|
|
8
|
+
};
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
11
|
+
});
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.unstubAllGlobals();
|
|
14
|
+
vi.resetAllMocks();
|
|
15
|
+
});
|
|
16
|
+
const dummyFile = new File([], "test.txt");
|
|
17
|
+
it("should upload a file to ChatGPT", () => {
|
|
18
|
+
const { result } = renderHook(() => useFiles());
|
|
19
|
+
result.current.upload(dummyFile);
|
|
20
|
+
expect(OpenaiMock.uploadFile).toHaveBeenCalledWith(dummyFile);
|
|
21
|
+
});
|
|
22
|
+
it("should download a file from ChatGPT", () => {
|
|
23
|
+
const fileId = "123";
|
|
24
|
+
const { result } = renderHook(() => useFiles());
|
|
25
|
+
result.current.download({ fileId });
|
|
26
|
+
expect(OpenaiMock.downloadFile).toHaveBeenCalledWith({ fileId });
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=use-files.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-files.test.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-files.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,MAAM,UAAU,GAAG;QACjB,UAAU,EAAE,EAAE,CAAC,EAAE,EAAE;QACnB,YAAY,EAAE,EAAE,CAAC,EAAE,EAAE;KACtB,CAAC;IAEF,UAAU,CAAC,GAAG,EAAE;QACd,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,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;IAE3C,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;QACzC,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEhD,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACjC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAChE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC7C,MAAM,MAAM,GAAG,KAAK,CAAC;QACrB,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEhD,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACpC,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { useSyncExternalStore } from "react";
|
|
2
2
|
import { SET_GLOBALS_EVENT_TYPE, SetGlobalsEvent, } from "../types.js";
|
|
3
3
|
export function useOpenAiGlobal(key) {
|
|
4
|
+
if (!window.openai) {
|
|
5
|
+
console.warn("openai is not defined on window. Please make sure to only call this hook inside the OpenAI iFrame skybridge runtime.");
|
|
6
|
+
}
|
|
4
7
|
return useSyncExternalStore((onChange) => {
|
|
5
8
|
const handleSetGlobal = (event) => {
|
|
6
9
|
const value = event.detail.globals[key];
|
|
7
10
|
if (value === undefined) {
|
|
8
11
|
return;
|
|
9
12
|
}
|
|
10
|
-
console.dir(event.detail.globals, { depth: Infinity });
|
|
11
13
|
onChange();
|
|
12
14
|
};
|
|
13
15
|
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobal, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-openai-global.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-openai-global.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EACL,sBAAsB,EACtB,eAAe,GAEhB,MAAM,aAAa,CAAC;AAErB,MAAM,UAAU,eAAe,CAC7B,GAAM;IAEN,OAAO,oBAAoB,CACzB,CAAC,QAAQ,EAAE,EAAE;QACX,MAAM,eAAe,GAAG,CAAC,KAAsB,EAAE,EAAE;YACjD,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACxC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO;YACT,CAAC;YAED,
|
|
1
|
+
{"version":3,"file":"use-openai-global.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-openai-global.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EACL,sBAAsB,EACtB,eAAe,GAEhB,MAAM,aAAa,CAAC;AAErB,MAAM,UAAU,eAAe,CAC7B,GAAM;IAEN,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QACnB,OAAO,CAAC,IAAI,CACV,sHAAsH,CACvH,CAAC;IACJ,CAAC;IAED,OAAO,oBAAoB,CACzB,CAAC,QAAQ,EAAE,EAAE;QACX,MAAM,eAAe,GAAG,CAAC,KAAsB,EAAE,EAAE;YACjD,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACxC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO;YACT,CAAC;YAED,QAAQ,EAAE,CAAC;QACb,CAAC,CAAC;QAEF,MAAM,CAAC,gBAAgB,CAAC,sBAAsB,EAAE,eAAe,EAAE;YAC/D,OAAO,EAAE,IAAI;SACd,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,mBAAmB,CAAC,sBAAsB,EAAE,eAAe,CAAC,CAAC;QACtE,CAAC,CAAC;IACJ,CAAC,EACD,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAC3B,CAAC;AACJ,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { RequestModalOptions } from "../types.js";
|
|
2
1
|
/**
|
|
3
2
|
* Triggers a modal containing the widget rendered in display mode "modal"
|
|
4
3
|
*/
|
|
5
|
-
export declare function useRequestModal(): (options:
|
|
4
|
+
export declare function useRequestModal(): (options: {
|
|
5
|
+
title: string;
|
|
6
|
+
}) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-request-modal.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-request-modal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-request-modal.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-request-modal.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO,CAAC,OAA0B,EAAE,EAAE;QACpC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC,CAAC;AACJ,CAAC"}
|