skybridge 0.0.0-dev.6554f40 → 0.0.0-dev.6b57ab9
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 +125 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/inferUtilityTypes.d.ts +46 -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 +31 -7
- package/dist/src/server/server.js +9 -1
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/test/utils.d.ts +41 -0
- package/dist/src/test/utils.js +94 -1
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/web/hooks/index.d.ts +14 -0
- package/dist/src/web/hooks/index.js +15 -0
- package/dist/src/web/hooks/index.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.d.ts +62 -0
- package/dist/src/web/{use-call-tool.js → hooks/use-call-tool.js} +28 -4
- package/dist/src/web/hooks/use-call-tool.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.test.js +163 -0
- package/dist/src/web/hooks/use-call-tool.test.js.map +1 -0
- package/dist/src/web/hooks/use-display-mode.d.ts +4 -0
- package/dist/src/web/hooks/use-display-mode.js +7 -0
- package/dist/src/web/hooks/use-display-mode.js.map +1 -0
- package/dist/src/web/hooks/use-display-mode.test.js +40 -0
- package/dist/src/web/hooks/use-display-mode.test.js.map +1 -0
- 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.d.ts +1 -0
- package/dist/src/web/hooks/use-locale.js +5 -0
- package/dist/src/web/hooks/use-locale.js.map +1 -0
- package/dist/src/web/hooks/use-locale.test.d.ts +1 -0
- package/dist/src/web/hooks/use-locale.test.js +21 -0
- package/dist/src/web/hooks/use-locale.test.js.map +1 -0
- package/dist/src/web/hooks/use-open-external.d.ts +1 -0
- package/dist/src/web/hooks/use-open-external.js +6 -0
- package/dist/src/web/hooks/use-open-external.js.map +1 -0
- package/dist/src/web/hooks/use-open-external.test.d.ts +1 -0
- package/dist/src/web/hooks/use-open-external.test.js +24 -0
- package/dist/src/web/hooks/use-open-external.test.js.map +1 -0
- package/dist/src/web/{use-openai-global.d.ts → hooks/use-openai-global.d.ts} +1 -1
- package/dist/src/web/{use-openai-global.js → hooks/use-openai-global.js} +4 -2
- package/dist/src/web/hooks/use-openai-global.js.map +1 -0
- package/dist/src/web/hooks/use-request-modal.d.ts +6 -0
- package/dist/src/web/hooks/use-request-modal.js +9 -0
- package/dist/src/web/hooks/use-request-modal.js.map +1 -0
- package/dist/src/web/hooks/use-request-modal.test.d.ts +1 -0
- package/dist/src/web/hooks/use-request-modal.test.js +24 -0
- package/dist/src/web/hooks/use-request-modal.test.js.map +1 -0
- package/dist/src/web/hooks/use-send-follow-up-message.d.ts +1 -0
- package/dist/src/web/hooks/use-send-follow-up-message.js +11 -0
- package/dist/src/web/hooks/use-send-follow-up-message.js.map +1 -0
- package/dist/src/web/hooks/use-theme.d.ts +1 -0
- package/dist/src/web/hooks/use-theme.js +5 -0
- package/dist/src/web/hooks/use-theme.js.map +1 -0
- package/dist/src/web/hooks/use-theme.test.d.ts +1 -0
- package/dist/src/web/hooks/use-theme.test.js +26 -0
- package/dist/src/web/hooks/use-theme.test.js.map +1 -0
- package/dist/src/web/hooks/use-tool-info.d.ts +25 -0
- package/dist/src/web/hooks/use-tool-info.js +22 -0
- package/dist/src/web/hooks/use-tool-info.js.map +1 -0
- 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.d.ts +1 -0
- package/dist/src/web/hooks/use-tool-info.test.js +59 -0
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -0
- package/dist/src/web/hooks/use-tool-output.d.ts +4 -0
- package/dist/src/web/hooks/use-tool-output.js +9 -0
- package/dist/src/web/hooks/use-tool-output.js.map +1 -0
- package/dist/src/web/hooks/use-tool-response-metadata.d.ts +4 -0
- package/dist/src/web/hooks/use-tool-response-metadata.js +8 -0
- package/dist/src/web/hooks/use-tool-response-metadata.js.map +1 -0
- package/dist/src/web/hooks/use-user-agent.d.ts +1 -0
- package/dist/src/web/hooks/use-user-agent.js +5 -0
- package/dist/src/web/hooks/use-user-agent.js.map +1 -0
- package/dist/src/web/hooks/use-user-agent.test.d.ts +1 -0
- package/dist/src/web/hooks/use-user-agent.test.js +31 -0
- package/dist/src/web/hooks/use-user-agent.test.js.map +1 -0
- package/dist/src/web/{use-widget-state.d.ts → hooks/use-widget-state.d.ts} +1 -2
- package/dist/src/web/{use-widget-state.js → hooks/use-widget-state.js} +3 -3
- package/dist/src/web/hooks/use-widget-state.js.map +1 -0
- package/dist/src/web/hooks/use-widget-state.test.d.ts +1 -0
- package/dist/src/web/hooks/use-widget-state.test.js +60 -0
- package/dist/src/web/hooks/use-widget-state.test.js.map +1 -0
- package/dist/src/web/index.d.ts +2 -5
- package/dist/src/web/index.js +2 -5
- 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 +107 -0
- package/dist/src/web/typed-hooks.js +111 -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 +87 -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 +17 -0
- package/dist/src/web/typed-hooks.test.js.map +1 -0
- package/dist/src/web/types.d.ts +30 -14
- 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/web/use-call-tool.d.ts +0 -54
- package/dist/src/web/use-call-tool.js.map +0 -1
- package/dist/src/web/use-call-tool.test.js +0 -66
- package/dist/src/web/use-call-tool.test.js.map +0 -1
- package/dist/src/web/use-openai-global.js.map +0 -1
- package/dist/src/web/use-tool-output.d.ts +0 -3
- package/dist/src/web/use-tool-output.js +0 -5
- package/dist/src/web/use-tool-output.js.map +0 -1
- package/dist/src/web/use-tool-response-metadata.d.ts +0 -3
- package/dist/src/web/use-tool-response-metadata.js +0 -5
- package/dist/src/web/use-tool-response-metadata.js.map +0 -1
- package/dist/src/web/use-widget-state.js.map +0 -1
- /package/dist/src/web/{use-call-tool.test.d.ts → hooks/use-call-tool.test.d.ts} +0 -0
- /package/dist/src/{test/setup.d.ts → web/hooks/use-display-mode.test.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -97,6 +97,84 @@ 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
|
+
> **Tip:** For the best TypeScript experience, use typed hooks throughout your application. They provide autocomplete, type safety, and better IDE support.
|
|
105
|
+
|
|
106
|
+
_Server setup (server/src/index.ts)_
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { McpServer } from "skybridge/server";
|
|
110
|
+
import { z } from "zod";
|
|
111
|
+
|
|
112
|
+
const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
113
|
+
.widget("search-voyage", {}, {
|
|
114
|
+
description: "Search for trips",
|
|
115
|
+
inputSchema: {
|
|
116
|
+
destination: z.string(),
|
|
117
|
+
departureDate: z.string().optional(),
|
|
118
|
+
},
|
|
119
|
+
outputSchema: {
|
|
120
|
+
results: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
121
|
+
totalCount: z.number(),
|
|
122
|
+
},
|
|
123
|
+
}, async ({ destination }) => {
|
|
124
|
+
// Your tool logic here...
|
|
125
|
+
return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
|
|
126
|
+
})
|
|
127
|
+
.widget("get-details", {}, {
|
|
128
|
+
inputSchema: { tripId: z.string() },
|
|
129
|
+
}, async ({ tripId }) => {
|
|
130
|
+
return { content: [{ type: "text", text: `Details for ${tripId}` }] };
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Export the server type for the client
|
|
134
|
+
export type AppType = typeof server;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
_One-time setup (web/src/skybridge.ts)_
|
|
138
|
+
|
|
139
|
+
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:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import type { AppType } from "../server"; // type-only import
|
|
143
|
+
import { createTypedHooks } from "skybridge/web";
|
|
144
|
+
|
|
145
|
+
export const { useCallTool, useToolInfo } = createTypedHooks<AppType>();
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
_Usage in widgets (web/src/widgets/search.tsx)_
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
import { useCallTool, useToolInfo } from "../skybridge"; // import typed hooks
|
|
152
|
+
|
|
153
|
+
export function SearchWidget() {
|
|
154
|
+
const { callTool, data, isPending } = useCallTool("search-voyage");
|
|
155
|
+
// ^ autocomplete for tool names
|
|
156
|
+
const toolInfo = useToolInfo<"search-voyage">();
|
|
157
|
+
// ^ autocomplete for widget names
|
|
158
|
+
|
|
159
|
+
const handleSearch = () => {
|
|
160
|
+
callTool({ destination: "Spain" });
|
|
161
|
+
// ^ autocomplete for input fields
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div>
|
|
166
|
+
<button onClick={handleSearch} disabled={isPending}>
|
|
167
|
+
Search
|
|
168
|
+
</button>
|
|
169
|
+
{toolInfo.isSuccess && (
|
|
170
|
+
<div>Found {toolInfo.output.totalCount} results</div>
|
|
171
|
+
// ^ typed output
|
|
172
|
+
)}
|
|
173
|
+
</div>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
100
178
|
**Hooks**
|
|
101
179
|
|
|
102
180
|
The `skybridge/web` package comes with a set of hooks to help you build your widgets :
|
|
@@ -104,7 +182,9 @@ The `skybridge/web` package comes with a set of hooks to help you build your wid
|
|
|
104
182
|
- `useOpenAiGlobal`: A generic hook to get any global data from the OpenAI iFrame skybridge runtime (in `window.openai`).
|
|
105
183
|
- `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
184
|
- `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.
|
|
185
|
+
- `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
186
|
- `useCallTool`: A @tanstack/react-query inspired hook to send make additional tool calls inside a widget.
|
|
187
|
+
- `createTypedHooks`: A factory that creates typed versions of `useCallTool` and `useToolInfo` with full type inference from your server type.
|
|
108
188
|
|
|
109
189
|
_useOpenAiGlobal_
|
|
110
190
|
|
|
@@ -130,6 +210,51 @@ import { useToolResponseMetadata } from "skybridge/web";
|
|
|
130
210
|
const toolResponseMetadata = useToolResponseMetadata();
|
|
131
211
|
```
|
|
132
212
|
|
|
213
|
+
_useToolInfo_
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { useToolInfo } from "skybridge/web";
|
|
217
|
+
|
|
218
|
+
const toolInfo = useToolInfo<{
|
|
219
|
+
input: { query: string };
|
|
220
|
+
output: { results: string[] };
|
|
221
|
+
responseMetadata: { id: number };
|
|
222
|
+
}>();
|
|
223
|
+
|
|
224
|
+
// toolInfo.input is typed based on the input type
|
|
225
|
+
// toolInfo.output is typed based on the output type (undefined when pending)
|
|
226
|
+
// toolInfo.status narrows correctly: "pending" | "success"
|
|
227
|
+
|
|
228
|
+
if (toolInfo.isPending) {
|
|
229
|
+
// toolInfo.output is undefined here
|
|
230
|
+
console.log(toolInfo.input.query);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (toolInfo.isSuccess) {
|
|
234
|
+
// toolInfo.output is typed here
|
|
235
|
+
console.log(toolInfo.output.results);
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
_useToolInfo_ with typed hooks (recommended)
|
|
240
|
+
|
|
241
|
+
```tsx
|
|
242
|
+
import { useToolInfo } from "../skybridge"; // import typed hooks
|
|
243
|
+
|
|
244
|
+
export function SearchWidget() {
|
|
245
|
+
const toolInfo = useToolInfo<"search-voyage">();
|
|
246
|
+
// ^ autocomplete for widget names
|
|
247
|
+
// toolInfo.input is typed as { destination: string; departureDate?: string; ... }
|
|
248
|
+
// toolInfo.output is typed as { results: Array<...>; totalCount: number; } | undefined
|
|
249
|
+
|
|
250
|
+
if (toolInfo.isSuccess) {
|
|
251
|
+
return <div>Found {toolInfo.output.totalCount} results</div>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return <div>Searching for {toolInfo.input.destination}...</div>;
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
133
258
|
_useCallTool_ in synchronous mode
|
|
134
259
|
|
|
135
260
|
```ts
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { McpServer, ToolDef } from "./server.js";
|
|
2
|
+
/**
|
|
3
|
+
* Any tool registry shape (includes both widgets and regular tools).
|
|
4
|
+
* Used as a constraint for type parameters that accept tool registries.
|
|
5
|
+
*/
|
|
6
|
+
export type AnyToolRegistry = Record<string, ToolDef>;
|
|
7
|
+
/**
|
|
8
|
+
* Extract the tool registry type from an McpServer instance.
|
|
9
|
+
* This includes both widgets (registered via widget()) and regular tools (registered via registerTool()).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type MyTools = InferTools<MyServer>;
|
|
14
|
+
* // { "search": ToolDef<...>, "calculate": ToolDef<...> }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export type InferTools<T> = T extends McpServer<infer W extends AnyToolRegistry> ? W : T extends McpServer<any> ? never : never;
|
|
18
|
+
/**
|
|
19
|
+
* Get a union of all tool names from an McpServer instance.
|
|
20
|
+
* This includes both widgets and regular tools.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* type Names = ToolNames<MyServer>;
|
|
25
|
+
* // "search" | "calculate" | "details"
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type ToolNames<T> = keyof InferTools<T> & string;
|
|
29
|
+
/**
|
|
30
|
+
* Get the input type for a specific tool (widget or regular tool).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* type SearchInput = ToolInput<MyServer, "search">;
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export type ToolInput<T, K extends ToolNames<T>> = InferTools<T>[K]["input"];
|
|
38
|
+
/**
|
|
39
|
+
* Get the output type for a specific tool (widget or regular tool).
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* type SearchOutput = ToolOutput<MyServer, "search">;
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export type ToolOutput<T, K extends ToolNames<T>> = InferTools<T>[K]["output"];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inferUtilityTypes.js","sourceRoot":"","sources":["../../../src/server/inferUtilityTypes.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +1,37 @@
|
|
|
1
|
-
import { McpServer as McpServerBase, type ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import type { Resource } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
-
import type { ZodRawShape } from "zod";
|
|
1
|
+
import { McpServer as McpServerBase, type ToolCallback, type RegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { Resource, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import type { ZodRawShape, ZodObject, infer as Infer } from "zod";
|
|
4
|
+
export type ToolDef<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<
|
|
9
|
+
type McpServerOriginalToolConfig = Omit<Parameters<McpServerBase["registerTool"]>[1], "inputSchema" | "outputSchema">;
|
|
10
|
+
export declare class McpServer<TTools extends Record<string, ToolDef> = {}> 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<TTools & {
|
|
15
|
+
[K in TName]: ToolDef<Infer<ZodObject<TInput>>, Infer<ZodObject<TOutput>>>;
|
|
16
|
+
}>;
|
|
17
|
+
registerTool<TName extends string, InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape = {}>(name: TName, config: {
|
|
18
|
+
title?: string;
|
|
19
|
+
description?: string;
|
|
8
20
|
inputSchema?: InputArgs;
|
|
9
21
|
outputSchema?: OutputArgs;
|
|
10
|
-
|
|
22
|
+
annotations?: ToolAnnotations;
|
|
23
|
+
_meta?: Record<string, unknown>;
|
|
24
|
+
}, cb: ToolCallback<InputArgs>): McpServer<TTools & {
|
|
25
|
+
[K in TName]: ToolDef<InputArgs extends ZodRawShape ? Infer<ZodObject<InputArgs>> : unknown, OutputArgs extends ZodRawShape ? Infer<ZodObject<OutputArgs>> : unknown>;
|
|
26
|
+
}>;
|
|
27
|
+
registerTool<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape = {}>(name: string, config: {
|
|
28
|
+
title?: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
inputSchema?: InputArgs;
|
|
31
|
+
outputSchema?: OutputArgs;
|
|
32
|
+
annotations?: ToolAnnotations;
|
|
33
|
+
_meta?: Record<string, unknown>;
|
|
34
|
+
}, cb: ToolCallback<InputArgs>): RegisteredTool;
|
|
11
35
|
private lookupDistFile;
|
|
12
36
|
}
|
|
13
37
|
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,17 @@ 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;
|
|
52
|
+
}
|
|
53
|
+
registerTool(name, config, cb) {
|
|
54
|
+
super.registerTool(name, config, cb);
|
|
55
|
+
return this;
|
|
48
56
|
}
|
|
49
57
|
lookupDistFile(key) {
|
|
50
58
|
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,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,aAAa,GAG3B,MAAM,yCAAyC,CAAC;AAMjD,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,IAAI,EAAE,CAAC;SAChC,CAAC;QACF,IAAI,UAAU,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;YACzC,gBAAgB,CAAC,0BAA0B,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC;QACxE,CAAC;QAED,IAAI,CAAC,QAAQ,CACX,IAAI,EACJ,GAAG,EACH;YACE,GAAG,cAAc;YACjB,KAAK,EAAE,gBAAgB;SACxB,EACD,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;YACpB,MAAM,SAAS,GACb,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;g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AAG,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.d.ts
CHANGED
|
@@ -8,6 +8,47 @@ export declare function createMockMcpServer(): {
|
|
|
8
8
|
mockResource: MockInstance<McpServer["resource"]>;
|
|
9
9
|
mockRegisterTool: MockInstance<McpServer["registerTool"]>;
|
|
10
10
|
};
|
|
11
|
+
export declare function createTestServer(): McpServer<{
|
|
12
|
+
"search-voyage": import("../server/server.js").ToolDef<{
|
|
13
|
+
destination: string;
|
|
14
|
+
departureDate?: string | undefined;
|
|
15
|
+
maxPrice?: number | undefined;
|
|
16
|
+
}, {
|
|
17
|
+
results: {
|
|
18
|
+
name: string;
|
|
19
|
+
id: string;
|
|
20
|
+
price: number;
|
|
21
|
+
}[];
|
|
22
|
+
totalCount: number;
|
|
23
|
+
}>;
|
|
24
|
+
} & {
|
|
25
|
+
"get-trip-details": import("../server/server.js").ToolDef<{
|
|
26
|
+
tripId: string;
|
|
27
|
+
}, {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
images: string[];
|
|
31
|
+
}>;
|
|
32
|
+
} & {
|
|
33
|
+
"no-input-widget": import("../server/server.js").ToolDef<{}, {}>;
|
|
34
|
+
} & {
|
|
35
|
+
"calculate-price": import("../server/server.js").ToolDef<{
|
|
36
|
+
tripId: string;
|
|
37
|
+
passengers: number;
|
|
38
|
+
}, {
|
|
39
|
+
totalPrice: number;
|
|
40
|
+
currency: string;
|
|
41
|
+
}>;
|
|
42
|
+
}>;
|
|
43
|
+
export declare function createMinimalTestServer(): McpServer<{
|
|
44
|
+
"search-voyage": import("../server/server.js").ToolDef<{
|
|
45
|
+
destination: string;
|
|
46
|
+
}, {
|
|
47
|
+
results: {
|
|
48
|
+
id: string;
|
|
49
|
+
}[];
|
|
50
|
+
}>;
|
|
51
|
+
}>;
|
|
11
52
|
/**
|
|
12
53
|
* Mock extra parameter for resource callback
|
|
13
54
|
*/
|
package/dist/src/test/utils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { vi } from "vitest";
|
|
2
|
-
import { McpServer
|
|
2
|
+
import { McpServer } from "../server/server.js";
|
|
3
|
+
import { z } from "zod";
|
|
3
4
|
/**
|
|
4
5
|
* Creates a real McpServer instance for testing
|
|
5
6
|
*/
|
|
@@ -18,6 +19,98 @@ export function createMockMcpServer() {
|
|
|
18
19
|
mockRegisterTool,
|
|
19
20
|
};
|
|
20
21
|
}
|
|
22
|
+
export function createTestServer() {
|
|
23
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {})
|
|
24
|
+
.widget("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
|
+
.widget("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
|
+
.widget("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
|
+
.registerTool("calculate-price", {
|
|
79
|
+
description: "Calculate trip price",
|
|
80
|
+
inputSchema: {
|
|
81
|
+
tripId: z.string(),
|
|
82
|
+
passengers: z.number(),
|
|
83
|
+
},
|
|
84
|
+
outputSchema: {
|
|
85
|
+
totalPrice: z.number(),
|
|
86
|
+
currency: z.string(),
|
|
87
|
+
},
|
|
88
|
+
}, async ({ tripId, passengers }) => {
|
|
89
|
+
return {
|
|
90
|
+
content: [{ type: "text", text: `Price for ${tripId}` }],
|
|
91
|
+
structuredContent: {
|
|
92
|
+
totalPrice: 1000 * passengers,
|
|
93
|
+
currency: "USD",
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
export function createMinimalTestServer() {
|
|
99
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {}).widget("search-voyage", {}, {
|
|
100
|
+
description: "Search for voyages",
|
|
101
|
+
inputSchema: {
|
|
102
|
+
destination: z.string(),
|
|
103
|
+
},
|
|
104
|
+
outputSchema: {
|
|
105
|
+
results: z.array(z.object({ id: z.string() })),
|
|
106
|
+
},
|
|
107
|
+
}, async ({ destination }) => {
|
|
108
|
+
return {
|
|
109
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
110
|
+
structuredContent: { results: [{ id: "1" }] },
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
21
114
|
/**
|
|
22
115
|
* Mock extra parameter for resource callback
|
|
23
116
|
*/
|
|
@@ -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;AAChD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;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,MAAM,UAAU,gBAAgB;IAC9B,OAAO,IAAI,SAAS,CAClB,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,EACtC,EAAE,CACH;SACE,MAAM,CACL,eAAe,EACf,EAAE,EACF;QACE,WAAW,EAAE,oBAAoB;QACjC,WAAW,E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AE,oBAAoB;QACjC,WAAW,EAAE;YACX,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;SACxB;QACD,YAAY,EAAE;YACZ,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;SAC/C;KACF,EACD,KAAK,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE;QACxB,OAAO;YACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB,WAAW,EAAE,EAAE,CAAC;YAClE,iBAAiB,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE;SAC9C,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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { useCallTool } from "./use-call-tool.js";
|
|
2
|
+
export { useDisplayMode } from "./use-display-mode.js";
|
|
3
|
+
export { useFiles } from "./use-files.js";
|
|
4
|
+
export { useLocale } from "./use-locale.js";
|
|
5
|
+
export { useOpenAiGlobal } from "./use-openai-global.js";
|
|
6
|
+
export { useOpenExternal } from "./use-open-external.js";
|
|
7
|
+
export { useRequestModal } from "./use-request-modal.js";
|
|
8
|
+
export { useSendFollowUpMessage } from "./use-send-follow-up-message.js";
|
|
9
|
+
export { useTheme } from "./use-theme.js";
|
|
10
|
+
export { useToolInfo } from "./use-tool-info.js";
|
|
11
|
+
export { useToolOutput } from "./use-tool-output.js";
|
|
12
|
+
export { useToolResponseMetadata } from "./use-tool-response-metadata.js";
|
|
13
|
+
export { useUserAgent } from "./use-user-agent.js";
|
|
14
|
+
export { useWidgetState } from "./use-widget-state.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { useCallTool } from "./use-call-tool.js";
|
|
2
|
+
export { useDisplayMode } from "./use-display-mode.js";
|
|
3
|
+
export { useFiles } from "./use-files.js";
|
|
4
|
+
export { useLocale } from "./use-locale.js";
|
|
5
|
+
export { useOpenAiGlobal } from "./use-openai-global.js";
|
|
6
|
+
export { useOpenExternal } from "./use-open-external.js";
|
|
7
|
+
export { useRequestModal } from "./use-request-modal.js";
|
|
8
|
+
export { useSendFollowUpMessage } from "./use-send-follow-up-message.js";
|
|
9
|
+
export { useTheme } from "./use-theme.js";
|
|
10
|
+
export { useToolInfo } from "./use-tool-info.js";
|
|
11
|
+
export { useToolOutput } from "./use-tool-output.js";
|
|
12
|
+
export { useToolResponseMetadata } from "./use-tool-response-metadata.js";
|
|
13
|
+
export { useUserAgent } from "./use-user-agent.js";
|
|
14
|
+
export { useWidgetState } from "./use-widget-state.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,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"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { CallToolArgs, CallToolResponse } from "../types.js";
|
|
2
|
+
type SideEffects<ToolArgs, ToolResponse> = {
|
|
3
|
+
onSuccess?: (data: ToolResponse, toolArgs: ToolArgs) => void;
|
|
4
|
+
onError?: (error: unknown, toolArgs: ToolArgs) => void;
|
|
5
|
+
onSettled?: (data: ToolResponse | undefined, error: unknown | undefined, toolArgs: ToolArgs) => void;
|
|
6
|
+
};
|
|
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
|
+
callTool: {
|
|
11
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
12
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
13
|
+
};
|
|
14
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
15
|
+
status: "idle";
|
|
16
|
+
isIdle: true;
|
|
17
|
+
isPending: false;
|
|
18
|
+
isSuccess: false;
|
|
19
|
+
isError: false;
|
|
20
|
+
data: undefined;
|
|
21
|
+
error: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
callTool: {
|
|
24
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
25
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
26
|
+
};
|
|
27
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
28
|
+
status: "pending";
|
|
29
|
+
isIdle: false;
|
|
30
|
+
isPending: true;
|
|
31
|
+
isSuccess: false;
|
|
32
|
+
isError: false;
|
|
33
|
+
data: undefined;
|
|
34
|
+
error: undefined;
|
|
35
|
+
} | {
|
|
36
|
+
callTool: {
|
|
37
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
38
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
39
|
+
};
|
|
40
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
41
|
+
status: "error";
|
|
42
|
+
isIdle: false;
|
|
43
|
+
isPending: false;
|
|
44
|
+
isSuccess: false;
|
|
45
|
+
isError: true;
|
|
46
|
+
data: undefined;
|
|
47
|
+
error: unknown;
|
|
48
|
+
} | {
|
|
49
|
+
callTool: {
|
|
50
|
+
(sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
51
|
+
(toolArgs: ToolArgs, sideEffects?: SideEffects<ToolArgs, CallToolResponse & ToolResponse>): void;
|
|
52
|
+
};
|
|
53
|
+
callToolAsync: CallToolAsyncFn<ToolArgs, CallToolResponse & ToolResponse>;
|
|
54
|
+
status: "success";
|
|
55
|
+
isIdle: false;
|
|
56
|
+
isPending: false;
|
|
57
|
+
isSuccess: true;
|
|
58
|
+
isError: false;
|
|
59
|
+
data: CallToolResponse & ToolResponse;
|
|
60
|
+
error: undefined;
|
|
61
|
+
};
|
|
62
|
+
export {};
|
|
@@ -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);
|
|
@@ -13,19 +13,43 @@ export const useCallTool = (name) => {
|
|
|
13
13
|
throw error;
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
|
-
const
|
|
17
|
-
|
|
16
|
+
const callToolAsync = (async (toolArgs) => {
|
|
17
|
+
if (toolArgs === undefined) {
|
|
18
|
+
return execute(null);
|
|
19
|
+
}
|
|
20
|
+
return execute(toolArgs);
|
|
21
|
+
});
|
|
22
|
+
function callTool(firstArg, sideEffects) {
|
|
23
|
+
let toolArgs;
|
|
24
|
+
if (firstArg &&
|
|
25
|
+
typeof firstArg === "object" &&
|
|
26
|
+
("onSuccess" in firstArg ||
|
|
27
|
+
"onError" in firstArg ||
|
|
28
|
+
"onSettled" in firstArg)) {
|
|
29
|
+
toolArgs = null; // no toolArgs provided
|
|
30
|
+
sideEffects = firstArg;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
toolArgs = (firstArg === undefined ? null : firstArg);
|
|
34
|
+
}
|
|
35
|
+
execute(toolArgs)
|
|
18
36
|
.then((data) => {
|
|
19
37
|
if (sideEffects?.onSuccess) {
|
|
20
38
|
sideEffects.onSuccess(data, toolArgs);
|
|
21
39
|
}
|
|
40
|
+
if (sideEffects?.onSettled) {
|
|
41
|
+
sideEffects.onSettled(data, undefined, toolArgs);
|
|
42
|
+
}
|
|
22
43
|
})
|
|
23
44
|
.catch((error) => {
|
|
24
45
|
if (sideEffects?.onError) {
|
|
25
46
|
sideEffects.onError(error, toolArgs);
|
|
26
47
|
}
|
|
48
|
+
if (sideEffects?.onSettled) {
|
|
49
|
+
sideEffects.onSettled(undefined, error, toolArgs);
|
|
50
|
+
}
|
|
27
51
|
});
|
|
28
|
-
}
|
|
52
|
+
}
|
|
29
53
|
const callToolState = {
|
|
30
54
|
status,
|
|
31
55
|
data,
|
|
@@ -0,0 +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;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,CAAC;YAEhE,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,gBAAgB,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAA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