@bymos/agentkit-sdk 0.6.3 → 0.7.0

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
@@ -16,36 +16,58 @@ pnpm add @bymos/agentkit-sdk
16
16
 
17
17
  ## Quick start
18
18
 
19
+ Both integration paths take the **same single-argument config**.
20
+
19
21
  ### CDN (no build step)
20
22
 
21
23
  ```html
22
- <script src="https://unpkg.com/@bymos/agentkit-sdk@0.5.0/agentkit-widget.iife.js"></script>
24
+ <script src="https://unpkg.com/@bymos/agentkit-sdk@0.7/agentkit-widget.iife.js"></script>
23
25
  <script>
24
26
  window.AgentKit.init({
25
27
  agentId: 'your-agent-id',
26
28
  tenantId: 'your-tenant-id',
27
29
  apiBaseUrl: 'https://api.klicforge.ai',
28
30
  title: 'Support',
29
- greeting: 'Hi! How can I help?',
30
31
  });
31
32
  </script>
32
33
  ```
33
34
 
35
+ The `@0.7` pin tracks the latest patch of that minor line. Pin an exact version
36
+ (`@0.7.0`) if you need byte-identical builds, and add
37
+ [SRI](https://developer.mozilla.org/docs/Web/Security/Subresource_Integrity)
38
+ (`integrity` + `crossorigin="anonymous"`) when you do — an SRI hash only holds
39
+ against an exact version.
40
+
34
41
  ### npm (ESM / CJS)
35
42
 
36
43
  ```ts
37
- import { init, renderWidget, buildWidgetCSS, normalizeConfig } from '@bymos/agentkit-sdk';
44
+ import { init } from '@bymos/agentkit-sdk';
38
45
 
39
- const config = {
46
+ const widget = init({
40
47
  agentId: 'your-agent-id',
41
48
  tenantId: 'your-tenant-id',
42
49
  apiBaseUrl: 'https://api.klicforge.ai',
43
- };
44
- const widget = init(config, renderWidget, buildWidgetCSS(normalizeConfig(config)));
50
+ });
45
51
 
46
52
  widget.on('message:received', (msg) => console.log(msg.content));
47
53
  ```
48
54
 
55
+ `init` and `mount` default their renderer and stylesheet to the bundled UI. To
56
+ supply your own UI layer, pass them explicitly —
57
+ `init(config, myRenderer, myCss)` — which is also the pre-0.6 call shape and
58
+ still works unchanged.
59
+
60
+ ---
61
+
62
+ ## Before it will run: allow your domain
63
+
64
+ The API rejects widget traffic from any origin that is not on the agent's
65
+ allowlist. In the KlicForge dashboard open your agent → **Channels** → **Widget**
66
+ and add every origin the widget loads from (`https://example.com`,
67
+ `https://www.example.com` — scheme and host must match exactly; there are no
68
+ wildcards). Without this the widget renders an access-restricted panel instead of
69
+ a chat.
70
+
49
71
  ---
50
72
 
51
73
  ## Configuration
@@ -53,24 +75,27 @@ widget.on('message:received', (msg) => console.log(msg.content));
53
75
  ```ts
54
76
  type AgentKitWidgetConfig = {
55
77
  // Required
56
- agentId: string; // Agent ID from your KlicForge dashboard
57
- tenantId: string; // Tenant ID from your KlicForge dashboard
78
+ agentId: string; // Agent ID or slug from your KlicForge dashboard
58
79
  apiBaseUrl: string; // Base URL of your KlicForge API
59
80
 
60
- // Identity (optional)
61
- userId?: string;
62
- authUserId?: string; // Your authenticated user ID — links widget conversations to users in your app (highest-priority contact identifier)
63
- name?: string;
64
- email?: string;
65
- sessionId?: string; // Resume a conversation across tabs/page loads (within a tab, sessions are automatic)
81
+ // Strongly recommended — required for conversation tracking and validation
82
+ tenantId?: string;
83
+
84
+ // Identity of the end user in your app (all optional; omit for anonymous)
85
+ user?: {
86
+ externalId?: string; // Your own identifier — DB ID, UUID, email, anything
87
+ authId?: string; // ID from your auth system (JWT sub, Clerk userId, …)
88
+ name?: string;
89
+ email?: string;
90
+ };
91
+ sessionId?: string; // Resume a conversation across tabs/page loads
66
92
 
67
93
  // Display (optional)
68
94
  mode?: 'floating' | 'inline'; // Default: 'floating'
69
- title?: string;
70
- greeting?: string;
95
+ title?: string; // Default: 'AI Assistant'
96
+ subtitle?: string; // Shown under the agent name in the header
97
+ description?: string; // Shown in the empty state
71
98
  avatarUrl?: string;
72
- subtitle?: string;
73
- privacyPolicyUrl?: string;
74
99
 
75
100
  // Behaviour (optional)
76
101
  streaming?: boolean; // Default: true
@@ -80,32 +105,49 @@ type AgentKitWidgetConfig = {
80
105
  theme?: {
81
106
  mode?: 'light' | 'dark' | 'system'; // Default: 'light'
82
107
  accentColor?: string; // Default: '#6366f1'
83
- fontFamily?: string;
108
+ fontFamily?: string; // Default: system UI stack
84
109
  borderRadius?: string; // Default: '12px'
85
110
  };
86
111
  };
87
112
  ```
88
113
 
89
- At init time the widget fetches any widget config set for your agent in the KlicForge dashboard (title, greeting, avatar, etc.) and uses it as defaults. Any values you pass here override the dashboard config.
114
+ Config is validated at init — an invalid value throws a `ZodError`, so wrap
115
+ `init()` in a try/catch if the values are user-supplied.
116
+
117
+ At init time the widget fetches the widget config set for your agent in the
118
+ KlicForge dashboard (title, avatar, suggested questions, attachment limits) and
119
+ uses it as defaults. Anything you pass here overrides it.
120
+
121
+ Suggested questions, photo/file upload and attachment limits are **server-only** —
122
+ configure them on the agent in the dashboard; they cannot be set from the host page.
123
+
124
+ Your agent always serves its **published** version. `preview`, `skipServerConfigFetch`
125
+ and `configChannel` are dashboard-internal: they only do anything for a caller
126
+ holding a KlicForge session cookie for the owning tenant, so setting them on a
127
+ public site has no effect.
90
128
 
91
129
  ---
92
130
 
93
131
  ## Widget instance API
94
132
 
95
- Both `init()` and `mount()` return a `WidgetInstance`:
133
+ `init()`, `mount()` and `mountFloating()` all return a `WidgetInstance`:
96
134
 
97
135
  ```ts
98
136
  widget.open();
99
137
  widget.close();
100
138
  widget.toggle();
101
139
  widget.sendMessage('Hello!');
102
- widget.reset(); // Clear conversation and show greeting again
103
- widget.destroy();
140
+ widget.sendMessage('See this', [{ file, kind: 'image' }]);
141
+ widget.reset(); // Clear the conversation and start over
142
+ widget.destroy(); // Tears down the DOM and closes the event stream
104
143
  widget.getState(); // → WidgetState
105
144
  widget.on(event, fn);
106
145
  widget.off(event, fn);
107
146
  ```
108
147
 
148
+ Call `destroy()` when unmounting in an SPA — it closes the operator-reply event
149
+ stream, which otherwise reconnects indefinitely.
150
+
109
151
  ---
110
152
 
111
153
  ## Events
@@ -121,9 +163,13 @@ widget.off(event, fn);
121
163
  | `stream:start` | — |
122
164
  | `stream:delta` | `{ delta, accumulated }` |
123
165
  | `stream:end` | `{ content }` |
166
+ | `control_mode_changed` | `{ mode }` |
124
167
  | `reset` | — |
125
168
  | `destroy` | — |
126
169
 
170
+ `control_mode_changed` fires when a human operator takes over the conversation
171
+ or hands it back to the agent.
172
+
127
173
  ---
128
174
 
129
175
  ## CDN global API (`window.AgentKit`)
@@ -138,24 +184,26 @@ widget.off(event, fn);
138
184
 
139
185
  ```html
140
186
  <div id="chat"></div>
141
- <script src="https://unpkg.com/@bymos/agentkit-sdk@0.5.0/agentkit-widget.iife.js"></script>
187
+ <script src="https://unpkg.com/@bymos/agentkit-sdk@0.7/agentkit-widget.iife.js"></script>
142
188
  <script>
143
189
  window.AgentKit.mount(document.getElementById('chat'), {
144
190
  agentId: 'your-agent-id',
145
191
  tenantId: 'your-tenant-id',
146
192
  apiBaseUrl: 'https://api.klicforge.ai',
147
- mode: 'inline',
148
193
  });
149
194
  </script>
150
195
  ```
151
196
 
197
+ `mount()` always renders inline — there is no need to pass `mode`.
198
+
152
199
  ---
153
200
 
154
201
  ## npm exports
155
202
 
156
203
  ```ts
157
204
  // Lifecycle
158
- import { init, mount, destroyWidget, listWidgets, createWidget } from '@bymos/agentkit-sdk';
205
+ import { init, mount, mountFloating, destroy } from '@bymos/agentkit-sdk';
206
+ import { destroyWidget, listWidgets, createWidget } from '@bymos/agentkit-sdk';
159
207
 
160
208
  // Config
161
209
  import { normalizeConfig } from '@bymos/agentkit-sdk';
@@ -163,7 +211,7 @@ import { normalizeConfig } from '@bymos/agentkit-sdk';
163
211
  // Session
164
212
  import { getSessionId, setSessionId, clearSession } from '@bymos/agentkit-sdk';
165
213
 
166
- // UI (requires preact peer dep)
214
+ // UI (requires the preact peer dep)
167
215
  import { renderWidget, buildWidgetCSS, buildThemeVars } from '@bymos/agentkit-sdk';
168
216
 
169
217
  // Lower-level
@@ -186,24 +234,32 @@ import type {
186
234
  } from '@bymos/agentkit-sdk';
187
235
  ```
188
236
 
237
+ `mountFloating(element, config)` anchors the launcher inside a specific element
238
+ rather than the page — useful for previews and bounded layouts.
239
+
189
240
  ---
190
241
 
191
242
  ## Session management
192
243
 
193
- Within a browser tab, sessions are automatic — the SDK reads and writes `sessionStorage` keyed by `agentId` with no configuration needed. Sessions are cleared when the tab closes.
244
+ Within a browser tab, sessions are automatic — the SDK reads and writes
245
+ `sessionStorage` keyed by `agentId` with no configuration needed. Sessions are
246
+ cleared when the tab closes, and storage access is failure-tolerant: in Safari
247
+ private mode or a partitioned third-party context the widget simply starts a new
248
+ conversation on each load rather than erroring.
194
249
 
195
- To persist a conversation across tabs or page reloads, save and restore the session ID yourself:
250
+ To persist a conversation across tabs or page reloads, save and restore the
251
+ session ID yourself:
196
252
 
197
253
  ```ts
198
254
  widget.on('conversation:started', ({ sessionId }) => {
199
255
  localStorage.setItem('chat-session', sessionId);
200
256
  });
201
257
 
202
- // Later, on next load:
258
+ // Later, on the next load:
203
259
  const widget = window.AgentKit.init({
204
260
  agentId: 'your-agent-id',
205
261
  tenantId: 'your-tenant-id',
206
- apiBaseUrl: '...',
262
+ apiBaseUrl: 'https://api.klicforge.ai',
207
263
  sessionId: localStorage.getItem('chat-session') ?? undefined,
208
264
  });
209
265
  ```
@@ -212,12 +268,14 @@ const widget = window.AgentKit.init({
212
268
 
213
269
  ## Theming
214
270
 
215
- The widget renders inside a **Shadow DOM** to prevent CSS conflicts with your page. Use the `theme` config to customise it:
271
+ The widget renders inside a **Shadow DOM** to prevent CSS conflicts with your
272
+ page. Use the `theme` config to customise it:
216
273
 
217
274
  ```ts
218
275
  window.AgentKit.init({
219
276
  agentId: 'your-agent-id',
220
- apiBaseUrl: '...',
277
+ tenantId: 'your-tenant-id',
278
+ apiBaseUrl: 'https://api.klicforge.ai',
221
279
  theme: {
222
280
  mode: 'dark',
223
281
  accentColor: '#ec4899',
@@ -227,6 +285,26 @@ window.AgentKit.init({
227
285
  });
228
286
  ```
229
287
 
288
+ Theme values become CSS custom properties inside the shadow root —
289
+ `--ak-accent`, `--ak-accent-hover`, `--ak-font`, `--ak-radius`. Setting those
290
+ variables on your own page has no effect; pass `theme` instead.
291
+
292
+ ---
293
+
294
+ ## Content Security Policy
295
+
296
+ If your site sends a CSP, the widget needs:
297
+
298
+ | Directive | Value |
299
+ | ------------- | ------------------------------------------------------------ |
300
+ | `script-src` | `https://unpkg.com` (CDN install only) |
301
+ | `connect-src` | your `apiBaseUrl` |
302
+ | `img-src` | your `apiBaseUrl` and any avatar host, plus `data:` |
303
+ | `style-src` | `'unsafe-inline'` — styles are injected into the shadow root |
304
+
305
+ Shadow DOM does not exempt inline styles from CSP, so `style-src 'unsafe-inline'`
306
+ is currently required.
307
+
230
308
  ---
231
309
 
232
310
  ## Requirements