skybridge 0.0.0-dev.ef13340 → 0.0.0-dev.f1722d8
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 +247 -2
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/inferUtilityTypes.d.ts +54 -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 +45 -9
- package/dist/src/server/server.js +24 -7
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/server/templateHelper.d.ts +9 -8
- package/dist/src/server/templateHelper.js +0 -8
- package/dist/src/server/templateHelper.js.map +1 -1
- package/dist/src/server/templates/production.hbs +2 -2
- package/dist/src/test/utils.d.ts +61 -0
- package/dist/src/test/utils.js +122 -1
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/test/widget.test.js +23 -2
- package/dist/src/test/widget.test.js.map +1 -1
- package/dist/src/web/create-store.d.ts +3 -0
- package/dist/src/web/create-store.js +103 -0
- package/dist/src/web/create-store.js.map +1 -0
- package/dist/src/web/data-llm.d.ts +13 -0
- package/dist/src/web/data-llm.js +67 -0
- package/dist/src/web/data-llm.js.map +1 -0
- package/dist/src/web/data-llm.test.js +76 -0
- package/dist/src/web/data-llm.test.js.map +1 -0
- package/dist/src/web/generate-helpers.d.ts +113 -0
- package/dist/src/web/generate-helpers.js +109 -0
- package/dist/src/web/generate-helpers.js.map +1 -0
- package/dist/src/web/generate-helpers.test-d.d.ts +1 -0
- package/dist/src/web/generate-helpers.test-d.js +153 -0
- package/dist/src/web/generate-helpers.test-d.js.map +1 -0
- package/dist/src/web/generate-helpers.test.d.ts +1 -0
- package/dist/src/web/generate-helpers.test.js +17 -0
- package/dist/src/web/generate-helpers.test.js.map +1 -0
- package/dist/src/web/hooks/index.d.ts +12 -0
- package/dist/src/web/hooks/index.js +13 -0
- package/dist/src/web/hooks/index.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.d.ts +100 -0
- package/dist/src/web/hooks/use-call-tool.js +60 -0
- package/dist/src/web/hooks/use-call-tool.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.test-d.d.ts +1 -0
- package/dist/src/web/hooks/use-call-tool.test-d.js +104 -0
- package/dist/src/web/hooks/use-call-tool.test-d.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.test.d.ts +1 -0
- package/dist/src/web/hooks/use-call-tool.test.js +171 -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.d.ts +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-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/hooks/use-widget-state.d.ts +4 -0
- package/dist/src/web/hooks/use-widget-state.js +54 -0
- 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 +5 -3
- package/dist/src/web/index.js +5 -3
- package/dist/src/web/index.js.map +1 -1
- package/dist/src/web/mount-widget.js +5 -0
- package/dist/src/web/mount-widget.js.map +1 -1
- package/dist/src/web/plugin/data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/data-llm.test.js +81 -0
- package/dist/src/web/plugin/data-llm.test.js.map +1 -0
- package/dist/src/web/{plugin.js → plugin/plugin.js} +7 -4
- package/dist/src/web/plugin/plugin.js.map +1 -0
- package/dist/src/web/plugin/transform-data-llm.d.ts +12 -0
- package/dist/src/web/plugin/transform-data-llm.js +93 -0
- package/dist/src/web/plugin/transform-data-llm.js.map +1 -0
- package/dist/src/web/plugin/transform-data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/transform-data-llm.test.js +81 -0
- package/dist/src/web/plugin/transform-data-llm.test.js.map +1 -0
- package/dist/src/web/proxy.d.ts +1 -0
- package/dist/src/web/proxy.js +48 -0
- package/dist/src/web/proxy.js.map +1 -0
- package/dist/src/web/types.d.ts +53 -15
- package/dist/src/web/types.js +1 -0
- 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 +13 -3
- package/dist/src/test/setup.js +0 -9
- package/dist/src/test/setup.js.map +0 -1
- package/dist/src/web/plugin.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/{test/setup.d.ts → web/data-llm.test.d.ts} +0 -0
- /package/dist/src/web/{plugin.d.ts → plugin/plugin.d.ts} +0 -0
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,126 @@ 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
|
+
.widget("inferred-output-widget", {}, {
|
|
79
|
+
description: "Widget with output inferred from callback",
|
|
80
|
+
inputSchema: {
|
|
81
|
+
query: z.string(),
|
|
82
|
+
},
|
|
83
|
+
}, async ({ query }) => {
|
|
84
|
+
return {
|
|
85
|
+
content: [{ type: "text", text: `Query: ${query}` }],
|
|
86
|
+
structuredContent: {
|
|
87
|
+
inferredResults: [{ id: "inferred-1", score: 0.95 }],
|
|
88
|
+
inferredCount: 1,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
})
|
|
92
|
+
.registerTool("calculate-price", {
|
|
93
|
+
description: "Calculate trip price",
|
|
94
|
+
inputSchema: {
|
|
95
|
+
tripId: z.string(),
|
|
96
|
+
passengers: z.number(),
|
|
97
|
+
},
|
|
98
|
+
outputSchema: {
|
|
99
|
+
totalPrice: z.number(),
|
|
100
|
+
currency: z.string(),
|
|
101
|
+
},
|
|
102
|
+
}, async ({ tripId, passengers }) => {
|
|
103
|
+
return {
|
|
104
|
+
content: [{ type: "text", text: `Price for ${tripId}` }],
|
|
105
|
+
structuredContent: {
|
|
106
|
+
totalPrice: 1000 * passengers,
|
|
107
|
+
currency: "USD",
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
})
|
|
111
|
+
.registerTool("inferred-tool", {
|
|
112
|
+
description: "Tool with output inferred from callback",
|
|
113
|
+
inputSchema: {
|
|
114
|
+
itemId: z.string(),
|
|
115
|
+
},
|
|
116
|
+
}, async ({ itemId }) => {
|
|
117
|
+
return {
|
|
118
|
+
content: [{ type: "text", text: `Item: ${itemId}` }],
|
|
119
|
+
structuredContent: {
|
|
120
|
+
itemDetails: { name: "Inferred Item", available: true },
|
|
121
|
+
fetchedAt: "2024-01-01",
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
export function createMinimalTestServer() {
|
|
127
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {}).widget("search-voyage", {}, {
|
|
128
|
+
description: "Search for voyages",
|
|
129
|
+
inputSchema: {
|
|
130
|
+
destination: z.string(),
|
|
131
|
+
},
|
|
132
|
+
outputSchema: {
|
|
133
|
+
results: z.array(z.object({ id: z.string() })),
|
|
134
|
+
},
|
|
135
|
+
}, async ({ destination }) => {
|
|
136
|
+
return {
|
|
137
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
138
|
+
structuredContent: { results: [{ id: "1" }] },
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
21
142
|
/**
|
|
22
143
|
* Mock extra parameter for resource callback
|
|
23
144
|
*/
|
|
@@ -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,MA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, describe, expect, it, vi, } from "vitest";
|
|
2
3
|
import { McpServer } from "../server/server.js";
|
|
3
|
-
import { createMockMcpServer,
|
|
4
|
+
import { createMockExtra, createMockMcpServer, resetTestEnv, setTestEnv, } from "./utils.js";
|
|
5
|
+
const mockManifest = {
|
|
6
|
+
"src/widgets/my-widget.tsx": { file: "my-widget.js" },
|
|
7
|
+
"style.css": { file: "style.css" },
|
|
8
|
+
};
|
|
9
|
+
vi.mock("node:fs", async () => {
|
|
10
|
+
const actual = await vi.importActual("node:fs");
|
|
11
|
+
const readFileSync = vi.fn((path, ...args) => {
|
|
12
|
+
if (typeof path === "string" && path.includes("manifest.json")) {
|
|
13
|
+
return JSON.stringify(mockManifest);
|
|
14
|
+
}
|
|
15
|
+
return actual.readFileSync(path, ...args);
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
readFileSync,
|
|
19
|
+
default: {
|
|
20
|
+
readFileSync,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
});
|
|
4
24
|
describe("McpServer.widget", () => {
|
|
5
25
|
let server;
|
|
6
26
|
let mockResource;
|
|
7
27
|
let mockRegisterTool;
|
|
28
|
+
let readFileSyncSpy = null;
|
|
8
29
|
beforeEach(() => {
|
|
9
30
|
({ server, mockResource, mockRegisterTool } = createMockMcpServer());
|
|
10
31
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"widget.test.js","sourceRoot":"","sources":["../../../src/test/widget.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,
|
|
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,EACF,EAAE,GAEH,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC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gB,CAC7B,CAAC;QACF,MAAM,CAAC,MAAM,CAAC,QAAQ,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"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type StateCreator } from "zustand";
|
|
2
|
+
import type { UnknownObject } from "./types.js";
|
|
3
|
+
export declare function createStore<T extends UnknownObject>(storeCreator: StateCreator<T, [], [], T>, defaultState?: T | (() => T)): import("zustand").UseBoundStore<import("zustand").StoreApi<T>>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
import { SET_GLOBALS_EVENT_TYPE } from "./types.js";
|
|
3
|
+
const WIDGET_CONTEXT_KEY = "__widget_context";
|
|
4
|
+
function filterWidgetContext(state) {
|
|
5
|
+
if (state === null) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const { [WIDGET_CONTEXT_KEY]: _, ...filteredState } = state;
|
|
9
|
+
return filteredState;
|
|
10
|
+
}
|
|
11
|
+
function serializeState(value) {
|
|
12
|
+
// Skip functions (methods)
|
|
13
|
+
if (typeof value === "function") {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
// Handle arrays
|
|
17
|
+
if (Array.isArray(value)) {
|
|
18
|
+
return value
|
|
19
|
+
.map((item) => serializeState(item))
|
|
20
|
+
.filter((item) => item !== undefined);
|
|
21
|
+
}
|
|
22
|
+
// Handle objects
|
|
23
|
+
if (value !== null && typeof value === "object") {
|
|
24
|
+
const serialized = {};
|
|
25
|
+
for (const [key, val] of Object.entries(value)) {
|
|
26
|
+
const serializedValue = serializeState(val);
|
|
27
|
+
if (serializedValue !== undefined) {
|
|
28
|
+
serialized[key] = serializedValue;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return serialized;
|
|
32
|
+
}
|
|
33
|
+
// Primitive values
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
function preserveWidgetContext(newState) {
|
|
37
|
+
if (newState === null) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const currentWindowState = window.openai?.widgetState;
|
|
41
|
+
if (currentWindowState !== null &&
|
|
42
|
+
currentWindowState !== undefined &&
|
|
43
|
+
WIDGET_CONTEXT_KEY in currentWindowState) {
|
|
44
|
+
return {
|
|
45
|
+
...newState,
|
|
46
|
+
[WIDGET_CONTEXT_KEY]: currentWindowState[WIDGET_CONTEXT_KEY],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return newState;
|
|
50
|
+
}
|
|
51
|
+
function getInitialState(defaultState) {
|
|
52
|
+
const widgetStateFromWindow = window.openai?.widgetState;
|
|
53
|
+
if (widgetStateFromWindow !== null && widgetStateFromWindow !== undefined) {
|
|
54
|
+
return filterWidgetContext(widgetStateFromWindow);
|
|
55
|
+
}
|
|
56
|
+
return typeof defaultState === "function"
|
|
57
|
+
? defaultState()
|
|
58
|
+
: defaultState ?? null;
|
|
59
|
+
}
|
|
60
|
+
export function createStore(storeCreator, defaultState) {
|
|
61
|
+
const initialState = getInitialState(defaultState);
|
|
62
|
+
const store = create()((...args) => {
|
|
63
|
+
const baseStore = storeCreator(...args);
|
|
64
|
+
// Merge with initial state from window.openai.widgetState or defaultState
|
|
65
|
+
if (initialState !== null) {
|
|
66
|
+
return { ...baseStore, ...initialState };
|
|
67
|
+
}
|
|
68
|
+
return baseStore;
|
|
69
|
+
});
|
|
70
|
+
// Subscribe to store changes and persist to window.openai.setWidgetState
|
|
71
|
+
let isInternalUpdate = false;
|
|
72
|
+
store.subscribe((state) => {
|
|
73
|
+
if (!isInternalUpdate && window.openai) {
|
|
74
|
+
// Only serialize the state data, not the methods
|
|
75
|
+
const serializedState = serializeState(state);
|
|
76
|
+
if (serializedState !== null && serializedState !== undefined) {
|
|
77
|
+
const stateToPersist = preserveWidgetContext(serializedState);
|
|
78
|
+
if (stateToPersist !== null) {
|
|
79
|
+
window.openai.setWidgetState(stateToPersist);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
// Listen to external widgetState changes and update the store
|
|
85
|
+
if (typeof window !== "undefined") {
|
|
86
|
+
const handleSetGlobals = (event) => {
|
|
87
|
+
const widgetState = event.detail.globals.widgetState;
|
|
88
|
+
if (widgetState !== undefined) {
|
|
89
|
+
const filteredState = filterWidgetContext(widgetState);
|
|
90
|
+
if (filteredState !== null) {
|
|
91
|
+
isInternalUpdate = true;
|
|
92
|
+
store.setState(filteredState);
|
|
93
|
+
isInternalUpdate = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobals, {
|
|
98
|
+
passive: true,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return store;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=create-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-store.js","sourceRoot":"","sources":["../../../src/web/create-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAqB,MAAM,SAAS,CAAC;AAEpD,OAAO,EAAE,sBAAsB,EAAwB,MAAM,YAAY,CAAC;AAE1E,MAAM,kBAAkB,GAAG,kBAA2B,CAAC;AAEvD,SAAS,mBAAmB,CAC1B,KAAe;IAEf,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,IA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Ae,KAAK,IAAI,IAAI,eAAe,KAAK,SAAS,EAAE,CAAC;gBAC9D,MAAM,cAAc,GAAG,qBAAqB,CAAC,eAAoB,CAAC,CAAC;gBACnE,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,8DAA8D;IAC9D,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,MAAM,gBAAgB,GAAG,CAAC,KAAsB,EAAE,EAAE;YAClD,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC;YACrD,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;gBAC9B,MAAM,aAAa,GAAG,mBAAmB,CAAC,WAAuB,CAAC,CAAC;gBACnE,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;oBAC3B,gBAAgB,GAAG,IAAI,CAAC;oBACxB,KAAK,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;oBAC9B,gBAAgB,GAAG,KAAK,CAAC;gBAC3B,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QAEF,MAAM,CAAC,gBAAgB,CAAC,sBAAsB,EAAE,gBAAgB,EAAE;YAChE,OAAO,EAAE,IAAI;SACd,CAAC,CAAC;IACL,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export type DataLLMContent = string;
|
|
3
|
+
export interface DataLLMNode {
|
|
4
|
+
id: string;
|
|
5
|
+
parentId: string | null;
|
|
6
|
+
content: string | null;
|
|
7
|
+
}
|
|
8
|
+
interface DataLLMProps {
|
|
9
|
+
content: DataLLMContent | null | undefined;
|
|
10
|
+
children?: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export declare function DataLLM({ content, children }: DataLLMProps): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useEffect, useId, } from "react";
|
|
3
|
+
const nodes = new Map();
|
|
4
|
+
function setNode(node) {
|
|
5
|
+
nodes.set(node.id, node);
|
|
6
|
+
onChange();
|
|
7
|
+
}
|
|
8
|
+
function removeNode(id) {
|
|
9
|
+
nodes.delete(id);
|
|
10
|
+
onChange();
|
|
11
|
+
}
|
|
12
|
+
function onChange() {
|
|
13
|
+
const description = getLLMDescriptionString();
|
|
14
|
+
window.openai.setWidgetState({
|
|
15
|
+
...window.openai.widgetState,
|
|
16
|
+
__widget_context: description,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const ParentIdContext = createContext(null);
|
|
20
|
+
export function DataLLM({ content, children }) {
|
|
21
|
+
const parentId = useContext(ParentIdContext);
|
|
22
|
+
const id = useId();
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (content) {
|
|
25
|
+
setNode({
|
|
26
|
+
id,
|
|
27
|
+
parentId,
|
|
28
|
+
content,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
removeNode(id);
|
|
33
|
+
}
|
|
34
|
+
return () => {
|
|
35
|
+
removeNode(id);
|
|
36
|
+
};
|
|
37
|
+
}, [id, parentId, content]);
|
|
38
|
+
return (_jsx(ParentIdContext.Provider, { value: id, children: children }));
|
|
39
|
+
}
|
|
40
|
+
function getLLMDescriptionString() {
|
|
41
|
+
const byParent = new Map();
|
|
42
|
+
for (const node of Array.from(nodes.values())) {
|
|
43
|
+
const key = node.parentId ?? null;
|
|
44
|
+
if (!byParent.has(key))
|
|
45
|
+
byParent.set(key, []);
|
|
46
|
+
byParent.get(key).push(node);
|
|
47
|
+
}
|
|
48
|
+
for (const list of byParent.values()) {
|
|
49
|
+
list.sort((a, b) => a.id.localeCompare(b.id));
|
|
50
|
+
}
|
|
51
|
+
const lines = [];
|
|
52
|
+
function traverseTree(parentId, depth) {
|
|
53
|
+
const children = byParent.get(parentId);
|
|
54
|
+
if (!children)
|
|
55
|
+
return;
|
|
56
|
+
for (const child of children) {
|
|
57
|
+
if (child.content && child.content.trim()) {
|
|
58
|
+
const indent = " ".repeat(depth);
|
|
59
|
+
lines.push(`${indent}- ${child.content.trim()}`);
|
|
60
|
+
}
|
|
61
|
+
traverseTree(child.id, depth + 1);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
traverseTree(null, 0);
|
|
65
|
+
return lines.join("\n");
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=data-llm.js.map
|
|
@@ -0,0 +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,KAAK,GAAG,IAAI,GAAG,EAAuB,CAAC;AAE7C,SAAS,OAAO,CAAC,IAAiB;IAChC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IACzB,QAAQ,EAAE,CAAC;AACb,CAAC;AAED,SAAS,UAAU,CAAC,EAAU;IAC5B,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACjB,QAAQ,EAAE,CAAC;AACb,CAAC;AAED,SAAS,QAAQ;IACf,MAAM,WAAW,GAAG,uBAAuB,EAAE,CAAC;IAC9C,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC;QAC3B,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW;QAC5B,gBAAgB,EAAE,WAAW;KAC9B,CAAC,CAAC;AACL,CAAC;AAED,MAAM,eAAe,GAAG,aAAa,CAAgB,IAAI,CAAC,CAAC;AAO3D,MAAM,UAAU,OAAO,CAAC,EAAE,OAAO,E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IAChD,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,SAAS,YAAY,CAAC,QAAuB,EAAE,KAAa;QAC1D,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ;YAAE,OAAO;QAEtB,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC7B,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;gBAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAClC,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,KAAK,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YACnD,CAAC;YACD,YAAY,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IAED,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAEtB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi, } from "vitest";
|
|
4
|
+
import { DataLLM } from "./data-llm.js";
|
|
5
|
+
describe("DataLLM", () => {
|
|
6
|
+
let OpenaiMock;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
OpenaiMock = {
|
|
9
|
+
widgetState: {},
|
|
10
|
+
setWidgetState: vi.fn(),
|
|
11
|
+
};
|
|
12
|
+
// Use Object.defineProperty to ensure it persists
|
|
13
|
+
Object.defineProperty(globalThis, "openai", {
|
|
14
|
+
value: OpenaiMock,
|
|
15
|
+
writable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
});
|
|
18
|
+
// Also set on window for browser-like environment
|
|
19
|
+
if (typeof window !== "undefined") {
|
|
20
|
+
Object.defineProperty(window, "openai", {
|
|
21
|
+
value: OpenaiMock,
|
|
22
|
+
writable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
27
|
+
});
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
vi.resetAllMocks();
|
|
30
|
+
// Keep the mock available for React cleanup, but reset it
|
|
31
|
+
if (typeof window !== "undefined" && window.openai) {
|
|
32
|
+
window.openai.setWidgetState = vi.fn();
|
|
33
|
+
window.openai.widgetState = {};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
it("should register a node with content and call setWidgetState", () => {
|
|
37
|
+
render(_jsx(DataLLM, { content: "Test content", children: _jsx("div", { children: "Child" }) }));
|
|
38
|
+
expect(OpenaiMock.setWidgetState).toHaveBeenCalled();
|
|
39
|
+
const callArgs = OpenaiMock.setWidgetState.mock.calls[0]?.[0];
|
|
40
|
+
expect(callArgs).toHaveProperty("__widget_context");
|
|
41
|
+
expect(callArgs?.__widget_context).toContain("- Test content");
|
|
42
|
+
});
|
|
43
|
+
it("should preserve existing widgetState when updating context", () => {
|
|
44
|
+
OpenaiMock.widgetState = { existingKey: "existingValue" };
|
|
45
|
+
render(_jsx(DataLLM, { content: "Test content", children: _jsx("div", { children: "Child" }) }));
|
|
46
|
+
const callArgs = OpenaiMock.setWidgetState.mock.calls[0]?.[0];
|
|
47
|
+
expect(callArgs).toHaveProperty("existingKey", "existingValue");
|
|
48
|
+
expect(callArgs).toHaveProperty("__widget_context");
|
|
49
|
+
});
|
|
50
|
+
it("should handle deeply nested DataLLM components", () => {
|
|
51
|
+
render(_jsxs(DataLLM, { content: "Level 1", children: [_jsx(DataLLM, { content: "Level 2A" }), _jsx(DataLLM, { content: "Level 2B", children: _jsx(DataLLM, { content: "Level 3", children: _jsx("div", { children: "Content" }) }) })] }));
|
|
52
|
+
const callArgs = OpenaiMock.setWidgetState.mock.calls[OpenaiMock.setWidgetState.mock.calls.length - 1]?.[0];
|
|
53
|
+
const context = callArgs?.__widget_context;
|
|
54
|
+
expect(context).toContain("- Level 1");
|
|
55
|
+
expect(context).toContain(" - Level 2A");
|
|
56
|
+
expect(context).toContain(" - Level 2B");
|
|
57
|
+
expect(context).toContain(" - Level 3");
|
|
58
|
+
});
|
|
59
|
+
it("should update context when content changes", () => {
|
|
60
|
+
const { rerender } = render(_jsx(DataLLM, { content: "Initial content", children: _jsx("div", { children: "Child" }) }));
|
|
61
|
+
const initialCalls = OpenaiMock.setWidgetState.mock.calls.length;
|
|
62
|
+
rerender(_jsx(DataLLM, { content: "Updated content", children: _jsx("div", { children: "Child" }) }));
|
|
63
|
+
expect(OpenaiMock.setWidgetState.mock.calls.length).toBeGreaterThan(initialCalls);
|
|
64
|
+
const lastCallArgs = OpenaiMock.setWidgetState.mock.calls[OpenaiMock.setWidgetState.mock.calls.length - 1]?.[0];
|
|
65
|
+
expect(lastCallArgs?.__widget_context).toContain("- Updated content");
|
|
66
|
+
});
|
|
67
|
+
it("should remove node and update context when component unmounts", () => {
|
|
68
|
+
const { unmount } = render(_jsx(DataLLM, { content: "Content to remove", children: _jsx("div", { children: "Child" }) }));
|
|
69
|
+
const callsBeforeUnmount = OpenaiMock.setWidgetState.mock.calls.length;
|
|
70
|
+
unmount();
|
|
71
|
+
expect(OpenaiMock.setWidgetState.mock.calls.length).toBeGreaterThan(callsBeforeUnmount);
|
|
72
|
+
const lastCallArgs = OpenaiMock.setWidgetState.mock.calls[OpenaiMock.setWidgetState.mock.calls.length - 1]?.[0];
|
|
73
|
+
expect(lastCallArgs?.__widget_context).not.toContain("Content to remove");
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
//# sourceMappingURL=data-llm.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-llm.test.js","sourceRoot":"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E,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+DAA+D,EAAE,GAAG,EAAE;QACvE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CACxB,KAAC,OAAO,IAAC,OAAO,EAAC,mBAAmB,YAClC,kCAAgB,GACR,CACX,CAAC;QAEF,MAAM,kBAAkB,GAAG,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;QAEvE,OAAO,EAAE,CAAC;QAEV,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,eAAe,CACjE,kBAAkB,CACnB,CAAC;QACF,MAAM,YAAY,GAChB,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAClC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAChD,EAAE,CAAC,CAAC,CAAC,CAAC;QACT,MAAM,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type CallToolState, type CallToolFn, type CallToolAsyncFn } from "./hooks/use-call-tool.js";
|
|
2
|
+
import { type ToolState } from "./hooks/use-tool-info.js";
|
|
3
|
+
import type { InferTools, ToolInput, ToolOutput } from "../server/index.js";
|
|
4
|
+
import type { CallToolResponse, Prettify, Objectify } from "./types.js";
|
|
5
|
+
type TypedCallToolResponse<TOutput> = CallToolResponse & {
|
|
6
|
+
structuredContent: TOutput;
|
|
7
|
+
};
|
|
8
|
+
type TypedCallToolReturn<TInput, TOutput> = Prettify<CallToolState<TypedCallToolResponse<TOutput>> & {
|
|
9
|
+
callTool: CallToolFn<TInput, TypedCallToolResponse<TOutput>>;
|
|
10
|
+
callToolAsync: CallToolAsyncFn<TInput, TypedCallToolResponse<TOutput>>;
|
|
11
|
+
}>;
|
|
12
|
+
type TypedToolInfoReturn<TInput, TOutput> = ToolState<Objectify<TInput>, Objectify<TOutput>, Objectify<{}>>;
|
|
13
|
+
/**
|
|
14
|
+
* Creates typed versions of skybridge hooks with full type inference
|
|
15
|
+
* for tool names, inputs, and outputs.
|
|
16
|
+
*
|
|
17
|
+
* This is the recommended way to use skybridge hooks in your widgets.
|
|
18
|
+
* Set this up once in a dedicated file and export the typed hooks for use across your app.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam ServerType - The type of your McpServer instance (use `typeof server`).
|
|
21
|
+
* Must be a server instance created with method chaining.
|
|
22
|
+
* TypeScript will validate that tools can be inferred from this type.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // server/src/index.ts
|
|
27
|
+
* const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
28
|
+
* .widget("search-voyage", {}, {
|
|
29
|
+
* inputSchema: { destination: z.string() },
|
|
30
|
+
* outputSchema: { results: z.array(z.string()) },
|
|
31
|
+
* }, async ({ destination }) => {
|
|
32
|
+
* return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* export type AppType = typeof server;
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // web/src/skybridge.ts (one-time setup)
|
|
41
|
+
* import type { AppType } from "../server";
|
|
42
|
+
* import { generateHelpers } from "skybridge/web";
|
|
43
|
+
*
|
|
44
|
+
* export const { useCallTool, useToolInfo } = generateHelpers<AppType>();
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* // web/src/widgets/search.tsx (usage)
|
|
50
|
+
* import { useCallTool, useToolInfo } from "../skybridge";
|
|
51
|
+
*
|
|
52
|
+
* export function SearchWidget() {
|
|
53
|
+
* const { callTool, data } = useCallTool("search-voyage");
|
|
54
|
+
* // ^ autocomplete for tool names
|
|
55
|
+
* callTool({ destination: "Spain" });
|
|
56
|
+
* // ^ autocomplete for input fields
|
|
57
|
+
*
|
|
58
|
+
* const toolInfo = useToolInfo<"search-voyage">();
|
|
59
|
+
* // ^ autocomplete for widget names
|
|
60
|
+
* // toolInfo.input is typed based on widget input schema
|
|
61
|
+
* // toolInfo.output.structuredContent is typed based on widget output schema
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function generateHelpers<ServerType = never>(): {
|
|
66
|
+
/**
|
|
67
|
+
* Typed version of `useCallTool` that provides autocomplete for tool names
|
|
68
|
+
* and type inference for inputs and outputs.
|
|
69
|
+
*
|
|
70
|
+
* @param name - The name of the widget to call. Autocompletes based on your server's widget registry.
|
|
71
|
+
* @returns A hook with typed `callTool` function and `data` property.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const { callTool, data, isPending } = useCallTool("search-voyage");
|
|
76
|
+
* // TypeScript knows callTool expects { destination: string }
|
|
77
|
+
* callTool({ destination: "Spain" });
|
|
78
|
+
*
|
|
79
|
+
* // data.structuredContent is typed based on your outputSchema
|
|
80
|
+
* if (data) {
|
|
81
|
+
* console.log(data.structuredContent.results);
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
useCallTool: <ToolName extends keyof InferTools<ServerType> & string>(name: ToolName) => TypedCallToolReturn<ToolInput<ServerType, ToolName>, ToolOutput<ServerType, ToolName>>;
|
|
86
|
+
/**
|
|
87
|
+
* Typed version of `useToolInfo` that provides autocomplete for widget names
|
|
88
|
+
* and type inference for inputs, outputs, and responseMetadata.
|
|
89
|
+
*
|
|
90
|
+
* @typeParam K - The name of the widget. Autocompletes based on your server's widget registry.
|
|
91
|
+
* @returns A discriminated union with `status: "pending" | "success"` that narrows correctly.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const toolInfo = useToolInfo<"search-voyage">();
|
|
96
|
+
* // toolInfo.input is typed as { destination: string; ... }
|
|
97
|
+
* // toolInfo.output is typed as { results: Array<...>; ... }
|
|
98
|
+
* // toolInfo.status narrows correctly: "pending" | "success"
|
|
99
|
+
*
|
|
100
|
+
* if (toolInfo.isPending) {
|
|
101
|
+
* // TypeScript knows output is undefined here
|
|
102
|
+
* console.log(toolInfo.input.destination);
|
|
103
|
+
* }
|
|
104
|
+
*
|
|
105
|
+
* if (toolInfo.isSuccess) {
|
|
106
|
+
* // TypeScript knows output is defined here
|
|
107
|
+
* console.log(toolInfo.output.results);
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
useToolInfo: <ToolName extends keyof InferTools<ServerType> & string>() => TypedToolInfoReturn<ToolInput<ServerType, ToolName>, ToolOutput<ServerType, ToolName>>;
|
|
112
|
+
};
|
|
113
|
+
export {};
|