@ensembleapp/client-sdk 0.0.38 → 0.0.40

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,11 +1,128 @@
1
1
  import React, { ReactElement, ReactNode } from 'react';
2
2
  import * as ai from 'ai';
3
3
  import { UIMessage } from 'ai';
4
+ import z, { z as z$1 } from 'zod';
4
5
  import { FlexibleSchema, InferSchema } from '@ai-sdk/provider-utils';
5
- import z from 'zod';
6
6
  import * as react_jsx_runtime from 'react/jsx-runtime';
7
7
  import { ClassValue } from 'clsx';
8
8
 
9
+ declare const VersionSelectorSchema: z.ZodUnion<readonly [z.ZodLiteral<"latest">, z.ZodLiteral<"published">, z.ZodNumber]>;
10
+ type VersionSelector = z.infer<typeof VersionSelectorSchema>;
11
+
12
+ declare const FeedbackRatingSchema: z.ZodEnum<{
13
+ positive: "positive";
14
+ negative: "negative";
15
+ }>;
16
+ type FeedbackRating = z.infer<typeof FeedbackRatingSchema>;
17
+ interface FeedbackNote {
18
+ noteId?: string;
19
+ [key: string]: unknown;
20
+ }
21
+ interface MessageFeedback {
22
+ rating?: FeedbackRating;
23
+ improvementText?: string;
24
+ notes?: FeedbackNote[];
25
+ }
26
+
27
+ /**
28
+ * Arguments for the collect_user_input tool.
29
+ */
30
+ declare const CollectUserInputArgsSchema: z$1.ZodObject<{
31
+ title: z$1.ZodOptional<z$1.ZodString>;
32
+ description: z$1.ZodOptional<z$1.ZodString>;
33
+ fields: z$1.ZodArray<z$1.ZodDiscriminatedUnion<[z$1.ZodObject<{
34
+ type: z$1.ZodLiteral<"choices">;
35
+ name: z$1.ZodString;
36
+ label: z$1.ZodString;
37
+ options: z$1.ZodArray<z$1.ZodObject<{
38
+ label: z$1.ZodString;
39
+ value: z$1.ZodString;
40
+ description: z$1.ZodOptional<z$1.ZodString>;
41
+ }, z$1.core.$strip>>;
42
+ multiSelect: z$1.ZodOptional<z$1.ZodBoolean>;
43
+ required: z$1.ZodOptional<z$1.ZodBoolean>;
44
+ defaultValue: z$1.ZodOptional<z$1.ZodUnion<readonly [z$1.ZodString, z$1.ZodArray<z$1.ZodString>]>>;
45
+ }, z$1.core.$strip>, z$1.ZodObject<{
46
+ type: z$1.ZodLiteral<"text">;
47
+ name: z$1.ZodString;
48
+ label: z$1.ZodString;
49
+ placeholder: z$1.ZodOptional<z$1.ZodString>;
50
+ multiline: z$1.ZodOptional<z$1.ZodBoolean>;
51
+ required: z$1.ZodOptional<z$1.ZodBoolean>;
52
+ defaultValue: z$1.ZodOptional<z$1.ZodString>;
53
+ }, z$1.core.$strip>, z$1.ZodObject<{
54
+ type: z$1.ZodLiteral<"number">;
55
+ name: z$1.ZodString;
56
+ label: z$1.ZodString;
57
+ min: z$1.ZodOptional<z$1.ZodNumber>;
58
+ max: z$1.ZodOptional<z$1.ZodNumber>;
59
+ required: z$1.ZodOptional<z$1.ZodBoolean>;
60
+ defaultValue: z$1.ZodOptional<z$1.ZodNumber>;
61
+ }, z$1.core.$strip>, z$1.ZodObject<{
62
+ type: z$1.ZodLiteral<"checkbox">;
63
+ name: z$1.ZodString;
64
+ label: z$1.ZodString;
65
+ defaultValue: z$1.ZodOptional<z$1.ZodBoolean>;
66
+ }, z$1.core.$strip>, z$1.ZodObject<{
67
+ type: z$1.ZodLiteral<"date">;
68
+ name: z$1.ZodString;
69
+ label: z$1.ZodString;
70
+ required: z$1.ZodOptional<z$1.ZodBoolean>;
71
+ defaultValue: z$1.ZodOptional<z$1.ZodString>;
72
+ }, z$1.core.$strip>], "type">>;
73
+ submitLabel: z$1.ZodOptional<z$1.ZodString>;
74
+ cancelLabel: z$1.ZodOptional<z$1.ZodString>;
75
+ allowFreeformResponse: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodBoolean>>;
76
+ }, z$1.core.$strip>;
77
+ type CollectUserInputArgs = z$1.infer<typeof CollectUserInputArgsSchema>;
78
+
79
+ /**
80
+ * fetch data via tool call.
81
+ */
82
+ type ToolCallConfig = {
83
+ toolId: string;
84
+ /**
85
+ * Select the tool version (default to 'latest' if not provided).
86
+ * - 'latest': pick draft if exists, fallback to latest published version
87
+ * - 'published': pick latest published version, fallback to draft version
88
+ * - number: specific tool version (1+)
89
+ */
90
+ version?: VersionSelector;
91
+ /**
92
+ * Tool input can be literal or templated string with expressions
93
+ */
94
+ inputs?: Record<string, unknown>;
95
+ };
96
+ /**
97
+ * Widget enrichment configuration.
98
+ * Keys are used to access the enrichment results in the render function.
99
+ *
100
+ * Example:
101
+ * ```typescript
102
+ * enrich: {
103
+ * vendorDetails: { toolId: 'vendor-tool', inputs: { ids: "${vendors}" } },
104
+ * pricing: { toolId: 'pricing-tool', inputs: { ids: "${vendors}" } },
105
+ * },
106
+ * render: (payload, enriched) => {
107
+ * const vendors = enriched?.vendorDetails?.data;
108
+ * const pricing = enriched?.pricing?.data;
109
+ * }
110
+ * ```
111
+ */
112
+ type WidgetEnrichConfig = Record<string, ToolCallConfig>;
113
+ /**
114
+ * Enrichment result containing the full tool response.
115
+ * This should mirror ToolResult on server-side without debug/metadata info.
116
+ */
117
+ type EnrichmentResult<TData = unknown> = {
118
+ success: boolean;
119
+ error?: {
120
+ message?: string;
121
+ code?: string;
122
+ };
123
+ data?: TData;
124
+ };
125
+
9
126
  /** Derived part type from UIMessage */
10
127
  type UIMessagePart = UIMessage['parts'][number];
11
128
  /**
@@ -35,6 +152,19 @@ interface ChatMessage {
35
152
  sections: ChatMessageSection[];
36
153
  }
37
154
 
155
+ /** Pending tool input detected from tool call (e.g., collect_user_input) */
156
+ interface PendingToolOutput {
157
+ toolCallId: string;
158
+ args: CollectUserInputArgs;
159
+ }
160
+ /** Pending tool approval detected from tool call */
161
+ interface PendingToolApproval {
162
+ toolCallId: string;
163
+ toolName: string;
164
+ args: Record<string, unknown>;
165
+ /** The approval ID required for addToolApprovalResponse */
166
+ approvalId: string;
167
+ }
38
168
  type DeprecatedChatConfig = {
39
169
  /** @deprecated use agentId instead */
40
170
  agentExecutionId?: string;
@@ -91,72 +221,49 @@ declare function useChat({ api, threadId, agentId, agentVersion, agentExecutionI
91
221
  } | undefined, options?: ai.ChatRequestOptions) => Promise<void>;
92
222
  stop: () => Promise<void>;
93
223
  setMessages: (messages: UIMessage<unknown, ai.UIDataTypes, ai.UITools>[] | ((messages: UIMessage<unknown, ai.UIDataTypes, ai.UITools>[]) => UIMessage<unknown, ai.UIDataTypes, ai.UITools>[])) => void;
224
+ addToolOutput: <TOOL extends string>({ state, tool, toolCallId, output, errorText, }: {
225
+ state?: "output-available";
226
+ tool: TOOL;
227
+ toolCallId: string;
228
+ output: unknown;
229
+ errorText?: never;
230
+ } | {
231
+ state: "output-error";
232
+ tool: TOOL;
233
+ toolCallId: string;
234
+ output?: never;
235
+ errorText: string;
236
+ }) => Promise<void>;
237
+ addToolApprovalResponse: ai.ChatAddToolApproveResponseFunction;
238
+ pendingToolOutput: PendingToolOutput | null;
239
+ pendingToolApproval: PendingToolApproval | null;
94
240
  };
95
241
 
96
- declare const VersionSelectorSchema: z.ZodUnion<readonly [z.ZodLiteral<"latest">, z.ZodLiteral<"published">, z.ZodNumber]>;
97
- type VersionSelector = z.infer<typeof VersionSelectorSchema>;
98
-
99
- declare const FeedbackRatingSchema: z.ZodEnum<{
100
- positive: "positive";
101
- negative: "negative";
102
- }>;
103
- type FeedbackRating = z.infer<typeof FeedbackRatingSchema>;
104
- interface FeedbackNote {
105
- noteId?: string;
106
- [key: string]: unknown;
242
+ /** Matches POST /api/tools/:toolId/invoke request body */
243
+ interface ToolInvokeRequest {
244
+ /** Tool arguments (defaults to empty object) */
245
+ args?: Record<string, unknown>;
246
+ /** Key/value context data passed to the tool */
247
+ dataContext?: Record<string, unknown>;
248
+ /** Tool version: 'latest' (default), 'published', or specific version number */
249
+ version?: 'latest' | 'published' | number;
107
250
  }
108
- interface MessageFeedback {
109
- rating?: FeedbackRating;
110
- improvementText?: string;
111
- notes?: FeedbackNote[];
112
- }
113
-
114
- /**
115
- * fetch data via tool call.
116
- */
117
- type ToolCallConfig = {
118
- toolId: string;
119
- /**
120
- * Select the tool version (default to 'latest' if not provided).
121
- * - 'latest': pick draft if exists, fallback to latest published version
122
- * - 'published': pick latest published version, fallback to draft version
123
- * - number: specific tool version (1+)
124
- */
125
- version?: VersionSelector;
126
- /**
127
- * Tool input can be literal or templated string with expressions
128
- */
129
- inputs?: Record<string, unknown>;
130
- };
131
- /**
132
- * Widget enrichment configuration.
133
- * Keys are used to access the enrichment results in the render function.
134
- *
135
- * Example:
136
- * ```typescript
137
- * enrich: {
138
- * vendorDetails: { toolId: 'vendor-tool', inputs: { ids: "${vendors}" } },
139
- * pricing: { toolId: 'pricing-tool', inputs: { ids: "${vendors}" } },
140
- * },
141
- * render: (payload, enriched) => {
142
- * const vendors = enriched?.vendorDetails?.data;
143
- * const pricing = enriched?.pricing?.data;
144
- * }
145
- * ```
146
- */
147
- type WidgetEnrichConfig = Record<string, ToolCallConfig>;
148
- /**
149
- * Enrichment result containing the full tool response.
150
- * This should mirror ToolResult on server-side without debug/metadata info.
151
- */
152
- type EnrichmentResult<TData = unknown> = {
251
+ /** Matches POST /api/tools/:toolId/invoke response body */
252
+ interface ToolInvokeResult {
153
253
  success: boolean;
254
+ /** Tool execution result (when success: true) */
255
+ data?: unknown;
256
+ /** Error details (when success: false) */
154
257
  error?: {
155
- message?: string;
258
+ message: string;
259
+ details?: string[];
156
260
  code?: string;
157
261
  };
158
- data?: TData;
159
- };
262
+ debug?: {
263
+ executionTime: number;
264
+ };
265
+ }
266
+ type InvokeTool = (toolId: string, request?: ToolInvokeRequest) => Promise<ToolInvokeResult>;
160
267
 
161
268
  /**
162
269
  * Client-specific types for the SDK.
@@ -194,6 +301,16 @@ interface FeedbackConfig extends FeedbackHandlers {
194
301
  interface MessageContext extends FeedbackHandlers {
195
302
  messageId: string;
196
303
  }
304
+ /**
305
+ * Options passed to widget render functions.
306
+ * Provides utilities for message context and tool invocation.
307
+ */
308
+ interface WidgetRenderOptions {
309
+ /** Message-level context with feedback handlers */
310
+ messageContext?: MessageContext;
311
+ /** Function to invoke tools from within widgets (client-side enrichment) */
312
+ invokeTool?: InvokeTool;
313
+ }
197
314
 
198
315
  /** Enriched results keyed by enrichment name */
199
316
  type EnrichedResults<T = unknown> = Record<string, EnrichmentResult<T>>;
@@ -210,9 +327,7 @@ interface SDKUIWidgetDefinition<TSchema extends FlexibleSchema = FlexibleSchema,
210
327
  widgetType: string;
211
328
  schema: TSchema;
212
329
  enrich?: WidgetEnrichConfig;
213
- render(payload: InferSchema<TSchema>, enriched: TEnriched, options: {
214
- messageContext?: MessageContext;
215
- }): ReactElement;
330
+ render(payload: InferSchema<TSchema>, enriched: TEnriched, options: WidgetRenderOptions): ReactElement;
216
331
  }
217
332
  /** Type guard to check if a widget is an SDK-provided widget */
218
333
  declare function isSDKWidget(widget: AnyUIWidgetDefinition): widget is SDKUIWidgetDefinition;
@@ -284,11 +399,9 @@ interface UIWidgetDefinition<TSchema extends FlexibleSchema = FlexibleSchema, TE
284
399
  * Render function that receives the payload and enriched results.
285
400
  * @param payload - The LLM-generated data matching the schema
286
401
  * @param enriched - Enrichment results keyed by name (empty object if no enrich config)
287
- * @param options - Render options (messageContext, etc.)
402
+ * @param options - Render options (messageContext, invokeTool)
288
403
  */
289
- render(payload: InferSchema<TSchema>, enriched: TEnriched, options: {
290
- messageContext?: MessageContext;
291
- }): unknown;
404
+ render(payload: InferSchema<TSchema>, enriched: TEnriched, options: WidgetRenderOptions): unknown;
292
405
  }
293
406
  /**
294
407
  * Helper to create customer widgets.
@@ -318,22 +431,38 @@ declare const createWidget: <TEnriched extends EnrichedResults = EnrichedResults
318
431
  schema: any;
319
432
  enrich?: WidgetEnrichConfig;
320
433
  reactDOM: CustomerReactDOM;
321
- render(payload: any, enriched: TEnriched, options: {
322
- messageContext?: MessageContext;
323
- }): unknown;
434
+ render(payload: any, enriched: TEnriched, options: WidgetRenderOptions): unknown;
324
435
  }) => UIWidgetDefinition<FlexibleSchema, TEnriched>;
325
436
 
326
437
  type DisplayMode = 'full' | 'brief';
327
438
 
328
439
  interface ChatWidgetStyles {
440
+ /**
441
+ * Theme mode: 'light', 'dark', or 'auto' (detects from user's system/app preference).
442
+ * When 'auto', the widget checks: CSS color-scheme, prefers-color-scheme media query,
443
+ * and common dark mode indicators (data-theme, class="dark", etc.)
444
+ * Default: 'auto'
445
+ */
446
+ theme?: 'light' | 'dark' | 'auto';
329
447
  primaryColor?: string;
330
448
  primaryTextColor?: string;
331
449
  backgroundColor?: string;
450
+ backgroundSecondary?: string;
451
+ backgroundTertiary?: string;
332
452
  borderColor?: string;
453
+ borderColorLight?: string;
333
454
  headerTextColor?: string;
334
455
  userBubbleTextColor?: string;
335
456
  assistantBubbleBackground?: string;
336
457
  assistantBubbleTextColor?: string;
458
+ /** Main text color */
459
+ textColor?: string;
460
+ /** Secondary/muted text color */
461
+ textSecondary?: string;
462
+ /** Muted text color (lighter than secondary) */
463
+ textMuted?: string;
464
+ /** Disabled text color */
465
+ textDisabled?: string;
337
466
  fontFamily?: string;
338
467
  borderRadius?: string;
339
468
  inputBackground?: string;
@@ -438,6 +567,35 @@ declare function useFeedback({ api, threadId, agentId, agentVersion, }: UseFeedb
438
567
  error: Error | null;
439
568
  };
440
569
 
570
+ /** Approval UI configuration returned from the server */
571
+ interface ApprovalUIConfig {
572
+ /** The message to display (evaluated from template) */
573
+ message: string;
574
+ /** Label for the approve button */
575
+ approveLabel: string;
576
+ /** Label for the deny button */
577
+ denyLabel: string;
578
+ }
579
+ interface FetchApprovalConfigOptions {
580
+ /** API configuration */
581
+ api: ApiConfig;
582
+ /** Agent ID that contains the tool */
583
+ agentId: string;
584
+ /** Agent version */
585
+ agentVersion?: AgentVersion;
586
+ /** Tool name (will be normalized on server) */
587
+ toolName: string;
588
+ /** Tool arguments for template evaluation */
589
+ args?: Record<string, unknown>;
590
+ /** Called on 401 to refresh token */
591
+ onAuthError?: () => Promise<string | null>;
592
+ }
593
+ /**
594
+ * Fetches the approval UI configuration for a tool.
595
+ * The server evaluates template expressions in message/labels with the provided args.
596
+ */
597
+ declare function fetchApprovalConfig(options: FetchApprovalConfigOptions): Promise<ApprovalUIConfig | null>;
598
+
441
599
  /**
442
600
  * Transforms a UIMessage into a ChatMessage with sections.
443
601
  *
@@ -492,6 +650,8 @@ interface MessageRendererProps {
492
650
  className?: string;
493
651
  /** Feedback config - if provided, feedback UI will be shown */
494
652
  feedback?: FeedbackConfig;
653
+ /** Function to invoke tools from widgets (client-side enrichment) */
654
+ invokeTool?: InvokeTool;
495
655
  }
496
656
  /**
497
657
  * MessageRenderer - renders message content with optional feedback UI.
@@ -499,7 +659,7 @@ interface MessageRendererProps {
499
659
  * Renders the content of a ChatMessage (text, tools, widgets, groups) and optionally
500
660
  * displays feedback buttons for assistant messages.
501
661
  */
502
- declare function MessageRenderer({ message, widgets, readonly, displayMode, tagExpansion, className, feedback, }: MessageRendererProps): react_jsx_runtime.JSX.Element;
662
+ declare function MessageRenderer({ message, widgets, readonly, displayMode, tagExpansion, className, feedback, invokeTool, }: MessageRendererProps): react_jsx_runtime.JSX.Element;
503
663
 
504
664
  interface DisplayModeProps {
505
665
  /** Message sections to render */
@@ -564,8 +724,13 @@ declare global {
564
724
  }
565
725
 
566
726
  declare const defaultChatWidgets: SDKUIWidgetDefinition[];
727
+ /**
728
+ * Hybrid vendor cards widget:
729
+ * - Server enriches vendor details (parallel to LLM, immediate render)
730
+ * - Client fetches distance matrix (progressive, fills in after render)
731
+ */
567
732
  declare const getVendorCardsWidget: (isProd?: boolean) => SDKUIWidgetDefinition[];
568
733
 
569
734
  declare function cn(...inputs: ClassValue[]): string;
570
735
 
571
- export { type AgentVersion, type AnyUIWidgetDefinition, type ApiConfig, BriefDisplayMode, BriefDisplayMode as 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 DisplayModeProps, type EmbeddableChatWidgetConfig, type EnrichedResults, type FeedbackConfig, type FeedbackHandlers, type FeedbackNote, type FeedbackRating, FullDisplayMode, FullDisplayMode as FullRenderer, type MessageContext, type MessageFeedback, MessageRenderer, type MessageRendererProps, type PopupAnchorConfig, PopupChatWidget, type PopupChatWidgetProps, type DisplayModeProps as 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, transformMessage, useChat, useFeedback };
736
+ export { type AgentVersion, type AnyUIWidgetDefinition, type ApiConfig, type ApprovalUIConfig, BriefDisplayMode, BriefDisplayMode as 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 DisplayModeProps, type EmbeddableChatWidgetConfig, type EnrichedResults, type FeedbackConfig, type FeedbackHandlers, type FeedbackNote, type FeedbackRating, type FetchApprovalConfigOptions, FullDisplayMode, FullDisplayMode as FullRenderer, type InvokeTool, type MessageContext, type MessageFeedback, MessageRenderer, type MessageRendererProps, type PendingToolApproval, type PendingToolOutput, type PopupAnchorConfig, PopupChatWidget, type PopupChatWidgetProps, type DisplayModeProps as RendererProps, type SDKUIWidgetDefinition, SDK_WIDGET_MARKER, type SubmitFeedbackParams, TagGroupDisplay, type TagGroupDisplayProps, ToolCallDisplay, type ToolCallDisplayProps, type ToolInvokeRequest, type ToolInvokeResult, type ToolPart, type UIMessagePart, type UIWidgetDefinition, type UseChatConfig, type UseFeedbackConfig, type WidgetEnrichConfig, type WidgetRenderOptions, cn, createSDKWidget, createWidget, defaultChatWidgets, fetchApprovalConfig, getVendorCardsWidget, isSDKWidget, registerChatWidgets, transformMessage, useChat, useFeedback };