@salesforce/sdk-chat 1.133.1 → 1.133.2

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.
Files changed (2) hide show
  1. package/README.md +291 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,291 @@
1
+ # @salesforce/sdk-chat
2
+
3
+ Chat SDK for participating in conversational flows across Salesforce application surfaces. Provides a unified API for sending messages, executing tools, managing widget state, and reading resources — regardless of whether the host is OpenAI ChatGPT, an MCP Apps host, or Salesforce Agentforce.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @salesforce/sdk-chat
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ import { createChatSDK } from "@salesforce/sdk-chat";
15
+
16
+ const sdk = await createChatSDK();
17
+
18
+ // Send a message to the host
19
+ await sdk.sendMessageToHost?.({ content: "Hello from the widget" });
20
+
21
+ // Read tool input provided by the host
22
+ const input = sdk.accessToolInput<{ query: string }>();
23
+ if (input) {
24
+ console.log(input.data.query);
25
+ }
26
+ ```
27
+
28
+ ## Initialization
29
+
30
+ ### Factory (Async)
31
+
32
+ `createChatSDK` detects the runtime surface and returns the appropriate implementation. Each call creates a new instance.
33
+
34
+ ```ts
35
+ const sdk = await createChatSDK();
36
+ ```
37
+
38
+ ### Singleton (Recommended)
39
+
40
+ `getChatSDK` caches the instance so all callers share one SDK. The Promise itself is cached, preventing race conditions from concurrent callers.
41
+
42
+ ```ts
43
+ import { getChatSDK } from "@salesforce/sdk-chat";
44
+
45
+ const sdk = await getChatSDK();
46
+ ```
47
+
48
+ ### Synchronous Access
49
+
50
+ After `getChatSDK()` has resolved, you can access the instance synchronously. Returns `null` if not yet initialized.
51
+
52
+ ```ts
53
+ import { getChatSDKSync } from "@salesforce/sdk-chat";
54
+
55
+ const sdk = getChatSDKSync(); // ChatSDK | null
56
+ ```
57
+
58
+ ### Options
59
+
60
+ ```ts
61
+ import { Surface } from "@salesforce/sdk-core";
62
+
63
+ const sdk = await createChatSDK({
64
+ // Force a specific surface (skip auto-detection)
65
+ surface: Surface.MCPApps,
66
+ // MCP Apps session options
67
+ mcpApps: {
68
+ appIdentity: { name: "my-app" },
69
+ handshakeTimeout: 5000,
70
+ },
71
+ });
72
+ ```
73
+
74
+ ### Reset (Testing)
75
+
76
+ ```ts
77
+ import { resetChatSDK } from "@salesforce/sdk-chat";
78
+
79
+ resetChatSDK(); // Clears cached singleton; next getChatSDK() creates fresh instance
80
+ ```
81
+
82
+ ## API Reference
83
+
84
+ All `ChatSDK` methods are optional — availability depends on the runtime surface. Always use optional chaining (`?.`) when calling them.
85
+
86
+ ### sendMessageToHost
87
+
88
+ Send a message into the conversation.
89
+
90
+ ```ts
91
+ await sdk.sendMessageToHost?.({
92
+ content: "Here are the results",
93
+ metadata: { source: "search" }, // optional
94
+ });
95
+ ```
96
+
97
+ | Param | Type | Description |
98
+ | ------------------ | ------------------------- | --------------------------- |
99
+ | `message.content` | `string` | Message text |
100
+ | `message.metadata` | `Record<string, unknown>` | Optional key-value metadata |
101
+
102
+ ### callTool
103
+
104
+ Execute a tool on the MCP server and get the result.
105
+
106
+ ```ts
107
+ const result = await sdk.callTool<{ items: string[] }>({
108
+ toolName: "search",
109
+ params: { query: "accounts" },
110
+ });
111
+ ```
112
+
113
+ | Param | Type | Description |
114
+ | ------------------- | ------------------------- | -------------------------- |
115
+ | `toolCall.toolName` | `string` | Name of the tool to invoke |
116
+ | `toolCall.params` | `Record<string, unknown>` | Optional tool parameters |
117
+
118
+ ### accessToolInput / accessToolOutput
119
+
120
+ Read the current tool input or output provided by the host. These are synchronous getters that return the latest state.
121
+
122
+ ```ts
123
+ const input = sdk.accessToolInput<{ accountId: string }>();
124
+ const output = sdk.accessToolOutput<{ name: string; revenue: number }>();
125
+
126
+ if (input) {
127
+ console.log(input.data.accountId);
128
+ }
129
+ ```
130
+
131
+ Returns `ToolState<T> | null` where `ToolState<T> = { data: T; timestamp?: number }`.
132
+
133
+ ### accessToolMetadata
134
+
135
+ Access metadata about the current tool (schema, description, response metadata).
136
+
137
+ ```ts
138
+ const meta = sdk.accessToolMetadata<{ schema: object }>();
139
+ ```
140
+
141
+ ### getWidgetState / setWidgetState
142
+
143
+ Persist widget state across tool invocations via the host's model context.
144
+
145
+ ```ts
146
+ // Save state
147
+ sdk.setWidgetState?.({ selectedTab: "overview", filters: ["active"] });
148
+
149
+ // Restore state
150
+ const state = sdk.getWidgetState<{ selectedTab: string }>();
151
+ ```
152
+
153
+ `setWidgetState` is fire-and-forget — it sends state to the host but does not wait for acknowledgment.
154
+
155
+ ### readResource
156
+
157
+ Read resource content by URI. MCP Apps only.
158
+
159
+ ```ts
160
+ const resource = await sdk.readResource<{ html: string }>({
161
+ uri: "salesforce://accounts/001xxx",
162
+ });
163
+ ```
164
+
165
+ ### openLink
166
+
167
+ Open a URL. On OpenAI, falls back to `window.open()` if the native API is unavailable.
168
+
169
+ ```ts
170
+ await sdk.openLink?.("https://salesforce.com/help");
171
+ ```
172
+
173
+ ### setDisplayMode
174
+
175
+ Request the host to change the widget's display mode.
176
+
177
+ ```ts
178
+ const result = await sdk.setDisplayMode?.("fullscreen");
179
+ // result.mode === "fullscreen"
180
+ ```
181
+
182
+ | Mode | Description |
183
+ | -------------- | --------------------------------- |
184
+ | `"inline"` | Normal embedded view in chat |
185
+ | `"fullscreen"` | Expanded full-screen view |
186
+ | `"pip"` | Picture-in-Picture floating panel |
187
+
188
+ ### getHostContext
189
+
190
+ Get environment information from the host. MCP Apps only.
191
+
192
+ ```ts
193
+ const ctx = sdk.getHostContext?.();
194
+ if (ctx) {
195
+ console.log(ctx.theme); // "light" | "dark"
196
+ console.log(ctx.displayMode); // "inline" | "fullscreen" | "pip"
197
+ console.log(ctx.locale); // "en-US"
198
+ console.log(ctx.maxHeight); // number (CSS pixels)
199
+ console.log(ctx.safeArea); // { top, right, bottom, left }
200
+ console.log(ctx.deviceCapabilities); // { touch?, hover? }
201
+ console.log(ctx.styles); // { variables?, css? }
202
+ }
203
+ ```
204
+
205
+ ### subscribe
206
+
207
+ Subscribe to SDK data changes (tool input, output, metadata, host context). Designed to work with React's `useSyncExternalStore`.
208
+
209
+ ```ts
210
+ const unsubscribe = sdk.subscribe?.(() => {
211
+ const output = sdk.accessToolOutput();
212
+ // re-render with new data
213
+ });
214
+
215
+ // Cleanup
216
+ unsubscribe?.();
217
+ ```
218
+
219
+ ### onToolCanceled
220
+
221
+ Register a callback for when the host cancels tool execution. MCP Apps only.
222
+
223
+ ```ts
224
+ const unsubscribe = sdk.onToolCanceled?.((reason) => {
225
+ console.log("Tool canceled:", reason);
226
+ });
227
+ ```
228
+
229
+ ### followUpActions
230
+
231
+ Get available follow-up actions (SalesforceACC only, currently returns `[]`).
232
+
233
+ ```ts
234
+ const actions = sdk.followUpActions?.() ?? [];
235
+ ```
236
+
237
+ ## Surface Capabilities
238
+
239
+ Not all methods are available on every surface. Use `getSurfaceCapabilities` from `@salesforce/sdk-core` to check at runtime, or refer to this matrix:
240
+
241
+ | Method | MCP Apps | OpenAI | Salesforce ACC |
242
+ | -------------------- | -------- | ------ | -------------- |
243
+ | `sendMessageToHost` | Yes | Yes | Yes |
244
+ | `callTool` | Yes | Yes | No |
245
+ | `accessToolInput` | Yes | Yes | No |
246
+ | `accessToolOutput` | Yes | Yes | No |
247
+ | `accessToolMetadata` | Yes | Yes | No |
248
+ | `getWidgetState` | Yes | Yes | No |
249
+ | `setWidgetState` | Yes | Yes | No |
250
+ | `readResource` | Yes | No | No |
251
+ | `openLink` | Yes | Yes | Yes |
252
+ | `setDisplayMode` | Yes | Yes | No |
253
+ | `getHostContext` | Yes | No | No |
254
+ | `onToolCanceled` | Yes | No | No |
255
+ | `subscribe` | Yes | Yes | Yes |
256
+
257
+ WebApp and Mosaic surfaces return an empty object with no methods.
258
+
259
+ ```ts
260
+ import { getSurface, getSurfaceCapabilities } from "@salesforce/sdk-core";
261
+
262
+ const caps = getSurfaceCapabilities(getSurface());
263
+ if (caps.callTool) {
264
+ await sdk.callTool?.({ toolName: "search", params: { q: "test" } });
265
+ }
266
+ ```
267
+
268
+ ## Exported Types
269
+
270
+ ```ts
271
+ import type {
272
+ ChatSDK,
273
+ ChatMessage,
274
+ ToolCall,
275
+ ToolState,
276
+ ResourceRequest,
277
+ DisplayMode,
278
+ HostContext,
279
+ SafeAreaInsets,
280
+ DeviceCapabilities,
281
+ SDKOptions,
282
+ } from "@salesforce/sdk-chat";
283
+
284
+ import type { ChatSDKOptions } from "@salesforce/sdk-chat";
285
+ ```
286
+
287
+ ## License
288
+
289
+ Copyright (c) 2026, Salesforce, Inc.
290
+ All rights reserved.
291
+ For full license text, see the LICENSE.txt file.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/sdk-chat",
3
- "version": "1.133.1",
3
+ "version": "1.133.2",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -26,7 +26,7 @@
26
26
  "test:coverage": "vitest run --coverage"
27
27
  },
28
28
  "dependencies": {
29
- "@salesforce/sdk-core": "^1.133.1"
29
+ "@salesforce/sdk-core": "^1.133.2"
30
30
  },
31
31
  "devDependencies": {
32
32
  "vite": "^7.3.1",