@creature-ai/sdk 0.1.1

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.
@@ -0,0 +1,152 @@
1
+ import { ChannelStatus, AppSession } from '../core/index.js';
2
+ export { ChannelClient, ChannelClientConfig, ChatGPTHostClient, McpHostClient, createChannel, createHost, detectEnvironment } from '../core/index.js';
3
+ import { T as ToolResult, W as WidgetState, E as Environment, H as HostClient, a as AppSessionState } from '../types-DcoqlK46.js';
4
+ export { b as AppSessionListener, A as AppSessionOptions, d as HostClientConfig, f as HostClientEvents, e as HostClientState, c as HostContext, L as LogLevel, S as StructuredWidgetState } from '../types-DcoqlK46.js';
5
+ import * as react from 'react';
6
+ export { applyDocumentTheme, applyHostFonts, applyHostStyleVariables, getDocumentTheme } from '@modelcontextprotocol/ext-apps';
7
+
8
+ interface UseHostConfig {
9
+ name: string;
10
+ version: string;
11
+ onToolInput?: (args: Record<string, unknown>) => void;
12
+ onToolResult?: (result: ToolResult) => void;
13
+ onThemeChange?: (theme: "light" | "dark") => void;
14
+ onTeardown?: () => Promise<void>;
15
+ /** Called when widget state changes (restored from host or updated) */
16
+ onWidgetStateChange?: (widgetState: WidgetState | null) => void;
17
+ }
18
+ /**
19
+ * Logger interface with convenience methods for each log level.
20
+ *
21
+ * Provides both a default `log(message, data?)` method (logs at "info" level)
22
+ * and explicit level methods: `debug()`, `info()`, `notice()`, `warn()`, `error()`.
23
+ */
24
+ interface Logger {
25
+ /**
26
+ * Log a message at "info" level (default).
27
+ * @param message - Log message
28
+ * @param data - Optional structured data
29
+ */
30
+ (message: string, data?: Record<string, unknown>): void;
31
+ /**
32
+ * Log at "debug" level. Gray in DevConsole, typically hidden by default.
33
+ */
34
+ debug: (message: string, data?: Record<string, unknown>) => void;
35
+ /**
36
+ * Log at "info" level. Normal text color in DevConsole.
37
+ */
38
+ info: (message: string, data?: Record<string, unknown>) => void;
39
+ /**
40
+ * Log at "notice" level. Blue in DevConsole, for notable events.
41
+ */
42
+ notice: (message: string, data?: Record<string, unknown>) => void;
43
+ /**
44
+ * Log at "warning" level. Yellow in DevConsole.
45
+ */
46
+ warn: (message: string, data?: Record<string, unknown>) => void;
47
+ /**
48
+ * Log at "error" level. Red in DevConsole.
49
+ */
50
+ error: (message: string, data?: Record<string, unknown>) => void;
51
+ }
52
+ interface UseHostReturn {
53
+ isReady: boolean;
54
+ callTool: <T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>) => Promise<ToolResult<T>>;
55
+ sendNotification: (method: string, params: unknown) => void;
56
+ environment: Environment;
57
+ widgetState: WidgetState | null;
58
+ setWidgetState: (state: WidgetState | null) => void;
59
+ /**
60
+ * Logger for sending messages to the host's DevConsole.
61
+ *
62
+ * @example
63
+ * ```tsx
64
+ * const { log } = useHost({ name: "my-app", version: "1.0.0" });
65
+ *
66
+ * // Default level (info)
67
+ * log("User clicked button");
68
+ *
69
+ * // With data
70
+ * log("Processing item", { itemId: 123 });
71
+ *
72
+ * // Specific levels
73
+ * log.debug("Verbose debugging info");
74
+ * log.info("General information");
75
+ * log.notice("Notable event");
76
+ * log.warn("Something looks off");
77
+ * log.error("Something went wrong", { error: err.message });
78
+ * ```
79
+ */
80
+ log: Logger;
81
+ }
82
+
83
+ /**
84
+ * React hook for connecting to an MCP Apps host.
85
+ *
86
+ * Creates a host client instance and manages its lifecycle. Automatically
87
+ * connects on mount and disconnects on unmount. Uses refs for callbacks
88
+ * to prevent reconnection loops when consumers pass inline functions.
89
+ *
90
+ * @param config - Configuration including app info and event handlers
91
+ * @returns Current state and methods for interacting with the host
92
+ *
93
+ * @example
94
+ * ```tsx
95
+ * const { isReady, callTool, log } = useHost({
96
+ * name: "my-app",
97
+ * version: "1.0.0",
98
+ * onToolResult: (result) => setData(result.structuredContent),
99
+ * });
100
+ *
101
+ * // Logging
102
+ * log("User action"); // default info level
103
+ * log.debug("Verbose info");
104
+ * log.error("Something failed", { error: err.message });
105
+ * ```
106
+ */
107
+ declare function useHost(config: UseHostConfig): UseHostReturn;
108
+
109
+ interface UseToolResultReturn<T> {
110
+ data: T | null;
111
+ title: string | null;
112
+ isError: boolean;
113
+ text: string | null;
114
+ onToolResult: (result: ToolResult) => void;
115
+ reset: () => void;
116
+ }
117
+ declare function useToolResult<T = Record<string, unknown>>(): UseToolResultReturn<T>;
118
+
119
+ type SetStateAction<T> = T | ((prev: T) => T);
120
+ interface WidgetStateContextValue {
121
+ widgetState: WidgetState | null;
122
+ setWidgetState: (state: WidgetState | null) => void;
123
+ }
124
+ declare const WidgetStateContext: react.Context<WidgetStateContextValue | null>;
125
+ declare function useWidgetState<T extends WidgetState>(initialState?: T | (() => T | null) | null): readonly [T | null, (state: SetStateAction<T | null>) => void];
126
+
127
+ interface UseChannelConfig<TReceive = unknown> {
128
+ onMessage?: (message: TReceive) => void;
129
+ enabled?: boolean;
130
+ }
131
+ interface UseChannelReturn<TSend = unknown> {
132
+ status: ChannelStatus;
133
+ error: string | undefined;
134
+ send: (message: TSend) => void;
135
+ }
136
+ declare function useChannel<TSend = unknown, TReceive = unknown>(url: string | undefined, config?: UseChannelConfig<TReceive>): UseChannelReturn<TSend>;
137
+
138
+ interface UseAppSessionConfig<TInternal extends Record<string, unknown> = Record<string, unknown>, TBackend extends Record<string, unknown> = Record<string, unknown>, TUi extends WidgetState | null = WidgetState | null> {
139
+ hostClient?: HostClient;
140
+ initialState?: Partial<AppSessionState<TInternal, TBackend, TUi>>;
141
+ id?: string;
142
+ }
143
+ interface UseAppSessionReturn<TInternal extends Record<string, unknown> = Record<string, unknown>, TBackend extends Record<string, unknown> = Record<string, unknown>, TUi extends WidgetState | null = WidgetState | null> {
144
+ session: AppSession<TInternal, TBackend, TUi>;
145
+ state: AppSessionState<TInternal, TBackend, TUi>;
146
+ ui: TUi;
147
+ setUi: (ui: TUi) => void;
148
+ updateUi: (partial: TUi extends null ? never : Partial<NonNullable<TUi>>) => void;
149
+ }
150
+ declare function useAppSession<TInternal extends Record<string, unknown> = Record<string, unknown>, TBackend extends Record<string, unknown> = Record<string, unknown>, TUi extends WidgetState | null = WidgetState | null>(config?: UseAppSessionConfig<TInternal, TBackend, TUi>): UseAppSessionReturn<TInternal, TBackend, TUi>;
151
+
152
+ export { AppSession, AppSessionState, ChannelStatus, Environment, HostClient, type Logger, ToolResult, type UseAppSessionConfig, type UseAppSessionReturn, type UseChannelConfig, type UseChannelReturn, type UseHostConfig, type UseHostReturn, type UseToolResultReturn, WidgetState, WidgetStateContext, useAppSession, useChannel, useHost, useToolResult, useWidgetState };