skybridge 0.0.0-dev.66b8f6b → 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.
Files changed (149) hide show
  1. package/README.md +321 -1
  2. package/dist/src/server/index.d.ts +4 -0
  3. package/dist/{server/index.d.ts → src/server/index.js} +1 -1
  4. package/dist/src/server/index.js.map +1 -0
  5. package/dist/src/server/inferUtilityTypes.d.ts +46 -0
  6. package/dist/src/server/inferUtilityTypes.js +2 -0
  7. package/dist/src/server/inferUtilityTypes.js.map +1 -0
  8. package/dist/src/server/server.d.ts +37 -0
  9. package/dist/src/server/server.js +62 -0
  10. package/dist/src/server/server.js.map +1 -0
  11. package/dist/src/server/templateHelper.d.ts +15 -0
  12. package/dist/src/server/templateHelper.js +29 -0
  13. package/dist/src/server/templateHelper.js.map +1 -0
  14. package/dist/src/server/templates/development.hbs +12 -0
  15. package/dist/src/server/templates/production.hbs +6 -0
  16. package/dist/{server → src/server}/widgetsDevServer.d.ts +5 -2
  17. package/dist/{server → src/server}/widgetsDevServer.js +5 -2
  18. package/dist/src/server/widgetsDevServer.js.map +1 -0
  19. package/dist/src/test/utils.d.ts +69 -0
  20. package/dist/src/test/utils.js +136 -0
  21. package/dist/src/test/utils.js.map +1 -0
  22. package/dist/src/test/widget.test.d.ts +1 -0
  23. package/dist/src/test/widget.test.js +90 -0
  24. package/dist/src/test/widget.test.js.map +1 -0
  25. package/dist/src/web/hooks/index.d.ts +14 -0
  26. package/dist/src/web/hooks/index.js +15 -0
  27. package/dist/src/web/hooks/index.js.map +1 -0
  28. package/dist/src/web/hooks/use-call-tool.d.ts +62 -0
  29. package/dist/src/web/hooks/use-call-tool.js +68 -0
  30. package/dist/src/web/hooks/use-call-tool.js.map +1 -0
  31. package/dist/src/web/hooks/use-call-tool.test.d.ts +1 -0
  32. package/dist/src/web/hooks/use-call-tool.test.js +163 -0
  33. package/dist/src/web/hooks/use-call-tool.test.js.map +1 -0
  34. package/dist/src/web/hooks/use-display-mode.d.ts +4 -0
  35. package/dist/src/web/hooks/use-display-mode.js +7 -0
  36. package/dist/src/web/hooks/use-display-mode.js.map +1 -0
  37. package/dist/src/web/hooks/use-display-mode.test.d.ts +1 -0
  38. package/dist/src/web/hooks/use-display-mode.test.js +40 -0
  39. package/dist/src/web/hooks/use-display-mode.test.js.map +1 -0
  40. package/dist/src/web/hooks/use-files.d.ts +10 -0
  41. package/dist/src/web/hooks/use-files.js +7 -0
  42. package/dist/src/web/hooks/use-files.js.map +1 -0
  43. package/dist/src/web/hooks/use-files.test.d.ts +1 -0
  44. package/dist/src/web/hooks/use-files.test.js +29 -0
  45. package/dist/src/web/hooks/use-files.test.js.map +1 -0
  46. package/dist/src/web/hooks/use-locale.d.ts +1 -0
  47. package/dist/src/web/hooks/use-locale.js +5 -0
  48. package/dist/src/web/hooks/use-locale.js.map +1 -0
  49. package/dist/src/web/hooks/use-locale.test.d.ts +1 -0
  50. package/dist/src/web/hooks/use-locale.test.js +21 -0
  51. package/dist/src/web/hooks/use-locale.test.js.map +1 -0
  52. package/dist/src/web/hooks/use-open-external.d.ts +1 -0
  53. package/dist/src/web/hooks/use-open-external.js +6 -0
  54. package/dist/src/web/hooks/use-open-external.js.map +1 -0
  55. package/dist/src/web/hooks/use-open-external.test.d.ts +1 -0
  56. package/dist/src/web/hooks/use-open-external.test.js +24 -0
  57. package/dist/src/web/hooks/use-open-external.test.js.map +1 -0
  58. package/dist/{web → src/web/hooks}/use-openai-global.d.ts +1 -1
  59. package/dist/{web → src/web/hooks}/use-openai-global.js +4 -2
  60. package/dist/src/web/hooks/use-openai-global.js.map +1 -0
  61. package/dist/src/web/hooks/use-request-modal.d.ts +6 -0
  62. package/dist/src/web/hooks/use-request-modal.js +9 -0
  63. package/dist/src/web/hooks/use-request-modal.js.map +1 -0
  64. package/dist/src/web/hooks/use-request-modal.test.d.ts +1 -0
  65. package/dist/src/web/hooks/use-request-modal.test.js +24 -0
  66. package/dist/src/web/hooks/use-request-modal.test.js.map +1 -0
  67. package/dist/src/web/hooks/use-send-follow-up-message.d.ts +1 -0
  68. package/dist/src/web/hooks/use-send-follow-up-message.js +11 -0
  69. package/dist/src/web/hooks/use-send-follow-up-message.js.map +1 -0
  70. package/dist/src/web/hooks/use-theme.d.ts +1 -0
  71. package/dist/src/web/hooks/use-theme.js +5 -0
  72. package/dist/src/web/hooks/use-theme.js.map +1 -0
  73. package/dist/src/web/hooks/use-theme.test.d.ts +1 -0
  74. package/dist/src/web/hooks/use-theme.test.js +26 -0
  75. package/dist/src/web/hooks/use-theme.test.js.map +1 -0
  76. package/dist/src/web/hooks/use-tool-info.d.ts +25 -0
  77. package/dist/src/web/hooks/use-tool-info.js +22 -0
  78. package/dist/src/web/hooks/use-tool-info.js.map +1 -0
  79. package/dist/src/web/hooks/use-tool-info.test-d.d.ts +1 -0
  80. package/dist/src/web/hooks/use-tool-info.test-d.js +74 -0
  81. package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -0
  82. package/dist/src/web/hooks/use-tool-info.test.d.ts +1 -0
  83. package/dist/src/web/hooks/use-tool-info.test.js +59 -0
  84. package/dist/src/web/hooks/use-tool-info.test.js.map +1 -0
  85. package/dist/src/web/hooks/use-tool-output.d.ts +4 -0
  86. package/dist/src/web/hooks/use-tool-output.js +9 -0
  87. package/dist/src/web/hooks/use-tool-output.js.map +1 -0
  88. package/dist/src/web/hooks/use-tool-response-metadata.d.ts +4 -0
  89. package/dist/src/web/hooks/use-tool-response-metadata.js +8 -0
  90. package/dist/src/web/hooks/use-tool-response-metadata.js.map +1 -0
  91. package/dist/src/web/hooks/use-user-agent.d.ts +1 -0
  92. package/dist/src/web/hooks/use-user-agent.js +5 -0
  93. package/dist/src/web/hooks/use-user-agent.js.map +1 -0
  94. package/dist/src/web/hooks/use-user-agent.test.d.ts +1 -0
  95. package/dist/src/web/hooks/use-user-agent.test.js +31 -0
  96. package/dist/src/web/hooks/use-user-agent.test.js.map +1 -0
  97. package/dist/src/web/hooks/use-widget-state.d.ts +4 -0
  98. package/dist/src/web/hooks/use-widget-state.js +30 -0
  99. package/dist/src/web/hooks/use-widget-state.js.map +1 -0
  100. package/dist/src/web/hooks/use-widget-state.test.d.ts +1 -0
  101. package/dist/src/web/hooks/use-widget-state.test.js +60 -0
  102. package/dist/src/web/hooks/use-widget-state.test.js.map +1 -0
  103. package/dist/src/web/index.d.ts +5 -0
  104. package/dist/src/web/index.js +6 -0
  105. package/dist/src/web/index.js.map +1 -0
  106. package/dist/src/web/mount-widget.js +19 -0
  107. package/dist/src/web/mount-widget.js.map +1 -0
  108. package/dist/src/web/plugin.d.ts +2 -0
  109. package/dist/src/web/plugin.js +28 -0
  110. package/dist/src/web/plugin.js.map +1 -0
  111. package/dist/src/web/proxy.d.ts +1 -0
  112. package/dist/src/web/proxy.js +48 -0
  113. package/dist/src/web/proxy.js.map +1 -0
  114. package/dist/src/web/typed-hooks.d.ts +107 -0
  115. package/dist/src/web/typed-hooks.js +111 -0
  116. package/dist/src/web/typed-hooks.js.map +1 -0
  117. package/dist/src/web/typed-hooks.test-d.d.ts +1 -0
  118. package/dist/src/web/typed-hooks.test-d.js +87 -0
  119. package/dist/src/web/typed-hooks.test-d.js.map +1 -0
  120. package/dist/src/web/typed-hooks.test.d.ts +1 -0
  121. package/dist/src/web/typed-hooks.test.js +17 -0
  122. package/dist/src/web/typed-hooks.test.js.map +1 -0
  123. package/dist/{web → src/web}/types.d.ts +39 -15
  124. package/dist/src/web/types.js.map +1 -0
  125. package/dist/vitest.config.d.ts +2 -0
  126. package/dist/vitest.config.js +8 -0
  127. package/dist/vitest.config.js.map +1 -0
  128. package/package.json +26 -12
  129. package/dist/server/index.js +0 -4
  130. package/dist/server/index.js.map +0 -1
  131. package/dist/server/middleware.d.ts +0 -3
  132. package/dist/server/middleware.js +0 -47
  133. package/dist/server/middleware.js.map +0 -1
  134. package/dist/server/server.d.ts +0 -12
  135. package/dist/server/server.js +0 -70
  136. package/dist/server/server.js.map +0 -1
  137. package/dist/server/widgetsDevServer.js.map +0 -1
  138. package/dist/web/index.d.ts +0 -4
  139. package/dist/web/index.js +0 -5
  140. package/dist/web/index.js.map +0 -1
  141. package/dist/web/mount-widget.js +0 -10
  142. package/dist/web/mount-widget.js.map +0 -1
  143. package/dist/web/types.js.map +0 -1
  144. package/dist/web/use-openai-global.js.map +0 -1
  145. package/dist/web/use-tool-output.d.ts +0 -3
  146. package/dist/web/use-tool-output.js +0 -5
  147. package/dist/web/use-tool-output.js.map +0 -1
  148. /package/dist/{web → src/web}/mount-widget.d.ts +0 -0
  149. /package/dist/{web → src/web}/types.js +0 -0
@@ -0,0 +1,69 @@
1
+ import { type MockInstance } from "vitest";
2
+ import { McpServer } from "../server/server.js";
3
+ /**
4
+ * Creates a real McpServer instance for testing
5
+ */
6
+ export declare function createMockMcpServer(): {
7
+ server: McpServer;
8
+ mockResource: MockInstance<McpServer["resource"]>;
9
+ mockRegisterTool: MockInstance<McpServer["registerTool"]>;
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
+ }>;
52
+ /**
53
+ * Mock extra parameter for resource callback
54
+ */
55
+ export declare function createMockExtra(host: string): {
56
+ requestInfo: {
57
+ headers: {
58
+ host: string;
59
+ };
60
+ };
61
+ };
62
+ /**
63
+ * Sets up environment variables for testing
64
+ */
65
+ export declare function setTestEnv(env: Record<string, string>): void;
66
+ /**
67
+ * Resets environment variables
68
+ */
69
+ export declare function resetTestEnv(): void;
@@ -0,0 +1,136 @@
1
+ import { vi } from "vitest";
2
+ import { McpServer } from "../server/server.js";
3
+ import { z } from "zod";
4
+ /**
5
+ * Creates a real McpServer instance for testing
6
+ */
7
+ export function createMockMcpServer() {
8
+ // Create a real McpServer instance
9
+ const server = new McpServer({
10
+ name: "alpic-openai-app",
11
+ version: "0.0.1",
12
+ }, { capabilities: {} });
13
+ // Mock the underlying methods to track calls
14
+ const mockResource = vi.spyOn(server, "resource");
15
+ const mockRegisterTool = vi.spyOn(server, "registerTool");
16
+ return {
17
+ server,
18
+ mockResource,
19
+ mockRegisterTool,
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
+ }
114
+ /**
115
+ * Mock extra parameter for resource callback
116
+ */
117
+ export function createMockExtra(host) {
118
+ return {
119
+ requestInfo: {
120
+ headers: { host },
121
+ },
122
+ };
123
+ }
124
+ /**
125
+ * Sets up environment variables for testing
126
+ */
127
+ export function setTestEnv(env) {
128
+ Object.assign(process.env, env);
129
+ }
130
+ /**
131
+ * Resets environment variables
132
+ */
133
+ export function resetTestEnv() {
134
+ delete process.env.NODE_ENV;
135
+ }
136
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1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BAAkB,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 @@
1
+ export {};
@@ -0,0 +1,90 @@
1
+ import * as fs from "node:fs";
2
+ import { afterEach, beforeEach, describe, expect, it, vi, } from "vitest";
3
+ import { McpServer } from "../server/server.js";
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
+ });
24
+ describe("McpServer.widget", () => {
25
+ let server;
26
+ let mockResource;
27
+ let mockRegisterTool;
28
+ let readFileSyncSpy = null;
29
+ beforeEach(() => {
30
+ ({ server, mockResource, mockRegisterTool } = createMockMcpServer());
31
+ });
32
+ afterEach(() => {
33
+ vi.clearAllMocks();
34
+ resetTestEnv();
35
+ });
36
+ it("should generate correct HTML for development mode", async () => {
37
+ setTestEnv({ NODE_ENV: "development" });
38
+ const mockToolCallback = vi.fn();
39
+ const mockResourceConfig = { description: "Test widget" };
40
+ const mockToolConfig = { description: "Test tool" };
41
+ server.widget("my-widget", mockResourceConfig, mockToolConfig, mockToolCallback);
42
+ // Get the resource callback function
43
+ const resourceCallback = mockResource.mock.calls[0]?.[3];
44
+ expect(resourceCallback).toBeDefined();
45
+ const serverUrl = "http://localhost:3000";
46
+ const mockExtra = createMockExtra("__not_used__");
47
+ const result = await resourceCallback(new URL("ui://widgets/my-widget.html"), mockExtra);
48
+ expect(result).toEqual({
49
+ contents: [
50
+ {
51
+ uri: "ui://widgets/my-widget.html",
52
+ mimeType: "text/html+skybridge",
53
+ text: expect.stringContaining('<div id="root"></div>'),
54
+ },
55
+ ],
56
+ });
57
+ // Check development-specific content
58
+ expect(result.contents[0]?.text).toContain(serverUrl + "/@react-refresh");
59
+ expect(result.contents[0]?.text).toContain(serverUrl + "/@vite/client");
60
+ expect(result.contents[0]?.text).toContain(serverUrl + "/src/widgets/my-widget.tsx");
61
+ });
62
+ it("should generate correct HTML for production mode", async () => {
63
+ setTestEnv({ NODE_ENV: "production" });
64
+ const mockToolCallback = vi.fn();
65
+ const mockResourceConfig = { description: "Test widget" };
66
+ const mockToolConfig = { description: "Test tool" };
67
+ server.widget("my-widget", mockResourceConfig, mockToolConfig, mockToolCallback);
68
+ // Get the resource callback function
69
+ const resourceCallback = mockResource.mock.calls[0]?.[3];
70
+ expect(resourceCallback).toBeDefined();
71
+ const serverUrl = "https://myapp.com";
72
+ const mockExtra = createMockExtra(serverUrl);
73
+ const result = await resourceCallback(new URL("ui://widgets/my-widget.html"), mockExtra);
74
+ expect(result).toEqual({
75
+ contents: [
76
+ {
77
+ uri: "ui://widgets/my-widget.html",
78
+ mimeType: "text/html+skybridge",
79
+ text: expect.stringContaining('<div id="root"></div>'),
80
+ },
81
+ ],
82
+ });
83
+ // Check production-specific content
84
+ expect(result.contents[0]?.text).not.toContain(serverUrl + "@react-refresh");
85
+ expect(result.contents[0]?.text).not.toContain(serverUrl + "@vite/client");
86
+ expect(result.contents[0]?.text).toContain(serverUrl + "/assets/my-widget.js");
87
+ expect(result.contents[0]?.text).toContain(serverUrl + "/assets/style.css");
88
+ });
89
+ });
90
+ //# sourceMappingURL=widget.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"widget.test.js","sourceRoot":"","sources":["../../../src/test/widget.test.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,MAAM,EACN,EAAE,EACF,EAAE,GAEH,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,YAAY,EACZ,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,MAAM,YAAY,GAAG;IACnB,2BAA2B,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE;IACrD,WAAW,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;CACnC,CAAC;AAEF,EAAE,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE;IAC5B,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,YAAY,CAA2B,SAAS,CAAC,CAAC;IAC1E,MAAM,YAAY,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,IAAY,EAAE,GAAG,IAAW,EAAE,EAAE;QAC1D,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;YAC/D,OAAO,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;QACtC,CAAC;QACD,OAAO,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;IAC5C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EvC,MAAM,SAAS,GAAG,mBAAmB,CAAC;QACtC,MAAM,SAAS,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;QAC7C,MAAM,MAAM,GAAG,MAAM,gBAAiB,CACpC,IAAI,GAAG,CAAC,6BAA6B,CAAC,EACtC,SAAS,CACV,CAAC;QAEF,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC;YACrB,QAAQ,EAAE;gBACR;oBACE,GAAG,EAAE,6BAA6B;oBAClC,QAAQ,EAAE,qBAAqB;oBAC/B,IAAI,EAAE,MAAM,CAAC,gBAAgB,CAAC,uBAAuB,CAAC;iBACvD;aACF;SACF,CAAC,CAAC;QAEH,oCAAoC;QACpC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,SAAS,CAC5C,SAAS,GAAG,gBAAgB,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,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 {};
@@ -0,0 +1,68 @@
1
+ import { useState } from "react";
2
+ export const useCallTool = (name) => {
3
+ const [{ status, data, error }, setCallToolState] = useState({ status: "idle", data: undefined, error: undefined });
4
+ const execute = async (toolArgs) => {
5
+ setCallToolState({ status: "pending", data: undefined, error: undefined });
6
+ try {
7
+ const data = await window.openai.callTool(name, toolArgs);
8
+ setCallToolState({ status: "success", data, error: undefined });
9
+ return data;
10
+ }
11
+ catch (error) {
12
+ setCallToolState({ status: "error", data: undefined, error });
13
+ throw error;
14
+ }
15
+ };
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)
36
+ .then((data) => {
37
+ if (sideEffects?.onSuccess) {
38
+ sideEffects.onSuccess(data, toolArgs);
39
+ }
40
+ if (sideEffects?.onSettled) {
41
+ sideEffects.onSettled(data, undefined, toolArgs);
42
+ }
43
+ })
44
+ .catch((error) => {
45
+ if (sideEffects?.onError) {
46
+ sideEffects.onError(error, toolArgs);
47
+ }
48
+ if (sideEffects?.onSettled) {
49
+ sideEffects.onSettled(undefined, error, toolArgs);
50
+ }
51
+ });
52
+ }
53
+ const callToolState = {
54
+ status,
55
+ data,
56
+ error,
57
+ isIdle: status === "idle",
58
+ isPending: status === "pending",
59
+ isSuccess: status === "success",
60
+ isError: status === "error",
61
+ };
62
+ return {
63
+ ...callToolState,
64
+ callTool,
65
+ callToolAsync,
66
+ };
67
+ };
68
+ //# sourceMappingURL=use-call-tool.js.map
@@ -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,E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AC,IAAI,EAAE,EAAE;YACb,IAAI,WAAW,EAAE,SAAS,EAAE,CAAC;gBAC3B,WAAW,CAAC,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACxC,CAAC;YACD,IAAI,WAAW,EAAE,SAAS,EAAE,CAAC;gBAC3B,WAAW,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;YACnD,CAAC;QACH,CAAC,CAAC;aACD,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"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,163 @@
1
+ import { act, renderHook, waitFor } from "@testing-library/react";
2
+ import { afterEach, beforeEach, describe, expect, it, vi, expectTypeOf, } from "vitest";
3
+ import { useCallTool } from "./use-call-tool.js";
4
+ describe("useCallTool - onSuccess callback", () => {
5
+ let OpenaiMock;
6
+ beforeEach(() => {
7
+ OpenaiMock = {
8
+ callTool: vi.fn(),
9
+ };
10
+ vi.stubGlobal("openai", OpenaiMock);
11
+ });
12
+ afterEach(() => {
13
+ vi.unstubAllGlobals();
14
+ vi.resetAllMocks();
15
+ });
16
+ const toolName = "test-tool";
17
+ const args = { input: "test input" };
18
+ const data = {
19
+ content: [{ type: "text", text: "test result" }],
20
+ structuredContent: { result: "test" },
21
+ isError: false,
22
+ result: "test result",
23
+ meta: {},
24
+ };
25
+ const error = new Error("test error");
26
+ it("should call window.openai.callTool with correct arguments", async () => {
27
+ const { result } = renderHook(() => useCallTool(toolName));
28
+ act(() => {
29
+ result.current.callTool(args);
30
+ });
31
+ expect(OpenaiMock.callTool).toHaveBeenCalledWith(toolName, args);
32
+ });
33
+ it("should call onSuccess callback with correct data and toolArgs on successful execution", async () => {
34
+ const onSuccess = vi.fn();
35
+ const onError = vi.fn();
36
+ OpenaiMock.callTool.mockResolvedValueOnce(data);
37
+ const { result } = renderHook(() => useCallTool(toolName));
38
+ act(() => {
39
+ result.current.callTool(args, {
40
+ onSuccess,
41
+ onError,
42
+ });
43
+ });
44
+ await waitFor(() => {
45
+ expect(onSuccess).toHaveBeenCalledWith(data, args);
46
+ expect(onError).not.toHaveBeenCalled();
47
+ });
48
+ });
49
+ it("should call onError callback with error and toolArgs on failed execution", async () => {
50
+ const onSuccess = vi.fn();
51
+ const onError = vi.fn();
52
+ OpenaiMock.callTool.mockRejectedValueOnce(error);
53
+ const { result } = renderHook(() => useCallTool(toolName));
54
+ act(() => {
55
+ result.current.callTool(args, {
56
+ onSuccess,
57
+ onError,
58
+ });
59
+ });
60
+ await waitFor(() => {
61
+ expect(onSuccess).not.toHaveBeenCalled();
62
+ expect(onError).toHaveBeenCalledWith(error, args);
63
+ });
64
+ });
65
+ it("should call onSettled callback with data and undefined error on successful execution", async () => {
66
+ const onSuccess = vi.fn();
67
+ const onError = vi.fn();
68
+ const onSettled = vi.fn();
69
+ OpenaiMock.callTool.mockResolvedValueOnce(data);
70
+ const { result } = renderHook(() => useCallTool(toolName));
71
+ act(() => {
72
+ result.current.callTool(args, {
73
+ onSuccess,
74
+ onError,
75
+ onSettled,
76
+ });
77
+ });
78
+ await waitFor(() => {
79
+ expect(onSuccess).toHaveBeenCalledWith(data, args);
80
+ expect(onSettled).toHaveBeenCalledWith(data, undefined, args);
81
+ expect(onError).not.toHaveBeenCalled();
82
+ });
83
+ });
84
+ it("should call onSettled callback with undefined data and error on failed execution", async () => {
85
+ const onSuccess = vi.fn();
86
+ const onError = vi.fn();
87
+ const onSettled = vi.fn();
88
+ OpenaiMock.callTool.mockRejectedValueOnce(error);
89
+ const { result } = renderHook(() => useCallTool(toolName));
90
+ act(() => {
91
+ result.current.callTool(args, {
92
+ onSuccess,
93
+ onError,
94
+ onSettled,
95
+ });
96
+ });
97
+ await waitFor(() => {
98
+ expect(onError).toHaveBeenCalledWith(error, args);
99
+ expect(onSettled).toHaveBeenCalledWith(undefined, error, args);
100
+ expect(onSuccess).not.toHaveBeenCalled();
101
+ });
102
+ });
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
+ });
163
+ //# sourceMappingURL=use-call-tool.test.js.map