@ensembleapp/client-sdk 0.0.29 → 0.0.31

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,8 +1,8 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
1
+ import React$1, { ReactElement } from 'react';
2
2
  import * as ai from 'ai';
3
3
  import { UIMessage } from 'ai';
4
- import z, { AnyZodObject } from 'zod';
5
- import React$1 from 'react';
4
+ import { FlexibleSchema, InferSchema } from '@ai-sdk/provider-utils';
5
+ import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { ClassValue } from 'clsx';
7
7
 
8
8
  /** Derived part type from UIMessage */
@@ -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 AnyZodObject = AnyZodObject, 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 AnyZodObject = AnyZodObject, TEnric
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 AnyZodObject, 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
 
@@ -207,6 +290,11 @@ interface ChatWidgetFeedbackOptions {
207
290
  /** Require comment when giving negative feedback. Default: false */
208
291
  requireCommentForNegative?: boolean;
209
292
  }
293
+ /** Handle exposed by ChatWidget ref for programmatic control */
294
+ interface ChatWidgetHandle {
295
+ /** Send a message programmatically */
296
+ sendMessage: (message: string) => void;
297
+ }
210
298
  interface ChatWidgetConfig extends UseChatConfig {
211
299
  /** Title for the Chat window */
212
300
  title?: string;
@@ -224,11 +312,11 @@ interface ChatWidgetConfig extends UseChatConfig {
224
312
  styles?: ChatWidgetStyles;
225
313
  voice?: ChatWidgetVoiceOptions;
226
314
  speechToText?: ChatWidgetSpeechToTextOptions;
227
- widgets?: UIWidgetDefinition[];
315
+ widgets?: AnyUIWidgetDefinition[];
228
316
  /** Feedback options for assistant messages. Enabled by default. */
229
317
  feedback?: ChatWidgetFeedbackOptions;
230
318
  }
231
- declare function ChatWidget({ api, threadId, agentId, agentVersion, agentExecutionId, dataContext, displayMode, onError, onAuthError, onFinish, onMessage, title, initialAssistantMessage, initialUserMessage, inputPlaceholder, className, styles: styleProps, voice, speechToText, widgets, feedback, }: ChatWidgetConfig): react_jsx_runtime.JSX.Element;
319
+ declare const ChatWidget: React$1.ForwardRefExoticComponent<ChatWidgetConfig & React$1.RefAttributes<ChatWidgetHandle>>;
232
320
 
233
321
  interface PopupAnchorConfig {
234
322
  enabled?: boolean;
@@ -344,7 +432,7 @@ declare function BriefRenderer({ sections, messageId, renderPart, renderGroup, }
344
432
  type RegisterChatWidgetsParams = {
345
433
  api: ApiConfig;
346
434
  threadId: string;
347
- widgets: UIWidgetDefinition[];
435
+ widgets: AnyUIWidgetDefinition[];
348
436
  /** Called when API returns 401/unauthorized. Return a new token to retry the request. */
349
437
  onAuthError?: () => Promise<string | null>;
350
438
  };
@@ -371,14 +459,16 @@ declare global {
371
459
  show: () => void;
372
460
  updateConfig: (config: Partial<EmbeddableChatWidgetConfig>) => void;
373
461
  updateToken: (token: string) => void;
374
- getVendorCardsWidget: (isProd?: boolean) => UIWidgetDefinition[];
462
+ getVendorCardsWidget: (isProd?: boolean) => AnyUIWidgetDefinition[];
463
+ /** Helper to create customer widgets with type inference */
464
+ createWidget: typeof createWidget;
375
465
  };
376
466
  }
377
467
  }
378
468
 
379
- declare const defaultChatWidgets: UIWidgetDefinition[];
380
- declare const getVendorCardsWidget: (isProd?: boolean) => UIWidgetDefinition[];
469
+ declare const defaultChatWidgets: SDKUIWidgetDefinition[];
470
+ declare const getVendorCardsWidget: (isProd?: boolean) => SDKUIWidgetDefinition[];
381
471
 
382
472
  declare function cn(...inputs: ClassValue[]): string;
383
473
 
384
- export { type AgentVersion, type ApiConfig, BriefRenderer, type ChatMessage, type ChatMessageSection, ChatWidget, type ChatWidgetFeedbackOptions, 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 };