@smithery/sdk 1.7.4 → 2.0.0
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/dist/index.d.ts +0 -5
- package/dist/index.js +1 -9
- package/dist/server/index.d.ts +0 -2
- package/dist/server/index.js +0 -1
- package/dist/server/stateful.js +0 -2
- package/package.json +4 -14
- package/dist/openai/index.d.ts +0 -2
- package/dist/openai/index.js +0 -1
- package/dist/openai/widget.d.ts +0 -67
- package/dist/openai/widget.js +0 -126
- package/dist/react/ErrorBoundary.d.ts +0 -18
- package/dist/react/ErrorBoundary.js +0 -28
- package/dist/react/hooks.d.ts +0 -33
- package/dist/react/hooks.js +0 -137
- package/dist/react/index.d.ts +0 -3
- package/dist/react/index.js +0 -2
- package/dist/react/types.d.ts +0 -66
- package/dist/react/types.js +0 -1
- package/dist/server/widget.d.ts +0 -6
- package/dist/server/widget.js +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,3 @@ export { createStatefulServer, type StatefulServerOptions, } from "./server/stat
|
|
|
4
4
|
export * from "./server/session.js";
|
|
5
5
|
export * from "./server/auth/identity.js";
|
|
6
6
|
export * from "./server/auth/oauth.js";
|
|
7
|
-
export { createWidgetServer } from "./server/widget.js";
|
|
8
|
-
export type { WidgetServerOptions } from "./server/widget.js";
|
|
9
|
-
export { widget } from "./openai/index.js";
|
|
10
|
-
export type { WidgetToolOptions, WidgetResponseOptions, WidgetResourceOptions, } from "./openai/index.js";
|
|
11
|
-
export type { Theme, DisplayMode, OpenAiGlobals, CallToolResponse, } from "./react/types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Smithery SDK – Main exports
|
|
2
|
-
// Use subpath imports for tree-shaking: @smithery/sdk/server, /helpers
|
|
2
|
+
// Use subpath imports for tree-shaking: @smithery/sdk/server, /helpers
|
|
3
3
|
// === Shared Utilities ===
|
|
4
4
|
export * from "./shared/config.js";
|
|
5
5
|
export * from "./shared/patch.js";
|
|
@@ -9,11 +9,3 @@ export { createStatefulServer, } from "./server/stateful.js";
|
|
|
9
9
|
export * from "./server/session.js";
|
|
10
10
|
export * from "./server/auth/identity.js";
|
|
11
11
|
export * from "./server/auth/oauth.js";
|
|
12
|
-
// === Widget SDK - Server ===
|
|
13
|
-
// createWidgetServer() - MCP server wrapper for widgets
|
|
14
|
-
export { createWidgetServer } from "./server/widget.js";
|
|
15
|
-
// === Widget SDK - OpenAI ===
|
|
16
|
-
// widget.response() - Standard success response
|
|
17
|
-
// widget.error() - Standard error response
|
|
18
|
-
// widget.resource() - Auto-generate widget HTML
|
|
19
|
-
export { widget } from "./openai/index.js";
|
package/dist/server/index.d.ts
CHANGED
package/dist/server/index.js
CHANGED
package/dist/server/stateful.js
CHANGED
|
@@ -31,7 +31,6 @@ export function createStatefulServer(createMcpServer, options) {
|
|
|
31
31
|
let transport;
|
|
32
32
|
if (sessionId && sessionStore.get(sessionId)) {
|
|
33
33
|
// Reuse existing transport
|
|
34
|
-
// biome-ignore lint/style/noNonNullAssertion: Not possible
|
|
35
34
|
transport = sessionStore.get(sessionId);
|
|
36
35
|
}
|
|
37
36
|
else if (!sessionId && isInitializeRequest(req.body)) {
|
|
@@ -133,7 +132,6 @@ export function createStatefulServer(createMcpServer, options) {
|
|
|
133
132
|
res.status(400).send("Invalid or expired session ID");
|
|
134
133
|
return;
|
|
135
134
|
}
|
|
136
|
-
// biome-ignore lint/style/noNonNullAssertion: Not possible
|
|
137
135
|
const transport = sessionStore.get(sessionId);
|
|
138
136
|
await transport.handleRequest(req, res);
|
|
139
137
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smithery/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "SDK to develop with Smithery",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -16,21 +16,12 @@
|
|
|
16
16
|
"./server/stateless.js": "./dist/server/stateless.js",
|
|
17
17
|
"./server/logger.js": "./dist/server/logger.js",
|
|
18
18
|
"./server/session.js": "./dist/server/session.js",
|
|
19
|
-
"./server/widget.js": "./dist/server/widget.js",
|
|
20
19
|
"./server/auth/oauth.js": "./dist/server/auth/oauth.js",
|
|
21
20
|
"./server/auth/identity.js": "./dist/server/auth/identity.js",
|
|
22
21
|
"./shared/config.js": "./dist/shared/config.js",
|
|
23
22
|
"./shared/patch.js": "./dist/shared/patch.js",
|
|
24
23
|
"./helpers": "./dist/helpers/index.js",
|
|
25
|
-
"./helpers/tool.js": "./dist/helpers/tool.js"
|
|
26
|
-
"./helpers/widget.js": "./dist/helpers/widget.js",
|
|
27
|
-
"./react": "./dist/react/index.js",
|
|
28
|
-
"./react/hooks.js": "./dist/react/hooks.js",
|
|
29
|
-
"./react/types.js": "./dist/react/types.js",
|
|
30
|
-
"./react/ErrorBoundary.js": "./dist/react/ErrorBoundary.js",
|
|
31
|
-
"./react/WidgetRoot.js": "./dist/react/WidgetRoot.js",
|
|
32
|
-
"./openai": "./dist/openai/index.js",
|
|
33
|
-
"./openai/widget.js": "./dist/openai/widget.js"
|
|
24
|
+
"./helpers/tool.js": "./dist/helpers/tool.js"
|
|
34
25
|
},
|
|
35
26
|
"files": [
|
|
36
27
|
"dist"
|
|
@@ -53,16 +44,15 @@
|
|
|
53
44
|
"lodash": "^4.17.21",
|
|
54
45
|
"okay-error": "^1.0.3",
|
|
55
46
|
"uuid": "^11.0.3",
|
|
56
|
-
"zod": "
|
|
47
|
+
"zod": ">=3.23.8 <4.0.0",
|
|
57
48
|
"zod-to-json-schema": "^3.24.1"
|
|
58
49
|
},
|
|
59
50
|
"devDependencies": {
|
|
60
|
-
"@biomejs/biome": "
|
|
51
|
+
"@biomejs/biome": "2.2.6",
|
|
61
52
|
"@types/express": "^5.0.1",
|
|
62
53
|
"@types/json-schema": "^7.0.15",
|
|
63
54
|
"@types/lodash": "^4.17.17",
|
|
64
55
|
"@types/node": "^20.0.0",
|
|
65
|
-
"@types/react": "^18.3.12",
|
|
66
56
|
"@types/uuid": "^9.0.7",
|
|
67
57
|
"dotenv": "^16.4.7",
|
|
68
58
|
"tsx": "^4.19.2",
|
package/dist/openai/index.d.ts
DELETED
package/dist/openai/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { widget } from "./widget.js";
|
package/dist/openai/widget.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
export interface WidgetResponseOptions<TState = Record<string, unknown>> {
|
|
3
|
-
structuredData: TState;
|
|
4
|
-
message: string;
|
|
5
|
-
meta?: Record<string, unknown>;
|
|
6
|
-
}
|
|
7
|
-
export interface WidgetResourceOptions {
|
|
8
|
-
name: string;
|
|
9
|
-
description?: string;
|
|
10
|
-
prefersBorder?: boolean;
|
|
11
|
-
csp?: {
|
|
12
|
-
connect_domains?: string[];
|
|
13
|
-
resource_domains?: string[];
|
|
14
|
-
};
|
|
15
|
-
bundlePath?: string;
|
|
16
|
-
bundleURL?: string;
|
|
17
|
-
cssURLs?: string | string[];
|
|
18
|
-
}
|
|
19
|
-
export interface WidgetToolOptions {
|
|
20
|
-
template: string;
|
|
21
|
-
invoking?: string;
|
|
22
|
-
invoked?: string;
|
|
23
|
-
widgetAccessible?: boolean;
|
|
24
|
-
}
|
|
25
|
-
export interface WidgetErrorOptions {
|
|
26
|
-
message: string;
|
|
27
|
-
details?: unknown;
|
|
28
|
-
meta?: Record<string, unknown>;
|
|
29
|
-
}
|
|
30
|
-
export declare function response<TState = Record<string, unknown>>(options: WidgetResponseOptions<TState>): {
|
|
31
|
-
structuredContent: Record<string, unknown>;
|
|
32
|
-
content: {
|
|
33
|
-
type: "text";
|
|
34
|
-
text: string;
|
|
35
|
-
}[];
|
|
36
|
-
_meta: Record<string, unknown>;
|
|
37
|
-
};
|
|
38
|
-
export declare function error(options: string | WidgetErrorOptions): {
|
|
39
|
-
_meta?: Record<string, unknown> | undefined;
|
|
40
|
-
content: {
|
|
41
|
-
type: "text";
|
|
42
|
-
text: string;
|
|
43
|
-
}[];
|
|
44
|
-
isError: boolean;
|
|
45
|
-
};
|
|
46
|
-
export declare function resource<TState = Record<string, unknown>>(options: WidgetResourceOptions): {
|
|
47
|
-
name: string;
|
|
48
|
-
uri: string;
|
|
49
|
-
handler: () => Promise<{
|
|
50
|
-
contents: Array<{
|
|
51
|
-
uri: string;
|
|
52
|
-
text: string;
|
|
53
|
-
mimeType: "text/html+skybridge";
|
|
54
|
-
_meta?: Record<string, unknown>;
|
|
55
|
-
}>;
|
|
56
|
-
}>;
|
|
57
|
-
toolConfig: (options?: Omit<WidgetToolOptions, "template">) => Record<string, unknown>;
|
|
58
|
-
response: (options: WidgetResponseOptions<TState>) => ReturnType<typeof response<TState>>;
|
|
59
|
-
register: (server: Pick<McpServer, "registerResource">) => void;
|
|
60
|
-
};
|
|
61
|
-
export declare function toolConfig(options: WidgetToolOptions): Record<string, unknown>;
|
|
62
|
-
export declare const widget: {
|
|
63
|
-
response: typeof response;
|
|
64
|
-
error: typeof error;
|
|
65
|
-
resource: typeof resource;
|
|
66
|
-
toolConfig: typeof toolConfig;
|
|
67
|
-
};
|
package/dist/openai/widget.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { resolve } from "node:path";
|
|
3
|
-
export function response(options) {
|
|
4
|
-
const { structuredData, message, meta = {} } = options;
|
|
5
|
-
return {
|
|
6
|
-
structuredContent: structuredData,
|
|
7
|
-
content: [
|
|
8
|
-
{
|
|
9
|
-
type: "text",
|
|
10
|
-
text: message,
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
_meta: meta,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
export function error(options) {
|
|
17
|
-
const isString = typeof options === "string";
|
|
18
|
-
const message = isString ? options : options.message;
|
|
19
|
-
const details = isString ? undefined : options.details;
|
|
20
|
-
const meta = isString ? undefined : options.meta;
|
|
21
|
-
const finalMeta = {
|
|
22
|
-
...(meta || {}),
|
|
23
|
-
};
|
|
24
|
-
if (details) {
|
|
25
|
-
finalMeta.errorDetails = details;
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
content: [
|
|
29
|
-
{
|
|
30
|
-
type: "text",
|
|
31
|
-
text: message,
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
isError: true,
|
|
35
|
-
...(Object.keys(finalMeta).length > 0 && { _meta: finalMeta }),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
export function resource(options) {
|
|
39
|
-
const { name, description, prefersBorder, csp, bundlePath = `.smithery/${name}.js`, bundleURL, } = options;
|
|
40
|
-
const uri = `ui://widget/${name}.html`;
|
|
41
|
-
const handler = async () => {
|
|
42
|
-
let scriptTag;
|
|
43
|
-
if (bundleURL) {
|
|
44
|
-
scriptTag = `<script type="module" src="${bundleURL}"></script>`;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
const js = await readFile(resolve(process.cwd(), bundlePath), "utf-8");
|
|
48
|
-
scriptTag = `<script type="module">${js}</script>`;
|
|
49
|
-
}
|
|
50
|
-
const cssUrls = Array.isArray(options.cssURLs)
|
|
51
|
-
? options.cssURLs
|
|
52
|
-
: options.cssURLs
|
|
53
|
-
? [options.cssURLs]
|
|
54
|
-
: [];
|
|
55
|
-
const cssLinks = cssUrls
|
|
56
|
-
.map(url => `<link rel="stylesheet" href="${url}">`)
|
|
57
|
-
.join("\n");
|
|
58
|
-
const html = `
|
|
59
|
-
<div id="${name}-root"></div>
|
|
60
|
-
${cssLinks}
|
|
61
|
-
${scriptTag}
|
|
62
|
-
`.trim();
|
|
63
|
-
const _meta = {};
|
|
64
|
-
if (description)
|
|
65
|
-
_meta["openai/widgetDescription"] = description;
|
|
66
|
-
if (prefersBorder !== undefined)
|
|
67
|
-
_meta["openai/widgetPrefersBorder"] = prefersBorder;
|
|
68
|
-
if (csp)
|
|
69
|
-
_meta["openai/widgetCSP"] = csp;
|
|
70
|
-
return {
|
|
71
|
-
contents: [
|
|
72
|
-
{
|
|
73
|
-
uri,
|
|
74
|
-
text: html,
|
|
75
|
-
mimeType: "text/html+skybridge",
|
|
76
|
-
...(Object.keys(_meta).length > 0 && { _meta }),
|
|
77
|
-
},
|
|
78
|
-
],
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
const toolConfig = (options) => {
|
|
82
|
-
return {
|
|
83
|
-
"openai/outputTemplate": uri,
|
|
84
|
-
"openai/widgetAccessible": options?.widgetAccessible ?? true,
|
|
85
|
-
...(options?.invoking && {
|
|
86
|
-
"openai/toolInvocation/invoking": options.invoking,
|
|
87
|
-
}),
|
|
88
|
-
...(options?.invoked && {
|
|
89
|
-
"openai/toolInvocation/invoked": options.invoked,
|
|
90
|
-
}),
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
const typedResponse = (options) => {
|
|
94
|
-
return response(options);
|
|
95
|
-
};
|
|
96
|
-
const register = (server) => {
|
|
97
|
-
server.registerResource(name, uri, {}, handler);
|
|
98
|
-
};
|
|
99
|
-
return {
|
|
100
|
-
name,
|
|
101
|
-
uri,
|
|
102
|
-
handler,
|
|
103
|
-
toolConfig,
|
|
104
|
-
response: typedResponse,
|
|
105
|
-
register,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
export function toolConfig(options) {
|
|
109
|
-
const meta = {
|
|
110
|
-
"openai/outputTemplate": `ui://widget/${options.template}.html`,
|
|
111
|
-
"openai/widgetAccessible": options.widgetAccessible ?? true,
|
|
112
|
-
};
|
|
113
|
-
if (options.invoking) {
|
|
114
|
-
meta["openai/toolInvocation/invoking"] = options.invoking;
|
|
115
|
-
}
|
|
116
|
-
if (options.invoked) {
|
|
117
|
-
meta["openai/toolInvocation/invoked"] = options.invoked;
|
|
118
|
-
}
|
|
119
|
-
return meta;
|
|
120
|
-
}
|
|
121
|
-
export const widget = {
|
|
122
|
-
response,
|
|
123
|
-
error,
|
|
124
|
-
resource,
|
|
125
|
-
toolConfig,
|
|
126
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Component, type ReactNode } from "react";
|
|
2
|
-
interface ErrorBoundaryProps {
|
|
3
|
-
children: ReactNode;
|
|
4
|
-
fallback?: (error: Error) => ReactNode;
|
|
5
|
-
}
|
|
6
|
-
interface ErrorBoundaryState {
|
|
7
|
-
hasError: boolean;
|
|
8
|
-
error: Error | null;
|
|
9
|
-
}
|
|
10
|
-
export declare class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
11
|
-
constructor(props: ErrorBoundaryProps);
|
|
12
|
-
static getDerivedStateFromError(error: Error): ErrorBoundaryState;
|
|
13
|
-
componentDidCatch(error: Error, errorInfo: {
|
|
14
|
-
componentStack?: string;
|
|
15
|
-
}): void;
|
|
16
|
-
render(): ReactNode;
|
|
17
|
-
}
|
|
18
|
-
export {};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { Component, createElement } from "react";
|
|
2
|
-
export class ErrorBoundary extends Component {
|
|
3
|
-
constructor(props) {
|
|
4
|
-
super(props);
|
|
5
|
-
this.state = { hasError: false, error: null };
|
|
6
|
-
}
|
|
7
|
-
static getDerivedStateFromError(error) {
|
|
8
|
-
return { hasError: true, error };
|
|
9
|
-
}
|
|
10
|
-
componentDidCatch(error, errorInfo) {
|
|
11
|
-
console.error("Widget error:", error, errorInfo);
|
|
12
|
-
}
|
|
13
|
-
render() {
|
|
14
|
-
if (this.state.hasError && this.state.error) {
|
|
15
|
-
if (this.props.fallback) {
|
|
16
|
-
return this.props.fallback(this.state.error);
|
|
17
|
-
}
|
|
18
|
-
return createElement("div", {
|
|
19
|
-
style: {
|
|
20
|
-
padding: "16px",
|
|
21
|
-
color: "#d32f2f",
|
|
22
|
-
fontFamily: "system-ui, sans-serif",
|
|
23
|
-
},
|
|
24
|
-
}, createElement("h3", { style: { margin: "0 0 8px 0", fontSize: "14px" } }, "Widget Error"), createElement("p", { style: { margin: "0", fontSize: "12px", opacity: 0.8 } }, this.state.error.message || "An error occurred"));
|
|
25
|
-
}
|
|
26
|
-
return this.props.children;
|
|
27
|
-
}
|
|
28
|
-
}
|
package/dist/react/hooks.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type SetStateAction } from "react";
|
|
2
|
-
import type { OpenAiGlobals } from "./types.js";
|
|
3
|
-
export declare function useOpenAiGlobal<K extends keyof OpenAiGlobals>(key: K): OpenAiGlobals[K] | undefined;
|
|
4
|
-
export declare function useTheme(): import("./types.js").Theme;
|
|
5
|
-
export declare function useDisplayMode(): import("./types.js").DisplayMode;
|
|
6
|
-
export declare function useUserAgent(): import("./types.js").UserAgent | undefined;
|
|
7
|
-
export declare function useMaxHeight(): number | undefined;
|
|
8
|
-
export declare function useSafeArea(): import("./types.js").SafeArea | undefined;
|
|
9
|
-
export declare function useLocale(): string;
|
|
10
|
-
export declare function useToolInput<T = Record<string, unknown>>(): T | undefined;
|
|
11
|
-
export declare function useToolOutput<TStructured = unknown, TMeta = Record<string, unknown>>(): {
|
|
12
|
-
content: {
|
|
13
|
-
type: string;
|
|
14
|
-
text: string;
|
|
15
|
-
}[];
|
|
16
|
-
structuredContent: TStructured | undefined;
|
|
17
|
-
meta: TMeta | undefined;
|
|
18
|
-
};
|
|
19
|
-
export declare function useToolResponseMetadata<T = Record<string, unknown>>(): T | undefined;
|
|
20
|
-
export declare function useCallTool<TArgs = Record<string, unknown>, TResult = unknown>(toolName: string): {
|
|
21
|
-
call: (args: TArgs) => Promise<void>;
|
|
22
|
-
isPending: boolean;
|
|
23
|
-
error: Error | null;
|
|
24
|
-
data: TResult | null;
|
|
25
|
-
};
|
|
26
|
-
export declare function useSendFollowUp(): {
|
|
27
|
-
send: (prompt: string) => Promise<void>;
|
|
28
|
-
isPending: boolean;
|
|
29
|
-
error: Error | null;
|
|
30
|
-
};
|
|
31
|
-
export declare function useRequestDisplayMode(): (mode: "inline" | "pip" | "fullscreen") => Promise<void>;
|
|
32
|
-
export declare function useWidgetState<T extends Record<string, unknown>>(defaultState: T | (() => T)): readonly [T, (state: SetStateAction<T>) => void];
|
|
33
|
-
export declare function useWidgetState<T extends Record<string, unknown>>(defaultState?: T | (() => T | null) | null): readonly [T | null, (state: SetStateAction<T | null>) => void];
|
package/dist/react/hooks.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { useSyncExternalStore, useCallback, useState, useEffect, } from "react";
|
|
2
|
-
const SET_GLOBALS_EVENT_TYPE = "openai:set_globals";
|
|
3
|
-
function getOpenAiGlobal(key) {
|
|
4
|
-
if (!window.openai)
|
|
5
|
-
return undefined;
|
|
6
|
-
return window.openai[key];
|
|
7
|
-
}
|
|
8
|
-
function subscribeToOpenAi(callback) {
|
|
9
|
-
const handler = () => callback();
|
|
10
|
-
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handler);
|
|
11
|
-
return () => window.removeEventListener(SET_GLOBALS_EVENT_TYPE, handler);
|
|
12
|
-
}
|
|
13
|
-
export function useOpenAiGlobal(key) {
|
|
14
|
-
return useSyncExternalStore(subscribeToOpenAi, () => getOpenAiGlobal(key), () => undefined);
|
|
15
|
-
}
|
|
16
|
-
export function useTheme() {
|
|
17
|
-
return useOpenAiGlobal("theme") ?? "dark";
|
|
18
|
-
}
|
|
19
|
-
export function useDisplayMode() {
|
|
20
|
-
return useOpenAiGlobal("displayMode") ?? "inline";
|
|
21
|
-
}
|
|
22
|
-
export function useUserAgent() {
|
|
23
|
-
return useOpenAiGlobal("userAgent");
|
|
24
|
-
}
|
|
25
|
-
export function useMaxHeight() {
|
|
26
|
-
return useOpenAiGlobal("maxHeight");
|
|
27
|
-
}
|
|
28
|
-
export function useSafeArea() {
|
|
29
|
-
return useOpenAiGlobal("safeArea");
|
|
30
|
-
}
|
|
31
|
-
export function useLocale() {
|
|
32
|
-
return useOpenAiGlobal("locale") ?? "en";
|
|
33
|
-
}
|
|
34
|
-
export function useToolInput() {
|
|
35
|
-
return useOpenAiGlobal("toolInput");
|
|
36
|
-
}
|
|
37
|
-
export function useToolOutput() {
|
|
38
|
-
const output = useOpenAiGlobal("toolOutput");
|
|
39
|
-
const meta = useOpenAiGlobal("toolResponseMetadata");
|
|
40
|
-
return {
|
|
41
|
-
content: output?.content ?? [],
|
|
42
|
-
structuredContent: output?.structuredContent,
|
|
43
|
-
meta: meta,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
export function useToolResponseMetadata() {
|
|
47
|
-
return useOpenAiGlobal("toolResponseMetadata");
|
|
48
|
-
}
|
|
49
|
-
export function useCallTool(toolName) {
|
|
50
|
-
const [isPending, setIsPending] = useState(false);
|
|
51
|
-
const [error, setError] = useState(null);
|
|
52
|
-
const [data, setData] = useState(null);
|
|
53
|
-
const callTool = useOpenAiGlobal("callTool");
|
|
54
|
-
const call = useCallback(async (args) => {
|
|
55
|
-
if (!callTool) {
|
|
56
|
-
setError(new Error("callTool not available"));
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
setIsPending(true);
|
|
60
|
-
setError(null);
|
|
61
|
-
try {
|
|
62
|
-
const result = await callTool(toolName, args);
|
|
63
|
-
if (result.isError) {
|
|
64
|
-
throw new Error(result.content?.[0]?.text ?? "Unknown error");
|
|
65
|
-
}
|
|
66
|
-
setData((result._meta?.gameState ??
|
|
67
|
-
result.structuredContent ??
|
|
68
|
-
null));
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
72
|
-
}
|
|
73
|
-
finally {
|
|
74
|
-
setIsPending(false);
|
|
75
|
-
}
|
|
76
|
-
}, [callTool, toolName]);
|
|
77
|
-
return { call, isPending, error, data };
|
|
78
|
-
}
|
|
79
|
-
export function useSendFollowUp() {
|
|
80
|
-
const [isPending, setIsPending] = useState(false);
|
|
81
|
-
const [error, setError] = useState(null);
|
|
82
|
-
const sendFollowUpMessage = useOpenAiGlobal("sendFollowUpMessage");
|
|
83
|
-
const send = useCallback(async (prompt) => {
|
|
84
|
-
if (!sendFollowUpMessage) {
|
|
85
|
-
setError(new Error("sendFollowUpMessage not available"));
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
setIsPending(true);
|
|
89
|
-
setError(null);
|
|
90
|
-
try {
|
|
91
|
-
await sendFollowUpMessage({ prompt });
|
|
92
|
-
}
|
|
93
|
-
catch (err) {
|
|
94
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
95
|
-
}
|
|
96
|
-
finally {
|
|
97
|
-
setIsPending(false);
|
|
98
|
-
}
|
|
99
|
-
}, [sendFollowUpMessage]);
|
|
100
|
-
return { send, isPending, error };
|
|
101
|
-
}
|
|
102
|
-
export function useRequestDisplayMode() {
|
|
103
|
-
const requestDisplayMode = useOpenAiGlobal("requestDisplayMode");
|
|
104
|
-
const request = useCallback(async (mode) => {
|
|
105
|
-
if (!requestDisplayMode) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
await requestDisplayMode({ mode });
|
|
109
|
-
}, [requestDisplayMode]);
|
|
110
|
-
return request;
|
|
111
|
-
}
|
|
112
|
-
export function useWidgetState(defaultState) {
|
|
113
|
-
const widgetStateFromWindow = useOpenAiGlobal("widgetState");
|
|
114
|
-
const [widgetState, _setWidgetState] = useState(() => {
|
|
115
|
-
if (widgetStateFromWindow != null) {
|
|
116
|
-
return widgetStateFromWindow;
|
|
117
|
-
}
|
|
118
|
-
return typeof defaultState === "function"
|
|
119
|
-
? defaultState()
|
|
120
|
-
: (defaultState ?? null);
|
|
121
|
-
});
|
|
122
|
-
useEffect(() => {
|
|
123
|
-
if (widgetStateFromWindow != null) {
|
|
124
|
-
_setWidgetState(widgetStateFromWindow);
|
|
125
|
-
}
|
|
126
|
-
}, [widgetStateFromWindow]);
|
|
127
|
-
const setWidgetState = useCallback((state) => {
|
|
128
|
-
_setWidgetState((prevState) => {
|
|
129
|
-
const newState = typeof state === "function" ? state(prevState) : state;
|
|
130
|
-
if (newState != null && window.openai?.setWidgetState) {
|
|
131
|
-
window.openai.setWidgetState(newState);
|
|
132
|
-
}
|
|
133
|
-
return newState;
|
|
134
|
-
});
|
|
135
|
-
}, []);
|
|
136
|
-
return [widgetState, setWidgetState];
|
|
137
|
-
}
|
package/dist/react/index.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export { useOpenAiGlobal, useTheme, useDisplayMode, useUserAgent, useMaxHeight, useSafeArea, useLocale, useToolInput, useToolOutput, useToolResponseMetadata, useWidgetState, useCallTool, useSendFollowUp, useRequestDisplayMode, } from "./hooks.js";
|
|
2
|
-
export type { Theme, DisplayMode, DeviceType, UserAgent, SafeAreaInsets, SafeArea, CallToolResponse, OpenAiGlobals, } from "./types.js";
|
|
3
|
-
export { ErrorBoundary } from "./ErrorBoundary.js";
|
package/dist/react/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export { useOpenAiGlobal, useTheme, useDisplayMode, useUserAgent, useMaxHeight, useSafeArea, useLocale, useToolInput, useToolOutput, useToolResponseMetadata, useWidgetState, useCallTool, useSendFollowUp, useRequestDisplayMode, } from "./hooks.js";
|
|
2
|
-
export { ErrorBoundary } from "./ErrorBoundary.js";
|
package/dist/react/types.d.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
export type Theme = "light" | "dark";
|
|
2
|
-
export type DisplayMode = "inline" | "pip" | "fullscreen";
|
|
3
|
-
export interface DeviceType {
|
|
4
|
-
type: "mobile" | "tablet" | "desktop" | "unknown";
|
|
5
|
-
}
|
|
6
|
-
export interface UserAgent {
|
|
7
|
-
device: DeviceType;
|
|
8
|
-
capabilities: {
|
|
9
|
-
hover: boolean;
|
|
10
|
-
touch: boolean;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export interface SafeAreaInsets {
|
|
14
|
-
top: number;
|
|
15
|
-
bottom: number;
|
|
16
|
-
left: number;
|
|
17
|
-
right: number;
|
|
18
|
-
}
|
|
19
|
-
export interface SafeArea {
|
|
20
|
-
insets: SafeAreaInsets;
|
|
21
|
-
}
|
|
22
|
-
export interface CallToolResponse {
|
|
23
|
-
content?: Array<{
|
|
24
|
-
type: string;
|
|
25
|
-
text: string;
|
|
26
|
-
}>;
|
|
27
|
-
structuredContent?: unknown;
|
|
28
|
-
_meta?: Record<string, unknown>;
|
|
29
|
-
isError?: boolean;
|
|
30
|
-
}
|
|
31
|
-
export interface OpenAiGlobals {
|
|
32
|
-
theme: Theme;
|
|
33
|
-
displayMode: DisplayMode;
|
|
34
|
-
userAgent: UserAgent;
|
|
35
|
-
locale: string;
|
|
36
|
-
maxHeight: number;
|
|
37
|
-
safeArea: SafeArea;
|
|
38
|
-
toolInput: Record<string, unknown>;
|
|
39
|
-
toolOutput: {
|
|
40
|
-
content?: Array<{
|
|
41
|
-
type: string;
|
|
42
|
-
text: string;
|
|
43
|
-
}>;
|
|
44
|
-
structuredContent?: unknown;
|
|
45
|
-
} | null;
|
|
46
|
-
toolResponseMetadata: Record<string, unknown> | null;
|
|
47
|
-
widgetState: Record<string, unknown> | null;
|
|
48
|
-
callTool: (name: string, args: Record<string, unknown>) => Promise<CallToolResponse>;
|
|
49
|
-
sendFollowUpMessage: (args: {
|
|
50
|
-
prompt: string;
|
|
51
|
-
}) => Promise<void>;
|
|
52
|
-
openExternal: (payload: {
|
|
53
|
-
href: string;
|
|
54
|
-
}) => void;
|
|
55
|
-
requestDisplayMode: (args: {
|
|
56
|
-
mode: DisplayMode;
|
|
57
|
-
}) => Promise<{
|
|
58
|
-
mode: DisplayMode;
|
|
59
|
-
}>;
|
|
60
|
-
setWidgetState: (state: Record<string, unknown>) => Promise<void>;
|
|
61
|
-
}
|
|
62
|
-
declare global {
|
|
63
|
-
interface Window {
|
|
64
|
-
openai?: OpenAiGlobals;
|
|
65
|
-
}
|
|
66
|
-
}
|
package/dist/react/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/server/widget.d.ts
DELETED