@ensembleapp/client-sdk 0.0.30 → 0.0.32

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 CHANGED
@@ -1,7 +1,7 @@
1
- import React$1 from 'react';
1
+ import React$1, { ReactElement } from 'react';
2
2
  import * as ai from 'ai';
3
3
  import { UIMessage } from 'ai';
4
- import z, { ZodObject } from 'zod';
4
+ import { FlexibleSchema, InferSchema } from '@ai-sdk/provider-utils';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { ClassValue } from 'clsx';
7
7
 
@@ -143,7 +143,45 @@ type EnrichmentResult<TData = unknown> = {
143
143
 
144
144
  /** Enriched results keyed by enrichment name */
145
145
  type EnrichedResults<T = unknown> = Record<string, EnrichmentResult<T>>;
146
- interface UIWidgetDefinition<TSchema extends ZodObject<any> = ZodObject<any>, TEnriched extends EnrichedResults = EnrichedResults> {
146
+ /** Union type for SDK to handle both customer and SDK UI widgets */
147
+ type AnyUIWidgetDefinition<TSchema extends FlexibleSchema = FlexibleSchema, TEnriched extends EnrichedResults = EnrichedResults> = UIWidgetDefinition<TSchema, TEnriched> | SDKUIWidgetDefinition<TSchema, TEnriched>;
148
+ /** Internal marker to identify SDK-provided widgets */
149
+ declare const SDK_WIDGET_MARKER: unique symbol;
150
+ /**
151
+ * INTERNAL - Widget definition for SDK and marketplace widgets.
152
+ * These render directly in the SDK's React tree (no DOM bridge needed).
153
+ */
154
+ interface SDKUIWidgetDefinition<TSchema extends FlexibleSchema = FlexibleSchema, TEnriched extends EnrichedResults = EnrichedResults> {
155
+ [SDK_WIDGET_MARKER]: true;
156
+ widgetType: string;
157
+ schema: TSchema;
158
+ enrich?: WidgetEnrichConfig;
159
+ render(payload: InferSchema<TSchema>, enriched: TEnriched): ReactElement;
160
+ }
161
+ /** Type guard to check if a widget is an SDK-provided widget */
162
+ declare function isSDKWidget(widget: AnyUIWidgetDefinition): widget is SDKUIWidgetDefinition;
163
+ /** Helper to create SDK widgets (used internally) */
164
+ declare function createSDKWidget<TSchema extends FlexibleSchema, TEnriched extends EnrichedResults = EnrichedResults>(widget: Omit<SDKUIWidgetDefinition<TSchema, TEnriched>, typeof SDK_WIDGET_MARKER>): SDKUIWidgetDefinition<TSchema, TEnriched>;
165
+ /**
166
+ * Type for customer's ReactDOM instance (supports React 16-18).
167
+ * Customers must provide their ReactDOM to enable cross-version widget rendering.
168
+ */
169
+ interface CustomerReactDOM {
170
+ render?: (element: unknown, container: HTMLElement) => void;
171
+ unmountComponentAtNode?: (container: HTMLElement) => boolean;
172
+ createRoot?: (container: HTMLElement, options?: {
173
+ identifierPrefix?: string;
174
+ onRecoverableError?: (error: Error) => void;
175
+ }) => {
176
+ render: (children: unknown) => void;
177
+ unmount: () => void;
178
+ };
179
+ }
180
+ /**
181
+ * Widget definition for customer-defined widgets.
182
+ * Customer widgets require `reactDOM` to enable cross-React version rendering.
183
+ */
184
+ interface UIWidgetDefinition<TSchema extends FlexibleSchema = FlexibleSchema, TEnriched extends EnrichedResults = EnrichedResults> {
147
185
  widgetType: string;
148
186
  schema: TSchema;
149
187
  /**
@@ -159,14 +197,59 @@ interface UIWidgetDefinition<TSchema extends ZodObject<any> = ZodObject<any>, TE
159
197
  * ```
160
198
  */
161
199
  enrich?: WidgetEnrichConfig;
200
+ /**
201
+ * Customer's ReactDOM instance for rendering widgets. REQUIRED.
202
+ *
203
+ * This ensures widgets render in the customer's React tree, enabling:
204
+ * - Full event handling (onClick, onChange, etc.)
205
+ * - React state and hooks inside widgets
206
+ * - Access to customer's React context
207
+ * - Cross-React version compatibility
208
+ *
209
+ * @example
210
+ * import ReactDOM from 'react-dom'; // React 16/17
211
+ * // or
212
+ * import ReactDOM from 'react-dom/client'; // React 18+
213
+ *
214
+ * const widget = {
215
+ * widgetType: 'person',
216
+ * schema: personSchema,
217
+ * reactDOM: ReactDOM,
218
+ * render: (payload) => <PersonCard {...payload} />
219
+ * };
220
+ */
221
+ reactDOM: CustomerReactDOM;
162
222
  /**
163
223
  * Render function that receives the payload and enriched results.
164
224
  * @param payload - The LLM-generated data matching the schema
165
225
  * @param enriched - Enrichment results keyed by name (empty object if no enrich config)
166
226
  */
167
- render(payload: z.infer<TSchema>, enriched: TEnriched): unknown;
227
+ render(payload: InferSchema<TSchema>, enriched: TEnriched): unknown;
168
228
  }
169
- declare const createWidget: <TSchema extends ZodObject<any>, TEnriched extends EnrichedResults = EnrichedResults>({ widgetType, schema, enrich, render, }: UIWidgetDefinition<TSchema, TEnriched>) => UIWidgetDefinition<TSchema, TEnriched>;
229
+ /**
230
+ * Helper to create customer widgets with type inference.
231
+ *
232
+ * Provides autocomplete and type checking for widget definitions.
233
+ * The render function receives typed payload based on your schema.
234
+ *
235
+ * @example
236
+ * ```typescript
237
+ * import ReactDOM from 'react-dom/client';
238
+ * import { createWidget } from '@ensembleapp/client-sdk';
239
+ * import { z } from 'zod';
240
+ *
241
+ * const productWidget = createWidget({
242
+ * widgetType: 'product-card',
243
+ * schema: z.object({
244
+ * name: z.string(),
245
+ * price: z.number(),
246
+ * }),
247
+ * reactDOM: ReactDOM,
248
+ * render: (payload) => <ProductCard {...payload} />
249
+ * });
250
+ * ```
251
+ */
252
+ declare const createWidget: <TSchema extends FlexibleSchema, TEnriched extends EnrichedResults = EnrichedResults>({ widgetType, schema, enrich, reactDOM, render, }: UIWidgetDefinition<TSchema, TEnriched>) => UIWidgetDefinition<TSchema, TEnriched>;
170
253
 
171
254
  type DisplayMode = 'full' | 'brief';
172
255
 
@@ -229,7 +312,7 @@ interface ChatWidgetConfig extends UseChatConfig {
229
312
  styles?: ChatWidgetStyles;
230
313
  voice?: ChatWidgetVoiceOptions;
231
314
  speechToText?: ChatWidgetSpeechToTextOptions;
232
- widgets?: UIWidgetDefinition[];
315
+ widgets?: AnyUIWidgetDefinition[];
233
316
  /** Feedback options for assistant messages. Enabled by default. */
234
317
  feedback?: ChatWidgetFeedbackOptions;
235
318
  }
@@ -349,7 +432,7 @@ declare function BriefRenderer({ sections, messageId, renderPart, renderGroup, }
349
432
  type RegisterChatWidgetsParams = {
350
433
  api: ApiConfig;
351
434
  threadId: string;
352
- widgets: UIWidgetDefinition[];
435
+ widgets: AnyUIWidgetDefinition[];
353
436
  /** Called when API returns 401/unauthorized. Return a new token to retry the request. */
354
437
  onAuthError?: () => Promise<string | null>;
355
438
  };
@@ -376,14 +459,16 @@ declare global {
376
459
  show: () => void;
377
460
  updateConfig: (config: Partial<EmbeddableChatWidgetConfig>) => void;
378
461
  updateToken: (token: string) => void;
379
- getVendorCardsWidget: (isProd?: boolean) => UIWidgetDefinition[];
462
+ getVendorCardsWidget: (isProd?: boolean) => AnyUIWidgetDefinition[];
463
+ /** Helper to create customer widgets with type inference */
464
+ createWidget: typeof createWidget;
380
465
  };
381
466
  }
382
467
  }
383
468
 
384
- declare const defaultChatWidgets: UIWidgetDefinition[];
385
- declare const getVendorCardsWidget: (isProd?: boolean) => UIWidgetDefinition[];
469
+ declare const defaultChatWidgets: SDKUIWidgetDefinition[];
470
+ declare const getVendorCardsWidget: (isProd?: boolean) => SDKUIWidgetDefinition[];
386
471
 
387
472
  declare function cn(...inputs: ClassValue[]): string;
388
473
 
389
- export { type AgentVersion, type ApiConfig, BriefRenderer, type ChatMessage, type ChatMessageSection, ChatWidget, type ChatWidgetFeedbackOptions, type ChatWidgetHandle, type ChatWidgetConfig as ChatWidgetProps, type ChatWidgetSpeechToTextOptions, type ChatWidgetStyles, type ChatWidgetVoiceOptions, type DisplayMode, type EmbeddableChatWidgetConfig, type EnrichedResults, type FeedbackRating, type FeedbackState, FullRenderer, type MessageFeedback, type PopupAnchorConfig, PopupChatWidget, type PopupChatWidgetProps, type RendererProps, type SubmitFeedbackParams, TagGroupDisplay, type TagGroupDisplayProps, ToolCallDisplay, type ToolCallDisplayProps, type ToolPart, type UIMessagePart, type UIWidgetDefinition, type UseChatConfig, type UseFeedbackConfig, type WidgetEnrichConfig, cn, createWidget, defaultChatWidgets, getVendorCardsWidget, registerChatWidgets, useChat, useFeedback };
474
+ export { type AgentVersion, type AnyUIWidgetDefinition, type ApiConfig, BriefRenderer, type ChatMessage, type ChatMessageSection, ChatWidget, type ChatWidgetFeedbackOptions, type ChatWidgetHandle, type ChatWidgetConfig as ChatWidgetProps, type ChatWidgetSpeechToTextOptions, type ChatWidgetStyles, type ChatWidgetVoiceOptions, type CustomerReactDOM, type DisplayMode, type EmbeddableChatWidgetConfig, type EnrichedResults, type FeedbackRating, type FeedbackState, FullRenderer, type MessageFeedback, type PopupAnchorConfig, PopupChatWidget, type PopupChatWidgetProps, type RendererProps, type SDKUIWidgetDefinition, SDK_WIDGET_MARKER, type SubmitFeedbackParams, TagGroupDisplay, type TagGroupDisplayProps, ToolCallDisplay, type ToolCallDisplayProps, type ToolPart, type UIMessagePart, type UIWidgetDefinition, type UseChatConfig, type UseFeedbackConfig, type WidgetEnrichConfig, cn, createSDKWidget, createWidget, defaultChatWidgets, getVendorCardsWidget, isSDKWidget, registerChatWidgets, useChat, useFeedback };