@ensembleapp/client-sdk 0.0.53 → 0.0.55

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/README.md CHANGED
@@ -63,6 +63,80 @@ function CustomChat() {
63
63
  }
64
64
  ```
65
65
 
66
+ ### Reading token usage
67
+
68
+ The server streams token/usage stats as a `data-usage` part. You can read them
69
+ three ways.
70
+
71
+ **1. `<ChatWidget>`: the `onUsage` callback**
72
+
73
+ ```tsx
74
+ import { ChatWidget, type ChatUsageData } from '@ensembleapp/client-sdk'
75
+
76
+ <ChatWidget
77
+ api={{ baseUrl: '/api', token: 'your-jwt-token' }}
78
+ agentId="agent-234"
79
+ threadId="user-123"
80
+ onUsage={(usage: ChatUsageData) => {
81
+ console.log(usage.inputTokens, usage.outputTokens)
82
+ console.log(usage.totalMessageCount, usage.messageHistoryLimit)
83
+ }}
84
+ />
85
+ ```
86
+
87
+ Or, to render the built-in counter (`↑ N in · ↓ N out`) above the input without
88
+ writing any code, set `showTokenUsage`:
89
+
90
+ ```tsx
91
+ <ChatWidget api={/* ... */} agentId="..." threadId="..." showTokenUsage />
92
+ ```
93
+
94
+ **2. `useChat` hook: the `usage` return value**
95
+
96
+ `usage` is reactive state (the latest turn), so it's the simplest way to render:
97
+
98
+ ```tsx
99
+ import { useChat, type ChatUsageData } from '@ensembleapp/client-sdk'
100
+
101
+ function CustomChat() {
102
+ const { messages, sendMessage, usage } = useChat({ /* ...config */ })
103
+ // ^^^^^ ChatUsageData | null
104
+
105
+ return <div>{usage && `${usage.inputTokens} in / ${usage.outputTokens} out`}</div>
106
+ }
107
+ ```
108
+
109
+ You can also pass `onUsage` to the hook if you prefer the event style.
110
+
111
+ **3. `extractUsageData`: for custom stream handling**
112
+
113
+ If you handle the stream at a lower level (e.g. via `onData`), pull usage out of
114
+ any data part:
115
+
116
+ ```tsx
117
+ import { extractUsageData } from '@ensembleapp/client-sdk'
118
+
119
+ onData: (dataPart) => {
120
+ const usage = extractUsageData(dataPart) // ChatUsageData | null
121
+ if (usage) { /* ... */ }
122
+ }
123
+ ```
124
+
125
+ **`ChatUsageData` shape**
126
+
127
+ ```ts
128
+ interface ChatUsageData {
129
+ inputTokens: number
130
+ outputTokens: number
131
+ maxInputTokens?: number
132
+ maxOutputTokens?: number
133
+ inputTokensPercent?: number
134
+ outputTokensPercent?: number
135
+ totalMessageCount?: number
136
+ messageHistoryLimit?: number
137
+ }
138
+ ```
139
+
66
140
  ## API Reference
67
141
 
68
142
  ### ChatWidget Props
@@ -72,6 +146,9 @@ function CustomChat() {
72
146
  - `onMessage?` (function): Callback when a new message is received
73
147
  - `onError?` (function): Callback when an error occurs
74
148
  - `onFinish?` (function): Callback when a conversation finishes
149
+ - `onUsage?` ((usage: ChatUsageData) => void): Callback with token/usage stats as they stream
150
+ - `onData?` ((dataPart) => void): Callback for every raw stream data part
151
+ - `showTokenUsage?` (boolean): Show the built-in input/output token counter above the input (default: false)
75
152
  - `className?` (string): Additional CSS classes
76
153
  - `placeholder?` (string): Input placeholder text
77
154
  - `welcomeMessage?` (string): Initial welcome message
@@ -81,10 +158,10 @@ function CustomChat() {
81
158
  ```tsx
82
159
  const {
83
160
  messages, // Array of chat messages
84
- isLoading, // Boolean indicating if request is in progress
85
- isError, // Boolean indicating if there's an error
161
+ status, // Current request status
86
162
  sendMessage, // Function to send a message
87
163
  stop, // Function to stop current request
88
164
  setMessages, // Function to manually set messages
165
+ usage, // ChatUsageData | null (latest token/usage stats)
89
166
  } = useChat(config)
90
167
  ```
package/dist/index.d.ts CHANGED
@@ -63,6 +63,29 @@ interface ApiConfig {
63
63
  /** Agent version selector: 'latest' (default), 'published', or a specific version number */
64
64
  type AgentVersion = 'latest' | 'published' | number;
65
65
  type StructuredMessagePayload = Record<string, unknown>;
66
+ /**
67
+ * Usage stats emitted by the server as a `data-usage` stream part and delivered
68
+ * to the {@link UseChatConfig.onUsage} callback.
69
+ */
70
+ interface ChatUsageData {
71
+ inputTokens: number;
72
+ outputTokens: number;
73
+ maxInputTokens?: number;
74
+ maxOutputTokens?: number;
75
+ inputTokensPercent?: number;
76
+ outputTokensPercent?: number;
77
+ /** Total number of messages in the conversation so far */
78
+ totalMessageCount?: number;
79
+ /** Maximum number of messages allowed in conversation history */
80
+ messageHistoryLimit?: number;
81
+ }
82
+ /**
83
+ * Extract usage stats from a stream data part. The server wraps usage as
84
+ * `{ type: 'data-usage', data: { ...ChatUsageData } }`; returns the full inner
85
+ * payload (including totalMessageCount / messageHistoryLimit) or null if the
86
+ * part is not a usage part.
87
+ */
88
+ declare function extractUsageData(dataPart: unknown): ChatUsageData | null;
66
89
  type UseChatConfig = DeprecatedChatConfig & {
67
90
  /** The server API configuration */
68
91
  api: ApiConfig;
@@ -80,15 +103,8 @@ type UseChatConfig = DeprecatedChatConfig & {
80
103
  onFinish?: (message: any) => void;
81
104
  onData?: (data: any) => void;
82
105
  onMessage?: (message: UIMessage) => void;
83
- /** Called when usage data is received (input/output token counts and percentages) */
84
- onUsage?: (usage: {
85
- inputTokens: number;
86
- outputTokens: number;
87
- maxInputTokens?: number;
88
- maxOutputTokens?: number;
89
- inputTokensPercent?: number;
90
- outputTokensPercent?: number;
91
- }) => void;
106
+ /** Called when usage data is received (token counts/percentages and message-history stats) */
107
+ onUsage?: (usage: ChatUsageData) => void;
92
108
  };
93
109
  declare function useChat({ api, threadId, agentId, agentVersion, agentExecutionId, dataContext, onError, onAuthError, onFinish, onData, onMessage, onUsage, }: UseChatConfig): {
94
110
  messages: ChatMessage[];
@@ -121,6 +137,7 @@ declare function useChat({ api, threadId, agentId, agentVersion, agentExecutionI
121
137
  addToolApprovalResponse: ai.ChatAddToolApproveResponseFunction;
122
138
  pendingToolOutput: PendingToolOutput | null;
123
139
  pendingToolApproval: PendingToolApproval | null;
140
+ usage: ChatUsageData | null;
124
141
  };
125
142
 
126
143
  /** Matches POST /api/tools/:toolId/invoke request body */
@@ -324,7 +341,7 @@ declare const createWidget: <TEnriched extends EnrichedResults = EnrichedResults
324
341
  render(payload: any, enriched: TEnriched, options: WidgetRenderOptions): unknown;
325
342
  }) => UIWidgetDefinition<FlexibleSchema, TEnriched>;
326
343
 
327
- type DisplayMode = 'full' | 'brief';
344
+ type DisplayMode = 'full' | 'brief' | 'hide-tools';
328
345
 
329
346
  interface ChatWidgetStyles {
330
347
  /**
@@ -405,7 +422,7 @@ interface ChatWidgetConfig extends UseChatConfig {
405
422
  initialUserMessage?: string;
406
423
  /** Placeholder text for the input box */
407
424
  inputPlaceholder?: string;
408
- /** Display mode: 'brief' (default) collapses previous steps, 'full' shows everything */
425
+ /** Display mode: 'brief' (default) collapses previous steps, 'full' shows everything, 'hide-tools' omits tool calls */
409
426
  displayMode?: DisplayMode;
410
427
  className?: string;
411
428
  styles?: ChatWidgetStyles;
@@ -414,6 +431,8 @@ interface ChatWidgetConfig extends UseChatConfig {
414
431
  widgets?: AnyUIWidgetDefinition[];
415
432
  /** Feedback options for assistant messages. Enabled by default. */
416
433
  feedback?: ChatWidgetFeedbackOptions;
434
+ /** Show a compact input/output token counter (from the server's usage data). Default: false. */
435
+ showTokenUsage?: boolean;
417
436
  }
418
437
  declare const ChatWidget: React.ForwardRefExoticComponent<ChatWidgetConfig & React.RefAttributes<ChatWidgetHandle>>;
419
438
 
@@ -537,8 +556,8 @@ interface MessageRendererProps {
537
556
  widgets?: AnyUIWidgetDefinition[];
538
557
  /** Readonly mode - disables all widget interactions */
539
558
  readonly?: boolean;
540
- /** Display mode: 'brief' collapses previous steps, 'full' shows everything */
541
- displayMode?: 'brief' | 'full';
559
+ /** Display mode: 'brief' collapses previous steps, 'full' shows everything, 'hide-tools' omits tool calls */
560
+ displayMode?: 'brief' | 'full' | 'hide-tools';
542
561
  /** Tag expansion state and handler */
543
562
  tagExpansion?: {
544
563
  state: Map<string, boolean>;
@@ -590,6 +609,11 @@ declare function BriefDisplayMode({ sections, messageId, renderPart, renderGroup
590
609
  */
591
610
  declare function FullDisplayMode({ sections, messageId, renderPart, renderGroup, }: DisplayModeProps): react_jsx_runtime.JSX.Element;
592
611
 
612
+ /**
613
+ * Hide-tools display mode - shows all text and widgets while omitting tool-call rows.
614
+ */
615
+ declare function HideToolsDisplayMode({ sections, messageId, renderPart, renderGroup, }: DisplayModeProps): react_jsx_runtime.JSX.Element;
616
+
593
617
  type RegisterChatWidgetsParams = {
594
618
  api: ApiConfig;
595
619
  threadId: string;
@@ -637,4 +661,4 @@ declare const getVendorCardsWidget: (isProd?: boolean) => SDKUIWidgetDefinition[
637
661
 
638
662
  declare function cn(...inputs: ClassValue[]): string;
639
663
 
640
- 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 FetchApprovalConfigOptions, FullDisplayMode, FullDisplayMode as FullRenderer, type InvokeTool, type MessageContext, MessageRenderer, type MessageRendererProps, type PendingToolApproval, type PendingToolOutput, type PopupAnchorConfig, PopupChatWidget, type PopupChatWidgetProps, type DisplayModeProps as RendererProps, type SDKUIWidgetDefinition, SDK_WIDGET_MARKER, type StructuredMessagePayload, type SubmitFeedbackParams, TagGroupDisplay, type TagGroupDisplayProps, ToolCallDisplay, type ToolCallDisplayProps, type ToolInvokeRequest, type ToolInvokeResult, type ToolPart, type UIMessagePart, type UIWidgetDefinition, type UseChatConfig, type UseFeedbackConfig, type WidgetRenderOptions, cn, createSDKWidget, createWidget, defaultChatWidgets, fetchApprovalConfig, getVendorCardsWidget, isSDKWidget, registerChatWidgets, transformMessage, useChat, useFeedback };
664
+ export { type AgentVersion, type AnyUIWidgetDefinition, type ApiConfig, type ApprovalUIConfig, BriefDisplayMode, BriefDisplayMode as BriefRenderer, type ChatMessage, type ChatMessageSection, type ChatUsageData, 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 FetchApprovalConfigOptions, FullDisplayMode, FullDisplayMode as FullRenderer, HideToolsDisplayMode, HideToolsDisplayMode as HideToolsRenderer, type InvokeTool, type MessageContext, MessageRenderer, type MessageRendererProps, type PendingToolApproval, type PendingToolOutput, type PopupAnchorConfig, PopupChatWidget, type PopupChatWidgetProps, type DisplayModeProps as RendererProps, type SDKUIWidgetDefinition, SDK_WIDGET_MARKER, type StructuredMessagePayload, type SubmitFeedbackParams, TagGroupDisplay, type TagGroupDisplayProps, ToolCallDisplay, type ToolCallDisplayProps, type ToolInvokeRequest, type ToolInvokeResult, type ToolPart, type UIMessagePart, type UIWidgetDefinition, type UseChatConfig, type UseFeedbackConfig, type WidgetRenderOptions, cn, createSDKWidget, createWidget, defaultChatWidgets, extractUsageData, fetchApprovalConfig, getVendorCardsWidget, isSDKWidget, registerChatWidgets, transformMessage, useChat, useFeedback };